Skip to content

Commit

Permalink
Bring more of the duplicated code into translucentRayColor
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinTimeCuber committed Dec 10, 2022
1 parent 893862a commit b06cb02
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions chunky/src/java/se/llbit/chunky/renderer/scene/PathTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,8 @@ public static boolean pathTrace(Scene scene, Ray ray, WorkerState state, int add
}

if (pathTrace(scene, refracted, state, 1, false)) {
// Calculate the color of the refracted ray
translucentRayColor(ray, pDiffuse);
// Set ray emittance and color based on its own color and the next ray's properties
ray.emittance.set(ray.color.x, ray.color.y, ray.color.z);
ray.color.x *= refracted.color.x;
ray.color.y *= refracted.color.y;
ray.color.z *= refracted.color.z;
ray.emittance.x *= refracted.emittance.x;
ray.emittance.y *= refracted.emittance.y;
ray.emittance.z *= refracted.emittance.z;
// Calculate the color and emittance of the refracted ray
translucentRayColor(ray, refracted, pDiffuse);
hit = true;
}
}
Expand All @@ -366,16 +358,8 @@ public static boolean pathTrace(Scene scene, Ray ray, WorkerState state, int add
transmitted.o.scaleAdd(Ray.OFFSET, transmitted.d);

if (pathTrace(scene, transmitted, state, 1, false)) {
// Calculate the color of the transmitted ray
translucentRayColor(ray, pDiffuse);
// Set ray emittance and color based on its own color and the next ray's properties
ray.emittance.set(ray.color.x, ray.color.y, ray.color.z);
ray.color.x *= transmitted.color.x;
ray.color.y *= transmitted.color.y;
ray.color.z *= transmitted.color.z;
ray.emittance.x *= transmitted.emittance.x;
ray.emittance.y *= transmitted.emittance.y;
ray.emittance.z *= transmitted.emittance.z;
// Calculate the color and emittance of the transmitted ray
translucentRayColor(ray, transmitted, pDiffuse);
hit = true;
}
}
Expand Down Expand Up @@ -449,11 +433,11 @@ public static boolean pathTrace(Scene scene, Ray ray, WorkerState state, int add
return hit;
}

private static void translucentRayColor(Ray ray, double pDiffuse) {
private static void translucentRayColor(Ray ray, Ray next, double opacity) {
// Color-based transmission value
double colorTrans = (ray.color.x + ray.color.y + ray.color.z)/3;
// Total amount of light we want to transmit (overall transparency of texture)
double shouldTrans = 1 - pDiffuse;
double shouldTrans = 1 - opacity;
// Amount of each color to transmit - default to overall transparency if RGB values add to 0 (e.g. regular glass)
double rTrans = shouldTrans, gTrans = shouldTrans, bTrans = shouldTrans;
if(colorTrans > 0) {
Expand All @@ -466,6 +450,13 @@ private static void translucentRayColor(Ray ray, double pDiffuse) {
ray.color.x = (1 - rTrans) * ray.color.x + rTrans;
ray.color.y = (1 - gTrans) * ray.color.y + gTrans;
ray.color.z = (1 - bTrans) * ray.color.z + bTrans;
ray.emittance.set(ray.color.x, ray.color.y, ray.color.z);
ray.color.x *= next.color.x;
ray.color.y *= next.color.y;
ray.color.z *= next.color.z;
ray.emittance.x *= next.emittance.x;
ray.emittance.y *= next.emittance.y;
ray.emittance.z *= next.emittance.z;
}

private static void sampleEmitterFace(Scene scene, Ray ray, Grid.EmitterPosition pos, int face, Vector4 result, double scaler, Random random) {
Expand Down

0 comments on commit b06cb02

Please sign in to comment.