Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- precision highp float;
- // Compiler should remove unneeded stuff
- uniform vec3 view_position;
- uniform vec3 light_globalAmbient;
- varying vec3 vPositionW;
- varying vec3 vNormalW;
- varying vec3 vTangentW;
- varying vec3 vBinormalW;
- varying vec2 vUv0;
- varying vec2 vUv1;
- varying vec4 vVertexColor;
- varying vec3 vNormalV;
- struct psInternalData {
- vec3 albedo;
- vec3 specularity;
- float glossiness;
- vec3 emission;
- vec3 normalW;
- mat3 TBN;
- vec3 viewDirW;
- vec3 reflDirW;
- vec3 diffuseLight;
- vec3 specularLight;
- float alpha;
- vec3 lightDirNormW;
- vec3 lightDirW;
- float atten;
- vec3 shadowCoord;
- vec2 uvOffset;
- };
- void getViewDir(inout psInternalData data) {
- data.viewDirW = normalize(view_position - vPositionW);
- }
- void getReflDir(inout psInternalData data) {
- data.reflDirW = normalize(-reflect(data.viewDirW, data.normalW));
- }
- void addAmbientConstant(inout psInternalData data) {
- data.diffuseLight += light_globalAmbient;
- }
- void getLightDirPoint(inout psInternalData data, vec3 lightPosW) {
- data.lightDirW = vPositionW - lightPosW;
- data.lightDirNormW = normalize(data.lightDirW);
- }
- float getFalloffLinear(inout psInternalData data, float lightRadius) {
- float d = length(data.lightDirW);
- return max(((lightRadius - d) / lightRadius), 0.0);
- }
- float getFalloffInvSquared(inout psInternalData data) {
- float sqrDist = dot(data.lightDirW, data.lightDirW);
- return 1.0 / sqrDist;
- }
- float getSpotEffect(inout psInternalData data, vec3 lightSpotDirW, float lightInnerConeAngle, float lightOuterConeAngle) {
- float cosAngle = dot(data.lightDirNormW, lightSpotDirW);
- return smoothstep(lightOuterConeAngle, lightInnerConeAngle, cosAngle);
- }
- varying vec2 vUvNormal;
- uniform vec3 light0_color;
- uniform vec3 light0_direction;
- uniform mat4 light0_shadowMatrix;
- uniform vec3 light0_shadowParams;
- uniform sampler2D light0_shadowMap;
- uniform vec3 light1_color;
- uniform vec3 light1_position;
- uniform float light1_radius;
- uniform vec3 light2_color;
- uniform vec3 light2_position;
- uniform float light2_radius;
- uniform vec3 light3_color;
- uniform vec3 light3_position;
- uniform float light3_radius;
- uniform vec3 light4_color;
- uniform vec3 light4_position;
- uniform float light4_radius;
- uniform vec3 light4_spotDirection;
- uniform float light4_innerConeAngle;
- uniform float light4_outerConeAngle;
- uniform mat4 light4_shadowMatrix;
- uniform vec3 light4_shadowParams;
- uniform sampler2D light4_shadowMap;
- uniform sampler2D texture_normalMap;
- uniform float material_bumpMapFactor;
- void getNormal(inout psInternalData data) {
- vec3 normalMap = texture2D(texture_normalMap, vUvNormal).xyz * 2.0 - 1.0;
- normalMap = normalize(mix(vec3(0.0, 0.0, 1.0), normalMap, material_bumpMapFactor));
- data.normalW = data.TBN * normalMap;
- }
- void getTBN(inout psInternalData data) {
- data.TBN = mat3(normalize(vTangentW), normalize(vBinormalW), normalize(vNormalW));
- }
- vec4 texture2DAlbedo(sampler2D tex, vec2 uv) {
- vec4 rgba = texture2D(tex, uv);
- rgba.rgb = pow(rgba.rgb, vec3(2.2));
- return rgba;
- }
- vec3 gammaCorrectOutput(vec3 color) {
- return pow(color, vec3(0.45));
- }
- uniform vec3 material_diffuse;
- void getAlbedo(inout psInternalData data) {
- data.albedo = material_diffuse.rgb;
- }
- uniform vec3 material_specular;
- void getSpecularity(inout psInternalData data) {
- data.specularity = material_specular;
- }
- // Easily tweakable but not very correct Fresnel
- uniform float material_fresnelFactor;
- void getFresnel(inout psInternalData data) {
- float fresnel = 1.0 - max(dot(data.normalW, data.viewDirW), 0.0);
- data.specularity *= pow(fresnel, material_fresnelFactor);
- }
- uniform float material_shininess;
- void getGlossiness(inout psInternalData data) {
- // Hack: On Mac OS X, calling pow with zero for the exponent generates hideous artifacts so bias up a little
- data.glossiness = material_shininess + 0.0001;
- }
- uniform float material_opacity;
- void getOpacity(inout psInternalData data) {
- data.alpha = material_opacity;
- }
- uniform vec3 material_emissive;
- vec3 getEmission(inout psInternalData data) {
- return material_emissive;
- }
- uniform samplerCube texture_cubeMap;
- uniform float material_reflectionFactor;
- void addCubemapReflection(inout psInternalData data) {
- data.specularLight += textureCube(texture_cubeMap, data.reflDirW).rgb * material_reflectionFactor;
- }
- float unpackFloat(vec4 rgbaDepth) {
- const vec4 bitShift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
- return dot(rgbaDepth, bitShift);
- }
- void getShadowCoord(inout psInternalData data, mat4 shadowMatrix, vec3 shadowParams) {
- vec4 projPos = shadowMatrix * vec4(vPositionW, 1.0);
- projPos.xyz /= projPos.w;
- projPos.z += shadowParams.z;
- data.shadowCoord = projPos.xyz;
- }
- bool shadowContained(psInternalData data) {
- bvec4 containedVec = bvec4(data.shadowCoord.x >= 0.0, data.shadowCoord.x <= 1.0, data.shadowCoord.y >= 0.0, data.shadowCoord.y <= 1.0);
- return all(bvec2(all(containedVec), data.shadowCoord.z <= 1.0));
- }
- float getShadow(inout psInternalData data, sampler2D shadowMap, vec3 shadowParams) {
- if (shadowContained(data)) {
- float depth = unpackFloat(texture2D(shadowMap, data.shadowCoord.xy));
- return (depth < data.shadowCoord.z) ? 0.0 : 1.0;
- }
- return 1.0;
- }
- float getShadowPCF3x3(inout psInternalData data, sampler2D shadowMap, vec3 shadowParams) {
- if (shadowContained(data)) {
- float shadowAccum = 0.0;
- vec3 shadowCoord = data.shadowCoord;
- float xoffset = 1.0 / shadowParams.x; // 1/shadow map width
- float yoffset = 1.0 / shadowParams.y; // 1/shadow map height
- float dx0 = -xoffset;
- float dy0 = -yoffset;
- float dx1 = xoffset;
- float dy1 = yoffset;
- mat3 shadowKernel;
- mat3 depthKernel;
- depthKernel[0][0] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(dx0, dy0)));
- depthKernel[0][1] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(dx0, 0.0)));
- depthKernel[0][2] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(dx0, dy1)));
- depthKernel[1][0] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(0.0, dy0)));
- depthKernel[1][1] = unpackFloat(texture2D(shadowMap, shadowCoord.xy));
- depthKernel[1][2] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(0.0, dy1)));
- depthKernel[2][0] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(dx1, dy0)));
- depthKernel[2][1] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(dx1, 0.0)));
- depthKernel[2][2] = unpackFloat(texture2D(shadowMap, shadowCoord.xy + vec2(dx1, dy1)));
- vec3 shadowZ = vec3(shadowCoord.z);
- shadowKernel[0] = vec3(lessThan(depthKernel[0], shadowZ));
- shadowKernel[0] *= vec3(0.25);
- shadowKernel[1] = vec3(lessThan(depthKernel[1], shadowZ));
- shadowKernel[1] *= vec3(0.25);
- shadowKernel[2] = vec3(lessThan(depthKernel[2], shadowZ));
- shadowKernel[2] *= vec3(0.25);
- vec2 fractionalCoord = 1.0 - fract( shadowCoord.xy * shadowParams.xy );
- shadowKernel[0] = mix(shadowKernel[1], shadowKernel[0], fractionalCoord.x);
- shadowKernel[1] = mix(shadowKernel[2], shadowKernel[1], fractionalCoord.x);
- vec4 shadowValues;
- shadowValues.x = mix(shadowKernel[0][1], shadowKernel[0][0], fractionalCoord.y);
- shadowValues.y = mix(shadowKernel[0][2], shadowKernel[0][1], fractionalCoord.y);
- shadowValues.z = mix(shadowKernel[1][1], shadowKernel[1][0], fractionalCoord.y);
- shadowValues.w = mix(shadowKernel[1][2], shadowKernel[1][1], fractionalCoord.y);
- shadowAccum = 1.0 - dot( shadowValues, vec4( 1.0 ) );
- return shadowAccum;
- }
- return 1.0;
- }
- float getLightDiffuse(inout psInternalData data) {
- return max(dot(data.normalW, -data.lightDirNormW), 0.0);
- }
- // Energy-conserving (hopefully) Blinn-Phong
- float getLightSpecular(inout psInternalData data) {
- vec3 h = normalize( -data.lightDirNormW - -data.viewDirW );
- float nh = max( dot( h, data.normalW ), 0.0 );
- return pow( nh, data.glossiness ) * (data.glossiness + 2.0)/8.0;
- }
- vec3 combineColor(inout psInternalData data) {
- return mix(data.albedo * data.diffuseLight, data.specularLight, data.specularity);
- }
- void main(void) {
- psInternalData data;
- data.diffuseLight = vec3(0);
- data.specularLight = vec3(0);
- getViewDir(data);
- getTBN(data);
- getNormal(data);
- getReflDir(data);
- getAlbedo(data);
- getSpecularity(data);
- getFresnel(data);
- getGlossiness(data);
- getOpacity(data);
- addAmbientConstant(data);
- addCubemapReflection(data);
- data.lightDirNormW = light0_direction;
- data.atten = 1.0;
- data.atten *= getLightDiffuse(data);
- getShadowCoord(data, light0_shadowMatrix, light0_shadowParams);
- data.atten *= getShadowPCF3x3(data, light0_shadowMap, light0_shadowParams);
- data.diffuseLight += data.atten * light0_color;
- data.atten *= getLightSpecular(data);
- data.specularLight += data.atten * light0_color;
- getLightDirPoint(data, light1_position);
- data.atten = getFalloffLinear(data, light1_radius);
- data.atten *= getLightDiffuse(data);
- data.diffuseLight += data.atten * light1_color;
- data.atten *= getLightSpecular(data);
- data.specularLight += data.atten * light1_color;
- getLightDirPoint(data, light2_position);
- data.atten = getFalloffLinear(data, light2_radius);
- data.atten *= getLightDiffuse(data);
- data.diffuseLight += data.atten * light2_color;
- data.atten *= getLightSpecular(data);
- data.specularLight += data.atten * light2_color;
- getLightDirPoint(data, light3_position);
- data.atten = getFalloffLinear(data, light3_radius);
- data.atten *= getLightDiffuse(data);
- data.diffuseLight += data.atten * light3_color;
- data.atten *= getLightSpecular(data);
- data.specularLight += data.atten * light3_color;
- getLightDirPoint(data, light4_position);
- data.atten = getFalloffLinear(data, light4_radius);
- data.atten *= getSpotEffect(data, light4_spotDirection, light4_innerConeAngle, light4_outerConeAngle);
- data.atten *= getLightDiffuse(data);
- getShadowCoord(data, light4_shadowMatrix, light4_shadowParams);
- data.atten *= getShadowPCF3x3(data, light4_shadowMap, light4_shadowParams);
- data.diffuseLight += data.atten * light4_color;
- data.atten *= getLightSpecular(data);
- data.specularLight += data.atten * light4_color;
- gl_FragColor.rgb = combineColor(data);
- gl_FragColor.rgb += getEmission(data);
- gl_FragColor.rgb = gammaCorrectOutput(gl_FragColor.rgb);
- gl_FragColor.a = data.alpha;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement