Skip to content

Commit

Permalink
Moved eye mask calcs to the control shader, need to fix the tess levels.
Browse files Browse the repository at this point in the history
  • Loading branch information
nitronoid committed Mar 29, 2018
1 parent ce52313 commit 173e93a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 75 deletions.
80 changes: 22 additions & 58 deletions shaders/owl_pbr_geo.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;

//in
in vec3 te_position[3];
in vec3 te_normal[3];
in vec2 te_uv[3];
in vec3 te_position[3];
in vec3 te_normal[3];
in vec2 te_uv[3];
in vec3 te_posA[3];
in vec3 te_posB[3];
in float te_maskA[3];
in float te_maskB[3];
in float te_bigMask[3];

//out
out vec2 TexCoords;
Expand All @@ -28,32 +33,6 @@ uniform float eyeThickness = 0.08;
uniform float eyeGap = 0.19;
uniform float eyeFuzz = 0.02;


mat4 rotationMatrix4d(vec3 axis, float angle)
{
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;

return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
0.0, 0.0, 0.0, 1.0);
}

mat3 rotationMatrix3d(vec3 axis, float angle)
{
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;

return mat3(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c);
}

// Linear interpolation at between x and y, at t
float lerp(float x, float y, float t)
{
Expand All @@ -79,7 +58,7 @@ float smoothpulsetraineven (float e0, float e1, float fuzz, float period, float
return smoothpulsetrain(e0-fuzz, e0, e1, e1+fuzz, period, x);
}

float eyezone (vec3 pos, float fuzz, float gap, float thickness, float warp, float expo)
float eyezone (vec3 pos, float fuzz, float gap, float thickness, float warp, float expo, float _mask)
{
float recipExpo = 1.0 / expo;
// calculate the current radius
Expand All @@ -91,37 +70,20 @@ float eyezone (vec3 pos, float fuzz, float gap, float thickness, float warp, flo
float adjustedFuzz = fuzz;
float elipses = 1 - smoothpulsetraineven(thickness*0.5, thickness + period * 0.5, adjustedFuzz, period, pow(r, recipExpo));

vec2 centre = vec2(0.5);
float dist = distance(centre, pos.xy);
float cap = 0.7;
float mask = 1.0 - smoothstep(cap - fuzz, cap + fuzz, dist);
return mask * clamp(elipses, 0.0, 1.0);
return clamp(elipses, 0.0, 1.0) * _mask;
}

float eyes(vec3 _pos, vec3 norm, float scale, vec3 translate, float twist, float fuzz, float gap, float thickness, float warp, float expo)
float eyes(vec3 _posA, vec3 _posB, float fuzz, float gap, float thickness, float warp, float expo, float _maskA, float _maskB)
{
vec3 posA = _pos;
vec3 posB = _pos;
// Add some noise based on P, and scale in x
vec3 variance = noise3(posA) * 0.066666;
posA += variance;
posB += variance;
float stretch = 1.15;
posA.y *= stretch;
posB.x *= -1.0;
posB.y *= stretch;

// Position the eye
posA = posA/scale - vec3(translate);
posB = posB/scale - vec3(translate);

vec3 upVec = vec3(0.0, 0.0, 1.0);
posA = rotationMatrix3d(upVec, twist) * posA;
posB = rotationMatrix3d(upVec, -twist) * posB;

float eyes = eyezone(posA, fuzz, gap, thickness + noise1(posA * 0.5) * 0.05, warp, expo)
+ eyezone(posB, fuzz, gap, thickness + noise1(posB * 0.5) * 0.05, warp, expo);
if(norm.z < 0.0) eyes = 0.0;
vec3 variance = noise3(_posA) * 0.066666;
_posA += variance;
_posB -= variance;


float eyes = eyezone(_posA, fuzz, gap, thickness + noise1(_posA * 0.5) * 0.05, warp, expo, _maskA)
+ eyezone(_posB, fuzz, gap, thickness + noise1(_posB * 0.5) * 0.05, warp, expo, _maskB);


return eyes;
}
Expand All @@ -133,7 +95,9 @@ void main( void )
Normal = normalize(te_normal[i]);
TexCoords = te_uv[i];

float height = eyes(te_position[i], te_normal[i], eyeScale, eyeTranslate, radians(eyeRotation), eyeFuzz, eyeGap, eyeThickness, eyeWarp, eyeExponent);


float height = eyes(te_posA[i], te_posB[i], eyeFuzz, eyeGap, eyeThickness, eyeWarp, eyeExponent, te_maskA[i], te_maskB[i]) * te_bigMask[i];

EyeVal = height;

Expand Down
98 changes: 89 additions & 9 deletions shaders/owl_pbr_tess_control.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,103 @@ in vec2 v_uv[];
out vec3 tc_position[];
out vec3 tc_normal[];
out vec2 tc_uv[];
out vec3 tc_posA[];
out vec3 tc_posB[];
out float tc_maskA[];
out float tc_maskB[];
out float tc_bigMask[];

uniform int innerTess = 16;
uniform int outerTess = 16;
uniform int innerTess = 64;
uniform int outerTess = 64;
uniform float eyeRotation = 7.0;
uniform float eyeScale = 1.55;
uniform vec3 eyeTranslate = vec3(0.21, 0.3, 0.0);
uniform float eyeFuzz = 0.02;

#define ID gl_InvocationID

mat4 rotationMatrix4d(vec3 axis, float angle)
{
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;

return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
0.0, 0.0, 0.0, 1.0);
}

mat3 rotationMatrix3d(vec3 axis, float angle)
{
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;

return mat3(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c);
}


vec3 eyePos(vec3 _pos, float scale, vec3 translate, float _twist)
{
vec3 newPos = _pos;
float stretch = 1.15;
newPos.y *= stretch;

newPos = newPos/scale - vec3(translate);

vec3 upVec = vec3(0.0, 0.0, 1.0);
newPos = rotationMatrix3d(upVec, _twist) * newPos;

return newPos;
}

float eyeMask(vec3 _pos, float _fuzz)
{
vec2 centre = vec2(0.5);
float dist = distance(centre, _pos.xy);
float cap = 0.7;
float mask = 1.0 - smoothstep(cap - _fuzz, cap + _fuzz, dist);
return clamp(mask, 0.0, 1.0);
}

float mask(float _maskA, float _maskB, float _fuzz, float _zDir)
{
return (_maskA + _maskB) * smoothstep(0.0, 0.0 + _fuzz, _zDir);
}

void main(void)
{
vec3 pos = v_position[ID];
float rotation = radians(eyeRotation);
vec3 posA = eyePos(pos, eyeScale, eyeTranslate, rotation);
pos.x *= -1.0;
vec3 posB = eyePos(pos, eyeScale, eyeTranslate, -rotation);

tc_posA[ID] = posA;
tc_posB[ID] = posB;

float maskA = eyeMask(posA, eyeFuzz);
float maskB = eyeMask(posB, eyeFuzz);
tc_maskA[ID] = maskA;
tc_maskB[ID] = maskB;
float bigMask = mask(maskA, maskB, eyeFuzz, v_normal[ID].z);
tc_bigMask[ID] = bigMask;

tc_position[ID] = v_position[ID];
tc_normal[ID] = v_normal[ID];
tc_uv[ID] = v_uv[ID];

if(ID == 0)
{
gl_TessLevelInner[0] = innerTess;
gl_TessLevelOuter[0] = outerTess;
gl_TessLevelOuter[1] = outerTess;
gl_TessLevelOuter[2] = outerTess;
}
int tessLevel = 1 + int(ceil(64 * smoothstep(0.0, 1.0, bigMask)));
// if(ID == 0)
// {
gl_TessLevelInner[ID] = tessLevel;
gl_TessLevelOuter[ID * 3] = tessLevel;
gl_TessLevelOuter[ID * 3 + 1] = tessLevel;
gl_TessLevelOuter[ID * 3 + 2] = tessLevel;
// }
}
31 changes: 23 additions & 8 deletions shaders/owl_pbr_tess_eval.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@

layout(triangles, equal_spacing, cw) in;

in vec3 tc_position[];
in vec3 tc_normal[];
in vec2 tc_uv[];
in vec3 tc_position[];
in vec3 tc_normal[];
in vec2 tc_uv[];
in vec3 tc_posA[];
in vec3 tc_posB[];
in float tc_maskA[];
in float tc_maskB[];
in float tc_bigMask[];

out vec3 te_position;
out vec3 te_normal;
out vec2 te_uv;
out vec3 te_position;
out vec3 te_normal;
out vec2 te_uv;
out vec3 te_posA;
out vec3 te_posB;
out float te_maskA;
out float te_maskB;
out float te_bigMask;

uniform mat4 MVP;

void main(void)
{
te_normal = (gl_TessCoord.x * tc_normal[0] + gl_TessCoord.y * tc_normal[1] + gl_TessCoord.z * tc_normal[2]);
te_uv = (gl_TessCoord.x * tc_uv[0] + gl_TessCoord.y * tc_uv[1] + gl_TessCoord.z * tc_uv[2]);
te_normal = (gl_TessCoord.x * tc_normal[0] + gl_TessCoord.y * tc_normal[1] + gl_TessCoord.z * tc_normal[2]);
te_uv = (gl_TessCoord.x * tc_uv[0] + gl_TessCoord.y * tc_uv[1] + gl_TessCoord.z * tc_uv[2]);
te_position = (gl_TessCoord.x * tc_position[0] + gl_TessCoord.y * tc_position[1] + gl_TessCoord.z * tc_position[2]);
te_posA = (gl_TessCoord.x * tc_posA[0] + gl_TessCoord.y * tc_posA[1] + gl_TessCoord.z * tc_posA[2]);
te_posB = (gl_TessCoord.x * tc_posB[0] + gl_TessCoord.y * tc_posB[1] + gl_TessCoord.z * tc_posB[2]);
te_maskA = (gl_TessCoord.x * tc_maskA[0] + gl_TessCoord.y * tc_maskA[1] + gl_TessCoord.z * tc_maskA[2]);
te_maskB = (gl_TessCoord.x * tc_maskB[0] + gl_TessCoord.y * tc_maskB[1] + gl_TessCoord.z * tc_maskB[2]);
te_bigMask = (gl_TessCoord.x * tc_bigMask[0] + gl_TessCoord.y * tc_bigMask[1] + gl_TessCoord.z * tc_bigMask[2]);
gl_Position = MVP * vec4(te_position, 1.0);
}

0 comments on commit 173e93a

Please sign in to comment.