Instruction parallel ray sphere intersection

This commit is contained in:
JozsefA 2021-04-20 14:05:50 -07:00
parent e50590f6b7
commit f75422f895
3 changed files with 71 additions and 41 deletions

View file

@ -11,6 +11,19 @@ public class ColorMatrices {
public static final float lumaG = 0.6094f;
public static final float lumaB = 0.0820f;
public static Matrix4f invert() {
Matrix4f invert = new Matrix4f();
invert.a00 = -1.0F;
invert.a11 = -1.0F;
invert.a22 = -1.0F;
invert.a33 = -1.0F;
invert.a30 = 1;
invert.a31 = 1;
invert.a32 = 1;
return invert;
}
public static Matrix4f grayscale() {
Matrix4f mat = new Matrix4f();
@ -78,14 +91,4 @@ public class ColorMatrices {
return mat;
}
public static Matrix4f hueShiftHSV(float rot) {
Matrix4f mat = new Matrix4f();
mat.loadIdentity();
//mat.a03 = 0.5f;
return mat;
}
}

View file

@ -111,14 +111,16 @@ public class EffectsHandler {
program.setCameraPos(cameraPos.inverse());
// int n = 20;
// int n = 64;
// double rad = 15;
// for (int i = 0; i < n; i++) {
// double angle = ((double) i) / n * Math.PI * 2;
// program.addSphere(new SphereFilterProgram.FilterSphere()
// .setCenter(pos1.subtract(cameraPos).add(Math.sin(angle) * rad, 0, Math.cos(angle) * rad))
// .setRadius(10)
// .setFeather(0f)
// .setCenter(new Vector3d(852, 77, -204).subtract(cameraPos).add(Math.sin(angle) * rad, 0, Math.cos(angle) * rad))
// .setRadius(15)
// .setFeather(3f)
// .setFade(1f)
// .setDensity(0.5f)
// .setFilter(ColorMatrices.hueShiftRGB((float) i / n * 360 + i / 2f)));
// }
@ -130,31 +132,37 @@ public class EffectsHandler {
.setDensity(1.3f)
.setFilter(ColorMatrices.grayscale()));
Matrix4f test = ColorMatrices.sepia(1f);
test.multiply(ColorMatrices.invert());
Matrix4f darken = new Matrix4f();
darken.loadIdentity();
darken.multiply(0.7f);
darken.a03 = 0.7f;
darken.a13 = 0.7f;
darken.a23 = 0.7f;
test.multiply(darken);
program.addSphere(new SphereFilterProgram.FilterSphere()
.setCenter(new Vector3d(858.5, 88, -259.5).subtract(cameraPos))
.setRadius(10f)
.setFeather(3f)
.setFade(1.8f)
.setDensity(1.3f)
.setDensity(0.5f)
.setStrength(1f)
.setFilter(ColorMatrices.sepia(1f)));
.setFilter(test));
Matrix4f test = ColorMatrices.grayscale();
Matrix4f colorize = new Matrix4f();
colorize.a00 = 1f;
colorize.a11 = 0.5f;
colorize.a22 = 0.5f;
colorize.a33 = 1f;
test = colorize;
program.addSphere(new SphereFilterProgram.FilterSphere()
.setCenter(new Vector3d(2310, 60, -954).subtract(cameraPos))
.setRadius(8f)
.setFeather(3f)
.setFade(0.1f)
.setDensity(0.5f)
.setFade(0.8f)
.setDensity(1.3f)
.setStrength(1f)
.setFilter(test));
.setFilter(ColorMatrices.grayscale()));
program.uploadFilters();

View file

@ -73,23 +73,34 @@ float surfaceFilterStrength(vec3 worldPos, vec4 sphere, float feather) {
return 1 - smoothstep(sphere.w, sphere.w + feather, distance);
}
vec2 raySphere(vec3 worldDir, vec3 position, float radius) {
float rayLengthSqr = dot(worldDir, worldDir);
float sphereDistSqr = dot(position, position);
const vec3 M = vec3(2., 2., 4.);
vec3 f = M * vec3(dot(-position, worldDir), vec2(rayLengthSqr));
vec2 s = vec2(f.x, radius);
vec2 s2 = s * s;
float c = sphereDistSqr - s2.y;
float dc = f.z * c;
float discriminant = s2.x - dc;
float hitDepth = (-f.x - sqrt(discriminant)) / f.y;
return vec2(discriminant, hitDepth);
}
float bubbleFilterStrength(vec3 worldDir, float depth, vec4 sphere, float feather, float density) {
vec3 position = sphere.xyz;
float rayLengthSqr = dot(worldDir, worldDir);
float b = 2.0 * dot(-position, worldDir);
float sphereDistSqr = dot(position, position);
float b2 = b*b;
float d = 4. * rayLengthSqr;
float e = 1. / (2.0*rayLengthSqr);
float radius = sphere.w + feather;
float c = sphereDistSqr - radius*radius;
float discriminant = b2 - d * c;
float hitDepth = (-b - sqrt(discriminant)) * e;
vec2 hit = raySphere(worldDir, position, sphere.w + feather);
float hitDepth = hit.y;
float strength = 0.;
if (discriminant > 0 && hitDepth > 0 && hitDepth < depth) {
//float boo = step(0., discriminant) * step(0., hitDepth) * step(0., depth - hitDepth);
if (hit.x > 0 && hitDepth > 0 && hitDepth < depth) {
vec3 hitPos = worldDir * hitDepth;
vec3 normal = normalize(hitPos - position);
@ -99,7 +110,7 @@ float bubbleFilterStrength(vec3 worldDir, float depth, vec4 sphere, float feathe
strength += mix(0., normalDot * normalDot * density, clamp(depth - hitDepth, 0., feather + 1.));
}
return clamp(strength, 0., 1.);
return clamp(strength, 0., 1.);// * boo;
}
float filterStrength(vec3 worldDir, float depth, vec4 sphere, vec4 data) {
@ -120,6 +131,7 @@ float filterStrength(vec3 worldDir, float depth, vec4 sphere, vec4 data) {
vec3 applyFilters(vec3 worldDir, float depth, vec3 diffuse) {
vec3 worldPos = worldDir * depth;
vec3 hsv = rgb2hsv(diffuse);
vec3 accum = vec3(diffuse);
for (int i = 0; i < uCount; i++) {
@ -127,9 +139,16 @@ vec3 applyFilters(vec3 worldDir, float depth, vec3 diffuse) {
float strength = filterStrength(worldDir, depth, s.sphere, s.data);
vec3 filtered = filterColor(s.colorOp, diffuse);
if (strength > 0) {
const float fcon = 0.;
accum = mix(accum, filtered, clamp(strength * s.data.w, 0., 1.));
vec3 formatted = mix(diffuse, hsv, fcon);
vec3 filtered = filterColor(s.colorOp, formatted);
filtered = mix(filtered, hsv2rgbWrapped(filtered), fcon);
float mixing = clamp(strength * s.data.w, 0., 1.);
accum = mix(accum, filtered, mixing);
}
}
return accum;