1
0

webgpu_tsl_galaxy.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/webgpu": "../build/three.webgpu.js",
  20. "three/tsl": "../build/three.tsl.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three/webgpu';
  27. import { color, cos, float, mix, range, sin, time, uniform, uv, vec3, vec4, PI2 } from 'three/tsl';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. let camera, scene, renderer, controls;
  31. init();
  32. function init() {
  33. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  34. camera.position.set( 4, 2, 5 );
  35. scene = new THREE.Scene();
  36. scene.background = new THREE.Color( 0x201919 );
  37. // galaxy
  38. const material = new THREE.SpriteNodeMaterial( {
  39. depthWrite: false,
  40. blending: THREE.AdditiveBlending
  41. } );
  42. const size = uniform( 0.08 );
  43. material.scaleNode = range( 0, 1 ).mul( size );
  44. const radiusRatio = range( 0, 1 );
  45. const radius = radiusRatio.pow( 1.5 ).mul( 5 ).toVar();
  46. const branches = 3;
  47. const branchAngle = range( 0, branches ).floor().mul( PI2.div( branches ) );
  48. const angle = branchAngle.add( time.mul( radiusRatio.oneMinus() ) );
  49. const position = vec3(
  50. cos( angle ),
  51. 0,
  52. sin( angle )
  53. ).mul( radius );
  54. const randomOffset = range( vec3( - 1 ), vec3( 1 ) ).pow3().mul( radiusRatio ).add( 0.2 );
  55. material.positionNode = position.add( randomOffset );
  56. const colorInside = uniform( color( '#ffa575' ) );
  57. const colorOutside = uniform( color( '#311599' ) );
  58. const colorFinal = mix( colorInside, colorOutside, radiusRatio.oneMinus().pow( 2 ).oneMinus() );
  59. const alpha = float( 0.1 ).div( uv().sub( 0.5 ).length() ).sub( 0.2 );
  60. material.colorNode = vec4( colorFinal, alpha );
  61. const mesh = new THREE.InstancedMesh( new THREE.PlaneGeometry( 1, 1 ), material, 20000 );
  62. scene.add( mesh );
  63. // debug
  64. const gui = new GUI();
  65. gui.add( size, 'value', 0, 1, 0.001 ).name( 'size' );
  66. gui.addColor( { color: colorInside.value.getHex( THREE.SRGBColorSpace ) }, 'color' )
  67. .name( 'colorInside' )
  68. .onChange( function ( value ) {
  69. colorInside.value.set( value );
  70. } );
  71. gui.addColor( { color: colorOutside.value.getHex( THREE.SRGBColorSpace ) }, 'color' )
  72. .name( 'colorOutside' )
  73. .onChange( function ( value ) {
  74. colorOutside.value.set( value );
  75. } );
  76. // renderer
  77. renderer = new THREE.WebGPURenderer( { antialias: true } );
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. renderer.setAnimationLoop( animate );
  81. document.body.appendChild( renderer.domElement );
  82. controls = new OrbitControls( camera, renderer.domElement );
  83. controls.enableDamping = true;
  84. controls.minDistance = 0.1;
  85. controls.maxDistance = 50;
  86. window.addEventListener( 'resize', onWindowResize );
  87. }
  88. function onWindowResize() {
  89. camera.aspect = window.innerWidth / window.innerHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. }
  93. function animate() {
  94. controls.update();
  95. renderer.render( scene, camera );
  96. }
  97. </script>
  98. </body>
  99. </html>
粤ICP备19079148号