brute force specular reflection
This commit is contained in:
35
index.html
35
index.html
@@ -98,6 +98,7 @@
|
||||
varying vec3 v_fragPos;
|
||||
varying vec2 v_uv;
|
||||
|
||||
uniform vec3 eyePos;
|
||||
uniform sampler2D displace_map;
|
||||
|
||||
vec3 lightPos = vec3(0.,0.,10.); //should be an directional light
|
||||
@@ -111,10 +112,10 @@
|
||||
//calculate normal
|
||||
float gridPointDelta = (1. / 256.);
|
||||
vec3 currPoint = vec3(0.0,0.0,displace.x);
|
||||
vec3 right = vec3(gridPointDelta,0.0,texture2D(displace_map,vec2(v_uv.x + gridPointDelta,v_uv.y)).x);
|
||||
vec3 left = vec3(-gridPointDelta,0.0,texture2D(displace_map,vec2(v_uv.x - gridPointDelta,v_uv.y)).x);
|
||||
vec3 up = vec3(0.,gridPointDelta,texture2D(displace_map,vec2(v_uv.x ,v_uv.y + gridPointDelta)).x);
|
||||
vec3 down = vec3(0.,-gridPointDelta,texture2D(displace_map,vec2(v_uv.x ,v_uv.y - gridPointDelta)).x);
|
||||
vec3 right = vec3(gridPointDelta,0.0,texture2D(displace_map,vec2(v_uv.x + gridPointDelta,v_uv.y)).x*0.1);
|
||||
vec3 left = vec3(-gridPointDelta,0.0,texture2D(displace_map,vec2(v_uv.x - gridPointDelta,v_uv.y)).x*0.1);
|
||||
vec3 up = vec3(0.,gridPointDelta,texture2D(displace_map,vec2(v_uv.x ,v_uv.y + gridPointDelta)).x*0.1);
|
||||
vec3 down = vec3(0.,-gridPointDelta,texture2D(displace_map,vec2(v_uv.x ,v_uv.y - gridPointDelta)).x*0.1);
|
||||
|
||||
//vec3 tangent = normalize(right - currPoint);
|
||||
//vec3 biTangent = normalize(up - currPoint);
|
||||
@@ -124,26 +125,28 @@
|
||||
vec3 normal = cross(tangent, biTangent);
|
||||
|
||||
vec3 norm = normalize(normal);
|
||||
vec3 lightDir = normalize(lightPos- v_fragPos);
|
||||
norm.y *= -1.; //Normal y direction is somehow inverted
|
||||
vec3 lightDir = normalize(lightPos - v_fragPos);
|
||||
|
||||
float diff = max(dot(norm,lightDir),0.0);
|
||||
vec3 diffuse = diff * lightColor;
|
||||
vec3 result = (diffuse) * vec3(0.0,0.0,1.0);
|
||||
|
||||
//Old lightning
|
||||
//vec3 toCameraVector = v_fragPos - vec3(0.,0.,0.);
|
||||
//vec3 reflec = normalize(reflect(toCameraVector, normal));
|
||||
vec3 toCameraVector = normalize(v_fragPos - eyePos);
|
||||
vec3 reflec = normalize(reflect(toCameraVector, norm));
|
||||
|
||||
//Schlicks approximation to Fresnelfactor
|
||||
//float n1 = 1., n2 = 1.33333;
|
||||
//float R0 = pow((n1-n2)/(n1+n2), 2.);
|
||||
//float fresnel = R0 + (1. - R0)*pow((1.-dot(normal,reflec)),5.) ;
|
||||
float n1 = 1., n2 = 1.33333;
|
||||
float R0 = pow((n1-n2)/(n1+n2), 2.);
|
||||
float fresnel = R0 + (1. - R0)*pow((1.-dot(norm,reflec)),5.) ;
|
||||
|
||||
//vec3 waterColor = vec3(34./255.,154./255.,211./255.);
|
||||
//vec3 lightColor = vec3(1.,1.,1.);
|
||||
//vec3 waterColor = vec3(34./255.,154./255.,211./255.);
|
||||
vec3 oceanColor = vec3(0,.04,.04) * 10.; // under-sea colour
|
||||
vec3 skyColor = vec3(1.,1.,1.);
|
||||
|
||||
//gl_FragColor = vec4(mix(waterColor, vec3(1.,1.,1.),fresnel).xyz, 1.);
|
||||
gl_FragColor = vec4(result,1.0);
|
||||
gl_FragColor = vec4(mix(oceanColor,skyColor,fresnel).xyz, 1.);
|
||||
//gl_FragColor = vec4(result,1.0);
|
||||
//gl_FragColor = vec4(normal,1.0);
|
||||
//gl_FragColor = vec4(displace.x,displace.x,displace.x,1.0); //Show Perlin Noise texture deactivate vertex distrotion before
|
||||
}
|
||||
@@ -151,9 +154,9 @@
|
||||
<script id="default-vs" type="x-shader/x-vertex">
|
||||
attribute vec3 positionAttr;
|
||||
|
||||
uniform mat4 view;
|
||||
uniform mat4 model;
|
||||
uniform mat4 projection;
|
||||
uniform float uTime;
|
||||
uniform sampler2D displace_map;
|
||||
|
||||
varying vec2 v_uv;
|
||||
@@ -162,7 +165,7 @@
|
||||
void main(void) {
|
||||
vec4 displace = texture2D(displace_map, vec2(positionAttr.x,positionAttr.y));
|
||||
vec4 worldPos = model * vec4(positionAttr.x,positionAttr.y,positionAttr.z + displace.x, 1.0);
|
||||
gl_Position = projection * worldPos;
|
||||
gl_Position = projection * view * worldPos;
|
||||
v_fragPos = worldPos.xyz;
|
||||
v_uv = positionAttr.xy;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user