webgl_lights_deferred_pointlights.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - deferred rendering</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> - deferred point lights WebGL demo by <a href="http://de.redplant.de" target=_blank>redPlant</a> -
  38. <a href="http://www.ir-ltd.net/infinite-3d-head-scan-released/" target="_blank">Lee Perry-Smith</a> head -
  39. light attenuation formula by <a href="http://imdoingitwrong.wordpress.com/tag/glsl/" target=_blank>Tom Madams</a>
  40. </div>
  41. <div id="container"></div>
  42. <script src="../build/three.min.js"></script>
  43. <script src="js/Detector.js"></script>
  44. <script src="js/libs/stats.min.js"></script>
  45. <script src="js/ShaderDeferred.js"></script>
  46. <script src="js/DeferredHelper.js"></script>
  47. <script src="js/shaders/CopyShader.js"></script>
  48. <script src="js/shaders/FXAAShader.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. <!--
  54. <script src="js/loaders/UTF8Loader.js"></script>
  55. <script src="js/loaders/MTLLoader.js"></script>
  56. -->
  57. <script>
  58. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  59. var SCALE = 0.75;
  60. var MARGIN = 100;
  61. var WIDTH = window.innerWidth;
  62. var HEIGHT = window.innerHeight - 2 * MARGIN;
  63. var SCALED_WIDTH = Math.floor( SCALE * WIDTH );
  64. var SCALED_HEIGHT = Math.floor( SCALE * HEIGHT );
  65. var NEAR = 1.0, FAR = 350.0;
  66. var VIEW_ANGLE = 45;
  67. // controls
  68. var mouseX = 0;
  69. var mouseY = 0;
  70. var targetX = 0, targetY = 0;
  71. var angle = 0;
  72. var target = new THREE.Vector3( 0, 0, 0 );
  73. var windowHalfX = window.innerWidth / 2;
  74. var windowHalfY = window.innerHeight / 2;
  75. // core
  76. var renderer, camera, scene, stats, clock;
  77. // lights
  78. var numLights = 50;
  79. var lights = [];
  80. //
  81. init();
  82. animate();
  83. // -----------------------------
  84. function init() {
  85. // renderer
  86. renderer = new THREE.WebGLRenderer( { alpha: false } );
  87. renderer.setSize( WIDTH, HEIGHT );
  88. renderer.setClearColorHex( 0x000000, 1 );
  89. renderer.autoClear = false;
  90. renderer.domElement.style.position = "absolute";
  91. renderer.domElement.style.top = MARGIN + "px";
  92. renderer.domElement.style.left = "0px";
  93. var container = document.getElementById( 'container' );
  94. container.appendChild( renderer.domElement );
  95. // camera
  96. camera = new THREE.PerspectiveCamera( VIEW_ANGLE, WIDTH / HEIGHT, NEAR, FAR );
  97. camera.position.z = 150;
  98. // scene
  99. scene = new THREE.Scene();
  100. scene.add( camera );
  101. // stats
  102. stats = new Stats();
  103. stats.domElement.style.position = 'absolute';
  104. stats.domElement.style.top = '8px';
  105. stats.domElement.style.zIndex = 100;
  106. container.appendChild( stats.domElement );
  107. // clock
  108. clock = new THREE.Clock();
  109. // deferred helper
  110. deferredHelper = new THREE.DeferredHelper( { renderer: renderer, width: SCALED_WIDTH, height: SCALED_HEIGHT, scale: SCALE, additiveSpecular: false, multiply: 2 } );
  111. // add lights
  112. initLights();
  113. // add objects
  114. initObjects();
  115. // events
  116. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  117. window.addEventListener( 'resize', onWindowResize, false );
  118. }
  119. // -----------------------------
  120. function initLights() {
  121. var distance = 40;
  122. // front light
  123. var light = new THREE.PointLight( 0xffffff, 1.5, 1.5 * distance );
  124. scene.add( light );
  125. lights.push( light );
  126. // random lights
  127. var c = new THREE.Vector3();
  128. for ( var i = 1; i < numLights; i ++ ) {
  129. var light = new THREE.PointLight( 0xffffff, 2.0, distance );
  130. c.set( Math.random(), Math.random(), Math.random() ).normalize();
  131. light.color.setRGB( c.x, c.y, c.z );
  132. light.color.convertGammaToLinear();
  133. scene.add( light );
  134. lights.push( light );
  135. }
  136. var geometry = new THREE.SphereGeometry( 0.7, 7, 7 );
  137. for ( var i = 0; i < numLights; i ++ ) {
  138. var light = lights[ i ];
  139. var material = new THREE.MeshBasicMaterial();
  140. material.color = light.color;
  141. var emitter = new THREE.Mesh( geometry, material );
  142. emitter.position = light.position;
  143. scene.add( emitter );
  144. }
  145. }
  146. // -----------------------------
  147. function initObjects() {
  148. /*
  149. var loader = new THREE.UTF8Loader();
  150. loader.load( "models/utf8/ben_dds.js", function ( object ) {
  151. object.scale.multiplyScalar( 150 );
  152. object.position.y = -75;
  153. scene.add( object );
  154. }, { normalizeRGB: true } );
  155. loader.load( "models/utf8/WaltHi.js", function ( object ) {
  156. object.position.y = -35;
  157. scene.add( object );
  158. }, { normalizeRGB: true } );
  159. */
  160. var loader = new THREE.JSONLoader();
  161. loader.load( "obj/leeperrysmith/LeePerrySmith.js", function( geometry, materials ) {
  162. var mapColor = THREE.ImageUtils.loadTexture( "obj/leeperrysmith/Map-COL.jpg" );
  163. var mapHeight = THREE.ImageUtils.loadTexture( "obj/leeperrysmith/Infinite-Level_02_Disp_NoSmoothUV-4096.jpg" );
  164. mapHeight.repeat.set( 0.998, 0.998 );
  165. mapHeight.offset.set( 0.001, 0.001 )
  166. mapHeight.wrapS = mapHeight.wrapT = THREE.RepeatWrapping;
  167. mapHeight.anisotropy = 4;
  168. mapHeight.format = THREE.RGBFormat;
  169. var material = new THREE.MeshPhongMaterial( { map: mapColor, bumpMap: mapHeight, bumpScale: 2.5, shininess: 75, specular: 0x090909 } );
  170. var object = new THREE.Mesh( geometry, material );
  171. object.scale.multiplyScalar( 8 );
  172. scene.add( object );
  173. } );
  174. var loader = new THREE.BinaryLoader();
  175. loader.load( "obj/female02/Female02_bin.js", function( geometry, materials ) {
  176. var material = new THREE.MeshPhongMaterial( { shininess: 175, specular: 0x999999 } );
  177. var object = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );
  178. object.position.x = -50;
  179. object.position.y = -48;
  180. object.scale.multiplyScalar( 0.45 );
  181. scene.add( object );
  182. } );
  183. loader.load( "obj/male02/Male02_bin.js", function( geometry, materials ) {
  184. var material = new THREE.MeshPhongMaterial( { shininess: 175, specular: 0x999999 } );
  185. var object = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );
  186. object.position.x = 50;
  187. object.position.y = -48;
  188. object.scale.multiplyScalar( 0.45 );
  189. scene.add( object );
  190. } );
  191. // create box
  192. var box = generateBox();
  193. box.scale.multiplyScalar( 8 );
  194. scene.add( box );
  195. }
  196. // -----------------------------
  197. function generateBox() {
  198. var object = new THREE.Object3D();
  199. var mapHeight2 = THREE.ImageUtils.loadTexture( "obj/lightmap/rocks.jpg" );
  200. mapHeight2.repeat.set( 3, 1.5 );
  201. mapHeight2.wrapS = mapHeight2.wrapT = THREE.RepeatWrapping;
  202. mapHeight2.anisotropy = 4;
  203. mapHeight2.format = THREE.RGBFormat;
  204. var mapHeight3 = THREE.ImageUtils.loadTexture( "textures/water.jpg" );
  205. mapHeight3.repeat.set( 16, 8 );
  206. mapHeight3.wrapS = mapHeight3.wrapT = THREE.RepeatWrapping;
  207. mapHeight3.anisotropy = 4;
  208. mapHeight3.format = THREE.RGBFormat;
  209. var geoPlane = new THREE.PlaneGeometry( 40, 20 );
  210. var matPlane = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 50, bumpMap: mapHeight2, bumpScale: 0.5 } );
  211. var matPlane2 = new THREE.MeshPhongMaterial( { color: 0x331919, specular: 0x111111, shininess: 50, bumpMap: mapHeight2, bumpScale: 1 } );
  212. var matPlane3 = new THREE.MeshPhongMaterial( { color: 0x00aaff, specular: 0xffffff, shininess: 200, bumpMap: mapHeight3, bumpScale: 1.2 } );
  213. var matPlane4 = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 50, bumpMap: mapHeight3, bumpScale: 1 } );
  214. // bottom
  215. var mesh = new THREE.Mesh( geoPlane, matPlane3 );
  216. mesh.position.z = -2;
  217. mesh.position.y = -6;
  218. mesh.rotation.x = -Math.PI/2;
  219. object.add( mesh );
  220. // top
  221. var mesh = new THREE.Mesh( geoPlane, matPlane4 );
  222. mesh.position.z = -2;
  223. mesh.position.y = 6;
  224. mesh.rotation.x = Math.PI/2;
  225. object.add( mesh );
  226. // back
  227. var mesh = new THREE.Mesh( geoPlane, matPlane );
  228. mesh.position.z = -4;
  229. mesh.position.y = 0;
  230. object.add( mesh );
  231. // right
  232. var mesh = new THREE.Mesh( geoPlane, matPlane );
  233. mesh.position.z = 0;
  234. mesh.position.y = 0;
  235. mesh.position.x = 13;
  236. mesh.rotation.y = -Math.PI/2;
  237. //object.add( mesh );
  238. // left
  239. var mesh = new THREE.Mesh( geoPlane, matPlane );
  240. mesh.position.z = 0;
  241. mesh.position.y = 0;
  242. mesh.position.x = -13;
  243. mesh.rotation.y = Math.PI/2;
  244. //object.add( mesh );
  245. return object;
  246. }
  247. // -----------------------------
  248. function onWindowResize( event ) {
  249. windowHalfX = window.innerWidth / 2;
  250. windowHalfY = window.innerHeight / 2;
  251. WIDTH = window.innerWidth;
  252. HEIGHT = window.innerHeight - 2 * MARGIN;
  253. SCALED_WIDTH = Math.floor( SCALE * WIDTH );
  254. SCALED_HEIGHT = Math.floor( SCALE * HEIGHT );
  255. renderer.setSize( WIDTH, HEIGHT );
  256. deferredHelper.setSize( SCALED_WIDTH, SCALED_HEIGHT );
  257. camera.aspect = WIDTH / HEIGHT;
  258. camera.updateProjectionMatrix();
  259. }
  260. function onDocumentMouseMove( event ) {
  261. mouseX = ( event.clientX - windowHalfX ) * 1;
  262. mouseY = ( event.clientY - windowHalfY ) * 1;
  263. }
  264. // -----------------------------
  265. function animate() {
  266. requestAnimationFrame( animate );
  267. render();
  268. stats.update();
  269. }
  270. function render() {
  271. // update lights
  272. var time = Date.now() * 0.0005;
  273. var x, y, z;
  274. for ( var i = 0, il = lights.length; i < il; i ++ ) {
  275. var light = lights[ i ];
  276. if ( i > 0 ) {
  277. x = Math.sin( time + i * 1.7 ) * 80;
  278. y = Math.cos( time + i * 1.5 ) * 40;
  279. z = Math.cos( time + i * 1.3 ) * 30;
  280. } else {
  281. x = Math.sin( time * 3 ) * 20;
  282. y = 15;
  283. z = Math.cos( time * 3 ) * 25 + 10;
  284. }
  285. light.position.set( x, y, z );
  286. }
  287. var delta = clock.getDelta();
  288. targetX = mouseX * .001;
  289. targetY = mouseY * .001;
  290. angle += 0.05 * ( targetX - angle );
  291. camera.position.x = -Math.sin( angle ) * 150;
  292. camera.position.z = Math.cos( angle ) * 150;
  293. camera.lookAt( target );
  294. deferredHelper.render( scene, camera );
  295. }
  296. </script>
  297. </body>
  298. </html>
粤ICP备19079148号