webgpu_skinning_instancing.html 4.5 KB

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