webgl_buffergeometry_morphtargets.html 4.5 KB

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