webgpu_skinning_points.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. <meta property="og:title" content="three.js webgpu - skinning points">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_skinning_points.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_skinning_points.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 Points</span>
  18. </div>
  19. <small>
  20. Colors and scale of the points are based on the speed of the animation.
  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 { color, computeSkinning, objectWorldMatrix, instancedArray, instanceIndex, Fn, shapeCircle } from 'three/tsl';
  36. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  37. let camera, scene, renderer;
  38. let mixer, timer;
  39. init();
  40. function init() {
  41. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  42. camera.position.set( 0, 300, - 85 );
  43. scene = new THREE.Scene();
  44. scene.background = new THREE.Color( 0x111111 );
  45. camera.lookAt( 0, 0, - 85 );
  46. scene.add( new THREE.AmbientLight( 0xffffff, 10 ) );
  47. timer = new THREE.Timer();
  48. timer.connect( document );
  49. const loader = new GLTFLoader();
  50. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  51. const object = gltf.scene;
  52. mixer = new THREE.AnimationMixer( object );
  53. const action = mixer.clipAction( gltf.animations[ 0 ] );
  54. action.play();
  55. object.traverse( function ( child ) {
  56. if ( child.isMesh ) {
  57. child.visible = false;
  58. const countOfPoints = child.geometry.getAttribute( 'position' ).count;
  59. const pointPositionArray = instancedArray( countOfPoints, 'vec3' ).setPBO( true );
  60. const pointSpeedArray = instancedArray( countOfPoints, 'vec3' ).setPBO( true );
  61. const pointSpeedAttribute = pointSpeedArray.toAttribute();
  62. const skinningPosition = computeSkinning( child );
  63. const materialPoints = new THREE.PointsNodeMaterial();
  64. materialPoints.colorNode = pointSpeedAttribute.mul( .6 ).mix( color( 0x0066ff ), color( 0xff9000 ) );
  65. materialPoints.opacityNode = shapeCircle();
  66. materialPoints.sizeNode = pointSpeedAttribute.length().exp().min( 5 ).mul( 5 ).add( 1 );
  67. materialPoints.sizeAttenuation = false;
  68. materialPoints.alphaTest = 0.5;
  69. const updateSkinningPoints = Fn( () => {
  70. const pointPosition = pointPositionArray.element( instanceIndex );
  71. const pointSpeed = pointSpeedArray.element( instanceIndex );
  72. const skinningWorldPosition = objectWorldMatrix( child ).mul( skinningPosition );
  73. const skinningSpeed = skinningWorldPosition.sub( pointPosition );
  74. pointSpeed.assign( skinningSpeed );
  75. pointPosition.assign( skinningWorldPosition );
  76. }, 'void' );
  77. materialPoints.positionNode = Fn( () => {
  78. updateSkinningPoints();
  79. return pointPositionArray.toAttribute();
  80. } )().compute( countOfPoints ).onInit( () => {
  81. // initialize point positions and speeds
  82. renderer.compute( updateSkinningPoints().compute( countOfPoints ) );
  83. } );
  84. const pointCloud = new THREE.Sprite( materialPoints );
  85. pointCloud.count = countOfPoints;
  86. scene.add( pointCloud );
  87. }
  88. } );
  89. object.scale.set( 100, 100, 100 );
  90. object.rotation.x = - Math.PI / 2;
  91. scene.add( object );
  92. } );
  93. //renderer
  94. renderer = new THREE.WebGPURenderer( { antialias: true } );
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. renderer.setAnimationLoop( animate );
  98. document.body.appendChild( renderer.domElement );
  99. window.addEventListener( 'resize', onWindowResize );
  100. }
  101. function onWindowResize() {
  102. camera.aspect = window.innerWidth / window.innerHeight;
  103. camera.updateProjectionMatrix();
  104. renderer.setSize( window.innerWidth, window.innerHeight );
  105. }
  106. function animate() {
  107. timer.update();
  108. const delta = timer.getDelta();
  109. if ( mixer ) mixer.update( delta );
  110. renderer.render( scene, camera );
  111. }
  112. </script>
  113. </body>
  114. </html>
粤ICP备19079148号