webgpu_instance_path.html 5.9 KB

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