webgpu_skinning_instancing.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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().then( animate ).catch( error );
  33. async 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. // random colors between instances from 0x000000 to 0xFFFFFF
  61. const randomColors = range( new THREE.Color( 0x000000 ), new THREE.Color( 0xFFFFFF ) );
  62. // random [ 0, 1 ] values between instances
  63. const randomMetalness = range( 0, 1 );
  64. child.material = new Nodes.MeshStandardNodeMaterial();
  65. child.material.roughness = .1;
  66. child.material.metalnessNode = randomMetalness;
  67. child.material.colorNode = mix( color( 0xFFFFFF ), randomColors, oscSine( timerLocal( .1 ) ) );
  68. child.isInstancedMesh = true;
  69. child.instanceMatrix = new THREE.InstancedBufferAttribute( new Float32Array( instanceCount * 16 ), 16 );
  70. child.count = instanceCount;
  71. for ( let i = 0; i < instanceCount; i ++ ) {
  72. dummy.position.x = - 200 + ( ( i % 5 ) * 70 );
  73. dummy.position.z = Math.floor( i / 5 ) * - 200;
  74. dummy.updateMatrix();
  75. dummy.matrix.toArray( child.instanceMatrix.array, i * 16 );
  76. }
  77. }
  78. } );
  79. scene.add( object );
  80. } );
  81. //renderer
  82. renderer = new WebGPURenderer();
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. renderer.outputEncoding = THREE.sRGBEncoding;
  86. renderer.toneMappingNode = new Nodes.ToneMappingNode( THREE.LinearToneMapping, 800 );
  87. document.body.appendChild( renderer.domElement );
  88. window.addEventListener( 'resize', onWindowResize );
  89. return renderer.init();
  90. }
  91. function onWindowResize() {
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. }
  96. function animate() {
  97. requestAnimationFrame( animate );
  98. const delta = clock.getDelta();
  99. if ( mixer ) mixer.update( delta );
  100. renderer.render( scene, camera );
  101. }
  102. function error( error ) {
  103. console.error( error );
  104. }
  105. </script>
  106. </body>
  107. </html>