Skip to content

Commit

Permalink
renderer2: r2 update #2
Browse files Browse the repository at this point in the history
  • Loading branch information
core-c authored and IR4T4 committed Dec 12, 2018
1 parent 8609df1 commit a793151
Show file tree
Hide file tree
Showing 21 changed files with 888 additions and 794 deletions.
218 changes: 97 additions & 121 deletions src/renderer2/glsl/forwardLighting_fp.glsl
@@ -1,12 +1,20 @@
/* forwardLighting_fp.glsl */
#include "lib/reliefMapping"
//#if defined(USE_NORMAL_MAPPING)
#include "lib/normalMapping"
//#if defined(USE_PARALLAX_MAPPING)
#include "lib/reliefMapping"
//#endif // USE_PARALLAX_MAPPING
//#endif // USE_NORMAL_MAPPING

uniform sampler2D u_DiffuseMap;
uniform sampler2D u_NormalMap;
uniform sampler2D u_SpecularMap;
uniform int u_AlphaTest;
uniform mat4 u_ViewMatrix;
uniform sampler2D u_AttenuationMapXY;
uniform sampler2D u_AttenuationMapZ;
#if defined(USE_NORMAL_MAPPING)
uniform sampler2D u_NormalMap;
uniform sampler2D u_SpecularMap;
#endif // USE_NORMAL_MAPPING

#if defined(LIGHT_DIRECTIONAL)
uniform sampler2D u_ShadowMap0;
Expand All @@ -20,10 +28,6 @@ uniform sampler2D u_ShadowMap0;
uniform samplerCube u_ShadowMap;
#endif

//uniform sampler2D u_RandomMap; // random normals

uniform vec3 u_ViewOrigin;

#if defined(LIGHT_DIRECTIONAL)
uniform vec3 u_LightDir;
#else
Expand All @@ -33,32 +37,40 @@ uniform vec3 u_LightColor;
uniform float u_LightRadius;
uniform float u_LightScale;
uniform float u_LightWrapAround;
uniform int u_AlphaTest;

uniform mat4 u_ShadowMatrix[MAX_SHADOWMAPS];
uniform vec4 u_ShadowParallelSplitDistances;
uniform float u_ShadowTexelSize;
uniform float u_ShadowBlur;

uniform mat4 u_ViewMatrix;

uniform vec4 u_PortalPlane;

uniform float u_DepthScale;

varying vec3 var_Position;
varying vec4 var_TexDiffuse;
varying vec4 var_TexNormal;
#if defined(USE_NORMAL_MAPPING)
varying vec2 var_TexSpecular;
#endif
varying vec4 var_TexAttenuation;
#if defined(USE_NORMAL_MAPPING)
varying vec4 var_Tangent;
varying vec4 var_Binormal;
#endif
varying vec4 var_Normal;
//varying vec4 var_Color;
#if defined(USE_NORMAL_MAPPING)
varying mat3 var_tangentMatrix;
varying vec2 var_TexSpecular;
varying vec3 var_ViewOrigin; // vieworigin - position !
varying vec3 var_ViewOrigin2; // vieworigin in worldspace
#if defined(USE_PARALLAX_MAPPING)
varying vec2 var_S; // size and start position of search in texture space
#endif // USE_PARALLAX_MAPPING
#endif // USE_NORMAL_MAPPING
#if defined(USE_PORTAL_CLIPPING)
varying float var_BackSide; // in front, or behind, the portalplane
#endif // USE_PORTAL_CLIPPING


/*
VSM Variance Shadow Mapping
ESM Exponential Shadow Maps
EVSM Exponential Variance Shadow Mapping
PCF Percentage-Closer Filtering
PCSS Percentage-Closer Soft Shadow
*/



/*
================
Expand All @@ -68,6 +80,7 @@ Given a normalized forward vector, create two
other perpendicular vectors
================
*/

