webgl_buffergeometry_morphtargets.html 4.6 KB

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