webgl_morphtargets.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - animation - 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> - WebGL morph target example
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { GUI } from './jsm/libs/dat.gui.module.js';
  17. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  18. var container;
  19. var camera, scene, renderer, raycaster;
  20. var mesh, mouse = new THREE.Vector2();
  21. init();
  22. animate();
  23. function init() {
  24. container = document.getElementById( 'container' );
  25. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  26. camera.position.z = 500;
  27. scene = new THREE.Scene();
  28. scene.background = new THREE.Color( 0x222222 );
  29. scene.fog = new THREE.Fog( 0x000000, 1, 15000 );
  30. var light = new THREE.PointLight( 0xffffff );
  31. light.position.z = 500;
  32. camera.add( light );
  33. scene.add( camera );
  34. var light = new THREE.AmbientLight( 0x111111 );
  35. scene.add( light );
  36. var geometry = new THREE.BoxGeometry( 100, 100, 100 );
  37. var material = new THREE.MeshLambertMaterial( { color: 0xff0000, morphTargets: true } );
  38. // construct 8 blend shapes
  39. for ( var i = 0; i < 8; i ++ ) {
  40. var vertices = [];
  41. for ( var v = 0; v < geometry.vertices.length; v ++ ) {
  42. vertices.push( geometry.vertices[ v ].clone() );
  43. if ( v === i ) {
  44. vertices[ vertices.length - 1 ].x *= 2;
  45. vertices[ vertices.length - 1 ].y *= 2;
  46. vertices[ vertices.length - 1 ].z *= 2;
  47. }
  48. }
  49. geometry.morphTargets.push( { name: "target" + i, vertices: vertices } );
  50. }
  51. geometry = new THREE.BufferGeometry().fromGeometry( geometry );
  52. mesh = new THREE.Mesh( geometry, material );
  53. scene.add( mesh );
  54. //
  55. var params = {
  56. influence1: 0,
  57. influence2: 0,
  58. influence3: 0,
  59. influence4: 0,
  60. influence5: 0,
  61. influence6: 0,
  62. influence7: 0,
  63. influence8: 0
  64. };
  65. var gui = new GUI();
  66. var folder = gui.addFolder( 'Morph Targets' );
  67. folder.add( params, 'influence1', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  68. mesh.morphTargetInfluences[ 0 ] = value;
  69. } );
  70. folder.add( params, 'influence2', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  71. mesh.morphTargetInfluences[ 1 ] = value;
  72. } );
  73. folder.add( params, 'influence3', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  74. mesh.morphTargetInfluences[ 2 ] = value;
  75. } );
  76. folder.add( params, 'influence4', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  77. mesh.morphTargetInfluences[ 3 ] = value;
  78. } );
  79. folder.add( params, 'influence5', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  80. mesh.morphTargetInfluences[ 4 ] = value;
  81. } );
  82. folder.add( params, 'influence6', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  83. mesh.morphTargetInfluences[ 5 ] = value;
  84. } );
  85. folder.add( params, 'influence7', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  86. mesh.morphTargetInfluences[ 6 ] = value;
  87. } );
  88. folder.add( params, 'influence8', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
  89. mesh.morphTargetInfluences[ 7 ] = value;
  90. } );
  91. folder.open();
  92. //
  93. raycaster = new THREE.Raycaster();
  94. renderer = new THREE.WebGLRenderer();
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. container.appendChild( renderer.domElement );
  98. //
  99. var controls = new OrbitControls( camera, renderer.domElement );
  100. controls.minDistance = 400;
  101. controls.maxDistance = 1000;
  102. //
  103. window.addEventListener( 'resize', onWindowResize, false );
  104. document.addEventListener( 'click', onClick, false );
  105. }
  106. function onClick( event ) {
  107. event.preventDefault();
  108. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  109. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  110. raycaster.setFromCamera( mouse, camera );
  111. var intersects = raycaster.intersectObject( mesh );
  112. if ( intersects.length > 0 ) {
  113. mesh.material.color.set( Math.random() * 0xffffff );
  114. }
  115. }
  116. function onWindowResize() {
  117. camera.aspect = window.innerWidth / window.innerHeight;
  118. camera.updateProjectionMatrix();
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. }
  121. function animate() {
  122. requestAnimationFrame( animate );
  123. render();
  124. }
  125. function render() {
  126. mesh.rotation.y += 0.01;
  127. renderer.render( scene, camera );
  128. }
  129. </script>
  130. </body>
  131. </html>