Skip to content

Commit

Permalink
Removed 'diffuse', 'specular', 'ambient' and 'reflection' flags
Browse files Browse the repository at this point in the history
  • Loading branch information
xeolabs committed Mar 26, 2015
1 parent dea4e39 commit 6574f8e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 216 deletions.
20 changes: 1 addition & 19 deletions src/core/display/chunks/flagsChunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
Expand Down
116 changes: 49 additions & 67 deletions src/core/display/programSourceFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("}");
}

Expand Down Expand Up @@ -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;");
Expand Down Expand Up @@ -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);");
Expand Down Expand Up @@ -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);");

Expand Down Expand Up @@ -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;");
Expand All @@ -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;");
Expand All @@ -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;");

Expand All @@ -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("}");
}
}

Expand All @@ -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);");
Expand All @@ -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;
}

})();
Loading

0 comments on commit 6574f8e

Please sign in to comment.