Skip to content

Commit 990f79c

Browse files
committed
Fix incorrect dark oak leaf color blending issue
The same render model was used for both LEAVES and LEAVES2, causing dark oak leaves to be treated as spruce leaves and acacia leaves to be treated as birch leaves. fixes #467 (github)
1 parent aadf251 commit 990f79c

File tree

3 files changed

+52
-24
lines changed

3 files changed

+52
-24
lines changed

ChangeLog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
LATEST
22

3+
* Fixed incorrect dark oak leaf color blending.
34
* Added new solid color sky mode.
45
* Fixed wrong texture for pink and gray glazed terracotta.
56

chunky/src/java/se/llbit/chunky/block/Block.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import se.llbit.chunky.world.Material;
9090
import se.llbit.json.JsonString;
9191
import se.llbit.json.JsonValue;
92+
import se.llbit.math.ColorUtil;
9293
import se.llbit.math.Ray;
9394

9495
import java.util.Arrays;
@@ -113,7 +114,7 @@ public class Block extends Material {
113114
private static final boolean UNKNOWN_INVISIBLE = !PersistentSettings.drawUnknownBlocks();
114115

115116
public static final int AIR_ID = 0x00;
116-
public static final Block AIR = new Block(AIR_ID, "Air", Texture.air);
117+
public static final Block AIR = new Block(AIR_ID, "block:air", Texture.air);
117118
public static final int STONE_ID = 0x01;
118119
public static final Block STONE = new Block(STONE_ID, "block:stone", Texture.stone) {
119120
final Texture[] texture = {
@@ -277,12 +278,31 @@ public class Block extends Material {
277278
};
278279
public static final int LEAVES_ID = 0x12;
279280
public static final Block LEAVES = new Block(LEAVES_ID, "block:leaves", Texture.oakLeaves) {
280-
final Texture[] texture =
281-
{Texture.oakLeaves, Texture.spruceLeaves, Texture.birchLeaves, Texture.jungleTreeLeaves};
281+
private final float[] SPRUCE_COLOR = new float[4];
282+
private final float[] BIRCH_COLOR = new float[4];
282283

283-
@Override public boolean intersect(Ray ray, Scene scene) {
284-
return LeafModel.intersect(ray, scene, getTexture(ray.getBlockData()));
284+
{
285+
// Spruce and birch colors are hard-coded.
286+
ColorUtil.getRGBAComponents(0x2b472b, SPRUCE_COLOR);
287+
ColorUtil.getRGBAComponents(0x3a4e25, BIRCH_COLOR);
288+
}
289+
290+
final Texture[] texture = {
291+
Texture.oakLeaves, Texture.spruceLeaves, Texture.birchLeaves, Texture.jungleTreeLeaves
292+
};
285293

294+
@Override public boolean intersect(Ray ray, Scene scene) {
295+
int data = ray.getBlockData();
296+
switch (data) {
297+
case 1:
298+
// Spruce leaf color is not based on biome.
299+
return LeafModel.intersect(ray, getTexture(data), SPRUCE_COLOR);
300+
case 2:
301+
// Birch leaf color is not based on biome.
302+
return LeafModel.intersect(ray, getTexture(data), BIRCH_COLOR);
303+
default:
304+
return LeafModel.intersect(ray, scene, getTexture(data));
305+
}
286306
}
287307

288308
final String[] woodType = {"oak", "spruce", "birch", "jungle",};

chunky/src/java/se/llbit/chunky/model/LeafModel.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,17 @@
2525
public class LeafModel {
2626
private static final AABB block = new AABB(0, 1, 0, 1, 0, 1);
2727

28-
private static final float[] SPRUCE_COLOR = new float[4];
29-
private static final float[] BIRCH_COLOR = new float[4];
30-
31-
static {
32-
// Spruce and birch colors are hard-coded.
33-
ColorUtil.getRGBAComponents(0x2b472b, SPRUCE_COLOR);
34-
ColorUtil.getRGBAComponents(0x3a4e25, BIRCH_COLOR);
35-
}
36-
28+
/**
29+
* Get leaf color at ray intersection, based on biome.
30+
*/
3731
public static boolean intersect(Ray ray, Scene scene, Texture texture) {
3832
ray.t = Double.POSITIVE_INFINITY;
3933
if (block.intersect(ray)) {
4034
float[] color = texture.getColor(ray.u, ray.v);
4135
if (color[3] > Ray.EPSILON) {
4236
ray.color.set(color);
4337
float[] biomeColor;
44-
int woodType = ray.getBlockData();
45-
if (woodType == 1) {
46-
// Use constant spruce color:
47-
biomeColor = SPRUCE_COLOR;
48-
} else if (woodType == 2) {
49-
// Use constant birch color:
50-
biomeColor = BIRCH_COLOR;
51-
} else {
52-
biomeColor = ray.getBiomeFoliageColor(scene);
53-
}
38+
biomeColor = ray.getBiomeFoliageColor(scene);
5439
ray.color.x *= biomeColor[0];
5540
ray.color.y *= biomeColor[1];
5641
ray.color.z *= biomeColor[2];
@@ -61,4 +46,26 @@ public static boolean intersect(Ray ray, Scene scene, Texture texture) {
6146
}
6247
return false;
6348
}
49+
50+
/**
51+
* Get leaf color at ray intersection, using base leaf color.
52+
*
53+
* @param leafColor base leaf color to blend the leaf texture with.
54+
*/
55+
public static boolean intersect(Ray ray, Texture texture, float[] leafColor) {
56+
ray.t = Double.POSITIVE_INFINITY;
57+
if (block.intersect(ray)) {
58+
float[] color = texture.getColor(ray.u, ray.v);
59+
if (color[3] > Ray.EPSILON) {
60+
ray.color.set(color);
61+
ray.color.x *= leafColor[0];
62+
ray.color.y *= leafColor[1];
63+
ray.color.z *= leafColor[2];
64+
ray.distance += ray.tNext;
65+
ray.o.scaleAdd(ray.tNext, ray.d);
66+
return true;
67+
}
68+
}
69+
return false;
70+
}
6471
}

0 commit comments

Comments
 (0)