void MakeNormalVectors(const vec3 forward, inout vec3 right, inout vec3 up)
{
// this rotate and negate guarantees a vector
Expand Down Expand Up @@ -140,9 +153,9 @@ float ChebyshevUpperBound(vec2 shadowMoments, float vertexDistance, float minVar
float pMax = variance / (variance + (d * d));

/*
#if defined(r_LightBleedReduction)
pMax = smoothstep(r_LightBleedReduction, 1.0, pMax);
#endif
// #if defined(r_LightBleedReduction)
// pMax = smoothstep(r_LightBleedReduction, 1.0, pMax);
// #endif
*/

// one-tailed Chebyshev with k > 0
Expand Down Expand Up @@ -581,24 +594,20 @@ float log_conv(float x0, float X, float y0, float Y)
void main()
{
#if defined(USE_PORTAL_CLIPPING)
if (var_BackSide < 0.0)
{
float dist = dot(var_Position.xyz, u_PortalPlane.xyz) - u_PortalPlane.w;
if (dist < 0.0)
{
discard;
return;
}
discard;
return;
}
#endif


#if 0
// create random noise vector
vec3 rand = RandomVec3(gl_FragCoord.st * r_FBufScale);

gl_FragColor = vec4(rand * 0.5 + 0.5, 1.0);
return;
#endif
//#if 0
// // create random noise vector
// vec3 rand = RandomVec3(gl_FragCoord.st * r_FBufScale);
// gl_FragColor = vec4(rand * 0.5 + 0.5, 1.0);
// return;
//#endif


float shadow = 1.0;
Expand All @@ -611,15 +620,15 @@ void main()
vec4 shadowMoments;
FetchShadowMoments(var_Position.xyz, shadowVert, shadowMoments);

// FIXME
#if 0 // defined(r_PCFSamples)
shadowMoments = PCF(var_Position.xyz, u_ShadowTexelSize * u_ShadowBlur, r_PCFSamples);
#endif
// // FIXME
// #if 0 // defined(r_PCFSamples)
// shadowMoments = PCF(var_Position.xyz, u_ShadowTexelSize * u_ShadowBlur, r_PCFSamples);
// #endif

#if 0
gl_FragColor = vec4(u_ShadowTexelSize * u_ShadowBlur * u_LightRadius, 0.0, 0.0, 1.0);
return;
#endif
//#if 0
// gl_FragColor = vec4(u_ShadowTexelSize * u_ShadowBlur * u_LightRadius, 0.0, 0.0, 1.0);
// return;
//#endif

#if defined(r_ShowParallelShadowSplits)
// transform to camera space
Expand Down Expand Up @@ -776,10 +785,10 @@ void main()
// const float SHADOW_BIAS = 0.01;
// float vertexDistance = length(I) / u_LightRadius - 0.01;

#if 0
gl_FragColor = vec4(u_ShadowTexelSize * u_ShadowBlur * length(I), 0.0, 0.0, 1.0);
return;
#endif
//#if 0
// gl_FragColor = vec4(u_ShadowTexelSize * u_ShadowBlur * length(I), 0.0, 0.0, 1.0);
// return;
//#endif

#if defined(r_PCFSamples)
vec4 shadowMoments = PCF(I, u_ShadowTexelSize * u_ShadowBlur * length(I), r_PCFSamples);
Expand Down Expand Up @@ -936,83 +945,71 @@ void main()

vec2 texDiffuse = var_TexDiffuse.st;

#if defined(USE_NORMAL_MAPPING)

// invert tangent space for twosided surfaces
mat3 tangentToWorldMatrix;
#if defined(TWOSIDED)
// positive check (don't use tangentToWorldMatrix function)
if (gl_FrontFacing)
{
tangentToWorldMatrix = mat3(-var_Tangent.xyz, -var_Binormal.xyz, -var_Normal.xyz);
}
else
#endif
{
tangentToWorldMatrix = mat3(var_Tangent.xyz, var_Binormal.xyz, var_Normal.xyz);
}
float dotNL;
vec3 specular;

#if defined(USE_NORMAL_MAPPING)

vec2 texNormal = var_TexNormal.st;
vec2 texSpecular = var_TexSpecular.st;

// compute view direction in world space
vec3 V = normalize(u_ViewOrigin - var_Position.xyz);
vec3 V = var_ViewOrigin.xyz; // tangentspace //vec3 V = normalize(u_ViewOrigin - var_Position.xyz);

#if defined(USE_PARALLAX_MAPPING)
// ray intersect in view direction

mat3 worldToTangentMatrix = transpose(tangentToWorldMatrix);

// compute view direction in tangent space
vec3 Vts = normalize(worldToTangentMatrix * V);

// size and start position of search in texture space
vec2 S = Vts.xy * -u_DepthScale / Vts.z;

float depth = RayIntersectDisplaceMap(texNormal, S, u_NormalMap);

float depth = RayIntersectDisplaceMap(texNormal, var_S, u_NormalMap);
// compute texcoords offset
vec2 texOffset = S * depth;
vec2 texOffset = var_S * depth;

texDiffuse.st += texOffset;
texNormal.st += texOffset;
texSpecular.st += texOffset;
texDiffuse += texOffset;
texNormal += texOffset;
texSpecular += texOffset;
#endif // USE_PARALLAX_MAPPING

// compute half angle in world space
vec3 H = normalize(L + V);
// normal
vec3 Ntex = texture2D(u_NormalMap, texNormal).xyz * 2.0 - 1.0;
// transform normal from tangentspace to worldspace
vec3 N = normalize(var_tangentMatrix * Ntex); // we must normalize to get a vector of unit-length.. reflect() needs it

// compute normal
vec3 N = computeNormal(texture2D(u_NormalMap, texNormal).xyz, tangentToWorldMatrix);
// the cosine of the angle between N & L (if N & L are vectors of unit length (normalized))
dotNL = dot(N, L);

vec3 R = reflect(-L, N);
// compute the specular term
if (dotNL > 0.0)
{
vec3 H = normalize(L + V); // the half-vector
float dotNH = max(0.0, dot(N, H));
vec4 map = texture2D(u_SpecularMap, texSpecular);
float exponent = map.a * r_SpecularExponent;
float intensity = pow(dotNH, exponent);
specular = map.rgb * u_LightColor * intensity; // * r_SpecularScale;
}
else
{
specular = vec3(0.0);
}

#else // else USE_NORMAL_MAPPING

vec3 N;
#if defined(TWOSIDED)
if (!gl_FrontFacing)
{
N = -normalize(var_Normal.xyz);
N = normalize(-var_Normal.xyz);
}
else
#endif
{
N = normalize(var_Normal.xyz);
}
dotNL = dot(N, L);

#endif // end USE_NORMAL_MAPPING

// compute the light term
#if defined(r_WrapAroundLighting)
float NL = clamp(dot(N, L) + u_LightWrapAround, 0.0, 1.0) / clamp(1.0 + u_LightWrapAround, 0.0, 1.0);
#else
float NL = clamp(dot(N, L), 0.0, 1.0);
#endif

// compute the diffuse term
vec4 diffuse = texture2D(u_DiffuseMap, texDiffuse.st);

#if defined(USE_ALPHA_TESTING)
if (u_AlphaTest == ATEST_GT_0 && diffuse.a <= 0.0)
{
Expand All @@ -1030,24 +1027,23 @@ void main()
return;
}
#endif
diffuse.rgb *= u_LightColor * NL;

#if defined(USE_NORMAL_MAPPING)
// compute the specular term
//vec3 specular = texture2D(u_SpecularMap, texSpecular).rgb * u_LightColor * pow(clamp(dot(N, H), 0.0, 1.0), r_SpecularExponent) * r_SpecularScale;
vec3 specular = texture2D(u_SpecularMap, texSpecular).rgb * u_LightColor * pow(max(dot(V, R), 0.0), r_SpecularExponent) * r_SpecularScale;
// compute the light term
#if defined(r_WrapAroundLighting)
float NL = clamp(dotNL + u_LightWrapAround, 0.0, 1.0) / clamp(1.0 + u_LightWrapAround, 0.0, 1.0);
#else
float NL = clamp(dotNL, 0.0, 1.0);
#endif
diffuse.rgb *= (u_LightColor * NL);


// compute light attenuation
#if defined(LIGHT_PROJ)
vec3 attenuationXY = texture2DProj(u_AttenuationMapXY, var_TexAttenuation.xyw).rgb;
vec3 attenuationZ = texture2D(u_AttenuationMapZ, vec2(var_TexAttenuation.z + 0.5, 0.0)).rgb; // FIXME

#elif defined(LIGHT_DIRECTIONAL)
vec3 attenuationXY = vec3(1.0);
vec3 attenuationZ = vec3(1.0);

#else
vec3 attenuationXY = texture2D(u_AttenuationMapXY, var_TexAttenuation.xy).rgb;
vec3 attenuationZ = texture2D(u_AttenuationMapZ, vec2(var_TexAttenuation.z, 0)).rgb;
Expand All @@ -1072,24 +1068,4 @@ void main()
color.gb *= var_TexNormal.pq;

gl_FragColor = color;

#if 0
#if defined(USE_PARALLAX_MAPPING)
gl_FragColor = vec4(vec3(1.0, 0.0, 0.0), diffuse.a);
#elif defined(USE_NORMAL_MAPPING)
gl_FragColor = vec4(vec3(0.0, 0.0, 1.0), diffuse.a);
#else
gl_FragColor = vec4(vec3(0.0, 1.0, 0.0), diffuse.a);
#endif
#endif

#if 0
#if defined(USE_VERTEX_SKINNING)
gl_FragColor = vec4(vec3(1.0, 0.0, 0.0), diffuse.a);
#elif defined(USE_VERTEX_ANIMATION)
gl_FragColor = vec4(vec3(0.0, 0.0, 1.0), diffuse.a);
#else
gl_FragColor = vec4(vec3(0.0, 1.0, 0.0), diffuse.a);
#endif
#endif
}

0 comments on commit a793151

Please sign in to comment.