webgl_morphnormals.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - morph normals - horse</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. background-color: #111;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. a { color: #f00 }
  16. #stats { position: absolute; top:0; left: 0 }
  17. #stats #fps { background: transparent !important }
  18. #stats #fps #fpsText { color: #666 !important }
  19. #stats #fps #fpsGraph { display: none }
  20. </style>
  21. </head>
  22. <body>
  23. <script src="../build/three.min.js"></script>
  24. <script src="js/Detector.js"></script>
  25. <script src="js/libs/stats.min.js"></script>
  26. <script>
  27. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  28. var SCREEN_WIDTH = window.innerWidth;
  29. var SCREEN_HEIGHT = window.innerHeight;
  30. var container, stats;
  31. var camera, scene, renderer;
  32. var mixers = [];
  33. var clock = new THREE.Clock();
  34. init();
  35. animate();
  36. function init() {
  37. container = document.createElement( 'div' );
  38. document.body.appendChild( container );
  39. var info = document.createElement( 'div' );
  40. info.style.position = 'absolute';
  41. info.style.top = '10px';
  42. info.style.width = '100%';
  43. info.style.textAlign = 'center';
  44. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - morph normals - model by <a href="http://mirada.com/">mirada</a> from <a href="http://ro.me">rome</a>';
  45. container.appendChild( info );
  46. //
  47. camera = new THREE.PerspectiveCamera( 40, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  48. camera.position.y = 300;
  49. camera.target = new THREE.Vector3( 0, 150, 0 );
  50. scene = new THREE.Scene();
  51. //
  52. scene.add( new THREE.HemisphereLight( 0x443333, 0x222233 ) );
  53. var light = new THREE.DirectionalLight( 0xffffff, 1 );
  54. light.position.set( 1, 1, 1 );
  55. scene.add( light );
  56. //
  57. var loader = new THREE.JSONLoader();
  58. loader.load( "models/animated/flamingo.js", function( geometry ) {
  59. var material = new THREE.MeshPhongMaterial( {
  60. color: 0xffffff,
  61. morphTargets: true,
  62. vertexColors: THREE.FaceColors,
  63. shading: THREE.FlatShading
  64. } );
  65. var mesh = new THREE.Mesh( geometry, material );
  66. mesh.position.x = - 150;
  67. mesh.position.y = 150;
  68. mesh.scale.set( 1.5, 1.5, 1.5 );
  69. scene.add( mesh );
  70. var mixer = new THREE.AnimationMixer( mesh );
  71. mixer.clipAction( geometry.animations[ 0 ] ).setDuration( 1 ).play();
  72. mixers.push( mixer );
  73. } );
  74. loader.load( "models/animated/flamingo.js", function( geometry ) {
  75. geometry.computeVertexNormals();
  76. geometry.computeMorphNormals();
  77. var material = new THREE.MeshPhongMaterial( {
  78. color: 0xffffff,
  79. morphTargets: true,
  80. morphNormals: true,
  81. vertexColors: THREE.FaceColors,
  82. shading: THREE.SmoothShading
  83. } );
  84. var mesh = new THREE.Mesh( geometry, material );
  85. mesh.position.x = 150;
  86. mesh.position.y = 150;
  87. mesh.scale.set( 1.5, 1.5, 1.5 );
  88. scene.add( mesh );
  89. var mixer = new THREE.AnimationMixer( mesh );
  90. mixer.clipAction( geometry.animations[ 0 ] ).setDuration( 1 ).play();
  91. mixers.push( mixer );
  92. } );
  93. //
  94. renderer = new THREE.WebGLRenderer( { antialias: true } );
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  97. container.appendChild( renderer.domElement );
  98. //
  99. stats = new Stats();
  100. container.appendChild( stats.domElement );
  101. //
  102. window.addEventListener( 'resize', onWindowResize, false );
  103. }
  104. //
  105. function onWindowResize( event ) {
  106. SCREEN_WIDTH = window.innerWidth;
  107. SCREEN_HEIGHT = window.innerHeight;
  108. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  109. camera.aspect = 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT;
  110. camera.updateProjectionMatrix();
  111. }
  112. //
  113. function animate() {
  114. requestAnimationFrame( animate );
  115. render();
  116. stats.update();
  117. }
  118. var radius = 600;
  119. var theta = 0;
  120. function render() {
  121. theta += 0.1;
  122. camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
  123. camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
  124. camera.lookAt( camera.target );
  125. var delta = clock.getDelta();
  126. for ( var i = 0; i < mixers.length; i ++ ) {
  127. mixers[ i ].update( delta );
  128. }
  129. renderer.clear();
  130. renderer.render( scene, camera );
  131. }
  132. </script>
  133. </body>
  134. </html>