Skip to content

Commit

Permalink
feat: rework sky shader keyframing
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceh121 committed Dec 2, 2023
1 parent 6dac92e commit a09bf4e
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 248 deletions.
132 changes: 9 additions & 123 deletions android/assets/shaders/sky.frag
Original file line number Diff line number Diff line change
@@ -1,90 +1,18 @@
precision highp float;

#define MORNING 0.0
#define NOON 0.25
#define EVENING_START 0.375
#define EVENING_MID 0.4375
#define EVENING_END 0.5
#define MIDNIGHT_START 0.625
#define MIDNIGHT 0.75
uniform vec4 skyTop;
uniform vec4 skyMiddle;
uniform vec4 skyBottom;

const vec3 skyTopNoon = vec3(0.09, 0.106, 0.129);
const vec3 skyMiddleNoon = vec3(0.741, 0.871, 0.882);
const vec3 skyBottomNoon = vec3(0.62, 0.663, 0.627);

const vec3 skyTopEvening = skyTopNoon;
const vec3 skyMiddleEvening = vec3(0.949, 0.851, 0.263);
const vec3 skyBottomEvening = skyBottomNoon;

const vec3 skyTopMidnight = vec3(0.054902, 0.086275, 0.121569);
const vec3 skyMiddleMidnight = vec3(0.266667, 0.321569, 0.337255);
const vec3 skyBottomMidnight = vec3(0.141176, 0.145098, 0.149020);

uniform float time;
uniform vec3 sunDir;

in vec4 vertPos;

float logFrac(float f) {
return log2(f * 255 + 1) / 8;
}

float progress(float start, float end) {
return (time - start) / (end - start);
}

float angle;
float sunSkyAngle;

vec3 topMorning() {
return mix(skyTopNoon, skyMiddleNoon,
logFrac(angle) * progress(MORNING, NOON));
}

vec3 bottomMorning() {
return mix(mix(skyTopNoon, skyBottomNoon, progress(MORNING, NOON)),
topMorning(), logFrac(angle));
}

vec3 topNoon() {
return mix(skyTopNoon, skyMiddleNoon, logFrac(angle));
}

vec3 bottomNoon() {
return mix(skyBottomNoon, topNoon(), angle);
}

vec3 topEveningStart() {
return mix(skyTopEvening, skyMiddleEvening, angle);
}

vec3 bottomEveningStart() {
return mix(skyBottomNoon, topEveningStart(), angle);
}

vec3 topEveningMid() {
return mix(skyTopEvening, skyMiddleEvening, angle * progress(EVENING_MID,
EVENING_END) * (1 - logFrac(sunSkyAngle)));
}

vec3 bottomEveningMid() {
return mix(skyBottomEvening, topEveningMid(), angle);
}

vec3 topMidnightStart() {
return mix(skyTopMidnight, skyMiddleMidnight, 1 - sunSkyAngle);
}

vec3 bottomMidnightStart() {
return mix(skyBottomMidnight, topMidnightStart(), angle);
}

vec3 topMidnight() {
return mix(skyTopMidnight, skyMiddleMidnight, angle);
}

