webgl_lights_deferred_animation.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - deferred rendering [morphing + skinning]</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 morphing and skinning animations -
  38. characters from <a href="http://www.sintel.org/">Sintel</a> and by <a href="http://opengameart.org/content/walk-cycles">Clint Bellanger</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/renderers/WebGLDeferredRenderer.js"></script>
  45. <script src="js/ShaderDeferred.js"></script>
  46. <script src="js/shaders/CopyShader.js"></script>
  47. <script src="js/shaders/FXAAShader.js"></script>
  48. <script src="js/postprocessing/EffectComposer.js"></script>
  49. <script src="js/postprocessing/RenderPass.js"></script>
  50. <script src="js/postprocessing/ShaderPass.js"></script>
  51. <script src="js/postprocessing/MaskPass.js"></script>
  52. <script>
  53. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  54. var SCALE = 0.75;
  55. var MARGIN = 100;
  56. var WIDTH = window.innerWidth;
  57. var HEIGHT = window.innerHeight - 2 * MARGIN;
  58. var NEAR = 1.0, FAR = 350.0;
  59. var VIEW_ANGLE = 45;
  60. // controls
  61. var mouseX = 0;
  62. var mouseY = 0;
  63. var targetX = 0, targetY = 0;
  64. var angle = 0;
  65. var target = new THREE.Vector3( 0, 0, 0 );
  66. var windowHalfX = window.innerWidth / 2;
  67. var windowHalfY = window.innerHeight / 2;
  68. // core
  69. var renderer, camera, scene, controls, stats, clock;
  70. // lights
  71. var numLights = 50;
  72. var lights = [];
  73. // morphs
  74. var morphs = [];
  75. // skins
  76. var skins = [];
  77. //
  78. init();
  79. animate();
  80. // -----------------------------
  81. function init() {
  82. // renderer
  83. renderer = new THREE.WebGLDeferredRenderer( { width: WIDTH, height: HEIGHT, scale: SCALE, brightness: 2, antialias: true } );
  84. renderer.domElement.style.position = "absolute";
  85. renderer.domElement.style.top = MARGIN + "px";
  86. renderer.domElement.style.left = "0px";
  87. var container = document.getElementById( 'container' );
  88. container.appendChild( renderer.domElement );
  89. // camera
  90. camera = new THREE.PerspectiveCamera( VIEW_ANGLE, WIDTH / HEIGHT, NEAR, FAR );
  91. camera.position.z = 150;
  92. // scene
  93. scene = new THREE.Scene();
  94. scene.add( camera );
  95. // stats
  96. stats = new Stats();
  97. stats.domElement.style.position = 'absolute';
  98. stats.domElement.style.top = '8px';
  99. stats.domElement.style.zIndex = 100;
  100. container.appendChild( stats.domElement );
  101. // clock
  102. clock = new THREE.Clock();
  103. // add lights
  104. initLights();
  105. // add objects
  106. initObjects();
  107. // events
  108. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  109. window.addEventListener( 'resize', onWindowResize, false );
  110. }
  111. // -----------------------------
  112. function initLights() {
  113. var distance = 40;
  114. // front light
  115. var light = new THREE.PointLight( 0xffffff, 1.5, 1.5 * distance );
  116. scene.add( light );
  117. lights.push( light );
  118. // random lights
  119. var c = new THREE.Vector3();
  120. for ( var i = 1; i < numLights; i ++ ) {
  121. var light = new THREE.PointLight( 0xffffff, 2.0, distance );
  122. c.set( Math.random(), Math.random(), Math.random() ).normalize();
  123. light.color.setRGB( c.x, c.y, c.z );
  124. scene.add( light );
  125. lights.push( light );
  126. }
  127. var geometry = new THREE.SphereGeometry( 0.7, 7, 7 );
  128. for ( var i = 0; i < numLights; i ++ ) {
  129. var light = lights[ i ];
  130. var material = new THREE.MeshBasicMaterial();
  131. material.color = light.color;
  132. var emitter = new THREE.Mesh( geometry, material );
  133. emitter.position = light.position;
  134. scene.add( emitter );
  135. }
  136. }
  137. function ensureLoop( animation ) {
  138. for ( var i = 0; i < animation.hierarchy.length; i ++ ) {
  139. var bone = animation.hierarchy[ i ];
  140. var first = bone.keys[ 0 ];
  141. var last = bone.keys[ bone.keys.length - 1 ];
  142. last.pos = first.pos;
  143. last.rot = first.rot;
  144. last.scl = first.scl;
  145. }
  146. }
  147. function initObjects() {
  148. // add animated model
  149. var loader = new THREE.JSONLoader();
  150. loader.load( "models/animated/elderlyWalk.js", function( geometry ) {
  151. geometry.computeMorphNormals();
  152. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x333333, shininess: 20, wrapAround: true, morphTargets: true, morphNormals: true, vertexColors: THREE.NoColors, shading: THREE.FlatShading } );
  153. var meshAnim = new THREE.MorphAnimMesh( geometry, material );
  154. meshAnim.duration = 3000;
  155. meshAnim.properties.delta = -13;
  156. meshAnim.scale.multiplyScalar( 50 );
  157. meshAnim.position.set( 180, -48, -10 );
  158. meshAnim.rotation.y = -Math.PI/2;
  159. scene.add( meshAnim );
  160. morphs.push( meshAnim );
  161. } );
  162. loader.load( "models/skinned/human_walk_0_female.js", function( geometry, materials ) {
  163. geometry.computeVertexNormals();
  164. geometry.computeBoundingBox();
  165. ensureLoop( geometry.animation );
  166. THREE.AnimationHandler.add( geometry.animation );
  167. for ( var i = 0, il = materials.length; i < il; i ++ ) {
  168. var originalMaterial = materials[ i ];
  169. originalMaterial.skinning = true;
  170. originalMaterial.map = undefined;
  171. originalMaterial.shading = THREE.SmoothShading;
  172. originalMaterial.color.setHSV( 0.01, 0.515, 0.5 );
  173. originalMaterial.ambient.copy( originalMaterial.color );
  174. originalMaterial.specular.setHSV( 0, 0, 0.1 );
  175. originalMaterial.shininess = 75;
  176. originalMaterial.wrapAround = true;
  177. originalMaterial.wrapRGB.set( 1, 0.5, 0.5 );
  178. }
  179. var s = 18.5;
  180. var material = new THREE.MeshFaceMaterial( materials );
  181. var mesh = new THREE.SkinnedMesh( geometry, material, false );
  182. mesh.scale.set( s, s, s );
  183. mesh.rotation.y = Math.PI/2;
  184. mesh.position.x = -100;
  185. mesh.position.y = -geometry.boundingBox.min.y * s - 48;
  186. mesh.position.z = 18;
  187. mesh.properties.delta = 25;
  188. scene.add( mesh );
  189. skins.push( mesh );
  190. animation = new THREE.Animation( mesh, "ActionFemale" );
  191. animation.JITCompile = false;
  192. animation.interpolationType = THREE.AnimationHandler.LINEAR;
  193. animation.play( true );
  194. animation.update( 0 );
  195. } );
  196. // add box
  197. var box = generateBox();
  198. box.scale.multiplyScalar( 8 );
  199. scene.add( box );
  200. }
  201. // -----------------------------
  202. function generateBox() {
  203. var object = new THREE.Object3D();
  204. var mapHeight2 = THREE.ImageUtils.loadTexture( "obj/lightmap/rocks.jpg" );
  205. mapHeight2.repeat.set( 3, 1.5 );
  206. mapHeight2.wrapS = mapHeight2.wrapT = THREE.RepeatWrapping;
  207. mapHeight2.anisotropy = 4;
  208. mapHeight2.format = THREE.RGBFormat;
  209. var mapHeight3 = THREE.ImageUtils.loadTexture( "textures/water.jpg" );
  210. mapHeight3.repeat.set( 16, 8 );
  211. mapHeight3.wrapS = mapHeight3.wrapT = THREE.RepeatWrapping;
  212. mapHeight3.anisotropy = 4;
  213. mapHeight3.format = THREE.RGBFormat;
  214. var geoPlane = new THREE.PlaneGeometry( 40, 20 );
  215. var matPlaneSide = new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x222222, shininess: 75, bumpMap: mapHeight2, bumpScale: 0.5 } );
  216. var matPlaneBottom = new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x222222, shininess: 75, bumpMap: mapHeight3, bumpScale: 0.5 } );
  217. var matPlaneTop = new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x222222, shininess: 75, bumpMap: mapHeight3, bumpScale: 1 } );
  218. // bottom
  219. var mesh = new THREE.Mesh( geoPlane, matPlaneBottom );
  220. mesh.position.z = -2;
  221. mesh.position.y = -6;
  222. mesh.rotation.x = -Math.PI/2;
  223. object.add( mesh );
  224. // top
  225. var mesh = new THREE.Mesh( geoPlane, matPlaneTop );
  226. mesh.position.z = -2;
  227. mesh.position.y = 7;
  228. mesh.rotation.x = Math.PI/2;
  229. object.add( mesh );
  230. // back
  231. var mesh = new THREE.Mesh( geoPlane, matPlaneSide );
  232. mesh.position.z = -4;
  233. mesh.position.y = 0;
  234. object.add( mesh );
  235. // right
  236. var mesh = new THREE.Mesh( geoPlane, matPlaneSide );
  237. mesh.position.z = 0;
  238. mesh.position.y = 0;
  239. mesh.position.x = 13;
  240. mesh.rotation.y = -Math.PI/2;
  241. //object.add( mesh );
  242. // left
  243. var mesh = new THREE.Mesh( geoPlane, matPlaneSide );
  244. mesh.position.z = 0;
  245. mesh.position.y = 0;
  246. mesh.position.x = -13;
  247. mesh.rotation.y = Math.PI/2;
  248. //object.add( mesh );
  249. return object;
  250. }
  251. // -----------------------------
  252. function onWindowResize( event ) {
  253. windowHalfX = window.innerWidth / 2;
  254. windowHalfY = window.innerHeight / 2;
  255. WIDTH = window.innerWidth;
  256. HEIGHT = window.innerHeight - 2 * MARGIN;
  257. renderer.setSize( WIDTH, HEIGHT );
  258. camera.aspect = WIDTH / HEIGHT;
  259. camera.updateProjectionMatrix();
  260. }
  261. function onDocumentMouseMove( event ) {
  262. mouseX = ( event.clientX - windowHalfX ) * 1;
  263. mouseY = ( event.clientY - windowHalfY ) * 1;
  264. }
  265. // -----------------------------
  266. function animate() {
  267. requestAnimationFrame( animate );
  268. render();
  269. stats.update();
  270. }
  271. function render() {
  272. var delta = clock.getDelta();
  273. var time = Date.now() * 0.0005;
  274. // update lights
  275. var x, y, z;
  276. for ( var i = 0, il = lights.length; i < il; i ++ ) {
  277. var light = lights[ i ];
  278. if ( i > 0 ) {
  279. x = Math.sin( time + i * 1.7 ) * 80;
  280. y = Math.cos( time + i * 1.5 ) * 40;
  281. z = Math.cos( time + i * 1.3 ) * 30;
  282. } else {
  283. x = Math.sin( time * 3 ) * 20;
  284. y = 15;
  285. z = Math.cos( time * 3 ) * 25 + 10;
  286. }
  287. light.position.set( x, y, z );
  288. }
  289. // update morphs
  290. for ( var i = 0; i < morphs.length; i ++ ) {
  291. var morph = morphs[ i ];
  292. morph.updateAnimation( 1000 * delta );
  293. morph.position.x += morph.properties.delta * delta;
  294. if ( morph.position.x < -50 ) morph.position.x = 200;
  295. }
  296. // update skins
  297. THREE.AnimationHandler.update( 0.4 * delta );
  298. for ( var i = 0; i < skins.length; i ++ ) {
  299. var skin = skins[ i ];
  300. skin.position.x += skin.properties.delta * delta;
  301. if ( skin.position.x > 200 ) skin.position.x = -200;
  302. }
  303. // update controls
  304. targetX = mouseX * .001;
  305. targetY = mouseY * .001;
  306. angle += 0.05 * ( targetX - angle );
  307. camera.position.x = -Math.sin( angle ) * 150;
  308. camera.position.z = Math.cos( angle ) * 150;
  309. camera.lookAt( target );
  310. // render
  311. renderer.render( scene, camera );
  312. }
  313. </script>
  314. </body>
  315. </html>