webgpu_morphtargets.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - morph targets</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="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - morph targets<br/>
  13. by <a href="https://discoverthreejs.com/" target="_blank" rel="noopener">Discover three.js</a>
  14. </div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/",
  20. "three/nodes": "./jsm/nodes/Nodes.js"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  29. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  30. let container, camera, scene, renderer, mesh;
  31. init();
  32. function init() {
  33. if ( WebGPU.isAvailable() === false ) {
  34. document.body.appendChild( WebGPU.getErrorMessage() );
  35. throw new Error( 'No WebGPU support' );
  36. }
  37. container = document.getElementById( 'container' );
  38. scene = new THREE.Scene();
  39. scene.background = new THREE.Color( 0x8FBCD4 );
  40. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 20 );
  41. camera.position.z = 10;
  42. scene.add( camera );
  43. scene.add( new THREE.AmbientLight( 0x8FBCD4, 1.5 ) );
  44. const pointLight = new THREE.PointLight( 0xffffff, 200 );
  45. camera.add( pointLight );
  46. const geometry = createGeometry();
  47. const material = new THREE.MeshPhongMaterial( {
  48. color: 0xff0000,
  49. flatShading: true
  50. } );
  51. mesh = new THREE.Mesh( geometry, material );
  52. scene.add( mesh );
  53. initGUI();
  54. renderer = new WebGPURenderer( { antialias: true } );
  55. renderer.setPixelRatio( window.devicePixelRatio );
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. renderer.setAnimationLoop( function () {
  58. renderer.render( scene, camera );
  59. } );
  60. container.appendChild( renderer.domElement );
  61. const controls = new OrbitControls( camera, renderer.domElement );
  62. controls.enableZoom = false;
  63. window.addEventListener( 'resize', onWindowResize );
  64. }
  65. function createGeometry() {
  66. const geometry = new THREE.BoxGeometry( 2, 2, 2, 32, 32, 32 );
  67. // create an empty array to hold targets for the attribute we want to morph
  68. // morphing positions and normals is supported
  69. geometry.morphAttributes.position = [];
  70. // the original positions of the cube's vertices
  71. const positionAttribute = geometry.attributes.position;
  72. // for the first morph target we'll move the cube's vertices onto the surface of a sphere
  73. const spherePositions = [];
  74. // for the second morph target, we'll twist the cubes vertices
  75. const twistPositions = [];
  76. const direction = new THREE.Vector3( 1, 0, 0 );
  77. const vertex = new THREE.Vector3();
  78. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  79. const x = positionAttribute.getX( i );
  80. const y = positionAttribute.getY( i );
  81. const z = positionAttribute.getZ( i );
  82. spherePositions.push(
  83. x * Math.sqrt( 1 - ( y * y / 2 ) - ( z * z / 2 ) + ( y * y * z * z / 3 ) ),
  84. y * Math.sqrt( 1 - ( z * z / 2 ) - ( x * x / 2 ) + ( z * z * x * x / 3 ) ),
  85. z * Math.sqrt( 1 - ( x * x / 2 ) - ( y * y / 2 ) + ( x * x * y * y / 3 ) )
  86. );
  87. // stretch along the x-axis so we can see the twist better
  88. vertex.set( x * 2, y, z );
  89. vertex.applyAxisAngle( direction, Math.PI * x / 2 ).toArray( twistPositions, twistPositions.length );
  90. }
  91. // add the spherical positions as the first morph target
  92. geometry.morphAttributes.position[ 0 ] = new THREE.Float32BufferAttribute( spherePositions, 3 );
  93. // add the twisted positions as the second morph target
  94. geometry.morphAttributes.position[ 1 ] = new THREE.Float32BufferAttribute( twistPositions, 3 );
  95. return geometry;
  96. }
  97. function initGUI() {
  98. // Set up dat.GUI to control targets
  99. const params = {
  100. Spherify: 0,
  101. Twist: 0,
  102. };
  103. const gui = new GUI( { title: 'Morph Targets' } );
  104. gui.add( params, 'Spherify', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  105. mesh.morphTargetInfluences[ 0 ] = value;
  106. } );
  107. gui.add( params, 'Twist', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  108. mesh.morphTargetInfluences[ 1 ] = value;
  109. } );
  110. }
  111. function onWindowResize() {
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. }
  116. </script>
  117. </body>
  118. </html>