webgl_buffergeometry_morphtargets.html 4.4 KB

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