webgl_morphtargets.html 4.7 KB

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