webgpu_morphtargets.html 4.9 KB

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