Skip to content

Commit de45cd8

Browse files
committed
Set sky color in-place.
1 parent 607a3cc commit de45cd8

3 files changed

Lines changed: 25 additions & 23 deletions

File tree

chunky/src/java/se/llbit/chunky/renderer/scene/Sky.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ public void getSkyDiffuseColorInner(Ray ray) {
287287
break;
288288
}
289289
case SIMULATED: {
290-
Vector3 color = skyCache.calcIncidentLight(ray);
291-
ray.color.set(color.x, color.y, color.z, 1);
290+
skyCache.calcIncidentLight(ray);
292291
break;
293292
}
294293
case SKYMAP_EQUIRECTANGULAR: {

chunky/src/java/se/llbit/chunky/renderer/scene/SkyCache.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020

2121
import java.util.concurrent.ExecutionException;
2222
import java.util.stream.IntStream;
23+
2324
import org.apache.commons.math3.util.FastMath;
2425
import se.llbit.chunky.main.Chunky;
2526
import se.llbit.log.Log;
26-
import se.llbit.math.ColorUtil;
27-
import se.llbit.math.QuickMath;
28-
import se.llbit.math.Ray;
29-
import se.llbit.math.Vector3;
27+
import se.llbit.math.*;
3028

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

137-
Vector3 color = getColorInterpolated(theta, phi);
138-
ColorUtil.RGBfromHSL(color, color.x, color.y, color.z);
139-
return color;
135+
getColorInterpolated(theta, phi, ray.color);
136+
ColorUtil.RGBfromHSL(ray.color, ray.color.x, ray.color.y, ray.color.z);
140137
}
141138

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

156-
double[] color = new double[3];
157153
for (int i = 0; i < 3; i++) {
158154
double y0 = interp1D(x, floorX, floorX + 1, skyTexture[floorX][floorY][i],
159-
skyTexture[floorX + 1][floorY][i]);
155+
skyTexture[floorX + 1][floorY][i]);
160156
double y1 = interp1D(x, floorX, floorX + 1, skyTexture[floorX][floorY + 1][i],
161-
skyTexture[floorX + 1][floorY + 1][i]);
162-
color[i] = interp1D(y, floorY, floorY + 1, y0, y1);
157+
skyTexture[floorX + 1][floorY + 1][i]);
158+
double c = interp1D(y, floorY, floorY + 1, y0, y1);
159+
if (i == 0) {
160+
out.x = c;
161+
} else if (i == 1) {
162+
out.y = c;
163+
} else if (i == 2) {
164+
out.z = c;
165+
}
166+
out.w = 1;
163167
}
164-
return new Vector3(color[0], color[1], color[2]);
165168
}
166169

167170
/**

chunky/src/java/se/llbit/math/ColorUtil.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,22 +291,22 @@ public static Vector3 RGBtoHSL(double r, double g, double b) {
291291
return color;
292292
}
293293

294-
public static void RGBfromHSL(Vector3 rgb, double hue, double saturation, double lightness) {
294+
public static void RGBfromHSL(Vector4 rgb, double hue, double saturation, double lightness) {
295295
double c = Math.min(1, (1 - Math.abs(2 * lightness - 1)) * saturation);
296296
double h = hue * 6;
297297
double x = c * (1 - Math.abs(h % 2 - 1));
298298
if (h < 1) {
299-
rgb.set(c, x, 0);
299+
rgb.set(c, x, 0, 1);
300300
} else if (h < 2) {
301-
rgb.set(x, c, 0);
301+
rgb.set(x, c, 0, 1);
302302
} else if (h < 3) {
303-
rgb.set(0, c, x);
303+
rgb.set(0, c, x, 1);
304304
} else if (h < 4) {
305-
rgb.set(0, x, c);
305+
rgb.set(0, x, c, 1);
306306
} else if (h < 5) {
307-
rgb.set(x, 0, c);
307+
rgb.set(x, 0, c, 1);
308308
} else {
309-
rgb.set(c, 0, x);
309+
rgb.set(c, 0, x, 1);
310310
}
311311
double m = Math.max(0, lightness - 0.5 * c);
312312
rgb.x += m;

0 commit comments

Comments
 (0)