webgl_buffergeometry_morphtargets.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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="http://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 {
  22. AmbientLight,
  23. BoxBufferGeometry,
  24. Color,
  25. Float32BufferAttribute,
  26. Mesh,
  27. MeshPhongMaterial,
  28. PerspectiveCamera,
  29. PointLight,
  30. Scene,
  31. Vector3,
  32. WebGLRenderer
  33. } from "../build/three.module.js";
  34. import { GUI } from './jsm/libs/dat.gui.module.js';
  35. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  36. var container, camera, scene, renderer, mesh;
  37. function init() {
  38. scene = new Scene();
  39. scene.background = new Color( 0x8FBCD4 );
  40. scene.add( new AmbientLight( 0x8FBCD4, 0.4 ) );
  41. container = document.getElementById( 'container' );
  42. camera = new PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 20 );
  43. camera.position.z = 8;
  44. scene.add( camera );
  45. var controls = new OrbitControls( camera, container );
  46. var pointLight = new PointLight( 0xffffff, 1 );
  47. camera.add( pointLight );
  48. var geometry = createGeometry();
  49. var material = new MeshPhongMaterial( {
  50. color: 0xff0000,
  51. flatShading: true,
  52. morphTargets: true
  53. } );
  54. mesh = new Mesh( geometry, material );
  55. scene.add( mesh );
  56. initGUI();
  57. renderer = new WebGLRenderer( { antialias: true } );
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.setAnimationLoop( function () {
  61. renderer.render( scene, camera );
  62. } );
  63. container.appendChild( renderer.domElement );
  64. window.addEventListener( 'resize', onWindowResize, false );
  65. }
  66. function createGeometry() {
  67. var geometry = new BoxBufferGeometry( 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. var positions = geometry.attributes.position.array;
  73. // for the first morph target we'll move the cube's vertices onto the surface of a sphere
  74. var spherePositions = [];
  75. // for the second morph target, we'll twist the cubes vertices
  76. var twistPositions = [];
  77. var direction = new Vector3( 1, 0, 0 ).normalize();
  78. var vertex = new Vector3();
  79. for ( var i = 0; i < positions.length; i += 3 ) {
  80. var x = positions[ i ];
  81. var y = positions[ i + 1 ];
  82. var z = positions[ i + 2 ];
  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 Float32BufferAttribute( spherePositions, 3 );
  94. // add the twisted positions as the second morph target
  95. geometry.morphAttributes.position[ 1 ] = new Float32BufferAttribute( twistPositions, 3 );
  96. return geometry;
  97. }
  98. function initGUI() {
  99. // Set up dat.GUI to control targets
  100. var params = {
  101. Spherify: 0,
  102. Twist: 0,
  103. };
  104. var gui = new GUI();
  105. var folder = gui.addFolder( 'Morph Targets' );
  106. folder.add( params, 'Spherify', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  107. mesh.morphTargetInfluences[ 0 ] = value;
  108. } );
  109. folder.add( params, 'Twist', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  110. mesh.morphTargetInfluences[ 1 ] = value;
  111. } );
  112. folder.open();
  113. }
  114. function onWindowResize() {
  115. camera.aspect = window.innerWidth / window.innerHeight;
  116. camera.updateProjectionMatrix();
  117. renderer.setSize( window.innerWidth, window.innerHeight );
  118. }
  119. init();
  120. </script>
  121. </body>
  122. </html>