Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vec3 light1Position = vec3(0.0, sin(TIME)*5.0, -5.0*sin(TIME)*1.0);
- vec3 light2Position = vec3(0.0, -1.0, 1.0);
- float objIndex = 0.0;
- // float distanceToSurface(vec3 ourPos){
- // vec3 sphereOrigin1 = vec3(2.0*sin(TIME), 0.0, 5.0);
- // float dist1 = sdSphere(ourPos, sphereOrigin1, 1.5);
- // vec3 sphereOrigin2 = vec3(-2.0, 0.0, 5.0);
- // float dist2 = sdSphere(ourPos, sphereOrigin2, 1.5);
- // // float lightSurf = sdSphere(ourPos, vec3(light1Position.xy, -light1Position.z), 0.1);
- // float mainSurf = min(dist1, dist2);
- // // if (lightSurf < mainSurf){
- // // objIndex = 1.0;
- // // } else {
- // // objIndex = 0.0;
- // // }
- // return mainSurf;
- // }
- vec3 rotateX(vec3 p, float angle)
- {
- float c = cos(angle);
- float s = sin(angle);
- return vec3(p.x, c*p.y+s*p.z, -s*p.y+c*p.z);
- }
- vec3 rotateY(vec3 p, float angle)
- {
- float c = cos(angle);
- float s = sin(angle);
- return vec3(c*p.x-s*p.z, p.y, s*p.x+c*p.z);
- }
- vec3 rotateZ(vec3 p, float angle)
- {
- float c = cos(angle);
- float s = sin(angle);
- return vec3(c*p.x+s*p.y, -s*p.x+c*p.y, p.z);
- }
- float sdSphere(vec3 p, vec3 offset, float s )
- {
- return length(p-offset)-s;
- }
- float sdPlane(vec3 p, vec4 n)
- {
- // n must be normalized
- return dot(p,n.xyz) + n.w;
- }
- float sdHexPrism(vec3 p, vec2 h)
- {
- const vec3 k = vec3(-0.8660254, 0.5, 0.57735);
- p = abs(p);
- p.xy -= 2.0*min(dot(k.xy, p.xy), 0.0)*k.xy;
- vec2 d = vec2(
- length(p.xy-vec2(clamp(p.x,-k.z*h.x,k.z*h.x), h.x))*sign(p.y-h.x),
- p.z-h.y );
- return min(max(d.x,d.y),0.0) + length(max(d,0.0));
- }
- float distanceToSurface(vec3 ourPos){
- vec3 sphereOrigin1 = vec3(0.5, 0.5, 0.5);
- ourPos = vec3(mod(ourPos.x, 1.0), mod(ourPos.y, 5.0), mod(ourPos.z, 1.0));
- float dist1 = sdSphere(ourPos, sphereOrigin1, 0.25);
- return dist1;
- }
- vec3 normal(vec3 testPos){
- const float eps = 0.001;
- vec3 p = testPos;
- return normalize(vec3(
- distanceToSurface(vec3(p.x + eps, p.y, p.z)) - distanceToSurface(vec3(p.x - eps, p.y, p.z)),
- distanceToSurface(vec3(p.x, p.y + eps, p.z)) - distanceToSurface(vec3(p.x, p.y - eps, p.z)),
- distanceToSurface(vec3(p.x, p.y, p.z + eps)) - distanceToSurface(vec3(p.x, p.y, p.z - eps))
- ));
- }
- // Tri-Planar blending function. Based on an old Nvidia tutorial.
- vec3 tex3D(sampler2D tex, in vec3 p, in vec3 n, float texZoom){
- n = max((abs(n) - 0.2)*7., 0.001); // max(abs(n), 0.001), etc.
- n /= (n.x + n.y + n.z);
- return (
- texture(tex, p.yz*texZoom)*n.x +
- texture(tex, p.zx*texZoom)*n.y +
- texture(tex, p.xy*texZoom)*n.z).xyz;
- }
- //SET UP THE CAMERA
- //DO THE MARCHING (let the scene understand our 3D surface)
- //COLOR THE SCENE
- #define NUMSTEPS 256
- vec4 renderMain(void) {
- vec3 rayDirection = normalize(vec3(_uvc, FOV));
- vec3 rayOrigin = vec3(0.0);
- vec3 currentPosition = rayOrigin;
- float dist = 0.0;
- float hit = 0.0;
- float stepsNeededToReachSurface = 0.0;
- vec3 hitPosition = vec3(0.0);
- for (int i = 0; i<NUMSTEPS; i++){
- dist = distanceToSurface(currentPosition);
- currentPosition += rayDirection*dist;
- if (dist < 0.0001){
- hit = 1.0;
- stepsNeededToReachSurface = i;
- hitPosition = currentPosition;
- break;
- }
- }
- vec3 bkgCol = vec3(1.0,0.75,0.0)*0.4*(1.5-_uv.y);
- vec3 surfaceNormal = normal(hitPosition);
- vec3 light1Direction = normalize(light1Position - rayDirection);
- float light1Diff = max(dot(surfaceNormal, light1Direction), 0.0);
- vec3 light1Color = vec3(1.0,0.0,0.4);
- vec3 light2Direction = normalize(light2Position - rayDirection);
- float light2Diff = max(dot(surfaceNormal, light2Direction), 0.0);
- vec3 light2Color = vec3(0.0,0.4,1.0);
- float spec1 = pow(max(dot(reflect(-light1Direction, surfaceNormal), -rayDirection), 0.0), 8.);
- float spec2 = pow(max(dot(reflect(-light2Direction, surfaceNormal), -rayDirection), 0.0), 8.);
- vec3 finalCol = vec3(0.0);
- finalCol += bkgCol*(1.0-hit);
- if (hit == 1.0){
- if (objIndex == 0.0){
- //We're dealing with our two large spheres
- finalCol += (light1Diff+spec1)*light1Color*hit*0.75;
- finalCol += (light2Diff+spec2)*light2Color*hit*0.75;
- // finalCol += tex3D(syn_UserImage, hitPosition+1.5, surfaceNormal, 0.25);
- } else if (objIndex == 1.0){
- finalCol += vec3(0.0,0.6,1.0)*0.5;
- }
- }
- return vec4(finalCol, 1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement