webgl_lights_deferred_morphs.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - deferred rendering [morphs]</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. background-color: #000;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. #info {
  14. position: absolute;
  15. top: 20px; width: 100%;
  16. color: #ffffff;
  17. padding: 5px;
  18. font-family: Monospace;
  19. font-size: 13px;
  20. text-align: center;
  21. }
  22. a {
  23. color: #ff0080;
  24. text-decoration: none;
  25. }
  26. a:hover {
  27. color: #0080ff;
  28. }
  29. #stats { position: absolute; top:10px; left: 5px }
  30. #stats #fps { background: transparent !important }
  31. #stats #fps #fpsText { color: #aaa !important }
  32. #stats #fps #fpsGraph { display: none }
  33. </style>
  34. </head>
  35. <body>
  36. <div id="info">
  37. <a href="http://threejs.org" target="_blank">three.js</a> - webgl deferred rendering with morph animation -
  38. character from <a href="http://www.sintel.org/">Sintel</a>
  39. </div>
  40. <div id="container"></div>
  41. <script src="js/libs/stats.min.js"></script>
  42. <script src="../build/three.min.js"></script>
  43. <script src="js/Detector.js"></script>
  44. <script src="js/ShaderDeferred.js"></script>
  45. <script src="js/DeferredHelper.js"></script>
  46. <script src="js/shaders/CopyShader.js"></script>
  47. <script src="js/shaders/FXAAShader.js"></script>
  48. <script src="js/shaders/ColorCorrectionShader.js"></script>
  49. <script src="js/postprocessing/EffectComposer.js"></script>
  50. <script src="js/postprocessing/RenderPass.js"></script>
  51. <script src="js/postprocessing/ShaderPass.js"></script>
  52. <script src="js/postprocessing/MaskPass.js"></script>
  53. <script>
  54. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  55. var SCALE = 0.75;
  56. var MARGIN = 100;
  57. var WIDTH = window.innerWidth;
  58. var HEIGHT = window.innerHeight - 2 * MARGIN;
  59. var SCALED_WIDTH = Math.floor( SCALE * WIDTH );
  60. var SCALED_HEIGHT = Math.floor( SCALE * HEIGHT );
  61. var NEAR = 1.0, FAR = 350.0;
  62. var VIEW_ANGLE = 45;
  63. // controls
  64. var mouseX = 0;
  65. var mouseY = 0;
  66. var targetX = 0, targetY = 0;
  67. var angle = 0;
  68. var target = new THREE.Vector3( 0, 0, 0 );
  69. var windowHalfX = window.innerWidth / 2;
  70. var windowHalfY = window.innerHeight / 2;
  71. // core
  72. var renderer, camera, scene, controls, stats, clock;
  73. // lights
  74. var numLights = 50;
  75. var lights = [];
  76. // morphs
  77. var morphs = [];
  78. //
  79. init();
  80. animate();
  81. // -----------------------------
  82. function init() {
  83. // renderer
  84. renderer = new THREE.WebGLRenderer( { alpha: false } );
  85. renderer.setSize( WIDTH, HEIGHT );
  86. renderer.setClearColorHex( 0x000000, 1 );
  87. renderer.domElement.style.position = "absolute";
  88. renderer.domElement.style.top = MARGIN + "px";
  89. renderer.domElement.style.left = "0px";
  90. var container = document.getElementById( 'container' );
  91. container.appendChild( renderer.domElement );
  92. // camera
  93. camera = new THREE.PerspectiveCamera( VIEW_ANGLE, WIDTH / HEIGHT, NEAR, FAR );
  94. camera.position.z = 150;
  95. // scene
  96. scene = new THREE.Scene();
  97. scene.add( camera );
  98. // stats
  99. stats = new Stats();
  100. stats.domElement.style.position = 'absolute';
  101. stats.domElement.style.top = '8px';
  102. stats.domElement.style.zIndex = 100;
  103. container.appendChild( stats.domElement );
  104. // clock
  105. clock = new THREE.Clock();
  106. // deferred helper
  107. deferredHelper = new THREE.DeferredHelper( { renderer: renderer, width: SCALED_WIDTH, height: SCALED_HEIGHT, additiveSpecular: true } );
  108. // add lights
  109. initLights();
  110. // add objects
  111. initObjects();
  112. // events
  113. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  114. window.addEventListener( 'resize', onWindowResize, false );
  115. }
  116. // -----------------------------
  117. function addObject( object, y, scale ) {
  118. object.position.y = y;
  119. object.scale.set( scale, scale, scale );
  120. scene.add( object );
  121. }
  122. // -----------------------------
  123. function initLights() {
  124. var distance = 40;
  125. // front light
  126. var light = new THREE.PointLight( 0xffffff, 1.5, 1.5 * distance );
  127. scene.add( light );
  128. lights.push( light );
  129. // random lights
  130. var c = new THREE.Vector3();
  131. for ( var i = 1; i < numLights; i ++ ) {
  132. var light = new THREE.PointLight( 0xffffff, 2.0, distance );
  133. c.set( Math.random(), Math.random(), Math.random() ).normalize();
  134. light.color.setRGB( c.x, c.y, c.z );
  135. light.color.convertGammaToLinear();
  136. scene.add( light );
  137. lights.push( light );
  138. }
  139. }
  140. function initObjects() {
  141. // add animated model
  142. var loader = new THREE.JSONLoader();
  143. loader.load( "models/animated/elderlyWalk.js", function( geometry ) {
  144. geometry.computeMorphNormals();
  145. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x333333, shininess: 20, morphTargets: true, morphNormals: true, vertexColors: THREE.NoColors, shading: THREE.FlatShading } );
  146. var meshAnim = new THREE.MorphAnimMesh( geometry, material );
  147. meshAnim.duration = 3000;
  148. meshAnim.properties.delta = -13;
  149. var s = 1;
  150. meshAnim.scale.set( s, s, s );
  151. meshAnim.position.x = 180;
  152. meshAnim.position.z = -10;
  153. meshAnim.rotation.y = -Math.PI/2;
  154. morphs.push( meshAnim );
  155. addObject( meshAnim, -48, 50 );
  156. } );
  157. // add box
  158. var object = generateBox();
  159. addObject( object, 0, 8 );
  160. }
  161. // -----------------------------
  162. function generateBox() {
  163. var object = new THREE.Object3D();
  164. var mapHeight2 = THREE.ImageUtils.loadTexture( "obj/lightmap/rocks.jpg" );
  165. mapHeight2.repeat.set( 3, 1.5 );
  166. mapHeight2.wrapS = mapHeight2.wrapT = THREE.RepeatWrapping;
  167. mapHeight2.anisotropy = 4;
  168. mapHeight2.format = THREE.RGBFormat;
  169. var mapHeight3 = THREE.ImageUtils.loadTexture( "textures/water.jpg" );
  170. mapHeight3.repeat.set( 16, 8 );
  171. mapHeight3.wrapS = mapHeight3.wrapT = THREE.RepeatWrapping;
  172. mapHeight3.anisotropy = 4;
  173. mapHeight3.format = THREE.RGBFormat;
  174. var geoPlane = new THREE.PlaneGeometry( 40, 20 );
  175. var matPlaneSide = new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x222222, shininess: 75, bumpMap: mapHeight2, bumpScale: 0.5 } );
  176. var matPlaneBottom = new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x222222, shininess: 75, bumpMap: mapHeight3, bumpScale: 0.5 } );
  177. var matPlaneTop = new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x222222, shininess: 75, bumpMap: mapHeight3, bumpScale: 1 } );
  178. // bottom
  179. var mesh = new THREE.Mesh( geoPlane, matPlaneBottom );
  180. mesh.position.z = -2;
  181. mesh.position.y = -6;
  182. mesh.rotation.x = -Math.PI/2;
  183. object.add( mesh );
  184. // top
  185. var mesh = new THREE.Mesh( geoPlane, matPlaneTop );
  186. mesh.position.z = -2;
  187. mesh.position.y = 7;
  188. mesh.rotation.x = Math.PI/2;
  189. object.add( mesh );
  190. // back
  191. var mesh = new THREE.Mesh( geoPlane, matPlaneSide );
  192. mesh.position.z = -4;
  193. mesh.position.y = 0;
  194. object.add( mesh );
  195. // right
  196. var mesh = new THREE.Mesh( geoPlane, matPlaneSide );
  197. mesh.position.z = 0;
  198. mesh.position.y = 0;
  199. mesh.position.x = 13;
  200. mesh.rotation.y = -Math.PI/2;
  201. //object.add( mesh );
  202. // left
  203. var mesh = new THREE.Mesh( geoPlane, matPlaneSide );
  204. mesh.position.z = 0;
  205. mesh.position.y = 0;
  206. mesh.position.x = -13;
  207. mesh.rotation.y = Math.PI/2;
  208. //object.add( mesh );
  209. return object;
  210. }
  211. // -----------------------------
  212. function onWindowResize( event ) {
  213. windowHalfX = window.innerWidth / 2;
  214. windowHalfY = window.innerHeight / 2;
  215. WIDTH = window.innerWidth;
  216. HEIGHT = window.innerHeight - 2 * MARGIN;
  217. SCALED_WIDTH = Math.floor( SCALE * WIDTH );
  218. SCALED_HEIGHT = Math.floor( SCALE * HEIGHT );
  219. renderer.setSize( WIDTH, HEIGHT );
  220. deferredHelper.setSize( SCALED_WIDTH, SCALED_HEIGHT );
  221. camera.aspect = WIDTH / HEIGHT;
  222. camera.updateProjectionMatrix();
  223. }
  224. function onDocumentMouseMove( event ) {
  225. mouseX = ( event.clientX - windowHalfX ) * 1;
  226. mouseY = ( event.clientY - windowHalfY ) * 1;
  227. }
  228. // -----------------------------
  229. function animate() {
  230. requestAnimationFrame( animate );
  231. render();
  232. stats.update();
  233. }
  234. function render() {
  235. var delta = clock.getDelta();
  236. var time = Date.now() * 0.0005;
  237. // update lights
  238. var x, y, z;
  239. for ( var i = 0, il = lights.length; i < il; i ++ ) {
  240. var light = lights[ i ];
  241. if ( i > 0 ) {
  242. x = Math.sin( time + i * 1.7 ) * 80;
  243. y = Math.cos( time + i * 1.5 ) * 40;
  244. z = Math.cos( time + i * 1.3 ) * 30;
  245. } else {
  246. x = Math.sin( time * 3 ) * 20;
  247. y = 15;
  248. z = Math.cos( time * 3 ) * 25 + 10;
  249. }
  250. light.position.set( x, y, z );
  251. }
  252. // update morphs
  253. for ( var i = 0; i < morphs.length; i ++ ) {
  254. morph = morphs[ i ];
  255. morph.updateAnimation( 1000 * delta );
  256. morph.position.x += morph.properties.delta * delta;
  257. if ( morph.position.x < -50 ) morph.position.x = 200;
  258. }
  259. // update controls
  260. targetX = mouseX * .001;
  261. targetY = mouseY * .001;
  262. angle += 0.05 * ( targetX - angle );
  263. camera.position.x = -Math.sin( angle ) * 150;
  264. camera.position.z = Math.cos( angle ) * 150;
  265. camera.lookAt( target );
  266. deferredHelper.render( scene, camera );
  267. }
  268. </script>
  269. </body>
  270. </html>
粤ICP备19079148号