webgl_lights_deferred_pointlights.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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: 0px; 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. </style>
  30. </head>
  31. <body>
  32. <div id="info">
  33. <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>.<br />
  34. Walt Disney head by <a href="http://davidoreilly.com/post/18087489343/disneyhead" target="_blank">David OReilly</a><br>
  35. Point Light attenuation formula by <a href="http://imdoingitwrong.wordpress.com/tag/glsl/" target=_blank>Tom Madams</a>
  36. </div>
  37. <div id="container"></div>
  38. <script src="../build/three.min.js"></script>
  39. <script src="js/Detector.js"></script>
  40. <script src="js/libs/stats.min.js"></script>
  41. <script src="js/shaders/CopyShader.js"></script>
  42. <script src="js/shaders/FXAAShader.js"></script>
  43. <script src="js/postprocessing/EffectComposer.js"></script>
  44. <script src="js/postprocessing/RenderPass.js"></script>
  45. <script src="js/postprocessing/ShaderPass.js"></script>
  46. <script src="js/postprocessing/MaskPass.js"></script>
  47. <script src="js/controls/TrackballControls.js"></script>
  48. <script src="js/loaders/ctm/lzma.js"></script>
  49. <script src="js/loaders/ctm/ctm.js"></script>
  50. <script src="js/loaders/ctm/CTMLoader.js"></script>
  51. <script src="js/loaders/UTF8Loader.js"></script>
  52. <script src="js/loaders/MTLLoader.js"></script>
  53. <script>
  54. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  55. var WIDTH = window.innerWidth;
  56. var HEIGHT = window.innerHeight;
  57. var NEAR = 1.0, FAR = 250.0;
  58. var VIEW_ANGLE = 45;
  59. var ASPECT = WIDTH / HEIGHT;
  60. // core
  61. var renderer, camera, controls, stats, clock;
  62. // scenes and scene nodes
  63. var lightScene, lightNode, scene, sceneNode, emitterScene, emitterNode, quadScene, quadNode;
  64. // rendertargets
  65. var rtColor, rtNormals, rtDepth, rtLightBuffer, rtEmitter;
  66. // composer
  67. var compColor, compNormals, compDepth, compLightBuffer, compFinal, compEmitter, compositePass;
  68. var effectFXAA;
  69. // materials
  70. var matNormal, matClipDepth, matBasic, matUnlit;
  71. var numLights = 50;
  72. var lights = new Array();
  73. // -----------------------
  74. // shader definitions
  75. // -----------------------
  76. var clipdepth_frag = ""+
  77. "varying vec4 clipPos;"+
  78. "void main() {"+
  79. "gl_FragColor = vec4( clipPos.z / clipPos.w, 1.0, 1.0, 1.0 );"+
  80. "}";
  81. var clipdepth_vert = "" +
  82. "varying vec4 clipPos;"+
  83. "void main() {"+
  84. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );"+
  85. "clipPos = gl_Position;"+
  86. "}";
  87. // -----------------------
  88. var normals_vert = "" +
  89. "varying vec3 normalView;"+
  90. "void main() {"+
  91. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );"+
  92. "normalView = normalize( normalMatrix * normal );"+
  93. "}";
  94. var normals_frag = "" +
  95. "varying vec3 normalView;"+
  96. "void main() {"+
  97. "gl_FragColor = vec4( vec3( normalView * 0.5 + 0.5 ), 1.0 );"+
  98. "}";
  99. // -----------------------
  100. var bump_vert = "" +
  101. "varying vec3 normalView;"+
  102. "varying vec2 vUv;"+
  103. "varying vec3 vViewPosition;"+
  104. "uniform vec4 offsetRepeat;"+
  105. "void main() {"+
  106. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );"+
  107. "gl_Position = projectionMatrix * mvPosition;"+
  108. "normalView = normalize( normalMatrix * normal );"+
  109. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;"+
  110. "vViewPosition = -mvPosition.xyz;"+
  111. "}";
  112. var bump_frag = "" +
  113. "#extension GL_OES_standard_derivatives : enable\n"+
  114. "varying vec3 normalView;"+
  115. "varying vec2 vUv;"+
  116. "varying vec3 vViewPosition;"+
  117. "uniform sampler2D bumpMap;"+
  118. "uniform float bumpScale;"+
  119. // Derivative maps - bump mapping unparametrized surfaces by Morten Mikkelsen
  120. // http://mmikkelsen3d.blogspot.sk/2011/07/derivative-maps.html
  121. // Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)
  122. "vec2 dHdxy_fwd() {"+
  123. "vec2 dSTdx = dFdx( vUv );"+
  124. "vec2 dSTdy = dFdy( vUv );"+
  125. "float Hll = bumpScale * texture2D( bumpMap, vUv ).x;"+
  126. "float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;"+
  127. "float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;"+
  128. "return vec2( dBx, dBy );"+
  129. "}"+
  130. "vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {"+
  131. "vec3 vSigmaX = dFdx( surf_pos );"+
  132. "vec3 vSigmaY = dFdy( surf_pos );"+
  133. "vec3 vN = surf_norm;"+ // normalized
  134. "vec3 R1 = cross( vSigmaY, vN );"+
  135. "vec3 R2 = cross( vN, vSigmaX );"+
  136. "float fDet = dot( vSigmaX, R1 );"+
  137. "vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );"+
  138. "return normalize( abs( fDet ) * surf_norm - vGrad );"+
  139. "}"+
  140. "void main() {"+
  141. "vec3 normal = normalize( normalView );"+
  142. "normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );"+
  143. "gl_FragColor = vec4( vec3( normal * 0.5 + 0.5 ), 1.0 );"+
  144. "}";
  145. // -----------------------
  146. var unlit_vert = "" +
  147. "varying vec4 clipPos;"+
  148. "void main() {"+
  149. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );"+
  150. "clipPos = gl_Position;"+
  151. "}";
  152. var unlit_frag = "" +
  153. "varying vec4 clipPos;"+
  154. "uniform sampler2D samplerDepth;"+
  155. "uniform float viewHeight;"+
  156. "uniform float viewWidth;"+
  157. "uniform vec3 lightColor;" +
  158. "void main() {"+
  159. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );"+
  160. "float z = texture2D( samplerDepth, texCoord ).x;"+
  161. "vec4 color = vec4( lightColor, 1.0 );"+
  162. "float depth = clipPos.z / clipPos.w;"+
  163. "if( depth > z && z > 0.0 ) color.w = 0.0;"+
  164. "gl_FragColor = color;"+
  165. "}";
  166. // -----------------------
  167. var deferredlight_vert = "" +
  168. "varying vec3 lightView;" +
  169. "uniform vec3 lightPos;" +
  170. "uniform mat4 matView;" +
  171. "void main() { " +
  172. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );"+
  173. "lightView = vec3( matView * vec4( lightPos, 1.0 ) );" +
  174. "}"
  175. var deferredlight_frag = "" +
  176. "varying vec3 lightView;"+
  177. "uniform sampler2D samplerColor;"+
  178. "uniform sampler2D samplerDepth;"+
  179. "uniform sampler2D samplerNormals;"+
  180. "uniform sampler2D samplerLightBuffer;"+
  181. "uniform float lightRadius;"+
  182. "uniform float lightIntensity;"+
  183. "uniform float viewHeight;"+
  184. "uniform float viewWidth;"+
  185. "uniform vec3 lightColor;"+
  186. "uniform mat4 matProjInverse;"+
  187. "void main() {"+
  188. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );"+
  189. "float z = texture2D( samplerDepth, texCoord ).x;"+
  190. "if ( z == 0.0 ) {"+
  191. "gl_FragColor = vec4( vec3( 0.0 ), 1.0 );"+
  192. "return;"+
  193. "}"+
  194. "float x = texCoord.x * 2.0 - 1.0;"+
  195. "float y = texCoord.y * 2.0 - 1.0;"+
  196. "vec4 projectedPos = vec4( x, y, z, 1.0 );"+
  197. "vec4 viewPos = matProjInverse * projectedPos;"+
  198. "viewPos.xyz /= viewPos.w;"+
  199. "viewPos.w = 1.0;"+
  200. "vec3 lightDir = lightView - viewPos.xyz;"+
  201. "float dist = length( lightDir );"+
  202. "lightDir = normalize( lightDir );"+
  203. "float cutoff = 0.3;"+
  204. "float denom = dist/lightRadius + 1.0;"+
  205. "float attenuation = 1.0 / ( denom * denom );"+
  206. "attenuation = ( attenuation - cutoff ) / ( 1.0 - cutoff );"+
  207. "attenuation = max( attenuation, 0.0 );"+
  208. "vec3 normal = texture2D( samplerNormals, texCoord ).xyz * 2.0 - 1.0;" +
  209. // wrap around lighting
  210. "float diffuseFull = max( dot( normal, lightDir ), 0.0 );" +
  211. "float diffuseHalf = max( 0.5 + 0.5 * dot( normal, lightDir ), 0.0 );" +
  212. "const vec3 wrapRGB = vec3( 0.8, 0.5, 0.5 );"+
  213. "vec3 diffuse = mix( vec3 ( diffuseFull ), vec3( diffuseHalf ), wrapRGB );"+
  214. // simple lighting
  215. //"float diffuseFull = max( dot( normal, lightDir ), 0.0 );" +
  216. //"vec3 diffuse = vec3 ( diffuseFull );"+
  217. // specular
  218. "const float shininess = 75.0;" +
  219. "const float specularIntensity = 0.4;"+
  220. "vec3 halfVector = normalize( lightDir - normalize( viewPos.xyz ) );" +
  221. "float dotNormalHalf = max( dot( normal, halfVector ), 0.0 );" +
  222. "vec3 specular = specularIntensity * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse;" +
  223. // color
  224. "vec4 albedo = texture2D( samplerColor, texCoord );"+
  225. // combine
  226. "vec4 color = vec4( 0.0 );"+
  227. "color.xyz = albedo.xyz * lightColor * lightIntensity;"+
  228. "color.w = attenuation;"+
  229. "gl_FragColor = color * vec4( diffuse + specular, 1.0 );" +
  230. "}";
  231. var composite_vert = "" +
  232. "varying vec2 texCoord;"+
  233. "void main() {"+
  234. "vec4 pos = vec4( sign( position.xy ), 0.0, 1.0 );"+
  235. "texCoord = pos.xy * vec2( 0.5, 0.5 ) + 0.5;"+
  236. "gl_Position = pos;"+
  237. "}";
  238. var composite_frag = "" +
  239. "varying vec2 texCoord;"+
  240. "uniform sampler2D samplerLightBuffer;" +
  241. "uniform sampler2D samplerEmitter;" +
  242. "uniform vec3 lightPos;" +
  243. "void main() {" +
  244. "vec3 color = texture2D( samplerLightBuffer, texCoord ).xyz;" +
  245. "vec3 emitter = texture2D( samplerEmitter, texCoord ).xyz;"+
  246. "if ( emitter != vec3( 0.0 ) ) {"+
  247. "gl_FragColor = vec4( emitter, 1.0 );" +
  248. "} else {"+
  249. "gl_FragColor = vec4( sqrt( color ), 1.0 );" +
  250. "}"+
  251. "}"
  252. // -----------------------
  253. var normalShader = {
  254. uniforms: {},
  255. vertexShader: normals_vert,
  256. fragmentShader: normals_frag
  257. };
  258. // -----------------------
  259. var bumpShader = {
  260. uniforms: {
  261. bumpMap: { type: "t", value: null },
  262. bumpScale: { type: "f", value: 1 },
  263. offsetRepeat : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) }
  264. },
  265. vertexShader: bump_vert,
  266. fragmentShader: bump_frag
  267. };
  268. // -----------------------
  269. var clipDepthShader = {
  270. uniforms: {},
  271. vertexShader: clipdepth_vert,
  272. fragmentShader: clipdepth_frag
  273. };
  274. // -----------------------
  275. var unlitShader = {
  276. uniforms: {
  277. samplerDepth: { type: "t", value: null },
  278. viewWidth: { type: "f", value: WIDTH },
  279. viewHeight: { type: "f", value: HEIGHT },
  280. lightColor: { type: "v3", value: new THREE.Vector3( 0, 0, 0 ) }
  281. },
  282. vertexShader: unlit_vert,
  283. fragmentShader: unlit_frag
  284. };
  285. // -----------------------
  286. var lightShader = {
  287. uniforms: {
  288. samplerLightBuffer: { type: "t", value: null },
  289. samplerNormals: { type: "t", value: null },
  290. samplerDepth: { type: "t", value: null },
  291. samplerColor: { type: "t", value: null },
  292. matView : { type: "m4", value: new THREE.Matrix4() },
  293. matProjInverse : { type: "m4", value: new THREE.Matrix4() },
  294. viewWidth: { type: "f", value: WIDTH },
  295. viewHeight: { type: "f", value: HEIGHT },
  296. lightPos: { type: "v3", value: new THREE.Vector3( 0, 0, 0 ) },
  297. lightColor: { type: "v3", value: new THREE.Vector3( 0, 0, 0 ) },
  298. lightIntensity: { type: "f", value: 1.0 },
  299. lightRadius: { type: "f", value: 1.0 }
  300. },
  301. vertexShader: deferredlight_vert,
  302. fragmentShader: deferredlight_frag
  303. };
  304. // -----------------------
  305. var compositeShader = {
  306. uniforms: {
  307. samplerLightBuffer: { type: "t", value: null },
  308. samplerEmitter: { type: "t", value: null }
  309. },
  310. vertexShader: composite_vert,
  311. fragmentShader: composite_frag
  312. };
  313. // -----------------------------
  314. function bootstrap() {
  315. renderer = new THREE.WebGLRenderer( { alpha: false } );
  316. renderer.setSize( WIDTH, HEIGHT );
  317. renderer.setClearColorHex( 0x000000, 1 );
  318. var container = document.getElementById( 'container' );
  319. container.appendChild( renderer.domElement );
  320. // scene camera
  321. camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR );
  322. camera.position.z = 150;
  323. controls = new THREE.TrackballControls( camera, renderer.domElement );
  324. // scene for walt's head model
  325. scene = new THREE.Scene();
  326. sceneNode = new THREE.Object3D();
  327. scene.add( sceneNode );
  328. scene.add( camera );
  329. // scene for light proxy geometry
  330. lightScene = new THREE.Scene();
  331. lightNode = new THREE.Object3D();
  332. lightScene.add( lightNode );
  333. // scene for the coloured emitter spheres
  334. emitterScene = new THREE.Scene();
  335. emitterNode = new THREE.Object3D();
  336. emitterScene.add( emitterNode );
  337. // full screen quad for compositing
  338. quadScene = new THREE.Scene();
  339. quadNode = new THREE.Object3D();
  340. quadScene.add( quadNode );
  341. quadNode.add( new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ) ) );
  342. // stats
  343. stats = new Stats();
  344. stats.domElement.style.position = 'absolute';
  345. stats.domElement.style.top = '8px';
  346. stats.domElement.style.zIndex = 100;
  347. container.appendChild( stats.domElement );
  348. // clock
  349. clock = new THREE.Clock();
  350. }
  351. // -----------------------------
  352. function createRenderTargets() {
  353. var rtParamsFloat = { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter, stencilBuffer: false,
  354. format: THREE.RGBAFormat, type: THREE.FloatType };
  355. var rtParamsUByte = { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter, stencilBuffer: false,
  356. format: THREE.RGBFormat, type: THREE.UnsignedByteType };
  357. // ----------------------------------------------------------
  358. // g-buffer
  359. // ----------------------------------------------------------
  360. rtNormals = new THREE.WebGLRenderTarget( WIDTH, HEIGHT, rtParamsFloat );
  361. rtDepth = new THREE.WebGLRenderTarget( WIDTH, HEIGHT, rtParamsFloat );
  362. rtColor = new THREE.WebGLRenderTarget( WIDTH, HEIGHT, rtParamsUByte );
  363. var passNormals = new THREE.RenderPass( scene, camera );
  364. compNormals = new THREE.EffectComposer( renderer, rtNormals );
  365. compNormals.addPass( passNormals );
  366. var passDepth = new THREE.RenderPass( scene, camera );
  367. compDepth = new THREE.EffectComposer( renderer, rtDepth );
  368. compDepth.addPass( passDepth );
  369. var passColor = new THREE.RenderPass( scene, camera );
  370. compColor = new THREE.EffectComposer( renderer, rtColor );
  371. compColor.addPass( passColor );
  372. // ----------------------------------------------------------
  373. // light emitter spheres
  374. // ----------------------------------------------------------
  375. var emitterPass = new THREE.RenderPass( emitterScene, camera );
  376. rtEmitter = new THREE.WebGLRenderTarget( WIDTH, HEIGHT, rtParamsFloat );
  377. compEmitter = new THREE.EffectComposer( renderer, rtEmitter );
  378. compEmitter.addPass( emitterPass );
  379. // ----------------------------------------------------------
  380. // lighting pass
  381. // ----------------------------------------------------------
  382. rtLightBuffer = new THREE.WebGLRenderTarget( WIDTH, HEIGHT, rtParamsFloat );
  383. rtLightBuffer.generateMipmaps = false;
  384. var passLight = new THREE.RenderPass( lightScene, camera );
  385. compLightBuffer = new THREE.EffectComposer( renderer, rtLightBuffer );
  386. compLightBuffer.addPass( passLight );
  387. lightShader.uniforms[ 'samplerColor' ].value = compColor.renderTarget2;
  388. lightShader.uniforms[ 'samplerNormals' ].value = compNormals.renderTarget2;
  389. lightShader.uniforms[ 'samplerDepth' ].value = compDepth.renderTarget2;
  390. lightShader.uniforms[ 'samplerLightBuffer' ].value = rtLightBuffer;
  391. for ( var x = 0; x < numLights; x ++ ) {
  392. var light = lights[ x ];
  393. // setup material
  394. var matLight = new THREE.ShaderMaterial({
  395. uniforms: THREE.UniformsUtils.clone( lightShader.uniforms ),
  396. vertexShader: lightShader.vertexShader,
  397. fragmentShader: lightShader.fragmentShader
  398. });
  399. matLight.blending = THREE.AdditiveBlending;
  400. matLight.transparent = true;
  401. matLight.depthWrite = false;
  402. matLight.uniforms[ "lightPos" ].value = light.position;
  403. matLight.uniforms[ "lightRadius" ].value = light.distance;
  404. matLight.uniforms[ "lightIntensity" ].value = light.intensity;
  405. matLight.uniforms[ "lightColor" ].value = light.color;
  406. // setup proxy geometry for this light
  407. var geomLight = new THREE.SphereGeometry( light.distance, 16, 10 );
  408. var meshLight = new THREE.Mesh( geomLight, matLight );
  409. lightNode.add( meshLight );
  410. // create emitter sphere
  411. var geomEmitter = new THREE.SphereGeometry( 0.7, 7, 7 );
  412. var matEmitter = new THREE.ShaderMaterial({
  413. uniforms: THREE.UniformsUtils.clone( unlitShader.uniforms ),
  414. vertexShader: unlitShader.vertexShader,
  415. fragmentShader: unlitShader.fragmentShader
  416. });
  417. var meshEmitter = new THREE.Mesh( geomEmitter, matEmitter );
  418. meshEmitter.position = light.position;
  419. emitterNode.add( meshEmitter );
  420. // add emitter to light node
  421. meshLight.emitter = meshEmitter;
  422. }
  423. // ----------------------------------------------------------
  424. // composite
  425. // ----------------------------------------------------------
  426. compositeShader.uniforms['samplerLightBuffer'].value = compLightBuffer.renderTarget2;
  427. compositeShader.uniforms['samplerEmitter'].value = compEmitter.renderTarget2;
  428. compositePass = new THREE.ShaderPass( compositeShader );
  429. compositePass.needsSwap = true;
  430. //compositePass.renderToScreen = true;
  431. effectFXAA = new THREE.ShaderPass( THREE.FXAAShader );
  432. var width = window.innerWidth || 2;
  433. var height = window.innerHeight || 2;
  434. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height );
  435. effectFXAA.renderToScreen = true;
  436. compFinal = new THREE.EffectComposer( renderer );
  437. compFinal.addPass( compositePass );
  438. compFinal.addPass( effectFXAA );
  439. }
  440. // -----------------------------
  441. function initScene( object, y, scale ) {
  442. var shader = THREE.ShaderLib[ "basic" ];
  443. object.traverse( function( node ) {
  444. if ( node.material ) {
  445. // color material
  446. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  447. var defines = { "USE_MAP": !!node.material.map, "GAMMA_INPUT": true };
  448. var material = new THREE.ShaderMaterial( { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, defines: defines } );
  449. uniforms.diffuse.value.copy( node.material.color );
  450. uniforms.map.value = node.material.map;
  451. if ( node.material.transparent ) {
  452. material.alphaTest = 0.1;
  453. }
  454. if ( node.material.name === "eyetrans" ) {
  455. material.visible = false;
  456. }
  457. node.properties.colorMaterial = material;
  458. // normal material
  459. if ( node.material.bumpMap ) {
  460. var uniforms = THREE.UniformsUtils.clone( bumpShader.uniforms );
  461. var normalMaterial = new THREE.ShaderMaterial( { uniforms: uniforms, vertexShader: bumpShader.vertexShader, fragmentShader: bumpShader.fragmentShader } );
  462. uniforms.bumpMap.value = node.material.bumpMap;
  463. uniforms.bumpScale.value = node.material.bumpScale;
  464. var offset = node.material.bumpMap.offset;
  465. var repeat = node.material.bumpMap.repeat;
  466. uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
  467. node.properties.normalMaterial = normalMaterial;
  468. } else {
  469. node.properties.normalMaterial = matNormal;
  470. }
  471. // depth material
  472. node.properties.depthMaterial = matClipDepth;
  473. }
  474. } );
  475. object.position.y = y;
  476. object.scale.set( scale, scale, scale );
  477. sceneNode.add( object );
  478. }
  479. // -----------------------------
  480. function initMaterials() {
  481. matNormal = new THREE.ShaderMaterial({
  482. uniforms: normalShader.uniforms,
  483. vertexShader: normalShader.vertexShader,
  484. fragmentShader: normalShader.fragmentShader
  485. });
  486. matClipDepth = new THREE.ShaderMaterial({
  487. uniforms: clipDepthShader.uniforms,
  488. vertexShader: clipDepthShader.vertexShader,
  489. fragmentShader: clipDepthShader.fragmentShader
  490. });
  491. }
  492. // -----------------------------
  493. function initLights() {
  494. var distance = 25;
  495. // front light
  496. var light = new THREE.PointLight();
  497. light.color = new THREE.Vector3( 1, 1, 1 );
  498. light.intensity = 1.5;
  499. light.distance = 1.5 * distance;
  500. lights.push( light );
  501. // random lights
  502. for ( var i = 1; i < numLights; i ++ ) {
  503. var light = new THREE.PointLight();
  504. light.color = new THREE.Vector3( Math.random(), Math.random(), Math.random() ).normalize();
  505. light.intensity = 1.0;
  506. light.distance = distance;
  507. lights.push( light );
  508. }
  509. }
  510. // -----------------------------
  511. function animate() {
  512. var delta = clock.getDelta();
  513. requestAnimationFrame( animate );
  514. controls.update( delta );
  515. stats.update();
  516. render();
  517. }
  518. // -----------------------------
  519. function render() {
  520. // -----------------------------
  521. // g-buffer color
  522. // -----------------------------
  523. sceneNode.traverse( function( node ) {
  524. if ( node.material ) {
  525. node.material = node.properties.colorMaterial;
  526. }
  527. } );
  528. compColor.render();
  529. // -----------------------------
  530. // g-buffer depth
  531. // -----------------------------
  532. sceneNode.traverse( function( node ) {
  533. if ( node.material ) {
  534. node.material = node.properties.depthMaterial;
  535. }
  536. } );
  537. compDepth.render();
  538. // -----------------------------
  539. // g-buffer normals
  540. // -----------------------------
  541. sceneNode.traverse( function( node ) {
  542. if ( node.material ) {
  543. node.material = node.properties.normalMaterial;
  544. }
  545. } );
  546. compNormals.render();
  547. // -----------------------------
  548. // emitter pass
  549. // -----------------------------
  550. for ( var i = 0, il = lightNode.children.length; i < il; i ++ ) {
  551. var light = lightNode.children[ i ];
  552. var color = light.material.uniforms[ "lightColor" ].value;
  553. var emitter = light.emitter;
  554. emitter.material.uniforms[ "samplerDepth" ].value = compDepth.renderTarget2;
  555. emitter.material.uniforms[ "lightColor" ].value = color;
  556. }
  557. compEmitter.render();
  558. // -----------------------------
  559. // light pass
  560. // -----------------------------
  561. for ( var i = 0, il = lightNode.children.length; i < il; i ++ ) {
  562. camera.projectionMatrixInverse.getInverse( camera.projectionMatrix );
  563. lightNode.children[ i ].material.uniforms[ "matProjInverse" ].value = camera.projectionMatrixInverse;
  564. lightNode.children[ i ].material.uniforms[ "matView" ].value = camera.matrixWorldInverse;
  565. }
  566. var time = Date.now() * 0.0005;
  567. // update lights
  568. var x, y, z;
  569. for ( var i = 0; i < numLights; i ++ ) {
  570. var lightPosition = lightNode.children[ i ].material.uniforms[ "lightPos" ].value;
  571. if ( i > 0 ) {
  572. x = Math.sin( time + i * 1.7 ) * 30;
  573. y = Math.cos( time + i * 1.5 ) * 40;
  574. z = Math.cos( time + i * 1.3 ) * 30;
  575. } else {
  576. x = Math.sin( time * 3 ) * 20;
  577. y = 15;
  578. z = Math.cos( time * 3 ) * 25 + 10;
  579. }
  580. lightPosition.x = x;
  581. lightPosition.y = y;
  582. lightPosition.z = z;
  583. lightNode.children[ i ].emitter.position = lightPosition;
  584. lightNode.children[ i ].position = lightPosition;
  585. lightNode.children[ i ].frustumCulled = false;
  586. }
  587. compLightBuffer.render();
  588. // -----------------------------
  589. // composite pass
  590. // -----------------------------
  591. compFinal.render();
  592. }
  593. // -----------------------------
  594. // entry point
  595. // -----------------------------
  596. bootstrap();
  597. initMaterials();
  598. initLights();
  599. createRenderTargets();
  600. var loader = new THREE.UTF8Loader();
  601. /*
  602. loader.load( "models/utf8/ben_dds.js", function ( object ) {
  603. initScene( object, -75, 150 );
  604. animate();
  605. }, { normalizeRGB: true } );
  606. */
  607. /*
  608. loader.load( "models/utf8/WaltHi.js", function ( object ) {
  609. initScene( object, -35, 1 );
  610. animate();
  611. }, { normalizeRGB: true } );
  612. */
  613. var loader = new THREE.JSONLoader();
  614. loader.load( "obj/leeperrysmith/LeePerrySmith.js", function( geometry, materials ) {
  615. var mapColor = THREE.ImageUtils.loadTexture( "obj/leeperrysmith/Map-COL.jpg" );
  616. var mapHeight = THREE.ImageUtils.loadTexture( "obj/leeperrysmith/Infinite-Level_02_Disp_NoSmoothUV-4096.jpg" );
  617. mapHeight.repeat.set( 0.998, 0.998 );
  618. mapHeight.offset.set( 0.001, 0.001 )
  619. mapHeight.wrapS = mapHeight.wrapT = THREE.RepeatWrapping;
  620. mapHeight.anisotropy = 4;
  621. mapHeight.format = THREE.RGBFormat;
  622. var material = new THREE.MeshPhongMaterial( { map: mapColor, bumpMap: mapHeight, bumpScale: 2.5 } );
  623. var object = new THREE.Mesh( geometry, material );
  624. initScene( object, 0, 10 );
  625. animate();
  626. } );
  627. </script>
  628. </body>
  629. </html>
粤ICP备19079148号