attribute vec4 aVertexPosition;
uniform vec2 iRes;
uniform float iSc;
varying highp vec2 vPos;
void main() {
vPos = aVertexPosition.xy*.5*iRes/iSc;
gl_Position = aVertexPosition;
}
precision highp float;
// major and minor radiuses of the torus
uniform float iR, ir;
uniform float RAYMARCH;
// signed distance to the torus
float sdTorus(in vec3 p){
float l = length(p.xy)-iR;
return length(vec2(l,p.z))-ir;
}
// Analytical torus intersection: https://www.shadertoy.com/view/4sBGDy
// Modified to reduce floating-point inaccuracy
// The MIT License
// Copyright © 2014 Inigo Quilez
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
float iTorus(in vec3 ro, in vec3 rd) {
float po = 1.0;
float Ra2 = iR*iR, ra2 = ir*ir;
float m = dot(ro,ro), n = dot(ro,rd), n2 = n*n;
float k = (m -ra2-Ra2)*.5;
float k3 = n;
float k2 = n2 + Ra2*rd.z*rd.z + k;
float k1 = k*n + Ra2*ro.z*rd.z;
float k0 = k*k + Ra2*ro.z*ro.z - Ra2*ra2;
#if 1
// modified from the original
if (abs(rd.z)< min(ir/iR,1.) && n2>m-(Ra2+ra2)) {
po = -1.0;
float tmp=k1; k1=k3; k3=tmp;
k0 = 1.0/k0; k1 = k1*k0; k2 = k2*k0; k3 = k3*k0;
}
#endif
float c2 = 2.0*k2 - 3.0*k3*k3;
float c1 = k3*(k3*k3 - k2) + k1;
float c0 = k3*(k3*(-3.0*k3*k3 + 4.0*k2) - 8.0*k1) + 4.0*k0;
c2 /= 3.0, c1 *= 2.0, c0 /= 3.0;
float Q = c2*c2 + c0, R = 3.0*c0*c2 - c2*c2*c2 - c1*c1;
float h = R*R - Q*Q*Q;
float z = 0.0;
if(h < 0.0) {
float sQ = sqrt(Q);
z = 2.0*sQ*cos(acos(R/(sQ*Q)) / 3.0);
} else {
float sQ = pow(sqrt(h) + abs(R), 1.0/3.0);
z = sign(R)*abs(sQ + Q/sQ);
}
z = c2 - z;
float d1 = z - 3.0*c2, d2 = z*z - 3.0*c0;
if (abs(d1) < 1.0e-4) {
if (d2 < 0.0) return -1.0;
d2 = sqrt(d2);
}
else {
if (d1 < 0.0) return -1.0;
d1 = sqrt(d1/2.0), d2 = c1/d1;
}
float t = 1e20;
h = d1*d1 - z + d2;
if (h > 0.0) {
h = sqrt(h);
float t1 = -d1 - h - k3; t1 = (po< 0.0)?2.0/t1:t1;
float t2 = -d1 + h - k3; t2 = (po< 0.0)?2.0/t2:t2;
t = min(t1,t2);
}
h = d1*d1 - z - d2;
if (h > 0.0) {
h = sqrt(h);
float t1 = d1 - h - k3; t1 = (po< 0.0)?2.0/t1:t1;
float t2 = d1 + h - k3; t2 = (po< 0.0)?2.0/t2:t2;
t = min(t,min(t1,t2));
}
// perform raymarching steps to reduce floating-point inaccuracy
for (int i=0;i< 2;i++) t += sdTorus(ro+rd*t);
return t;
}
vec3 nTorus(in vec3 p) {
return normalize(p*(dot(p,p) - ir*ir - iR*iR*vec3(1,1,-1)));
}
varying vec2 vPos;
uniform vec3 iUd, iVd;
void main() {
vec3 w = cross(iUd, iVd);
vec3 ro = 10.1*w + vPos.x*iUd + vPos.y*iVd;
vec3 rd = -w;
vec3 col = vec3(0.0);
float t = 0.0;
if (RAYMARCH > 0.) {
const float MAX_STEP = 256.0;
for (float i=0.; i< MAX_STEP;i+=1.0) {
float dt = sdTorus(ro+rd*t);
t += dt;
if (dt< 0.001) break;
if (t>21.||i+1.5>MAX_STEP) {
gl_FragColor = vec4(0,0,0,1);
return;
}
}
}
else t = iTorus(ro, rd);
if (t>0.) {
vec3 p = ro+rd*t;
vec3 n = nTorus(p);
col = vec3(.4)+.3*n;
col += vec3(0.5)*pow(clamp(dot(n,w),0.,1.),10.);
}
gl_FragColor = vec4(0.9*clamp(col,vec3(0),vec3(1)),1.0);
}