1
0

webgpu_skinning_instancing.html 4.4 KB

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