1
0

webgpu_instance_path.html 5.6 KB

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