Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 120
- // Varying.
- varying vec3 worldNormal;
- varying vec3 worldPos;
- // Global directional light uniforms.
- uniform vec4 dirLightDir;
- uniform vec4 dirLightColor;
- uniform vec4 dirLightAmbient;
- uniform vec4 dirShadowColor;
- // Misc uniforms.
- uniform vec3 camPos;
- uniform mat4 obj2World;
- uniform mat4 world2Cam;
- uniform int isParticle;
- uniform int doColorMultiply;
- uniform int glow;
- uniform sampler2D tex;
- // Surface calculations, including specular power.
- varying vec2 texCoord;
- vec4 viewDelta;
- float specular;
- float NdotL;
- vec3 reflectVec;
- // // MODS // //
- float power = 0.275f; // light strength, default is 0.4f
- float glareReduction = 3f; // affects glare on bricks, default is 1f
- float radiance = 1.25f; // enhances the glowiness of glowing bricks, default is 1f
- vec4 glowTint = vec4(0.05f, 0f, 0.05f, 0f); // shifts the hue of glowing bricks, default is 0,0,0,0
- // // MODS // //
- void calculateSurface(vec4 color, inout vec4 albedo)
- {
- viewDelta.xyz = worldPos - camPos;
- viewDelta.w = length(viewDelta.xyz);
- viewDelta.xyz = -normalize(viewDelta.xyz);
- vec4 texAlbedo = texture2D(tex, texCoord);
- albedo.rgb = mix(color.rgb, texAlbedo.rgb, texAlbedo.a);
- if(doColorMultiply == 1)
- albedo *= gl_Color;
- albedo.a = color.a;
- NdotL = max(dot(worldNormal, dirLightDir.xyz), 0.0f);
- reflectVec = normalize(reflect(-dirLightDir.xyz, worldNormal));
- specular = pow(max(dot(reflectVec, viewDelta.xyz), 0.0f), 12.0f) * length(texAlbedo.rgb) / glareReduction;
- //albedo.rgb = normalize(viewDelta.xyz);
- }
- // Fogging.
- uniform vec4 fogBaseColor;
- uniform vec4 fogConsts;
- uniform sampler2D fogTex;
- varying vec2 fogCoords;
- void applyFog(inout vec4 albedo)
- {
- // Calculate fog.
- vec4 fogColor = texture2D(fogTex, fogCoords) * fogBaseColor;
- // Blend it.
- albedo = mix(albedo, fogColor, fogColor.a);
- }
- // Point lighting
- uniform vec4 pointLightPos0;
- uniform vec4 pointLightColor0;
- uniform float pointLightRadius0;
- uniform vec4 pointLightPos1;
- uniform vec4 pointLightColor1;
- uniform float pointLightRadius1;
- uniform vec4 pointLightPos2;
- uniform vec4 pointLightColor2;
- uniform float pointLightRadius2;
- uniform vec4 pointLightPos3;
- uniform vec4 pointLightColor3;
- uniform float pointLightRadius3;
- uniform vec4 pointLightPos4;
- uniform vec4 pointLightColor4;
- uniform float pointLightRadius4;
- uniform vec4 pointLightPos5;
- uniform vec4 pointLightColor5;
- uniform float pointLightRadius5;
- uniform vec4 pointLightPos6;
- uniform vec4 pointLightColor6;
- uniform float pointLightRadius6;
- uniform vec4 pointLightPos7;
- uniform vec4 pointLightColor7;
- uniform float pointLightRadius7;
- vec4 accumulatePointLights()
- {
- vec4 pointLightTotal = vec4(0.0f, 0.0f, 0.0f, 0.0f);
- vec3 lightDelta = vec3(0.0f, 0.0f, 0.0f);
- float lightDot = 0.0f;
- float ratio = 0.0f;
- // Calculate effects of the 8 point lights.
- lightDelta = worldPos.xyz - pointLightPos0.xyz;
- lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
- ratio = 1.0f - (length(lightDelta) / pointLightRadius0);
- ratio = ratio * ratio * ratio * power;
- ratio = max(ratio, 0.0f);
- pointLightTotal.xyz += ratio * lightDot * pointLightColor0.xyz;
- lightDelta = worldPos.xyz - pointLightPos1.xyz;
- lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
- ratio = 1.0f - (length(lightDelta) / pointLightRadius1);
- ratio = ratio * ratio * ratio * power;
- ratio = max(ratio, 0.0f);
- pointLightTotal.xyz += ratio * lightDot * pointLightColor1.xyz;
- lightDelta = worldPos.xyz - pointLightPos2.xyz;
- lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
- ratio = 1.0f - (length(lightDelta) / pointLightRadius2);
- ratio = ratio * ratio * ratio * power;
- ratio = max(ratio, 0.0f);
- pointLightTotal.xyz += ratio * lightDot * pointLightColor2.xyz;
- lightDelta = worldPos.xyz - pointLightPos3.xyz;
- lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
- ratio = 1.0f - (length(lightDelta) / pointLightRadius3);
- ratio = ratio * ratio * ratio * power;
- ratio = max(ratio, 0.0f);
- pointLightTotal.xyz += ratio * lightDot * pointLightColor3.xyz;
- lightDelta = worldPos.xyz - pointLightPos4.xyz;
- lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
- ratio = 1.0f - (length(lightDelta) / pointLightRadius4);
- ratio = ratio * ratio * ratio * power;
- ratio = max(ratio, 0.0f);
- pointLightTotal.xyz += ratio * lightDot * pointLightColor4.xyz;
- lightDelta = worldPos.xyz - pointLightPos5.xyz;
- lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
- ratio = 1.0f - (length(lightDelta) / pointLightRadius5);
- ratio = ratio * ratio * ratio * power;
- ratio = max(ratio, 0.0f);
- pointLightTotal.xyz += ratio * lightDot * pointLightColor5.xyz;
- lightDelta = worldPos.xyz - pointLightPos6.xyz;
- lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
- ratio = 1.0f - (length(lightDelta) / pointLightRadius6);
- ratio = ratio * ratio * ratio * power;
- ratio = max(ratio, 0.0f);
- pointLightTotal.xyz += ratio * lightDot * pointLightColor6.xyz;
- lightDelta = worldPos.xyz - pointLightPos7.xyz;
- lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
- ratio = 1.0f - (length(lightDelta) / pointLightRadius7);
- ratio = ratio * ratio * ratio * power;
- ratio = max(ratio, 0.0f);
- pointLightTotal.xyz += ratio * lightDot * pointLightColor7.xyz;
- return pointLightTotal;
- }
- vec4 accumulateParticlePointLights()
- {
- vec4 pointLightTotal = vec4(0.0f, 0.0f, 0.0f, 0.0f);
- vec3 lightDelta = vec3(0.0f, 0.0f, 0.0f);
- float ratio = 0.0f;
- // Calculate effects of the 8 point lights.
- lightDelta = worldPos.xyz - pointLightPos0.xyz;
- ratio = 1.0f - (length(lightDelta) / pointLightRadius0);
- ratio = ratio * ratio * ratio * power;
- ratio = max(ratio, 0.0f);
- pointLightTotal.xyz += ratio * pointLightColor0.xyz;
- lightDelta = worldPos.xyz - pointLightPos1.xyz;
- ratio = 1.0f - (length(lightDelta) / pointLightRadius1);
- ratio = ratio * ratio * ratio * power;
- ratio = max(ratio, 0.0f);
- pointLightTotal.xyz += ratio * pointLightColor1.xyz;
- return pointLightTotal;
- }
- // Combine specular and direct lighting terms.
- // note: if we make combinedColor "out" only, it throws a potentially uninitialized value warning, so we've made it inout
- void applyLighting(inout vec4 combinedColor, vec4 albedo, float occlusionFactor)
- {
- //large normal means glowing object
- if(glow == 1 || (worldNormal.x + worldNormal.y + worldNormal.z) > 2.0f)
- {
- combinedColor = albedo * radiance + glowTint;
- return;
- }
- vec4 dirLightSpecular = occlusionFactor * specular * dirLightColor;
- dirLightSpecular *= 0.5f; //arbitrary adjustment
- vec4 dirLightDirect = ((NdotL * dirLightColor) * occlusionFactor) + (dirLightAmbient * occlusionFactor) + (dirShadowColor * (1.0f - occlusionFactor));
- if(NdotL <= 0.04f)
- {
- dirLightDirect = dirShadowColor;
- dirLightSpecular = vec4(0.0f, 0.0f, 0.0f, 0.0f);
- }
- else if(NdotL <= 0.1)
- {
- float val = (NdotL - 0.04f) / (0.1f - 0.04f);
- dirLightDirect = (dirLightDirect * val) + (dirShadowColor * (1.0f - val));
- dirLightSpecular = dirLightSpecular * val;
- }
- dirLightDirect += accumulatePointLights();
- dirLightSpecular.a = length(dirLightSpecular.rgb);
- dirLightDirect.a *= min(occlusionFactor + 0.75f, 1.0f);
- combinedColor.rgb = dirLightDirect.rgb * albedo.rgb;
- combinedColor.a = albedo.a;
- combinedColor += dirLightSpecular;
- }
- void main()
- {
- vec4 albedo = vec4(0.0f, 0.0f, 0.0f, 0.0f);
- calculateSurface(gl_Color, albedo);
- float occlusionFactor = 0.0f;
- if(NdotL > -0.01f)
- {
- occlusionFactor = 1.0f;
- }
- // Apply lighting and fog.
- vec4 fragColor = vec4(0.0f, 0.0f, 0.0f, 0.0f);
- if(isParticle == 1)
- {
- vec4 texAlbedo = texture2D(tex, texCoord);
- fragColor = texAlbedo * gl_Color;
- fragColor.a = texAlbedo.a * gl_Color.a;
- vec4 dirLightDirect = (dirLightColor * occlusionFactor) + (dirLightAmbient * occlusionFactor) + (dirShadowColor * (1.0f - occlusionFactor));
- vec4 plt = accumulateParticlePointLights();
- vec4 lightTotal = dirLightDirect + plt;
- lightTotal.x = clamp(lightTotal.x, 0.0f, 1.2f);
- lightTotal.y = clamp(lightTotal.y, 0.0f, 1.2f);
- lightTotal.z = clamp(lightTotal.z, 0.0f, 1.2f);
- fragColor = texAlbedo * gl_Color * lightTotal;
- applyFog(fragColor);
- fragColor.a = texAlbedo.a * gl_Color.a;
- }
- else
- {
- applyLighting(fragColor, albedo, occlusionFactor);
- applyFog(fragColor);
- }
- gl_FragColor = fragColor;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement