wall-saber intersection painted on wall shader

This commit is contained in:
Diego F. Goberna
2018-11-09 21:58:16 +01:00
parent ba7dcc9857
commit 9b48e9966c
2 changed files with 61 additions and 8 deletions

View File

@@ -3,18 +3,20 @@ AFRAME.registerShader('wall-shader', {
schema: {
iTime: {type: 'time', is: 'uniform'},
tex: {type: 'map', is: 'uniform'},
env: {type: 'map', is: 'uniform'}
env: {type: 'map', is: 'uniform'},
hitRight: {type: 'vec3', is: 'uniform', default: {x: 0, y: 1, z: 0}},
hitLeft: {type: 'vec3', is: 'uniform', default: {x: 0, y: 0, z: 0}}
},
vertexShader: `
varying vec2 uvs;
varying vec3 nrml;
varying vec3 pos;
varying vec3 worldPos;
void main() {
uvs.xy = uv.xy;
nrml.xyz = normal.xyz;
vec4 p = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
pos = position;
worldPos = (modelMatrix * vec4( position, 1.0 )).xyz;
gl_Position = p;
}
`,
@@ -23,10 +25,12 @@ AFRAME.registerShader('wall-shader', {
// based on https://www.shadertoy.com/view/ldlXRS
varying vec2 uvs;
varying vec3 nrml;
varying vec3 pos;
varying vec3 worldPos;
uniform float iTime;
uniform sampler2D tex;
uniform sampler2D env;
uniform vec3 hitRight;
uniform vec3 hitLeft;
#define time iTime/1000.0*0.15
#define tau 6.2831853
@@ -56,6 +60,10 @@ AFRAME.registerShader('wall-shader', {
return fbm(p*makem2(time*0.2));
}
vec3 drawCircle(vec3 p, vec3 center, float radius, float edgeWidth, vec3 color) {
return color*(1.0-smoothstep(radius, radius+edgeWidth, length(p-center)));
}
void main() {
vec2 p = uvs.xy-0.5;// / iResolution.xy-0.5;
@@ -74,15 +82,18 @@ AFRAME.registerShader('wall-shader', {
col += smoothstep(0.48, 0.495, abs(pp.x));
col += smoothstep(0.48, 0.495, abs(pp.y));
col += drawCircle(worldPos, hitRight, 0.04, 0.05, vec3(1.0, 0.4, 0.4));
col += drawCircle(worldPos, hitRight, 0.02, 0.005, vec3(1.0, 1.0, 1.0));
col += drawCircle(worldPos, hitLeft, 0.04, 0.05, vec3(1.0, 0.4, 0.4));
col += drawCircle(worldPos, hitLeft, 0.02, 0.005, vec3(1.0, 1.0, 1.0));
//gl_FragColor = vec4(col, 1.0);
// add environment reflection
vec3 worldNormal = normalize( ( vec4( nrml + col, 0.0 ) * viewMatrix ).xyz );
vec3 reflectVec = normalize(reflect(normalize(pos - cameraPosition), worldNormal));
vec3 reflectVec = normalize(reflect(normalize(worldPos - cameraPosition), normalize(nrml + col)));
vec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0, 0.0, 1.0 ) );
gl_FragColor = vec4(texture2D(env, reflectView.xy * 0.5 + 0.5).xyz + col, 1.0);
gl_FragColor = vec4(texture2D(env, reflectView.xy * 0.5 + 0.5).xyz * 0.05 + col, 0.9 + col.x);
}
`
});