webgpu_skinning_instancing.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - WebGPU - Skinning Instancing</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 - Skinning Instancing
  12. </div>
  13. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/",
  19. "three/nodes": "./jsm/nodes/Nodes.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import * as Nodes from 'three/nodes';
  26. import { mix, range, color, oscSine, timerLocal } from 'three/nodes';
  27. import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
  28. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  29. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  30. let camera, scene, renderer;
  31. let mixer, clock;
  32. init();
  33. function init() {
  34. if ( WebGPU.isAvailable() === false ) {
  35. document.body.appendChild( WebGPU.getErrorMessage() );
  36. throw new Error( 'No WebGPU support' );
  37. }
  38. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  39. camera.position.set( 1, 2, 3 );
  40. scene = new THREE.Scene();
  41. camera.lookAt( 0, 1, 0 );
  42. clock = new THREE.Clock();
  43. //lights
  44. const centerLight = new THREE.PointLight( 0xff9900, 1, 100 );
  45. centerLight.position.y = 4.5;
  46. centerLight.position.z = - 2;
  47. centerLight.power = 1700;
  48. scene.add( centerLight );
  49. const cameraLight = new THREE.PointLight( 0x0099ff, 1, 100 );
  50. cameraLight.power = 1700;
  51. camera.add( cameraLight );
  52. scene.add( camera );
  53. const loader = new FBXLoader();
  54. loader.load( 'models/fbx/Samba Dancing.fbx', ( object ) => {
  55. object.scale.setScalar( .01 );
  56. mixer = new THREE.AnimationMixer( object );
  57. const action = mixer.clipAction( object.animations[ 0 ] );
  58. action.play();
  59. const instanceCount = 30;
  60. const dummy = new THREE.Object3D();
  61. object.traverse( ( child ) => {
  62. if ( child.isMesh ) {
  63. const oscNode = oscSine( timerLocal( .1 ) );
  64. // random colors between instances from 0x000000 to 0xFFFFFF
  65. const randomColors = range( new THREE.Color( 0x000000 ), new THREE.Color( 0xFFFFFF ) );
  66. // random [ 0, 1 ] values between instances
  67. const randomMetalness = range( 0, 1 );
  68. child.material = new Nodes.MeshStandardNodeMaterial();
  69. child.material.roughness = .1;
  70. child.material.metalnessNode = mix( 0.0, randomMetalness, oscNode );
  71. child.material.colorNode = mix( color( 0xFFFFFF ), randomColors, oscNode );
  72. child.isInstancedMesh = true;
  73. child.instanceMatrix = new THREE.InstancedBufferAttribute( new Float32Array( instanceCount * 16 ), 16 );
  74. child.count = instanceCount;
  75. for ( let i = 0; i < instanceCount; i ++ ) {
  76. dummy.position.x = - 200 + ( ( i % 5 ) * 70 );
  77. dummy.position.z = Math.floor( i / 5 ) * - 200;
  78. dummy.updateMatrix();
  79. dummy.matrix.toArray( child.instanceMatrix.array, i * 16 );
  80. }
  81. }
  82. } );
  83. scene.add( object );
  84. } );
  85. //renderer
  86. renderer = new WebGPURenderer();
  87. renderer.setPixelRatio( window.devicePixelRatio );
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. renderer.setAnimationLoop( animate );
  90. renderer.outputEncoding = THREE.sRGBEncoding;
  91. renderer.toneMappingNode = new Nodes.ToneMappingNode( THREE.LinearToneMapping, .17 );
  92. document.body.appendChild( renderer.domElement );
  93. window.addEventListener( 'resize', onWindowResize );
  94. }
  95. function onWindowResize() {
  96. camera.aspect = window.innerWidth / window.innerHeight;
  97. camera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. function animate() {
  101. const delta = clock.getDelta();
  102. if ( mixer ) mixer.update( delta );
  103. renderer.render( scene, camera );
  104. }
  105. </script>
  106. </body>
  107. </html>