2
0

webgldeferred_arealights.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 area lights WebGL demo by <a href="http://de.redplant.de" target=_blank>redPlant</a> -
  38. based on <a href="http://www.gamedev.net/topic/552315-glsl-area-light-implementation/" target=_blank>ArKano22's</a> glsl implementation
  39. </div>
  40. <div id="container"></div>
  41. <script src="../build/three.min.js"></script>
  42. <script src="js/loaders/BinaryLoader.js"></script>
  43. <script src="js/renderers/WebGLDeferredRenderer.js"></script>
  44. <script src="js/ShaderDeferred.js"></script>
  45. <script src="js/shaders/CopyShader.js"></script>
  46. <script src="js/shaders/FXAAShader.js"></script>
  47. <script src="js/shaders/ConvolutionShader.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 src="js/postprocessing/BloomPass.js"></script>
  53. <script src="js/Detector.js"></script>
  54. <script src="js/libs/stats.min.js"></script>
  55. <script>
  56. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  57. var SCALE = 1;
  58. var MARGIN = 100;
  59. var WIDTH = window.innerWidth;
  60. var HEIGHT = window.innerHeight - 2 * MARGIN;
  61. var NEAR = 1.0, FAR = 350.0;
  62. var VIEW_ANGLE = 40;
  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, stats, clock;
  73. // lights
  74. var areaLight1, areaLight2, areaLight3;
  75. //
  76. init();
  77. animate();
  78. // -----------------------------
  79. function init() {
  80. // renderer
  81. renderer = new THREE.WebGLDeferredRenderer( { width: WIDTH, height: HEIGHT, scale: SCALE, antialias: true, tonemapping: THREE.FilmicOperator, brightness: 2.5 } );
  82. renderer.domElement.style.position = "absolute";
  83. renderer.domElement.style.top = MARGIN + "px";
  84. renderer.domElement.style.left = "0px";
  85. var container = document.getElementById( 'container' );
  86. container.appendChild( renderer.domElement );
  87. // effects
  88. var bloomEffect = new THREE.BloomPass( 0.65 );
  89. renderer.addEffect( bloomEffect );
  90. // camera
  91. camera = new THREE.PerspectiveCamera( VIEW_ANGLE, WIDTH / HEIGHT, NEAR, FAR );
  92. camera.position.y = 40;
  93. // scene
  94. scene = new THREE.Scene();
  95. scene.add( camera );
  96. // stats
  97. stats = new Stats();
  98. stats.domElement.style.position = 'absolute';
  99. stats.domElement.style.top = '8px';
  100. stats.domElement.style.zIndex = 100;
  101. container.appendChild( stats.domElement );
  102. // clock
  103. clock = new THREE.Clock();
  104. // add lights
  105. initLights();
  106. // add objects
  107. initObjects();
  108. // events
  109. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  110. window.addEventListener( 'resize', onWindowResize, false );
  111. }
  112. // -----------------------------
  113. function createAreaEmitter( light ) {
  114. var geometry = new THREE.CubeGeometry( 1, 1, 1 );
  115. var material = new THREE.MeshBasicMaterial( { color: light.color.getHex(), vertexColors: THREE.FaceColors } );
  116. var backColor = 0x222222;
  117. geometry.faces[ 5 ].color.setHex( backColor );
  118. geometry.faces[ 4 ].color.setHex( backColor );
  119. geometry.faces[ 2 ].color.setHex( backColor );
  120. geometry.faces[ 1 ].color.setHex( backColor );
  121. geometry.faces[ 0 ].color.setHex( backColor );
  122. var emitter = new THREE.Mesh( geometry, material );
  123. emitter.position = light.position;
  124. emitter.quaternion = light.quaternion;
  125. emitter.scale.set( light.width * 2, 0.2, light.height * 2 );
  126. return emitter;
  127. }
  128. function setupAreaLight( light ) {
  129. var matrix = light.matrixWorld;
  130. light.right.set( 1, 0, 0 );
  131. light.normal.set( 0, -1, 0 );
  132. light.right.applyMatrix4( matrix );
  133. light.normal.applyMatrix4( matrix );
  134. }
  135. function initLights() {
  136. areaLight1 = new THREE.AreaLight( 0xffffff, 1 );
  137. areaLight1.position.set( 0.0001, 10.0001, -18.5001 );
  138. areaLight1.rotation.set( -0.74719, 0.0001, 0.0001 );
  139. areaLight1.width = 10;
  140. areaLight1.height = 1;
  141. scene.add( areaLight1 );
  142. var meshEmitter = createAreaEmitter( areaLight1 );
  143. scene.add( meshEmitter );
  144. //
  145. areaLight2 = new THREE.AreaLight( 0x33ff66, 1.5 );
  146. areaLight2.position.set( -19.0001, 3.0001, 0.0001 );
  147. areaLight2.rotation.set( -1.5707, 0.0001, 1.5707 );
  148. areaLight2.width = 8;
  149. areaLight2.height = 1;
  150. scene.add( areaLight2 );
  151. var meshEmitter = createAreaEmitter( areaLight2 );
  152. scene.add( meshEmitter );
  153. //
  154. areaLight3 = new THREE.AreaLight( 0x3366ff, 1.5 );
  155. areaLight3.position.set( 19.0001, 3.0001, 0.0001 );
  156. areaLight3.rotation.set( 1.5707, 0.0001, -1.5707 );
  157. areaLight3.width = 8;
  158. areaLight3.height = 1;
  159. scene.add( areaLight3 );
  160. var meshEmitter = createAreaEmitter( areaLight3 );
  161. scene.add( meshEmitter );
  162. }
  163. // -----------------------------
  164. function initObjects() {
  165. var loader = new THREE.BinaryLoader();
  166. loader.load( "obj/box/box.js", function ( geometry, materials ) {
  167. var material = new THREE.MeshPhongMaterial( { color: 0xffaa55, specular: 0x888888, shininess: 200 } );
  168. var object = new THREE.Mesh( geometry, material );
  169. object.scale.multiplyScalar( 2 );
  170. scene.add( object );
  171. } );
  172. /*
  173. var loader = new THREE.BinaryLoader();
  174. loader.load( "obj/veyron/VeyronNoUv_bin.js", function ( geometry ) {
  175. var r = "textures/cube/Bridge2/";
  176. var urls = [
  177. r + "posx.jpg", r + "negx.jpg",
  178. r + "posy.jpg", r + "negy.jpg",
  179. r + "posz.jpg", r + "negz.jpg"
  180. ];
  181. var textureCube = THREE.ImageUtils.loadTextureCube( urls );
  182. textureCube.format = THREE.RGBFormat;
  183. var materials = [
  184. // tires + inside
  185. new THREE.MeshLambertMaterial( {
  186. color: 0x050505
  187. } ),
  188. // wheels + extras chrome
  189. new THREE.MeshLambertMaterial( {
  190. color: 0xffffff,
  191. envMap: textureCube
  192. } ),
  193. // back / top / front torso
  194. new THREE.MeshLambertMaterial( {
  195. color: 0x000000,
  196. ambient: 0x000000,
  197. envMap: textureCube,
  198. combine: THREE.MixOperation,
  199. reflectivity: 0.15
  200. } ),
  201. // glass
  202. new THREE.MeshLambertMaterial( {
  203. color: 0x101046,
  204. envMap: textureCube,
  205. opacity: 0.25,
  206. transparent: true
  207. } ),
  208. // sides torso
  209. new THREE.MeshLambertMaterial( {
  210. color: 0xffffff,
  211. envMap: textureCube
  212. } ),
  213. // engine
  214. new THREE.MeshLambertMaterial( {
  215. color: 0xffffff,
  216. envMap: textureCube
  217. } ),
  218. // backlights
  219. new THREE.MeshLambertMaterial( {
  220. color: 0xff0000,
  221. opacity: 0.5,
  222. transparent: true
  223. } ),
  224. // backsignals
  225. new THREE.MeshLambertMaterial( {
  226. color: 0xffbb00,
  227. opacity: 0.5,
  228. transparent: true
  229. } )
  230. ];
  231. var object = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );
  232. object.position.y = 5;
  233. object.rotation.y = Math.PI / 4;
  234. object.scale.multiplyScalar( 0.1 );
  235. scene.add( object );
  236. } );
  237. */
  238. }
  239. // -----------------------------
  240. function onWindowResize( event ) {
  241. windowHalfX = window.innerWidth / 2;
  242. windowHalfY = window.innerHeight / 2;
  243. WIDTH = window.innerWidth;
  244. HEIGHT = window.innerHeight - 2 * MARGIN;
  245. renderer.setSize( WIDTH, HEIGHT );
  246. camera.aspect = WIDTH / HEIGHT;
  247. camera.updateProjectionMatrix();
  248. }
  249. function onDocumentMouseMove( event ) {
  250. mouseX = ( event.clientX - windowHalfX ) * 1;
  251. mouseY = ( event.clientY - windowHalfY ) * 1;
  252. }
  253. // -----------------------------
  254. function animate() {
  255. requestAnimationFrame( animate );
  256. render();
  257. stats.update();
  258. }
  259. function render() {
  260. // update camera
  261. var delta = clock.getDelta();
  262. targetX = mouseX * .001;
  263. targetY = mouseY * .001;
  264. angle += 0.05 * ( targetX - angle );
  265. camera.position.x = -Math.sin( angle ) * 40;
  266. camera.position.z = Math.cos( angle ) * 40;
  267. camera.lookAt( target );
  268. var time = Date.now();
  269. areaLight1.position.x = Math.sin( Date.now() * 0.001 ) * 9;
  270. areaLight1.position.y = Math.sin( Date.now() * 0.0013 ) * 5 + 5;
  271. areaLight2.position.y = Math.sin( Date.now() * 0.0011 ) * 3 + 5;
  272. areaLight2.position.z = Math.sin( Date.now() * 0.00113 ) * 10;
  273. areaLight3.position.y = Math.sin( Date.now() * 0.00111 ) * 3 + 5;
  274. areaLight3.position.z = Math.sin( Date.now() * 0.001113 ) * 10;
  275. // render
  276. renderer.render( scene, camera );
  277. }
  278. </script>
  279. </body>
  280. </html>