webgpu_skinning_instancing.html 4.2 KB

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