webgpu_skinning_instancing.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. <meta property="og:title" content="three.js webgpu - skinning instancing">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_skinning_instancing.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_skinning_instancing.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Skinning Instancing</span>
  18. </div>
  19. <small>
  20. Skinning with multiple instances of the same model.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { pass, mix, range, color, oscSine, time } from 'three/tsl';
  36. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  37. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  38. let camera, scene, renderer;
  39. let renderPipeline;
  40. let mixer, timer;
  41. init();
  42. function init() {
  43. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 40 );
  44. camera.position.set( 1, 2, 3 );
  45. scene = new THREE.Scene();
  46. camera.lookAt( 0, 1, 0 );
  47. timer = new THREE.Timer();
  48. timer.connect( document );
  49. // lights
  50. const centerLight = new THREE.PointLight( 0xff9900, 1, 100 );
  51. centerLight.position.y = 4.5;
  52. centerLight.position.z = - 2;
  53. centerLight.power = 400;
  54. scene.add( centerLight );
  55. const cameraLight = new THREE.PointLight( 0x0099ff, 1, 100 );
  56. cameraLight.power = 400;
  57. camera.add( cameraLight );
  58. scene.add( camera );
  59. const geometry = new THREE.PlaneGeometry( 1000, 1000 );
  60. geometry.rotateX( - Math.PI / 2 );
  61. const plane = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, visible: true } ) );
  62. scene.add( plane );
  63. const loader = new GLTFLoader();
  64. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  65. const object = gltf.scene;
  66. mixer = new THREE.AnimationMixer( object );
  67. const action = mixer.clipAction( gltf.animations[ 0 ] );
  68. action.play();
  69. const instanceCount = 30;
  70. const dummy = new THREE.Object3D();
  71. object.traverse( ( child ) => {
  72. if ( child.isMesh ) {
  73. const oscNode = oscSine( time.mul( .1 ) );
  74. // random colors between instances from 0x000000 to 0xFFFFFF
  75. const randomColors = range( new THREE.Color( 0x000000 ), new THREE.Color( 0xFFFFFF ) );
  76. // random [ 0, 1 ] values between instances
  77. const randomMetalness = range( 0, 1 );
  78. child.material = new THREE.MeshStandardNodeMaterial();
  79. child.material.roughness = .1;
  80. child.material.metalnessNode = mix( 0.0, randomMetalness, oscNode );
  81. child.material.colorNode = mix( color( 0xFFFFFF ), randomColors, oscNode );
  82. child.isInstancedMesh = true;
  83. child.instanceMatrix = new THREE.InstancedBufferAttribute( new Float32Array( instanceCount * 16 ), 16 );
  84. child.count = instanceCount;
  85. for ( let i = 0; i < instanceCount; i ++ ) {
  86. dummy.position.x = - 200 + ( ( i % 5 ) * 70 );
  87. dummy.position.y = Math.floor( i / 5 ) * - 200;
  88. dummy.updateMatrix();
  89. dummy.matrix.toArray( child.instanceMatrix.array, i * 16 );
  90. }
  91. }
  92. } );
  93. scene.add( object );
  94. } );
  95. // renderer
  96. renderer = new THREE.WebGPURenderer( { antialias: true } );
  97. renderer.setPixelRatio( window.devicePixelRatio );
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. renderer.setAnimationLoop( animate );
  100. document.body.appendChild( renderer.domElement );
  101. // post processing
  102. const scenePass = pass( scene, camera );
  103. const scenePassColor = scenePass.getTextureNode();
  104. const scenePassDepth = scenePass.getLinearDepthNode().remapClamp( .15, .3 );
  105. const scenePassColorBlurred = gaussianBlur( scenePassColor );
  106. scenePassColorBlurred.directionNode = scenePassDepth;
  107. renderPipeline = new THREE.RenderPipeline( renderer );
  108. renderPipeline.outputNode = scenePassColorBlurred;
  109. // events
  110. window.addEventListener( 'resize', onWindowResize );
  111. }
  112. function onWindowResize() {
  113. camera.aspect = window.innerWidth / window.innerHeight;
  114. camera.updateProjectionMatrix();
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. }
  117. function animate() {
  118. timer.update();
  119. const delta = timer.getDelta();
  120. if ( mixer ) mixer.update( delta );
  121. renderPipeline.render();
  122. }
  123. </script>
  124. </body>
  125. </html>
粤ICP备19079148号