Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "Common/ShaderLib/GLSLCompat.glsllib"
- #define PI 3.14159265
- #define MTL_BACKGROUND -1.0
- #define MTL_GROUND 1.0
- #define MTL_AX 2.0
- #define MTL_AY 3.0
- #define MTL_AZ 4.0
- #define MTL_OBJ1 5.0
- #define MTL_OBJ2 6.0
- #define MTL_GREY 7.0
- #define NORMAL_EPS 0.01
- #define NEAR_CLIP_PLANE 0.01
- #define FAR_CLIP_PLANE 1000.0
- #define MAX_RAYCAST_STEPS 200
- #define STEP_DAMPING 0.90
- #define DIST_EPSILON 0.002
- #define GLOBAL_LIGHT_COLOR vec3(0.8,1.0,0.9)
- #define SPEC_COLOR vec3(0.8, 0.90, 0.60)
- #define BACKGROUND_COLOR vec3(0.3, 0.342, 0.5)
- #define CAM_DIST 5.0
- #define CAM_H 2.0
- #define CAM_FOV_FACTOR 2.5
- #define LOOK_AT_H 0.0
- #define LOOK_AT vec3(0.0, LOOK_AT_H, 0.0)
- #define Point3 vec3
- varying vec4 vertColor;
- varying vec3 normal;
- varying float PointSize;
- varying vec4 Position;
- varying float voxelSize;
- varying vec2 resolution;
- varying vec2 center;
- varying vec3 camPos;
- varying vec3 camDir;
- varying vec3 objectViewDir;
- varying vec3 objectPos;
- varying Point3 boxCenter;
- /**
- * Signed distance function for a cube centered at the origin
- * with width = height = length = 2.0
- */
- float cubeSDF(vec3 p) {
- // If d.x < 0, then -1 < p.x < 1, and same logic applies to p.y, p.z
- // So if all components of d are negative, then p is inside the unit cube
- vec3 d = abs(p) - vec3(0.5);
- // Assuming p is inside the cube, how far is it from the surface?
- // Result will be negative or zero.
- float insideDistance = min(max(d.x, max(d.y, d.z)), 0.0);
- // Assuming p is outside the cube, how far is it from the surface?
- // Result will be positive or zero.
- float outsideDistance = length(max(d, 0.0));
- return insideDistance + outsideDistance;
- }
- float sphere(vec3 p, float r) { return length(p) - r; }
- vec2 planet(vec3 p){
- vec2 base = vec2(sphere(p,voxelSize), MTL_GREY);
- return base;
- }
- vec2 map(vec3 p) {
- //vec2 res = grid(p);
- //vec2 cs = coordSys(p);
- //add(res, cs);
- vec2 res = planet(p);
- // res.x = opU(res.x, vec2(distf(p), MTL_GREY).x);
- return res;
- }
- vec2 rayMarch(in vec3 ro, in vec3 rd) {
- float t = NEAR_CLIP_PLANE;
- float m = MTL_BACKGROUND;
- for (int i=0; i < MAX_RAYCAST_STEPS; i++) {
- vec2 res = map(ro + rd*t);
- if (res.x < DIST_EPSILON || t>FAR_CLIP_PLANE) break;
- t += res.x*STEP_DAMPING;
- m = res.y;
- }
- if (t > FAR_CLIP_PLANE) m = MTL_BACKGROUND;
- return vec2(t, m);
- }
- vec3 calcNormal(in vec3 p)
- {
- vec2 d = vec2(NORMAL_EPS, 0.0);
- return normalize(vec3(
- map(p + d.xyy).x - map(p - d.xyy).x,
- map(p + d.yxy).x - map(p - d.yxy).x,
- map(p + d.yyx).x - map(p - d.yyx).x));
- }
- vec3 applyFog(vec3 col, float dist) {
- return mix(col, BACKGROUND_COLOR, 1.0 - exp(-0.0015*dist*dist));
- }
- vec3 getMaterialColor(float matID) {
- vec3 col = BACKGROUND_COLOR;
- if (matID <= MTL_GROUND) col = vec3(0.3, 0.3, 0.5);
- else if (matID <= MTL_AX) col = vec3(1.0, 0.0, 0.0);
- else if (matID <= MTL_AY) col = vec3(0.0, 1.0, 0.0);
- else if (matID <= MTL_AZ) col = vec3(0.0, 0.0, 1.0);
- else if (matID <= MTL_OBJ1) col = vec3(0.8, 0.8, 1.8);
- else if (matID <= MTL_OBJ2) col = vec3(1.4, 1.3, 0.3);
- else if (matID <= MTL_GREY) col = vec3(0.8, 0.8, 0.8);
- else col = vec3(0.7, 0.7, 1.8);
- return col;
- }
- vec3 render(in vec3 ro, in vec3 rd) {
- vec2 res = rayMarch(ro, rd);
- float t = res.x;
- float mtlID = res.y;
- vec3 lig = -rd;
- vec3 pos = ro + t*rd;
- vec3 nor = calcNormal(pos);
- vec3 mtlColor = getMaterialColor(mtlID);
- float ambient = 0.05;
- float diffuse = clamp(dot(nor, lig), 0.0, 1.0);
- vec3 col = mtlColor*(ambient + GLOBAL_LIGHT_COLOR*diffuse);
- col = applyFog(col, t);
- return vec3(clamp(col, 0.0, 1.0));
- }
- vec3 getRayDir(vec3 camPos, vec3 viewDir, vec2 pixelPos) {
- vec3 camRight = normalize(cross(viewDir, vec3(0.0, 1.0, 0.0)));
- vec3 camUp = normalize(cross(camRight, viewDir));
- return normalize(pixelPos.x*(camRight*-1.) + pixelPos.y*camUp + CAM_FOV_FACTOR*viewDir);
- }
- void main() {
- vec3 camTarget = (normalize(camDir)*1.0f);
- camTarget = camTarget+camPos;
- vec2 q = gl_FragCoord.xy/resolution.xy;
- vec2 p = -1.0+2.0*q;
- p.x *= resolution.x/resolution.y;
- vec3 rayDirVec = normalize(objectViewDir);
- vec3 rayStartPos = objectPos;
- vec3 rayDir = getRayDir(camPos,normalize(camTarget - camPos), p);
- vec3 color = render(camPos, rayDir);
- if(color == BACKGROUND_COLOR){discard;}
- gl_FragColor = vec4(color, 1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement