Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 062f909

Browse files
Maximilian StiedeShirleyNekoDev
Maximilian Stiede
authored andcommittedMay 20, 2024·
apply feedback
1 parent 346b318 commit 062f909

File tree

7 files changed

+246
-222
lines changed

7 files changed

+246
-222
lines changed
 

‎chunky/src/java/se/llbit/chunky/renderer/scene/sky/CelestialBodyType.java

-219
This file was deleted.

‎chunky/src/java/se/llbit/chunky/renderer/scene/sky/Sun.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import se.llbit.chunky.renderer.Refreshable;
2525
import se.llbit.chunky.renderer.SceneIOProvider;
2626
import se.llbit.chunky.renderer.scene.Scene;
27-
import se.llbit.chunky.resources.Texture;
27+
import se.llbit.chunky.renderer.scene.sky.celestialbodies.CelestialBodyType;
2828
import se.llbit.json.JsonObject;
2929
import se.llbit.math.QuickMath;
3030
import se.llbit.math.Ray;
@@ -146,7 +146,7 @@ public class Sun implements JsonSerializable {
146146

147147
private final Refreshable scene;
148148

149-
private CelestialBodyType type = new CelestialBodyType.Sun();
149+
private CelestialBodyType type = CelestialBodyType.DEFAULT;
150150

151151
/**
152152
* Sun radius
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package se.llbit.chunky.renderer.scene.sky.celestialbodies;
2+
3+
import se.llbit.chunky.renderer.SceneIOProvider;
4+
import se.llbit.chunky.resources.Texture;
5+
import se.llbit.json.JsonObject;
6+
import se.llbit.log.Log;
7+
import se.llbit.util.Registerable;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
import java.util.function.Supplier;
12+
13+
public abstract class CelestialBodyType implements Registerable {
14+
private static final String CONFIG_TYPE_KEY = "celestialBodyType";
15+
16+
public final static CelestialBodyType DEFAULT = new Sun();
17+
18+
public static final Map<String, Supplier<CelestialBodyType>> TYPES = new HashMap<>(3);
19+
static {
20+
TYPES.put(Sun.ID, Sun::new);
21+
TYPES.put(Moon.ID, Moon::new);
22+
TYPES.put(Custom.ID, Custom::new);
23+
}
24+
25+
public static CelestialBodyType newFromJson(JsonObject obj) {
26+
String typeStr = obj.get(CONFIG_TYPE_KEY).asString(Sun.ID);
27+
Supplier<CelestialBodyType> create = TYPES.get(typeStr);
28+
if(create == null) {
29+
Log.warnf("Unknown celestial body type \"%s\"", typeStr);
30+
return DEFAULT;
31+
}
32+
CelestialBodyType type = create.get();
33+
type.importFromJson(obj);
34+
return type;
35+
}
36+
37+
protected void importFromJson(JsonObject obj) {}
38+
39+
public void appendToConfig(JsonObject obj) {
40+
obj.add(CONFIG_TYPE_KEY, getId());
41+
}
42+
43+
/**
44+
* will be called when a scene is loaded to load associated custom textures
45+
*/
46+
public void loadCustomTextures(SceneIOProvider ioContext) {
47+
}
48+
49+
public abstract Texture getTexture();
50+
51+
@Override
52+
public String getDescription() {
53+
return getName();
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package se.llbit.chunky.renderer.scene.sky.celestialbodies;
2+
3+
import se.llbit.chunky.renderer.SceneIOProvider;
4+
import se.llbit.chunky.resources.Texture;
5+
import se.llbit.json.JsonObject;
6+
import se.llbit.log.Log;
7+
import se.llbit.resources.ImageLoader;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
12+
public class Custom extends CelestialBodyType {
13+
public static final String ID = "CUSTOM";
14+
15+
@Override
16+
public String getId() {
17+
return ID;
18+
}
19+
20+
@Override
21+
public String getName() {
22+
return "Custom";
23+
}
24+
25+
@Override
26+
public String getDescription() {
27+
return "Custom celestial body texture";
28+
}
29+
30+
private final Texture texture = new Texture();
31+
private String fileName;
32+
33+
public Custom() {
34+
texture.setTexture(Sun.texture);
35+
}
36+
37+
@Override
38+
protected void importFromJson(JsonObject obj) {
39+
fileName = obj.get("customTextureFile").asString(null);
40+
}
41+
42+
@Override
43+
public void appendToConfig(JsonObject obj) {
44+
super.appendToConfig(obj);
45+
obj.add("customTextureFile", fileName);
46+
}
47+
48+
public void loadCustomTextures(SceneIOProvider ioContext) {
49+
if (fileName != null) {
50+
try {
51+
setFile(ioContext.resolveLinkedFile(fileName));
52+
} catch (IOException ex) {
53+
Log.error("Failed to find custom skymap file: " + fileName);
54+
}
55+
}
56+
}
57+
58+
@Override
59+
public Texture getTexture() {
60+
return texture;
61+
}
62+
63+
public String getFileName() {
64+
return fileName;
65+
}
66+
67+
public void setFile(File file) {
68+
try {
69+
texture.setTexture(ImageLoader.read(file));
70+
} catch (IOException ex) {
71+
Log.error("Failed to load custom skymap: " + file, ex);
72+
}
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package se.llbit.chunky.renderer.scene.sky.celestialbodies;
2+
3+
import se.llbit.chunky.resources.BitmapImage;
4+
import se.llbit.chunky.resources.Texture;
5+
import se.llbit.json.JsonObject;
6+
import se.llbit.log.Log;
7+
8+
public class Moon extends CelestialBodyType {
9+
public static final String ID = "MOON";
10+
11+
@Override
12+
public String getId() {
13+
return ID;
14+
}
15+
16+
@Override
17+
public String getName() {
18+
return "Moon";
19+
}
20+
21+
public enum Phase {
22+
NEW_MOON(0, 1),
23+
WAXING_CRESCENT(1, 1),
24+
FIRST_QUARTER(2, 1),
25+
WAXING_GIBBOUS(3, 1),
26+
FULL_MOON(0, 0),
27+
WANING_GIBBOUS(1, 0),
28+
LAST_QUARTER(2, 0),
29+
WANING_CRESCENT(3, 0);
30+
31+
final byte textureAtlasPosX, textureAtlasPosY;
32+
33+
Phase(int x, int y) {
34+
textureAtlasPosX = (byte) x;
35+
textureAtlasPosY = (byte) y;
36+
}
37+
}
38+
39+
public static final Texture textureAtlas = new Texture();
40+
private final Texture texture = new Texture();
41+
private Phase phase;
42+
43+
public Moon() {
44+
setPhase(Phase.FULL_MOON);
45+
}
46+
47+
@Override
48+
protected void importFromJson(JsonObject obj) {
49+
setPhase(Phase.valueOf(obj.get("moonPhase").asString(Phase.FULL_MOON.toString())));
50+
}
51+
52+
@Override
53+
public void appendToConfig(JsonObject obj) {
54+
super.appendToConfig(obj);
55+
obj.add("moonPhase", phase.name());
56+
}
57+
58+
@Override
59+
public Texture getTexture() {
60+
return texture;
61+
}
62+
63+
public Phase getPhase() {
64+
return phase;
65+
}
66+
67+
public void setPhase(Phase phase) {
68+
this.phase = phase;
69+
70+
if (textureAtlas.getWidth() == textureAtlas.getHeight()) {
71+
// only has 1 phase (phases atlas not found, fallback to default moon texture)
72+
Log.info("Moon texture did not contain multiple phases");
73+
texture.setTexture(textureAtlas);
74+
return;
75+
}
76+
77+
int textureSize = textureAtlas.getHeight() / 2;
78+
BitmapImage phaseTexture = new BitmapImage(textureSize, textureSize);
79+
phaseTexture.blit(
80+
textureAtlas.getBitmap(),
81+
0, 0,
82+
phase.textureAtlasPosX * textureSize,
83+
phase.textureAtlasPosY * textureSize,
84+
(phase.textureAtlasPosX + 1) * textureSize,
85+
(phase.textureAtlasPosY + 1) * textureSize
86+
);
87+
texture.setTexture(phaseTexture.rotated270());
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package se.llbit.chunky.renderer.scene.sky.celestialbodies;
2+
3+
import se.llbit.chunky.resources.Texture;
4+
5+
public class Sun extends CelestialBodyType {
6+
public static final String ID = "SUN";
7+
8+
@Override
9+
public String getId() {
10+
return ID;
11+
}
12+
13+
@Override
14+
public String getName() {
15+
return "Sun";
16+
}
17+
18+
public static final Texture texture = new Texture();
19+
20+
@Override
21+
public Texture getTexture() {
22+
return texture;
23+
}
24+
}

‎chunky/src/java/se/llbit/chunky/resources/TexturePackLoader.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package se.llbit.chunky.resources;
1818

1919
import se.llbit.chunky.renderer.scene.PlayerModel;
20-
import se.llbit.chunky.renderer.scene.sky.CelestialBodyType.*;
20+
import se.llbit.chunky.renderer.scene.sky.celestialbodies.Moon;
21+
import se.llbit.chunky.renderer.scene.sky.celestialbodies.Sun;
2122
import se.llbit.chunky.resources.texturepack.*;
2223

2324
import java.lang.reflect.Field;

0 commit comments

Comments
 (0)
Please sign in to comment.