bug with three.js r150 ?
Opened this issue · 1 comments
x1911 commented
x1911 commented
I may have found a temporary solution.
in fs
` // getRefractedColor functions start
vec3 getRefractedColor(vec3 refractionDir, vec3 hitPoint, float refractionIndex) {
// move the hitpoint inside the mesh with epsilon
hitPoint += refractionDir * 0.0001;
// raymarch!
float stepSize = 0.02;
float stepMult = 1.5;
vec3 lastP = hitPoint;
vec3 p = hitPoint;
vec3 hitPNormal;
float currStepSize = stepSize;
float transmissionDistance = 0.0;
for(int i = 0; i < 20; i++) {
p += currStepSize * refractionDir;
// project p in uv space
vec4 projCoord = vProjViewMatrix * vec4(p, 1.0);
projCoord.xyz /= projCoord.w;
vec2 pNDC = projCoord.xy;
vec2 pUV = pNDC * 0.5 + 0.5;
// get depth at point
vec4 backBuffer = texture2D(uBackFaceBuffer, pUV);
float depth = backBuffer.w;
vec3 norm = backBuffer.xyz;
// get p depth
float pDepth = abs((vViewMatrix * vec4(p,1.0)).z) * uCameraFarInverse;
if(pDepth > depth) {
vec3 hitp = binarySearchHitPoint(lastP, p, refractionDir);
p = hitp;
// ************ get the hitpoint normal
vec4 projCoord = vProjViewMatrix * vec4(p, 1.0);
projCoord.xyz /= projCoord.w;
vec2 pNDC = projCoord.xy;
vec2 pUV = pNDC * 0.5 + 0.5;
// get depth at point
hitPNormal = texture2D(uBackFaceBuffer, pUV).xyz;
// ************ get the hitpoint normal - END
break;
}
lastP = p;
currStepSize *= stepMult;
}
transmissionDistance = length(hitPoint - p);
// ******************** recalc directions
vec3 outward_normal;
vec3 reflected = reflect(refractionDir, hitPNormal);
float ni_over_nt;
vec3 refr;
// vec3 refracted;
float reflect_prob;
float cosine;
if (dot(refractionDir, hitPNormal) > 0.0) {
outward_normal = -hitPNormal;
ni_over_nt = refractionIndex;
cosine = refractionIndex * dot(refractionDir, hitPNormal);
} else {
outward_normal = hitPNormal;
ni_over_nt = 1.0 / refractionIndex;
cosine = -dot(refractionDir, hitPNormal);
}
// if (refract2(refractionDir, outward_normal, ni_over_nt, refracted)) {
if (refract2(refractionDir, outward_normal, ni_over_nt, refr)) {
float r0 = (1.0 - refractionIndex) / (1.0 + refractionIndex);
r0 *= r0;
reflect_prob = r0 + (1.0 - r0) * pow((1.0 - cosine), 5.0);
} else {
reflect_prob = 1.0;
}
// ******************** recalc directions - END
// ******************** get colors
vec3 col;
vec3 colrefl;
vec3 colrefr;
// if(refracted.y < 0.0) {
if(refr.y < 0.0) {
// float t = p.y / abs(refracted.y);
// vec3 planeHitP = p + refracted * t;
float t = p.y / abs(refr.y);
vec3 planeHitP = p + refr * t;
if(abs(planeHitP.x) < planeSize && abs(planeHitP.z) < planeSize) {
colrefr = planeColor;
} else {
// colrefr = getSkyboxColor(refracted);
colrefr = getSkyboxColor(refr);
}
} else {
// colrefr = getSkyboxColor(refracted);
colrefr = getSkyboxColor(refr);
}
if(reflected.y < 0.0) {
float t = p.y / abs(reflected.y);
vec3 planeHitP = p + reflected * t;
if(abs(planeHitP.x) < planeSize && abs(planeHitP.z) < planeSize) {
colrefl = planeColor;
} else {
colrefl = getSkyboxColor(reflected);
}
} else {
colrefl = getSkyboxColor(reflected);
}
col = colrefl * (reflect_prob * uReflectionFactor) + colrefr * (1.0 - reflect_prob);
// ******************** get colors
vec3 transm = vec3(1.0);
// const int steps = 8;
const int steps = 15;
float step = transmissionDistance / float(steps);
float fc = uExtintionFactor * 0.07;
// raymarching transmission color
// float noiseStrength = 0.8;
float noiseSpeed = 0.5;
float noiseTimeSpeed = 0.5;
for(int i = 0; i < steps; i++) {
vec3 np = hitPoint + refractionDir * float(i) * step;
vec3 nnp = np;
vec3 w = normalize(np - vec3(0.75, 1.5, 0.0));
vec3 u = vec3(0.0,0.0,1.0);
// vec3 timeOffset = uTime * normalize(np - vec3(0.75, 1.5, 0.0));
vec3 timeOffset = cos(uTime) * w + sin(uTime) * u;
float colorNoiseX = noise(np * noiseSpeed + timeOffset * noiseTimeSpeed);
float colorNoiseY = noise(np * noiseSpeed + timeOffset * noiseTimeSpeed + vec3(15.3278, 125.19879, 0.0));
float colorNoiseZ = noise(np * noiseSpeed + timeOffset * noiseTimeSpeed + vec3(2.6008, 78.19879, 543.12993));
float targ = length(nnp * 0.8 * uExtinctionFX1.w - vec3(0.75, 1.5, 0.0));
float targAperture = 0.25;
// wave raymarch
if(uExtinctionFX1.z > 0.5) {
nnp = np + sin(np.x * 2.5 + uTime * 1.5) * 0.3;
targ = nnp.y - 0.85 * uExtinctionFX1.w;
} else {
nnp = np + vec3(colorNoiseX, colorNoiseY, colorNoiseZ) * 1.05;
vec3 diff = nnp - vec3(3.3, 4.5, 0.0);
float angle = (atan(diff.x, diff.y) + PI) / (PI * 2.0);
targ = length(diff) + sin(angle * 32.0 * PI + uTime * 1.5) * 0.4;
targ *= 0.475;
targAperture = 0.5 + colorNoiseX * 0.75;
}
// what's the color at np?
vec3 col1 = uExtintionColor1;
vec3 col2 = uExtintionColor2;
if(uExtinctionFX1.x > 0.5) {
col1 = vec3(colorNoiseX, colorNoiseY, colorNoiseZ) * 0.85;
}
if(uExtinctionFX1.y > 0.5) {
col2 = vec3(colorNoiseX, colorNoiseY, colorNoiseZ) * 0.85;
}
if(targ < 1.0) {
transm *= exp(-step * col2 * fc);
} else if (targ > 1.0 && targ < 1.0 + targAperture ) {
float t = (targ - 1.0) / targAperture;
transm *= exp(-step * (col1 * t + col2 * (1.0 - t)) * fc );
} else if ( targ < (1.0 + targAperture) * 1.85 ) {
transm *= exp( -step * col1 * fc );
} else {
// transm = (col1) * targAperture;
// transm *= exp(-step * col1 * uExtintionFactor);
}
}
// return col * uExtintionColor2 * transm;
col *= transm;
return col;
}
`