webgpu_skinning_instancing.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/",
  18. "three/nodes": "./jsm/nodes/Nodes.js"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { pass, mix, range, color, oscSine, timerLocal, MeshStandardNodeMaterial } from 'three/nodes';
  25. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  26. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  27. import WebGL from 'three/addons/capabilities/WebGL.js';
  28. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  29. import PostProcessing from 'three/addons/renderers/common/PostProcessing.js';
  30. let camera, scene, renderer;
  31. let postProcessing;
  32. let mixer, clock;
  33. init();
  34. function init() {
  35. if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {
  36. document.body.appendChild( WebGPU.getErrorMessage() );
  37. throw new Error( 'No WebGPU or WebGL2 support' );
  38. }
  39. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 40 );
  40. camera.position.set( 1, 2, 3 );
  41. scene = new THREE.Scene();
  42. camera.lookAt( 0, 1, 0 );
  43. clock = new THREE.Clock();
  44. // lights
  45. const centerLight = new THREE.PointLight( 0xff9900, 1, 100 );
  46. centerLight.position.y = 4.5;
  47. centerLight.position.z = - 2;
  48. centerLight.power = 400;
  49. scene.add( centerLight );
  50. const cameraLight = new THREE.PointLight( 0x0099ff, 1, 100 );
  51. cameraLight.power = 400;
  52. camera.add( cameraLight );
  53. scene.add( camera );
  54. const geometry = new THREE.PlaneGeometry( 1000, 1000 );
  55. geometry.rotateX( - Math.PI / 2 );
  56. const plane = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, visible: true } ) );
  57. scene.add( plane );
  58. const loader = new GLTFLoader();
  59. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  60. const object = gltf.scene;
  61. mixer = new THREE.AnimationMixer( object );
  62. const action = mixer.clipAction( gltf.animations[ 0 ] );
  63. action.play();
  64. const instanceCount = 30;
  65. const dummy = new THREE.Object3D();
  66. object.traverse( ( child ) => {
  67. if ( child.isMesh ) {
  68. const oscNode = oscSine( timerLocal( .1 ) );
  69. // random colors between instances from 0x000000 to 0xFFFFFF
  70. const randomColors = range( new THREE.Color( 0x000000 ), new THREE.Color( 0xFFFFFF ) );
  71. // random [ 0, 1 ] values between instances
  72. const randomMetalness = range( 0, 1 );
  73. child.material = new MeshStandardNodeMaterial();
  74. child.material.roughness = .1;
  75. child.material.metalnessNode = mix( 0.0, randomMetalness, oscNode );
  76. child.material.colorNode = mix( color( 0xFFFFFF ), randomColors, oscNode );
  77. child.isInstancedMesh = true;
  78. child.instanceMatrix = new THREE.InstancedBufferAttribute( new Float32Array( instanceCount * 16 ), 16 );
  79. child.count = instanceCount;
  80. for ( let i = 0; i < instanceCount; i ++ ) {
  81. dummy.position.x = - 200 + ( ( i % 5 ) * 70 );
  82. dummy.position.y = Math.floor( i / 5 ) * - 200;
  83. dummy.updateMatrix();
  84. dummy.matrix.toArray( child.instanceMatrix.array, i * 16 );
  85. }
  86. }
  87. } );
  88. scene.add( object );
  89. } );
  90. // renderer
  91. renderer = new WebGPURenderer( { antialias: true } );
  92. renderer.setPixelRatio( window.devicePixelRatio );
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. renderer.setAnimationLoop( animate );
  95. document.body.appendChild( renderer.domElement );
  96. // post processing
  97. const scenePass = pass( scene, camera );
  98. const scenePassColor = scenePass.getTextureNode();
  99. const scenePassDepth = scenePass.getLinearDepthNode().remapClamp( .15, .3 );
  100. const scenePassColorBlurred = scenePassColor.gaussianBlur();
  101. scenePassColorBlurred.directionNode = scenePassDepth;
  102. postProcessing = new PostProcessing( renderer );
  103. postProcessing.outputNode = scenePassColorBlurred;
  104. // events
  105. window.addEventListener( 'resize', onWindowResize );
  106. }
  107. function onWindowResize() {
  108. camera.aspect = window.innerWidth / window.innerHeight;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. }
  112. function animate() {
  113. const delta = clock.getDelta();
  114. if ( mixer ) mixer.update( delta );
  115. postProcessing.render();
  116. }
  117. </script>
  118. </body>
  119. </html>