webgpu_tsl_galaxy.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - galaxy</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js webgpu</a> - galaxy
  12. <br>
  13. Based on <a href="https://threejs-journey.com/lessons/animated-galaxy" target="_blank" rel="noopener">Three.js Journey</a> lessons
  14. </div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.webgpu.js",
  19. "three/tsl": "../build/three.webgpu.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { color, cos, float, mix, range, sin, timerLocal, uniform, uv, vec3, vec4, PI2 } from 'three/tsl';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. let camera, scene, renderer, controls;
  30. init();
  31. function init() {
  32. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  33. camera.position.set( 4, 2, 5 );
  34. scene = new THREE.Scene();
  35. scene.background = new THREE.Color( 0x201919 );
  36. // galaxy
  37. const material = new THREE.SpriteNodeMaterial( {
  38. transparent: true,
  39. depthWrite: false,
  40. blending: THREE.AdditiveBlending
  41. } );
  42. const size = uniform( 0.08 );
  43. material.scaleNode = range( 0, 1 ).mul( size );
  44. const time = timerLocal();
  45. const radiusRatio = range( 0, 1 );
  46. const radius = radiusRatio.pow( 1.5 ).mul( 5 ).toVar();
  47. const branches = 3;
  48. const branchAngle = range( 0, branches ).floor().mul( PI2.div( branches ) );
  49. const angle = branchAngle.add( time.mul( radiusRatio.oneMinus() ) );
  50. const position = vec3(
  51. cos( angle ),
  52. 0,
  53. sin( angle )
  54. ).mul( radius );
  55. const randomOffset = range( vec3( - 1 ), vec3( 1 ) ).pow( 3 ).mul( radiusRatio ).add( 0.2 );
  56. material.positionNode = position.add( randomOffset );
  57. const colorInside = uniform( color( '#ffa575' ) );
  58. const colorOutside = uniform( color( '#311599' ) );
  59. const colorFinal = mix( colorInside, colorOutside, radiusRatio.oneMinus().pow( 2 ).oneMinus() );
  60. const alpha = float( 0.1 ).div( uv().sub( 0.5 ).length() ).sub( 0.2 );
  61. material.colorNode = vec4( colorFinal, alpha );
  62. const mesh = new THREE.InstancedMesh( new THREE.PlaneGeometry( 1, 1 ), material, 20000 );
  63. scene.add( mesh );
  64. // debug
  65. const gui = new GUI();
  66. gui.add( size, 'value', 0, 1, 0.001 ).name( 'size' );
  67. gui.addColor( { color: colorInside.value.getHex( THREE.SRGBColorSpace ) }, 'color' )
  68. .name( 'colorInside' )
  69. .onChange( function ( value ) {
  70. colorInside.value.set( value );
  71. } );
  72. gui.addColor( { color: colorOutside.value.getHex( THREE.SRGBColorSpace ) }, 'color' )
  73. .name( 'colorOutside' )
  74. .onChange( function ( value ) {
  75. colorOutside.value.set( value );
  76. } );
  77. // renderer
  78. renderer = new THREE.WebGPURenderer( { antialias: true } );
  79. renderer.setPixelRatio( window.devicePixelRatio );
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. renderer.setAnimationLoop( animate );
  82. document.body.appendChild( renderer.domElement );
  83. controls = new OrbitControls( camera, renderer.domElement );
  84. controls.enableDamping = true;
  85. controls.minDistance = 0.1;
  86. controls.maxDistance = 50;
  87. window.addEventListener( 'resize', onWindowResize );
  88. }
  89. function onWindowResize() {
  90. camera.aspect = window.innerWidth / window.innerHeight;
  91. camera.updateProjectionMatrix();
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. }
  94. async function animate() {
  95. controls.update();
  96. renderer.render( scene, camera );
  97. }
  98. </script>
  99. </body>
  100. </html>