From 6574f8eb0fd7fe8233ef191c17fe3c90263f92c8 Mon Sep 17 00:00:00 2001 From: xeolabs Date: Thu, 26 Mar 2015 15:59:05 +0100 Subject: [PATCH] Removed 'diffuse', 'specular', 'ambient' and 'reflection' flags --- src/core/display/chunks/flagsChunk.js | 20 +--- src/core/display/programSourceFactory.js | 116 +++++++++----------- src/core/scene/flags.js | 132 +---------------------- 3 files changed, 52 insertions(+), 216 deletions(-) diff --git a/src/core/display/chunks/flagsChunk.js b/src/core/display/chunks/flagsChunk.js index 7c17eedd..dd0350c4 100644 --- a/src/core/display/chunks/flagsChunk.js +++ b/src/core/display/chunks/flagsChunk.js @@ -9,13 +9,7 @@ SceneJS_ChunkFactory.createChunkType({ var draw = this.program.draw; - this._uBackfaceTexturingDraw = draw.getUniformLocation("SCENEJS_uBackfaceTexturing"); - this._uBackfaceLightingDraw = draw.getUniformLocation("SCENEJS_uBackfaceLighting"); - this._uSpecularLightingDraw = draw.getUniformLocation("SCENEJS_uSpecularLighting"); this._uClippingDraw = draw.getUniformLocation("SCENEJS_uClipping"); - this._uAmbientDraw = draw.getUniformLocation("SCENEJS_uAmbient"); - this._uDiffuseDraw = draw.getUniformLocation("SCENEJS_uDiffuse"); - this._uReflectionDraw = draw.getUniformLocation("SCENEJS_uReflection"); var pick = this.program.pick; @@ -75,21 +69,9 @@ SceneJS_ChunkFactory.createChunkType({ gl.uniform1i(this._uClippingPick, this.core.clipping); } else { - var drawUniforms = (this.core.backfaceTexturing ? 1 : 0) + - (this.core.backfaceLighting ? 2 : 0) + - (this.core.specular ? 4 : 0) + - (this.core.clipping ? 8 : 0) + - (this.core.ambient ? 16 : 0) + - (this.core.diffuse ? 32 : 0) + - (this.core.reflection ? 64 : 0); + var drawUniforms = (this.core.clipping ? 1 : 0); if (this.program.drawUniformFlags != drawUniforms) { - gl.uniform1i(this._uBackfaceTexturingDraw, this.core.backfaceTexturing); - gl.uniform1i(this._uBackfaceLightingDraw, this.core.backfaceLighting); - gl.uniform1i(this._uSpecularLightingDraw, this.core.specular); gl.uniform1i(this._uClippingDraw, this.core.clipping); - gl.uniform1i(this._uAmbientDraw, this.core.ambient); - gl.uniform1i(this._uDiffuseDraw, this.core.diffuse); - gl.uniform1i(this._uReflectionDraw, this.core.reflection); this.program.drawUniformFlags = drawUniforms; } } diff --git a/src/core/display/programSourceFactory.js b/src/core/display/programSourceFactory.js index 20b87b44..f6623f9d 100644 --- a/src/core/display/programSourceFactory.js +++ b/src/core/display/programSourceFactory.js @@ -122,20 +122,14 @@ var SceneJS_ProgramSourceFactory = new (function () { src.push("void main(void) {"); if (clipping) { - src.push("if (SCENEJS_uClipping) {"); - src.push(" float dist;"); + src.push(" float dist = 0.0;"); for (var i = 0; i < states.clips.clips.length; i++) { - src.push(" if (SCENEJS_uClipMode" + i + " != 0.0) {"); - src.push(" dist = dot(SCENEJS_vWorldVertex.xyz, SCENEJS_uClipNormalAndDist" + i + ".xyz) - SCENEJS_uClipNormalAndDist" + i + ".w;"); - src.push(" if (SCENEJS_uClipMode" + i + " == 1.0) {"); - src.push(" if (dist > 0.0) { discard; }"); - src.push(" }"); - src.push(" if (SCENEJS_uClipMode" + i + " == 2.0) {"); - src.push(" if (dist > 0.0) { discard; }"); - src.push(" }"); - src.push(" }"); + src.push(" if (SCENEJS_uClipMode" + i + " != 0.0) {"); + src.push(" dist += clamp(dot(SCENEJS_vWorldVertex.xyz, SCENEJS_uClipNormalAndDist" + i + ".xyz) - SCENEJS_uClipNormalAndDist" + i + ".w, 0.0, 1000.0);"); + src.push(" }"); } + src.push(" if (dist > 0.0) { discard; }"); src.push("}"); } @@ -572,13 +566,7 @@ var SceneJS_ProgramSourceFactory = new (function () { /* True when lighting */ - src.push("uniform bool SCENEJS_uBackfaceTexturing;"); - src.push("uniform bool SCENEJS_uBackfaceLighting;"); - src.push("uniform bool SCENEJS_uSpecularLighting;"); src.push("uniform bool SCENEJS_uClipping;"); - src.push("uniform bool SCENEJS_uAmbient;"); - src.push("uniform bool SCENEJS_uDiffuse;"); - src.push("uniform bool SCENEJS_uReflection;"); // Added in v4.0 to support depth targets src.push("uniform bool SCENEJS_uDepthMode;"); @@ -632,22 +620,17 @@ var SceneJS_ProgramSourceFactory = new (function () { if (clipping) { src.push("if (SCENEJS_uClipping) {"); - src.push(" float dist;"); + src.push(" float dist = 0.0;"); for (var i = 0; i < states.clips.clips.length; i++) { - src.push(" if (SCENEJS_uClipMode" + i + " != 0.0) {"); - src.push(" dist = dot(SCENEJS_vWorldVertex.xyz, SCENEJS_uClipNormalAndDist" + i + ".xyz) - SCENEJS_uClipNormalAndDist" + i + ".w;"); - src.push(" if (SCENEJS_uClipMode" + i + " == 1.0) {"); - src.push(" if (dist > 0.0) { discard; }"); - src.push(" }"); - src.push(" if (SCENEJS_uClipMode" + i + " == 2.0) {"); - src.push(" if (dist > 0.0) { discard; }"); - src.push(" }"); - src.push(" }"); + src.push(" if (SCENEJS_uClipMode" + i + " != 0.0) {"); + src.push(" dist += clamp(dot(SCENEJS_vWorldVertex.xyz, SCENEJS_uClipNormalAndDist" + i + ".xyz) - SCENEJS_uClipNormalAndDist" + i + ".w, 0.0, 1000.0);"); + src.push(" }"); } + src.push(" if (dist > 0.0) { discard; }"); src.push("}"); } - src.push(" vec3 ambient= SCENEJS_uAmbient ? SCENEJS_uAmbientColor : vec3(0.0, 0.0, 0.0);"); + src.push(" vec3 ambient= SCENEJS_uAmbientColor;"); if (texturing && states.geometry.uvBuf && fragmentHooks.texturePos) { src.push(fragmentHooks.texturePos + "(SCENEJS_vUVCoord);"); @@ -707,10 +690,6 @@ var SceneJS_ProgramSourceFactory = new (function () { var layer; if (texturing) { - if (normals) { - src.push("if (SCENEJS_uBackfaceTexturing || dot(SCENEJS_vViewNormal, SCENEJS_vViewEyeVec) > 0.0) {"); - } - src.push(" vec4 texturePos;"); src.push(" vec2 textureCoord=vec2(0.0,0.0);"); @@ -801,13 +780,9 @@ var SceneJS_ProgramSourceFactory = new (function () { } } - if (normals) { - src.push("}"); - } } if (normals && cubeMapping) { - src.push("if (SCENEJS_uReflection) {"); // Flag which can enable/disable reflection src.push("vec3 envLookup = reflect(SCENEJS_vViewEyeVec, viewNormalVec);"); src.push("envLookup.y = envLookup.y * -1.0;"); // Need to flip textures on Y-axis for some reason src.push("vec4 envColor;"); @@ -816,15 +791,12 @@ var SceneJS_ProgramSourceFactory = new (function () { src.push("envColor = textureCube(SCENEJS_uCubeMapSampler" + i + ", envLookup);"); src.push("color = mix(color, envColor.rgb, specular * SCENEJS_uCubeMapIntensity" + i + ");"); } - src.push("}"); } src.push(" vec4 fragColor;"); if (normals) { - src.push("if (SCENEJS_uBackfaceLighting || dot(viewNormalVec, SCENEJS_vViewEyeVec) > 0.0) {"); - src.push(" vec3 lightValue = vec3(0.0, 0.0, 0.0);"); src.push(" vec3 specularValue = vec3(0.0, 0.0, 0.0);"); src.push(" vec3 viewLightVec;"); @@ -846,7 +818,6 @@ var SceneJS_ProgramSourceFactory = new (function () { src.push("dotN = max(dot(normalize(viewNormalVec), normalize(viewLightVec)), 0.0);"); - //src.push("if (dotN > 0.0) {"); src.push("lightDist = SCENEJS_vViewLightVecAndDist" + i + ".w;"); @@ -856,16 +827,12 @@ var SceneJS_ProgramSourceFactory = new (function () { " SCENEJS_uLightAttenuation" + i + "[2] * lightDist * lightDist);"); if (light.diffuse) { - src.push("if (SCENEJS_uDiffuse) {"); src.push(" lightValue += dotN * SCENEJS_uLightColor" + i + " * attenuation;"); - src.push("}"); } if (light.specular) { - src.push("if (SCENEJS_uSpecularLighting) {"); src.push(" specularValue += specularColor * SCENEJS_uLightColor" + i + " * specular * pow(max(dot(reflect(normalize(-viewLightVec), normalize(-viewNormalVec)), normalize(-SCENEJS_vViewVertex.xyz)), 0.0), shine) * attenuation;"); - src.push("}"); } } @@ -874,24 +841,17 @@ var SceneJS_ProgramSourceFactory = new (function () { src.push("dotN = max(dot(normalize(viewNormalVec), normalize(viewLightVec)), 0.0);"); if (light.diffuse) { - src.push("if (SCENEJS_uDiffuse) {"); src.push(" lightValue += dotN * SCENEJS_uLightColor" + i + ";"); - src.push("}"); } if (light.specular) { - src.push("if (SCENEJS_uSpecularLighting) {"); - src.push(" specularValue += specularColor * SCENEJS_uLightColor" + i + + src.push("specularValue += specularColor * SCENEJS_uLightColor" + i + " * specular * pow(max(dot(reflect(normalize(-viewLightVec), normalize(-viewNormalVec)), normalize(-SCENEJS_vViewVertex.xyz)), 0.0), shine);"); - src.push("}"); } } } src.push(" fragColor = vec4((specularValue.rgb + color.rgb * (lightValue.rgb + ambient.rgb)) + (emit * color.rgb), alpha);"); - src.push(" } else {"); - src.push(" fragColor = vec4((color.rgb + (emit * color.rgb)) * (vec3(1.0, 1.0, 1.0) + ambient.rgb), alpha);"); - src.push(" }"); } else { // No normals src.push("fragColor = vec4((color.rgb + (emit * color.rgb)) * (vec3(1.0, 1.0, 1.0) + ambient.rgb), alpha);"); @@ -903,25 +863,47 @@ var SceneJS_ProgramSourceFactory = new (function () { if (false && debugCfg.whitewash === true) { src.push(" gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);"); } else { - src.push(" if (SCENEJS_uDepthMode) {"); - src.push(" float depth = length(SCENEJS_vViewVertex) / (SCENEJS_uZFar - SCENEJS_uZNear);"); - src.push(" const vec4 bias = vec4(1.0 / 255.0,"); - src.push(" 1.0 / 255.0,"); - src.push(" 1.0 / 255.0,"); - src.push(" 0.0);"); - src.push(" float r = depth;"); - src.push(" float g = fract(r * 255.0);"); - src.push(" float b = fract(g * 255.0);"); - src.push(" float a = fract(b * 255.0);"); - src.push(" vec4 colour = vec4(r, g, b, a);"); - src.push(" gl_FragColor = colour - (colour.yzww * bias);"); - src.push(" } else {"); - src.push(" gl_FragColor = fragColor;"); - src.push(" };"); + + if (hasDepthTarget(states)) { + + // Only compile in depth mode support if a depth render target is present + + src.push(" if (SCENEJS_uDepthMode) {"); + src.push(" float depth = length(SCENEJS_vViewVertex) / (SCENEJS_uZFar - SCENEJS_uZNear);"); + src.push(" const vec4 bias = vec4(1.0 / 255.0,"); + src.push(" 1.0 / 255.0,"); + src.push(" 1.0 / 255.0,"); + src.push(" 0.0);"); + src.push(" float r = depth;"); + src.push(" float g = fract(r * 255.0);"); + src.push(" float b = fract(g * 255.0);"); + src.push(" float a = fract(b * 255.0);"); + src.push(" vec4 colour = vec4(r, g, b, a);"); + src.push(" gl_FragColor = colour - (colour.yzww * bias);"); + src.push(" } else {"); + src.push(" gl_FragColor = fragColor;"); + src.push(" };"); + + } else { + src.push(" gl_FragColor = fragColor;"); + } } src.push("}"); +// console.log(src.join("\n")); return src; }; + function hasDepthTarget(states) { + if (states.renderTarget && states.renderTarget.targets) { + var targets = states.renderTarget.targets; + for (var i = 0, len = targets.length; i < len; i++) { + if (targets[i].bufType === "depth") { + return true; + } + } + } + return false; + } + })(); \ No newline at end of file diff --git a/src/core/scene/flags.js b/src/core/scene/flags.js index a6db91a7..7b19a76c 100644 --- a/src/core/scene/flags.js +++ b/src/core/scene/flags.js @@ -13,13 +13,7 @@ enabled : true, // Node not culled from traversal transparent: false, // Node transparent - works in conjunction with matarial alpha properties backfaces: true, // Show backfaces - frontface: "ccw", // Default vertex winding for front face - backfaceLighting: true, // Shading enabled for backfaces - backfaceTexturing: true, // Texturing enabled for backfaces - diffuse: true, // Diffuse lighting enabled - specular: true, // Specular lighting enabled - ambient: true, // Ambient lighting enabled - reflection : true // Reflection enabled by default + frontface: "ccw" // Default vertex winding for front face }; var coreStack = []; @@ -48,13 +42,6 @@ this._core.transparent = false; // Node transparent - works in conjunction with matarial alpha properties this._core.backfaces = true; // Show backfaces this._core.frontface = "ccw"; // Default vertex winding for front face - this._core.backfaceLighting = true; // Shading enabled for backfaces - this._core.backfaceTexturing = true; // Texturing enabled for backfaces - this._core.diffuse = true; // Diffuse lighting enabled by default - this._core.specular = true; // Specular lighting enabled by default - this._core.ambient = true; // Ambient lighting enabled by default - this._core.reflection = true; // Reflection enabled by default - if (params.flags) { // 'flags' property is actually optional in the node definition this.setFlags(params.flags); } @@ -94,37 +81,6 @@ core.frontface = flags.frontface; this._engine.display.imageDirty = true; } - - if (flags.backfaceLighting != undefined) { - core.backfaceLighting = !!flags.backfaceLighting; - this._engine.display.imageDirty = true; - } - - if (flags.backfaceTexturing != undefined) { - core.backfaceTexturing = !!flags.backfaceTexturing; - this._engine.display.imageDirty = true; - } - - if (flags.diffuse != undefined) { - core.diffuse = !!flags.diffuse; - this._engine.display.imageDirty = true; - } - - if (flags.specular != undefined) { - core.specular = !!flags.specular; - this._engine.display.imageDirty = true; - } - - if (flags.ambient != undefined) { - core.ambient = !!flags.ambient; - this._engine.display.imageDirty = true; - } - - if (flags.reflection != undefined) { - core.reflection = !!flags.reflection; - this._engine.display.imageDirty = true; - } - return this; }; @@ -150,13 +106,7 @@ enabled : core.enabled, transparent: core.transparent, backfaces: core.backfaces, - frontface: core.frontface, - diffuse: core.diffuse, - specular: core.specular, - ambient: core.ambient, - backfaceLighting: core.backfaceLighting, - backfaceTexturing: core.backfaceTexturing, - reflection: core.reflection + frontface: core.frontface }; }; @@ -237,84 +187,6 @@ return this._core.frontface; }; - SceneJS.Flags.prototype.setBackfaceLighting = function(backfaceLighting) { - backfaceLighting = !!backfaceLighting; - if (this._core.backfaceLighting != backfaceLighting) { - this._core.backfaceLighting = backfaceLighting; - this._engine.display.imageDirty = true; - } - return this; - }; - - SceneJS.Flags.prototype.getBackfaceLighting = function() { - return this._core.backfaceLighting; - }; - - SceneJS.Flags.prototype.setBackfaceTexturing = function(backfaceTexturing) { - backfaceTexturing = !!backfaceTexturing; - if (this._core.backfaceTexturing != backfaceTexturing) { - this._core.backfaceTexturing = backfaceTexturing; - this._engine.display.imageDirty = true; - } - return this; - }; - - SceneJS.Flags.prototype.getBackfaceTexturing = function() { - return this._core.backfaceTexturing; - }; - - SceneJS.Flags.prototype.setDiffuse = function(diffuse) { - diffuse = !!diffuse; - if (this._core.diffuse != diffuse) { - this._core.diffuse = diffuse; - this._engine.display.imageDirty = true; - } - return this; - }; - - SceneJS.Flags.prototype.getDiffuse = function() { - return this._core.diffuse; - }; - - SceneJS.Flags.prototype.setSpecular = function(specular) { - specular = !!specular; - if (this._core.specular != specular) { - this._core.specular = specular; - this._engine.display.imageDirty = true; - } - return this; - }; - - SceneJS.Flags.prototype.getSpecular = function() { - return this._core.specular; - }; - - SceneJS.Flags.prototype.setAmbient = function(ambient) { - ambient = !!ambient; - if (this._core.ambient != ambient) { - this._core.ambient = ambient; - this._engine.display.imageDirty = true; - } - return this; - }; - - SceneJS.Flags.prototype.getAmbient = function() { - return this._core.ambient; - }; - - SceneJS.Flags.prototype.setReflection = function(reflection) { - reflection = !!reflection; - if (this._core.reflection != reflection) { - this._core.reflection = reflection; - this._engine.display.imageDirty = true; - } - return this; - }; - - SceneJS.Flags.prototype.getReflection = function() { - return this._core.reflection; - }; - SceneJS.Flags.prototype._compile = function(ctx) { this._engine.display.flags = coreStack[stackLen++] = this._core; this._compileNodes(ctx);