Skip to content

Commit

Permalink
Set sky color in-place.
Browse files Browse the repository at this point in the history
  • Loading branch information
leMaik committed Nov 1, 2023
1 parent 45b2a00 commit 21c99ac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
3 changes: 1 addition & 2 deletions chunky/src/java/se/llbit/chunky/renderer/scene/Sky.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,7 @@ public void getSkyDiffuseColorInner(Ray ray) {
break;
}
case SIMULATED: {
Vector3 color = skyCache.calcIncidentLight(ray);
ray.color.set(color.x, color.y, color.z, 1);
skyCache.calcIncidentLight(ray);
break;
}
case SKYMAP_EQUIRECTANGULAR: {
Expand Down
31 changes: 17 additions & 14 deletions chunky/src/java/se/llbit/chunky/renderer/scene/SkyCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@

import java.util.concurrent.ExecutionException;
import java.util.stream.IntStream;

import org.apache.commons.math3.util.FastMath;
import se.llbit.chunky.main.Chunky;
import se.llbit.log.Log;
import se.llbit.math.ColorUtil;
import se.llbit.math.QuickMath;
import se.llbit.math.Ray;
import se.llbit.math.Vector3;
import se.llbit.math.*;

/**
* A sky cache. Precalculates sky colors and them uses cached values with bilinear interpolation.
Expand Down Expand Up @@ -128,15 +126,14 @@ public void setSimulatedSkyMode(SimulatedSky skyMode) {
* @param ray Ray to calculate the incident light for
* @return Incident light color (RGB)
*/
public Vector3 calcIncidentLight(Ray ray) {
public void calcIncidentLight(Ray ray) {
double theta = FastMath.atan2(ray.d.z, ray.d.x);
theta /= PI * 2;
theta = ((theta % 1) + 1) % 1;
double phi = (FastMath.asin(QuickMath.clamp(ray.d.y, -1, 1)) + PI / 2) / PI;

Vector3 color = getColorInterpolated(theta, phi);
ColorUtil.RGBfromHSL(color, color.x, color.y, color.z);
return color;
getColorInterpolated(theta, phi, ray.color);
ColorUtil.RGBfromHSL(ray.color, ray.color.x, ray.color.y, ray.color.z);
}

// Linear interpolation between 2 points in 1 dimension
Expand All @@ -147,21 +144,27 @@ private static double interp1D(double x, double x0, double x1, double y0, double
/**
* Calculate the bilinearly interpolated value from the cache.
*/
private Vector3 getColorInterpolated(double normX, double normY) {
private void getColorInterpolated(double normX, double normY, Vector4 out) {
double x = normX * skyResolution;
double y = normY * skyResolution;
int floorX = (int) QuickMath.clamp(x, 0, skyResolution - 1);
int floorY = (int) QuickMath.clamp(y, 0, skyResolution - 1);

double[] color = new double[3];
for (int i = 0; i < 3; i++) {
double y0 = interp1D(x, floorX, floorX + 1, skyTexture[floorX][floorY][i],
skyTexture[floorX + 1][floorY][i]);
skyTexture[floorX + 1][floorY][i]);
double y1 = interp1D(x, floorX, floorX + 1, skyTexture[floorX][floorY + 1][i],
skyTexture[floorX + 1][floorY + 1][i]);
color[i] = interp1D(y, floorY, floorY + 1, y0, y1);
skyTexture[floorX + 1][floorY + 1][i]);
double c = interp1D(y, floorY, floorY + 1, y0, y1);
if (i == 0) {
out.x = c;
} else if (i == 1) {
out.y = c;
} else if (i == 2) {
out.z = c;
}
out.w = 1;
}
return new Vector3(color[0], color[1], color[2]);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions chunky/src/java/se/llbit/math/ColorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,22 +291,22 @@ public static Vector3 RGBtoHSL(double r, double g, double b) {
return color;
}

public static void RGBfromHSL(Vector3 rgb, double hue, double saturation, double lightness) {
public static void RGBfromHSL(Vector4 rgb, double hue, double saturation, double lightness) {
double c = Math.min(1, (1 - Math.abs(2 * lightness - 1)) * saturation);
double h = hue * 6;
double x = c * (1 - Math.abs(h % 2 - 1));
if (h < 1) {
rgb.set(c, x, 0);
rgb.set(c, x, 0, 1);
} else if (h < 2) {
rgb.set(x, c, 0);
rgb.set(x, c, 0, 1);
} else if (h < 3) {
rgb.set(0, c, x);
rgb.set(0, c, x, 1);
} else if (h < 4) {
rgb.set(0, x, c);
rgb.set(0, x, c, 1);
} else if (h < 5) {
rgb.set(x, 0, c);
rgb.set(x, 0, c, 1);
} else {
rgb.set(c, 0, x);
rgb.set(c, 0, x, 1);
}
double m = Math.max(0, lightness - 0.5 * c);
rgb.x += m;
Expand Down

0 comments on commit 21c99ac

Please sign in to comment.