webgpu_skinning_points.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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, timer;
  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. timer = new THREE.Timer();
  44. timer.connect( document );
  45. const loader = new GLTFLoader();
  46. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  47. const object = gltf.scene;
  48. mixer = new THREE.AnimationMixer( object );
  49. const action = mixer.clipAction( gltf.animations[ 0 ] );
  50. action.play();
  51. object.traverse( function ( child ) {
  52. if ( child.isMesh ) {
  53. child.visible = false;
  54. const countOfPoints = child.geometry.getAttribute( 'position' ).count;
  55. const pointPositionArray = instancedArray( countOfPoints, 'vec3' ).setPBO( true );
  56. const pointSpeedArray = instancedArray( countOfPoints, 'vec3' ).setPBO( true );
  57. const pointSpeedAttribute = pointSpeedArray.toAttribute();
  58. const skinningPosition = computeSkinning( child );
  59. const materialPoints = new THREE.PointsNodeMaterial();
  60. materialPoints.colorNode = pointSpeedAttribute.mul( .6 ).mix( color( 0x0066ff ), color( 0xff9000 ) );
  61. materialPoints.opacityNode = shapeCircle();
  62. materialPoints.sizeNode = pointSpeedAttribute.length().exp().min( 5 ).mul( 5 ).add( 1 );
  63. materialPoints.sizeAttenuation = false;
  64. materialPoints.alphaTest = 0.5;
  65. const updateSkinningPoints = Fn( () => {
  66. const pointPosition = pointPositionArray.element( instanceIndex );
  67. const pointSpeed = pointSpeedArray.element( instanceIndex );
  68. const skinningWorldPosition = objectWorldMatrix( child ).mul( skinningPosition );
  69. const skinningSpeed = skinningWorldPosition.sub( pointPosition );
  70. pointSpeed.assign( skinningSpeed );
  71. pointPosition.assign( skinningWorldPosition );
  72. }, 'void' );
  73. materialPoints.positionNode = Fn( () => {
  74. updateSkinningPoints();
  75. return pointPositionArray.toAttribute();
  76. } )().compute( countOfPoints ).onInit( () => {
  77. // initialize point positions and speeds
  78. renderer.compute( updateSkinningPoints().compute( countOfPoints ) );
  79. } );
  80. const pointCloud = new THREE.Sprite( materialPoints );
  81. pointCloud.count = countOfPoints;
  82. scene.add( pointCloud );
  83. }
  84. } );
  85. object.scale.set( 100, 100, 100 );
  86. object.rotation.x = - Math.PI / 2;
  87. scene.add( object );
  88. } );
  89. //renderer
  90. renderer = new THREE.WebGPURenderer( { antialias: true } );
  91. renderer.setPixelRatio( window.devicePixelRatio );
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. renderer.setAnimationLoop( animate );
  94. document.body.appendChild( renderer.domElement );
  95. window.addEventListener( 'resize', onWindowResize );
  96. }
  97. function onWindowResize() {
  98. camera.aspect = window.innerWidth / window.innerHeight;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. }
  102. function animate() {
  103. timer.update();
  104. const delta = timer.getDelta();
  105. if ( mixer ) mixer.update( delta );
  106. renderer.render( scene, camera );
  107. }
  108. </script>
  109. </body>
  110. </html>
粤ICP备19079148号