webgpu_instance_path.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - geometry path</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</a> webgpu - geometry path
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three/webgpu';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  27. import { abs, add, instancedBufferAttribute, positionLocal, mod, time, sin, vec3, select, float, screenUV, color } from 'three/tsl';
  28. let camera, scene, renderer, controls;
  29. const count = 1000;
  30. init();
  31. async function init() {
  32. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 100 );
  33. camera.position.z = 15;
  34. scene = new THREE.Scene();
  35. scene.backgroundNode = screenUV.distance( .5 ).remap( 0, 0.65 ).mix( color( 0x94254c ), color( 0x000000 ) );
  36. // generate a path representing a heart shape
  37. const x = 0, y = 0;
  38. const path = new THREE.Path()
  39. .moveTo( x - 2.5, y - 2.5 )
  40. .bezierCurveTo( x - 2.5, y - 2.5, x - 2, y, x, y )
  41. .bezierCurveTo( x + 3, y, x + 3, y - 3.5, x + 3, y - 3.5 )
  42. .bezierCurveTo( x + 3, y - 5.5, x + 1, y - 7.7, x - 2.5, y - 9.5 )
  43. .bezierCurveTo( x - 6, y - 7.7, x - 8, y - 5.5, x - 8, y - 3.5 )
  44. .bezierCurveTo( x - 8, y - 3.5, x - 8, y, x - 5, y )
  45. .bezierCurveTo( x - 3.5, y, x - 2.5, y - 2.5, x - 2.5, y - 2.5 );
  46. // generate instanced ico-spheres along the path
  47. const geometry = new THREE.IcosahedronGeometry( 0.1 );
  48. const material = new THREE.MeshStandardNodeMaterial();
  49. const mesh = new THREE.Mesh( geometry, material );
  50. mesh.position.set( 2.5, 5, 0 );
  51. mesh.count = count;
  52. mesh.frustumCulled = false;
  53. scene.add( mesh );
  54. // instance data
  55. const v = new THREE.Vector3();
  56. const c = new THREE.Color();
  57. const positions = [];
  58. const times = [];
  59. const seeds = [];
  60. const colors = [];
  61. for ( let i = 0; i < count; i ++ ) {
  62. const t = i / count;
  63. path.getPointAt( t, v );
  64. v.x += ( 0.5 - Math.random() );
  65. v.y += ( 0.5 - Math.random() );
  66. v.z = ( 0.5 - Math.random() );
  67. positions.push( v.x, v.y, v.z );
  68. times.push( t );
  69. seeds.push( Math.random() );
  70. c.setHSL( 0.75 + ( Math.random() * 0.25 ), 1, 0.4 );
  71. colors.push( c.r, c.g, c.b );
  72. }
  73. const positionAttribute = new THREE.InstancedBufferAttribute( new Float32Array( positions ), 3 );
  74. const colorAttribute = new THREE.InstancedBufferAttribute( new Float32Array( colors ), 3 );
  75. const timeAttribute = new THREE.InstancedBufferAttribute( new Float32Array( times ), 1 );
  76. const seedAttribute = new THREE.InstancedBufferAttribute( new Float32Array( seeds ), 1 );
  77. // TSL
  78. const instancePosition = instancedBufferAttribute( positionAttribute );
  79. const instanceColor = instancedBufferAttribute( colorAttribute );
  80. const instanceSeed = instancedBufferAttribute( seedAttribute );
  81. const instanceTime = instancedBufferAttribute( timeAttribute );
  82. const localTime = instanceTime.add( time );
  83. const modTime = mod( time.mul( 0.4 ), 1 );
  84. const s0 = sin( localTime.add( instanceSeed ) ).mul( 0.25 );
  85. const dist = abs( instanceTime.sub( modTime ) ).toConst(); // modTime and instanceTime are in the range [0,1]
  86. const wrapDist = select( dist.greaterThan( 0.5 ), dist.oneMinus(), dist ).toConst(); // the normalized distance should wrap around 0/1
  87. const s1 = select( wrapDist.greaterThan( 0.1 ), float( 1 ), wrapDist.remap( 0, 0.1, 3, 1 ) ); // compute a scale in a range around the current interpolated value
  88. const offset = vec3( instancePosition.x, instancePosition.y.add( s0 ), instancePosition.z ).toConst( 'offset' );
  89. material.positionNode = add( positionLocal.mul( s1 ), offset );
  90. material.colorNode = instanceColor;
  91. //
  92. renderer = new THREE.WebGPURenderer( { antialias: true } );
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. renderer.setAnimationLoop( animate );
  96. renderer.toneMapping = THREE.NeutralToneMapping;
  97. document.body.appendChild( renderer.domElement );
  98. await renderer.init();
  99. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  100. scene.environment = pmremGenerator.fromScene( new RoomEnvironment(), 0.04 ).texture;
  101. controls = new OrbitControls( camera, renderer.domElement );
  102. controls.enableDamping = true;
  103. //
  104. window.addEventListener( 'resize', onWindowResize );
  105. }
  106. function onWindowResize() {
  107. camera.aspect = window.innerWidth / window.innerHeight;
  108. camera.updateProjectionMatrix();
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. }
  111. function animate() {
  112. controls.update();
  113. renderer.render( scene, camera );
  114. }
  115. </script>
  116. </body>
  117. </html>
粤ICP备19079148号