1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| #define r iResolution #define PI 3.14 #define lightDir vec3(0.41,-0.72,.2) #define light2Dir vec3(-0.71,-0.12,.4) #define rot(d) mat2(cos(d),sin(d),-sin(d),cos(d)) #define it 0.02 #define maxIters 200
float sdCylinder( vec3 p, vec2 h ) { vec2 d = abs(vec2(length(p.xz),p.y)) - h; return min(max(d.x,d.y),0.0) + length(max(d,0.0)); } 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 clamp((0.2+h.x),0.,1.)*(d.x+d.y)+ length(max(d,0.)); }
float func(vec3 p){ vec2 mouseInput = iMouse.xy/r.xy-vec2(.5)+vec2(-0.1,0.1); p.xy *= rot( mouseInput.x*2.*PI+iTime); p.yz *= rot(mouseInput.y*PI); vec3 s = vec3(1.2,.32,1.2); if(max(max(abs(p.x),abs(p.y)),abs(p.z))<1.75) p = p - s*round(p/s); float frame = sdCylinder(p,vec2(1.1*0.35,.027)); float inframe = sdCylinder(p,vec2(1.08*0.35,1.5)); float body = sdCylinder(p,vec2(1.1*0.35, .02)); p.yz *=rot(3.14/180.*90.); float text = sdHexPrism(p,vec2(.5*0.35,.04127)); //return inframe; //return frame; //return max(frame,-inframe); return min(min(max(frame,-inframe),body),text); //return sdBoxFrame(p,vec3(1.),.1); //return max(0.,iMouse.x/iMouse.y-sdHexPrism(vec3(p.xy,0.),vec2(1.))); return 1.; } #if 0 vec3 normal( vec3 pos ) { vec3 n = vec3(0.0); for( int i=0; i<4; i++ ) { vec3 e = 0.5773*(2.0*vec3((((i+3)>>1)&1),((i>>1)&1),(i&1))-1.0); n += e*func(pos+0.0005*e); //if( n.x+n.y+n.z>100.0 ) break; } return normalize(n); } #else
vec3 normal(vec3 p) { float delta = 0.01; float fx = (func(p + vec3(delta, 0.0, 0.0)) - func(p - vec3(delta, 0.0, 0.0))) / (2.0 * delta); float fy = (func(p + vec3(0.0, delta, 0.0)) - func(p - vec3(0.0, delta, 0.0))) / (2.0 * delta); float fz = (func(p + vec3(0.0, 0.0, delta)) - func(p - vec3(0.0, 0.0, delta))) / (2.0 * delta); vec3 gradient = vec3(-fx, -fy, -fz); return normalize(gradient); } #endif
vec3 color(in vec3 pos,in vec3 viewDir){ vec3 res = vec3(0.); vec3 ambientColor = vec3(0.7,0.6,0.65); float ambient = .0; vec3 diffuseColor = pow(vec3(.4,.3,.2)*3.2,vec3(1.)); float diffuse = .1; //check intersect with z=0 vec3 rayPos;
rayPos = pos-viewDir*(pos.x/viewDir.x);
vec3 lpos = pos; for(int i=0;i++<maxIters;){ float Vh = func(lpos); //float Vh = func(lpos.xy)-lpos.z; //if(abs(Vh)<it/length(normalV)){ if(Vh<it){ vec3 normalV = normal(lpos);
diffuse = pow(clamp(dot(-normalize(viewDir+lightDir),normal(lpos)),0.,1.),11.); res+=(diffuseColor*diffuse); diffuse = pow(clamp(dot(-normalize(viewDir+light2Dir),normal(lpos)),0.,1.),11.); res+=(diffuseColor*diffuse); // diffuse = clamp(dot(normal(lpos),lightDir),0.,1.); //res+=(diffuseColor*diffuse); //diffuse = clamp(dot(normal(lpos),light2Dir),0.,1.); //res+=(diffuseColor*diffuse); break; } #if 0 lpos += it*abs(Vh)*viewDir*(-4.-5.*pow(dot(normalV,-viewDir),0.01)); #else lpos += -it*(0.14+2.5*abs(Vh))*viewDir; #endif } if (res.x<0.1) res+=.1; return (res); }
mat3 camera(vec3 cameraPos, vec3 lookAtPoint) { vec3 cd = normalize(lookAtPoint - cameraPos); // camera direction vec3 cr = normalize(cross(vec3(0, 0, 1), cd)); // camera right vec3 cu = normalize(cross(cd, cr)); // camera up return mat3(-cr, cu, -cd); }
void mainImage( out vec4 o, in vec2 c ) { // Normalized pixel coordinates (from 0 to 1) vec2 uv = c/r.y; uv-=vec2(.5*r.x/r.y,.5); float camDis = 4.; vec2 mouseInput = iMouse.xy/r.xy-vec2(.5)+vec2(-0.1,0.1); //mouseInput.y = min(max(mouseInput.y,-0.4),-0.2); vec3 camPos = vec3(0.6, 1.,0.1); camPos *= camDis; mat3 mat = camera(camPos, vec3(0.)); vec3 viewDir = normalize(mat * vec3(-uv.xy, 1.0)); o.xyz = color(camPos,normalize(viewDir));
}
|