webgpu_tsl_galaxy.html 4.1 KB

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