webgpu_skinning_points.html 4.0 KB

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