vec3 bottomMidnight() {
return mix(skyBottomMidnight, skyMiddleMidnight, angle);
float logFrac(float f) {
return log2(f * 255 + 1) / 8;
}

void main() {
Expand All @@ -96,56 +24,14 @@ void main() {
angle = 1 - angle;
sunSkyAngle = dot(normVert, sunDir);
sunSkyAngle = 1 - sunSkyAngle;
if (time > MORNING && time < NOON) {
gl_FragColor = vec4(topMorning(), 1);
} else if (time > NOON && time < EVENING_START) {
gl_FragColor = vec4(
mix(topMorning(), topNoon(), progress(NOON, EVENING_START)),
1);
} else if (time > EVENING_START && time < EVENING_MID) {
gl_FragColor = vec4(
mix(topNoon(), topEveningStart(),
progress(EVENING_START, EVENING_MID)), 1);
} else if (time > EVENING_MID && time < EVENING_END) {
gl_FragColor = vec4(
mix(topEveningStart(), topEveningMid(),
progress(EVENING_MID, EVENING_END)), 1);
} else if (time > EVENING_END && time < MIDNIGHT_START) {
gl_FragColor = vec4(
mix(topEveningMid(), topMidnightStart(),
progress(EVENING_END, MIDNIGHT_START)), 1);
} else if (time > MIDNIGHT_START && time < MIDNIGHT) {
gl_FragColor = vec4(
mix(topMidnightStart(), topMidnight(),
progress(MIDNIGHT_START, MIDNIGHT)), 1);
}

gl_FragColor = mix(skyTop, skyMiddle, logFrac(angle));
} else { // bottom half
angle += 1;
// need to invert one of those dirs
sunSkyAngle = dot(-1 * normVert, sunDir);
sunSkyAngle += 1;
if (time > MORNING && time < NOON) {
gl_FragColor = vec4(bottomMorning(), 1);
} else if (time > NOON && time < EVENING_START) {
gl_FragColor = vec4(
mix(bottomMorning(), bottomNoon(),
progress(NOON, EVENING_START)), 1);
} else if (time > EVENING_START && time < EVENING_MID) {
gl_FragColor = vec4(
mix(bottomNoon(), bottomEveningStart(),
progress(EVENING_START, EVENING_MID)), 1);
} else if (time > EVENING_MID && time < EVENING_END) {
gl_FragColor = vec4(
mix(bottomEveningStart(), bottomEveningMid(),
progress(EVENING_MID, EVENING_END)), 1);
} else if (time > EVENING_END && time < MIDNIGHT_START) {
gl_FragColor = vec4(
mix(bottomEveningMid(), bottomMidnightStart(),
progress(EVENING_END, MIDNIGHT_START)), 1);
} else if (time > MIDNIGHT_START && time < MIDNIGHT) {
gl_FragColor = vec4(
mix(bottomMidnightStart(), bottomMidnight(),
progress(MIDNIGHT_START, MIDNIGHT)), 1);
}

gl_FragColor = mix(skyMiddle, skyBottom, logFrac(angle));
}
}
58 changes: 47 additions & 11 deletions android/assets/skies.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,114 @@
"sunColor": {
},
"sunLightColor": {
"MORNING": {
"0": {
"r": 0.949,
"g": 0.851,
"b": 0.263,
"a": 1.0
},
"NOON": {
"0.25": {
"r": 0.8,
"g": 0.8,
"b": 0.8,
"a": 1.0
},
"EVENING_START": {
"0.375": {
"r": 0.849,
"g": 0.79,
"b": 0.763,
"a": 1.0
},
"EVENING_MID": {
"0.4375": {
"r": 0.949,
"g": 0.851,
"b": 0.263,
"a": 1.0
},
"EVENING_END": {
"0.5": {
"r": 0.949,
"g": 0.851,
"b": 0.263,
"a": 1.0
},
"MIDNIGHT_START": {
"0.625": {
"r": 0.949,
"g": 0.851,
"b": 0.263,
"a": 1.0
}
},
"ambLightColor": {
"MORNING": {
"0": {
"r": 0.4,
"g": 0.4,
"b": 0.4,
"a": 1.0
},
"NOON": {
"0.25": {
"r": 0.6,
"g": 0.6,
"b": 0.6,
"a": 1.0
},
"MIDNIGHT_START": {
"0.625": {
"r": 0.5,
"g": 0.5,
"b": 0.5,
"a": 1.0
},
"MIDNIGHT": {
"0.75": {
"r": 0.3,
"g": 0.3,
"b": 0.3,
"a": 1.0
},
"NIGHT_END": {
"0.875": {
"r": 0.5,
"g": 0.5,
"b": 0.5,
"a": 1.0
}
},
"skyTopColor": {
"0": {
"r": 0.09,
"g": 0.106,
"b": 0.129,
"a": 1.0
},
"0.5": {
"r": 0.09,
"g": 0.106,
"b": 0.129,
"a": 1.0
},
"0.75": {
"r": 0.054902,
"g": 0.086275,
"b": 0.121569,
"a": 1.0
}
},
"skyMiddleColor": {
"0": {
"r": 0.741,
"g": 0.871,
"b": 0.882,
"a": 1.0
},
"0.5": {
"r": 0.949,
"g": 0.851,
"b": 0.263,
"a": 1.0
},
"0.75": {
"r": 0.266667,
"g": 0.321569,
"b": 0.337255,
"a": 1.0
}
},
"skyBottomColor": {
}
Expand Down
71 changes: 49 additions & 22 deletions core/src/me/vinceh121/wanderer/glx/SkyProperties.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,68 @@
package me.vinceh121.wanderer.glx;

import java.util.EnumMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.TreeMap;

import com.badlogic.gdx.graphics.Color;

import me.vinceh121.wanderer.glx.SkyboxRenderer.TimeRange;

public class SkyProperties {
private final Map<TimeRange, Color> sunColor = new EnumMap<>(TimeRange.class);
private final Map<TimeRange, Color> sunLightColor = new EnumMap<>(TimeRange.class);
private final Map<TimeRange, Color> ambLightColor = new EnumMap<>(TimeRange.class);
private final Map<TimeRange, Color> skyTopColor = new EnumMap<>(TimeRange.class);
private final Map<TimeRange, Color> skyMiddleColor = new EnumMap<>(TimeRange.class);
private final Map<TimeRange, Color> skyBottomColor = new EnumMap<>(TimeRange.class);
private final NavigableMap<Float, Color> sunColor = new TreeMap<>();
private final NavigableMap<Float, Color> sunLightColor = new TreeMap<>();
private final NavigableMap<Float, Color> ambLightColor = new TreeMap<>();
private final NavigableMap<Float, Color> skyTopColor = new TreeMap<>();
private final NavigableMap<Float, Color> skyMiddleColor = new TreeMap<>();
private final NavigableMap<Float, Color> skyBottomColor = new TreeMap<>();

public NavigableMap<Float, Color> getSunColor() {
return sunColor;
}

public NavigableMap<Float, Color> getSunLightColor() {
return sunLightColor;
}

public NavigableMap<Float, Color> getAmbLightColor() {
return ambLightColor;
}

public Map<TimeRange, Color> getSunColor() {
return this.sunColor;
public NavigableMap<Float, Color> getSkyTopColor() {
return skyTopColor;
}

public Map<TimeRange, Color> getSunLightColor() {
return this.sunLightColor;
public NavigableMap<Float, Color> getSkyMiddleColor() {
return skyMiddleColor;
}

public Map<TimeRange, Color> getAmbLightColor() {
return this.ambLightColor;
public NavigableMap<Float, Color> getSkyBottomColor() {
return skyBottomColor;
}

public Map<TimeRange, Color> getSkyTopColor() {
return this.skyTopColor;
@Override
public int hashCode() {
return Objects.hash(ambLightColor, skyBottomColor, skyMiddleColor, skyTopColor, sunColor, sunLightColor);
}

public Map<TimeRange, Color> getSkyMiddleColor() {
return this.skyMiddleColor;
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SkyProperties other = (SkyProperties) obj;
return Objects.equals(ambLightColor, other.ambLightColor)
&& 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);
}

public Map<TimeRange, Color> getSkyBottomColor() {
return this.skyBottomColor;
@Override
public String toString() {
return "SkyProperties [sunColor=" + sunColor + ", sunLightColor=" + sunLightColor + ", ambLightColor="
+ ambLightColor + ", skyTopColor=" + skyTopColor + ", skyMiddleColor=" + skyMiddleColor
+ ", skyBottomColor=" + skyBottomColor + "]";
}
}
Loading

0 comments on commit a09bf4e

Please sign in to comment.