webgpu_skinning_instancing.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.webgpu.js",
  17. "three/tsl": "../build/three.webgpu.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { pass, mix, range, color, oscSine, timerLocal, gaussianBlur } from 'three/tsl';
  25. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  26. let camera, scene, renderer;
  27. let postProcessing;
  28. let mixer, clock;
  29. init();
  30. function init() {
  31. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 40 );
  32. camera.position.set( 1, 2, 3 );
  33. scene = new THREE.Scene();
  34. camera.lookAt( 0, 1, 0 );
  35. clock = new THREE.Clock();
  36. // lights
  37. const centerLight = new THREE.PointLight( 0xff9900, 1, 100 );
  38. centerLight.position.y = 4.5;
  39. centerLight.position.z = - 2;
  40. centerLight.power = 400;
  41. scene.add( centerLight );
  42. const cameraLight = new THREE.PointLight( 0x0099ff, 1, 100 );
  43. cameraLight.power = 400;
  44. camera.add( cameraLight );
  45. scene.add( camera );
  46. const geometry = new THREE.PlaneGeometry( 1000, 1000 );
  47. geometry.rotateX( - Math.PI / 2 );
  48. const plane = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, visible: true } ) );
  49. scene.add( plane );
  50. const loader = new GLTFLoader();
  51. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  52. const object = gltf.scene;
  53. mixer = new THREE.AnimationMixer( object );
  54. const action = mixer.clipAction( gltf.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 THREE.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.y = 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 THREE.WebGPURenderer( { antialias: true } );
  84. renderer.setPixelRatio( window.devicePixelRatio );
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. renderer.setAnimationLoop( animate );
  87. document.body.appendChild( renderer.domElement );
  88. // post processing
  89. const scenePass = pass( scene, camera );
  90. const scenePassColor = scenePass.getTextureNode();
  91. const scenePassDepth = scenePass.getLinearDepthNode().remapClamp( .15, .3 );
  92. const scenePassColorBlurred = gaussianBlur( scenePassColor );
  93. scenePassColorBlurred.directionNode = scenePassDepth;
  94. postProcessing = new THREE.PostProcessing( renderer );
  95. postProcessing.outputNode = scenePassColorBlurred;
  96. // events
  97. window.addEventListener( 'resize', onWindowResize );
  98. }
  99. function onWindowResize() {
  100. camera.aspect = window.innerWidth / window.innerHeight;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. }
  104. function animate() {
  105. const delta = clock.getDelta();
  106. if ( mixer ) mixer.update( delta );
  107. postProcessing.render();
  108. }
  109. </script>
  110. </body>
  111. </html>