webgpu_skinning_instancing.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, 1, 5000 );
  39. camera.position.set( 100, 200, 300 );
  40. scene = new THREE.Scene();
  41. camera.lookAt( 0, 100, 0 );
  42. clock = new THREE.Clock();
  43. //lights
  44. const centerLight = new THREE.PointLight( 0xff9900, .8, 7000 );
  45. centerLight.position.y = 450;
  46. centerLight.position.z = - 200;
  47. scene.add( centerLight );
  48. const cameraLight = new THREE.PointLight( 0x0099ff, .7, 7000 );
  49. camera.add( cameraLight );
  50. scene.add( camera );
  51. const loader = new FBXLoader();
  52. loader.load( 'models/fbx/Samba Dancing.fbx', ( object ) => {
  53. mixer = new THREE.AnimationMixer( object );
  54. const action = mixer.clipAction( object.animations[ 0 ] );
  55. action.play();
  56. const instanceCount = 30;
  57. const dummy = new THREE.Object3D();
  58. object.traverse( ( child ) => {
  59. if ( child.isMesh ) {
  60. const oscNode = oscSine( timerLocal( .1 ) );
  61. // random colors between instances from 0x000000 to 0xFFFFFF
  62. const randomColors = range( new THREE.Color( 0x000000 ), new THREE.Color( 0xFFFFFF ) );
  63. // random [ 0, 1 ] values between instances
  64. const randomMetalness = range( 0, 1 );
  65. child.material = new Nodes.MeshStandardNodeMaterial();
  66. child.material.roughness = .1;
  67. child.material.metalnessNode = mix( 0.0, randomMetalness, oscNode );
  68. child.material.colorNode = mix( color( 0xFFFFFF ), randomColors, oscNode );
  69. child.isInstancedMesh = true;
  70. child.instanceMatrix = new THREE.InstancedBufferAttribute( new Float32Array( instanceCount * 16 ), 16 );
  71. child.count = instanceCount;
  72. for ( let i = 0; i < instanceCount; i ++ ) {
  73. dummy.position.x = - 200 + ( ( i % 5 ) * 70 );
  74. dummy.position.z = Math.floor( i / 5 ) * - 200;
  75. dummy.updateMatrix();
  76. dummy.matrix.toArray( child.instanceMatrix.array, i * 16 );
  77. }
  78. }
  79. } );
  80. scene.add( object );
  81. } );
  82. //renderer
  83. renderer = new WebGPURenderer();
  84. renderer.setPixelRatio( window.devicePixelRatio );
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. renderer.setAnimationLoop( animate );
  87. renderer.outputEncoding = THREE.sRGBEncoding;
  88. renderer.toneMappingNode = new Nodes.ToneMappingNode( THREE.LinearToneMapping, 800 );
  89. document.body.appendChild( renderer.domElement );
  90. window.addEventListener( 'resize', onWindowResize );
  91. }
  92. function onWindowResize() {
  93. camera.aspect = window.innerWidth / window.innerHeight;
  94. camera.updateProjectionMatrix();
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. }
  97. function animate() {
  98. const delta = clock.getDelta();
  99. if ( mixer ) mixer.update( delta );
  100. renderer.render( scene, camera );
  101. }
  102. </script>
  103. </body>
  104. </html>