webgpu_skinning_points.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Skinning Points</span>
  14. </div>
  15. <small>
  16. Colors and scale of the points are based on the speed of the animation.
  17. </small>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.webgpu.js",
  23. "three/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three/webgpu';
  31. import { color, computeSkinning, objectWorldMatrix, instancedArray, instanceIndex, Fn, shapeCircle } from 'three/tsl';
  32. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  33. let camera, scene, renderer;
  34. let mixer, clock;
  35. init();
  36. function init() {
  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. scene.background = new THREE.Color( 0x111111 );
  41. camera.lookAt( 0, 0, - 85 );
  42. scene.add( new THREE.AmbientLight( 0xffffff, 10 ) );
  43. clock = new THREE.Clock();
  44. const loader = new GLTFLoader();
  45. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  46. const object = gltf.scene;
  47. mixer = new THREE.AnimationMixer( object );
  48. const action = mixer.clipAction( gltf.animations[ 0 ] );
  49. action.play();
  50. object.traverse( function ( child ) {
  51. if ( child.isMesh ) {
  52. child.visible = false;
  53. const countOfPoints = child.geometry.getAttribute( 'position' ).count;
  54. const pointPositionArray = instancedArray( countOfPoints, 'vec3' ).setPBO( true );
  55. const pointSpeedArray = instancedArray( countOfPoints, 'vec3' ).setPBO( true );
  56. const pointSpeedAttribute = pointSpeedArray.toAttribute();
  57. const skinningPosition = computeSkinning( child );
  58. const materialPoints = new THREE.PointsNodeMaterial();
  59. materialPoints.colorNode = pointSpeedAttribute.mul( .6 ).mix( color( 0x0066ff ), color( 0xff9000 ) );
  60. materialPoints.opacityNode = shapeCircle();
  61. materialPoints.sizeNode = pointSpeedAttribute.length().exp().min( 5 ).mul( 5 ).add( 1 );
  62. materialPoints.sizeAttenuation = false;
  63. const updateSkinningPoints = Fn( () => {
  64. const pointPosition = pointPositionArray.element( instanceIndex );
  65. const pointSpeed = pointSpeedArray.element( instanceIndex );
  66. const skinningWorldPosition = objectWorldMatrix( child ).mul( skinningPosition );
  67. const skinningSpeed = skinningWorldPosition.sub( pointPosition );
  68. pointSpeed.assign( skinningSpeed );
  69. pointPosition.assign( skinningWorldPosition );
  70. }, 'void' );
  71. materialPoints.positionNode = Fn( () => {
  72. updateSkinningPoints();
  73. return pointPositionArray.toAttribute();
  74. } )().compute( countOfPoints ).onInit( () => {
  75. // initialize point positions and speeds
  76. renderer.compute( updateSkinningPoints().compute( countOfPoints ) );
  77. } );
  78. const pointCloud = new THREE.Sprite( materialPoints );
  79. pointCloud.count = countOfPoints;
  80. scene.add( pointCloud );
  81. }
  82. } );
  83. object.scale.set( 100, 100, 100 );
  84. object.rotation.x = - Math.PI / 2;
  85. scene.add( object );
  86. } );
  87. //renderer
  88. renderer = new THREE.WebGPURenderer( { antialias: true } );
  89. renderer.setPixelRatio( window.devicePixelRatio );
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. renderer.setAnimationLoop( animate );
  92. document.body.appendChild( renderer.domElement );
  93. window.addEventListener( 'resize', onWindowResize );
  94. }
  95. function onWindowResize() {
  96. camera.aspect = window.innerWidth / window.innerHeight;
  97. camera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. function animate() {
  101. const delta = clock.getDelta();
  102. if ( mixer ) mixer.update( delta );
  103. renderer.render( scene, camera );
  104. }
  105. </script>
  106. </body>
  107. </html>
粤ICP备19079148号