webgpu_skinning_points.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - WebGPU - Skinning Points</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 Points
  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 { uniform, skinning, PointsNodeMaterial } 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. let camera, scene, renderer;
  30. let mixer, clock;
  31. init();
  32. function init() {
  33. if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {
  34. document.body.appendChild( WebGPU.getErrorMessage() );
  35. throw new Error( 'No WebGPU or WebGL2 support' );
  36. }
  37. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  38. camera.position.set( 0, 300, - 85 );
  39. scene = new THREE.Scene();
  40. camera.lookAt( 0, 0, - 85 );
  41. clock = new THREE.Clock();
  42. const loader = new GLTFLoader();
  43. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  44. const object = gltf.scene;
  45. mixer = new THREE.AnimationMixer( object );
  46. const action = mixer.clipAction( gltf.animations[ 0 ] );
  47. action.play();
  48. object.traverse( function ( child ) {
  49. if ( child.isMesh ) {
  50. child.visible = false;
  51. const materialPoints = new PointsNodeMaterial();
  52. materialPoints.colorNode = uniform( new THREE.Color() );
  53. materialPoints.positionNode = skinning( child );
  54. const pointCloud = new THREE.Points( child.geometry, materialPoints );
  55. scene.add( pointCloud );
  56. }
  57. } );
  58. scene.add( object );
  59. } );
  60. //renderer
  61. renderer = new WebGPURenderer( { antialias: true } );
  62. renderer.setPixelRatio( window.devicePixelRatio );
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. renderer.setAnimationLoop( animate );
  65. document.body.appendChild( renderer.domElement );
  66. window.addEventListener( 'resize', onWindowResize );
  67. }
  68. function onWindowResize() {
  69. camera.aspect = window.innerWidth / window.innerHeight;
  70. camera.updateProjectionMatrix();
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. }
  73. function animate() {
  74. const delta = clock.getDelta();
  75. if ( mixer ) mixer.update( delta );
  76. renderer.render( scene, camera );
  77. }
  78. </script>
  79. </body>
  80. </html>