Skip to content

Commit

Permalink
feat: sun shine
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceh121 committed Dec 3, 2023
1 parent 0877b45 commit a365829
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
9 changes: 7 additions & 2 deletions android/assets/shaders/sky.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ precision highp float;
uniform vec4 skyTop;
uniform vec4 skyMiddle;
uniform vec4 skyBottom;
uniform vec4 sunShine;

uniform vec3 sunDir;

Expand All @@ -16,19 +17,23 @@ void main() {
vec3 normVert = normalize(vertPos.xyz);
// [-1; 1]
float angle = dot(normVert, vec3(0, 1, 0));
float sunSkyAngle;

if (angle > 0) { // top half
angle = 1 - angle;
float sunSkyAngle = dot(normVert, sunDir);
sunSkyAngle = dot(normVert, sunDir);
sunSkyAngle = 1 - sunSkyAngle;

gl_FragColor = mix(skyTop, skyMiddle, logFrac(angle));
} else { // bottom half
angle += 1;
// need to invert one of those dirs
float sunSkyAngle = dot(-1 * normVert, sunDir);
sunSkyAngle = dot(-1 * normVert, sunDir);
sunSkyAngle += 1;

gl_FragColor = mix(skyMiddle, skyBottom, logFrac(angle));
}

gl_FragColor = mix(gl_FragColor, sunShine,
sunShine.a * (1 - logFrac(sunSkyAngle)));
}
20 changes: 20 additions & 0 deletions android/assets/skies.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,26 @@
}
},
"skyBottomColor": {
},
"sunShineColor": {
"0.375": {
"r": 0,
"g": 0,
"b": 0,
"a": 0
},
"0.5": {
"r": 0.949,
"g": 0.851,
"b": 0.263,
"a": 1
},
"0.625": {
"r": 0,
"g": 0,
"b": 0,
"a": 0
}
}
}
}
13 changes: 10 additions & 3 deletions core/src/me/vinceh121/wanderer/glx/SkyProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SkyProperties {
private final NavigableMap<Float, Color> skyTopColor = new TreeMap<>();
private final NavigableMap<Float, Color> skyMiddleColor = new TreeMap<>();
private final NavigableMap<Float, Color> skyBottomColor = new TreeMap<>();
private final NavigableMap<Float, Color> sunShineColor = new TreeMap<>();

public NavigableMap<Float, Color> getSunColor() {
return sunColor;
Expand All @@ -38,9 +39,14 @@ public NavigableMap<Float, Color> getSkyBottomColor() {
return skyBottomColor;
}

public NavigableMap<Float, Color> getSunShineColor() {
return sunShineColor;
}

@Override
public int hashCode() {
return Objects.hash(ambLightColor, skyBottomColor, skyMiddleColor, skyTopColor, sunColor, sunLightColor);
return Objects
.hash(ambLightColor, skyBottomColor, skyMiddleColor, skyTopColor, sunColor, sunLightColor, sunShineColor);
}

@Override
Expand All @@ -56,13 +62,14 @@ public boolean equals(Object obj) {
&& Objects.equals(skyBottomColor, other.skyBottomColor)
&& Objects.equals(skyMiddleColor, other.skyMiddleColor)
&& Objects.equals(skyTopColor, other.skyTopColor) && Objects.equals(sunColor, other.sunColor)
&& Objects.equals(sunLightColor, other.sunLightColor);
&& Objects.equals(sunLightColor, other.sunLightColor)
&& Objects.equals(sunShineColor, other.sunShineColor);
}

@Override
public String toString() {
return "SkyProperties [sunColor=" + sunColor + ", sunLightColor=" + sunLightColor + ", ambLightColor="
+ ambLightColor + ", skyTopColor=" + skyTopColor + ", skyMiddleColor=" + skyMiddleColor
+ ", skyBottomColor=" + skyBottomColor + "]";
+ ", skyBottomColor=" + skyBottomColor + ", sunShineColor=" + sunShineColor + "]";
}
}
15 changes: 13 additions & 2 deletions core/src/me/vinceh121/wanderer/glx/SkyShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import com.badlogic.gdx.math.Vector3;

public class SkyShader extends WandererShader {
public final int u_sunDir, u_skyTop, u_skyMiddle, u_skyBottom;
public final int u_sunDir, u_skyTop, u_skyMiddle, u_skyBottom, u_sunShine;
private final Vector3 sunDir = new Vector3(0, 1, 0);
private final Color skyTop = new Color();
private final Color skyMiddle = new Color();
private final Color skyBottom = new Color();
private final Color sunShine = new Color();

public SkyShader(final Renderable renderable, final Config config) {
super(renderable, config);
Expand All @@ -20,6 +21,7 @@ public SkyShader(final Renderable renderable, final Config config) {
this.u_skyTop = this.register("skyTop");
this.u_skyMiddle = this.register("skyMiddle");
this.u_skyBottom = this.register("skyBottom");
this.u_sunShine = this.register("sunShine");
}

@Override
Expand All @@ -28,6 +30,7 @@ public void render(final Renderable renderable, final Attributes combinedAttribu
this.set(this.u_skyTop, this.skyTop);
this.set(this.u_skyMiddle, this.skyMiddle);
this.set(this.u_skyBottom, this.skyBottom);
this.set(this.u_sunShine, this.sunShine);

super.render(renderable, combinedAttributes);
}
Expand Down Expand Up @@ -70,7 +73,15 @@ public Color getSkyBottom() {
return skyBottom;
}

public void setSkyBottom(Color skyBottm) {
public void setSkyBottom(Color skyBottom) {
this.skyBottom.set(skyBottom);
}

public Color getSunShine() {
return sunShine;
}

public void setSunShine(Color sunShine) {
this.sunShine.set(sunShine);
}
}
10 changes: 8 additions & 2 deletions core/src/me/vinceh121/wanderer/glx/SkyboxRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public void update(final float time) {
this.shader.setSkyTop(this.interpolatedColor(time, this.skyProperties.getSkyTopColor()));
this.shader.setSkyMiddle(this.interpolatedColor(time, this.skyProperties.getSkyMiddleColor()));
this.shader.setSkyBottom(this.interpolatedColor(time, this.skyProperties.getSkyBottomColor()));
this.shader.setSunShine(this.interpolatedColor(time, this.skyProperties.getSunShineColor()));

System.out.println(this.shader.getSunShine());
}

this.sunDir.scl(-1);
Expand All @@ -121,8 +124,11 @@ public void update(final float time) {
}

private Color interpolatedColor(final float time, final NavigableMap<Float, Color> colors) {
// only way left is null is that time is negative, which shouldn't happen
final Entry<Float, Color> left = colors.floorEntry(time);
Entry<Float, Color> left = colors.floorEntry(time);

if (left == null) { // wrap around
left = colors.lastEntry();
}

Entry<Float, Color> right = colors.ceilingEntry(time);

Expand Down

0 comments on commit a365829

Please sign in to comment.