Skip to content

Commit

Permalink
Cleaup, fix orbits
Browse files Browse the repository at this point in the history
  • Loading branch information
GlennFolker committed Dec 13, 2024
1 parent b2cd8a4 commit 0318f24
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 390 deletions.
2 changes: 2 additions & 0 deletions assets/shaders/confictura/black-hole-stencil.frag
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define HIGHP

in vec2 v_texCoords;

out vec4 fragColor;
Expand Down
94 changes: 51 additions & 43 deletions assets/shaders/confictura/black-hole.frag
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
#define HIGHP

in vec3 v_position;
in vec2 v_texCoords;

out vec4 fragColor;

uniform vec3 u_relCamPos;
uniform vec3 u_camPos;
uniform vec2 u_viewport;
uniform vec3 u_relCamPos;
uniform vec3 u_center;

uniform float u_radius;
uniform float u_horizon;
uniform sampler2D u_ref;

uniform mat4 u_cubeView[6];
uniform mat4 u_cubeInvView[6];
uniform mat4 u_proj;
uniform mat4 u_invProj;
uniform float u_far;
uniform vec2 u_depthRange;

uniform samplerCube u_ref;
uniform samplerCube u_depth;

bool intersectSphere(vec3 origin, vec3 dir, float radius, out vec2 result){
float b = dot(origin, dir);
Expand All @@ -31,53 +36,56 @@ bool intersectSphere(vec3 origin, vec3 dir, float radius, out vec2 result){
return true;
}

vec4 far(vec3 origin, vec3 ray){
float far = (u_far - origin.z) / ray.z;
vec3 intersect = origin + far * ray;
vec4 clip = u_proj * vec4(intersect, 1.0);

return clip;
int cubeIndex(vec3 dir){
vec3 absDir = abs(dir);
if(absDir.x >= absDir.y && absDir.x >= absDir.z) {
return (dir.x > 0.0) ? 0 : 1;
}else if(absDir.y >= absDir.x && absDir.y >= absDir.z) {
return (dir.y > 0.0) ? 2 : 3;
}else {
return (dir.z > 0.0) ? 4 : 5;
}
}

vec2 coords(vec4 far){
return (far.xyz / far.w).xy * 0.5 + vec2(0.5);
}
vec3 rotate(vec3 v, vec3 axis, float angle){
float cosTheta = cos(angle);
float sinTheta = sin(angle);

vec2 coords(vec3 origin, vec3 ray){
return coords(far(origin, ray));
return
v * cosTheta +
cross(axis, v) * sinTheta +
axis * dot(axis, v) * (1.0 - cosTheta);
}

void main(){
vec3 ray = normalize(v_position - u_camPos);
vec2 ndc = (gl_FragCoord.xy / u_viewport) * 2.0 - 1.0;
vec4 view = u_invProj * vec4(ndc, -1.0, 1.0);
vec3 ray = normalize(view.xyz / view.w - u_camPos);

vec2 bound;
if(!intersectSphere(u_relCamPos, ray, u_radius, bound)) discard;

float intensity = smoothstep(0.0, 1.0, pow((bound.y - bound.x) / (u_radius * 2.0), 3.2));
float dist = length(u_relCamPos + ((bound.x + bound.y) / 2.0) * ray) / u_radius;

vec4 center = far(u_camPos, normalize(u_center - u_camPos));
vec4 current = far(u_camPos, ray);
vec2 centerCoord = coords(center);
vec2 currentCoord = coords(current);
vec2 dir = currentCoord - centerCoord;
int faceIndex = cubeIndex(ray);
float near = u_depthRange.x, far = u_depthRange.y;

vec3 newRay;
{
vec4 clip = current;
clip.xy -= (dir / dist) * intensity * 3.0 * clip.w;
float zCamera = (near * far) / (far + (near - far) * texture(u_depth, ray).r);
vec3 rayDirCameraSpace = (u_cubeView[faceIndex] * vec4(ray, 0.0)).xyz;
vec3 worldPos = (u_cubeInvView[faceIndex] * vec4(zCamera * rayDirCameraSpace, 1.0)).xyz;

vec4 world = u_invProj * clip;
newRay = normalize(world.xyz / world.w - u_camPos);
vec2 bound;
if(intersectSphere(u_relCamPos, ray, u_radius, bound) && bound.x < distance(u_camPos, worldPos)){
far = gl_DepthRange.far, near = gl_DepthRange.near;
vec4 clip = u_proj * vec4(u_camPos + ray * bound.x, 1.0);
gl_FragDepth = (((far - near) * (clip.z / clip.w)) + near + far) / 2.0;

vec3 hit = u_camPos + ray * (bound.x + bound.y) / 2.0 - u_center;
vec3 axis = normalize(cross(hit, u_center - u_camPos));
float dist = length(hit) / u_radius;
float inner = 1.0 - smoothstep(0.0, 1.0, pow(1.0 - max(-dist + u_horizon, 0.0) / u_horizon, 30.0));

fragColor = mix(
texture(u_ref, rotate(ray, axis, 2.0944 * pow(1.0 - dist, 3.0))),
vec4(vec3(0.0), 1.0),
inner
);
}else{
discard;
}

vec3 origin = u_camPos + bound.x * ray;
vec4 shift = texture(u_ref, coords(origin, newRay));

float inner = 1.0 - smoothstep(0.0, 1.0, pow(1.0 - max(-dist + u_horizon, 0.0) / u_horizon, 30.0));
fragColor = mix(
shift,
vec4(vec3(0.0), 1.0),
inner
);
}
15 changes: 5 additions & 10 deletions assets/shaders/confictura/black-hole.vert
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#define HIGHP

in vec3 a_position;
in vec4 a_position;
in vec2 a_texCoord0;

out vec3 v_position;

uniform mat4 u_proj;
uniform mat4 u_trans;
uniform float u_radius;
out vec2 v_texCoords;

void main(){
vec4 pos = u_trans * vec4(a_position, 1.0);

v_position = pos.xyz;
gl_Position = u_proj * pos;
v_texCoords = a_texCoord0;
gl_Position = a_position;
}
91 changes: 0 additions & 91 deletions assets/shaders/confictura/ray-black-hole.frag

This file was deleted.

11 changes: 0 additions & 11 deletions assets/shaders/confictura/screenspace.frag

This file was deleted.

13 changes: 0 additions & 13 deletions assets/shaders/confictura/screenspace.vert

This file was deleted.

25 changes: 14 additions & 11 deletions src/confictura/content/CPlanets.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ public static void load(){
satelliteErekir = new Satellite("satellite-erekir", Planets.erekir, 0.525f);

blackHole = new BlackHole("black-hole", 12f){{
camRadius = -4f;
orbitSpacing = 16f;
}};

new Planet("test", blackHole, 1f){{
generator = new SerpuloPlanetGenerator();
meshLoader = () -> new HexMesh(this, 6);
cloudMeshLoader = () -> new MultiMesh(
new HexSkyMesh(this, 11, 0.15f, 0.13f, 5, new Color().set(Pal.spore).mul(0.9f).a(0.75f), 2, 0.45f, 0.9f, 0.38f),
new HexSkyMesh(this, 1, 0.6f, 0.16f, 5, Color.white.cpy().lerp(Pal.spore, 0.55f).a(0.75f), 2, 0.45f, 1f, 0.41f)
);
atmosphereColor = Color.valueOf("3c1b8f");
atmosphereRadIn = 0.02f;
atmosphereRadOut = 0.3f;
}};
for(int i = 0; i < 3; i++){
new Planet("test" + i, blackHole, 1f){{
generator = new SerpuloPlanetGenerator();
meshLoader = () -> new HexMesh(this, 6);
cloudMeshLoader = () -> new MultiMesh(
new HexSkyMesh(this, 11, 0.15f, 0.13f, 5, new Color().set(Pal.spore).mul(0.9f).a(0.75f), 2, 0.45f, 0.9f, 0.38f),
new HexSkyMesh(this, 1, 0.6f, 0.16f, 5, Color.white.cpy().lerp(Pal.spore, 0.55f).a(0.75f), 2, 0.45f, 1f, 0.41f)
);
atmosphereColor = Color.valueOf("3c1b8f");
atmosphereRadIn = 0.02f;
atmosphereRadOut = 0.3f;
}};
}
}
}
4 changes: 0 additions & 4 deletions src/confictura/graphics/CShaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public final class CShaders{
public static ModelPropShader modelProp;
public static BlackHoleShader blackHole;
public static BlackHoleStencilShader blackHoleStencil;
public static ScreenspaceShader screenspace;
public static RayBlackHoleShader rayBlackHole;

private CShaders(){
throw new AssertionError();
Expand All @@ -40,8 +38,6 @@ public static void load(){
modelProp = new ModelPropShader();
blackHole = new BlackHoleShader();
blackHoleStencil = new BlackHoleStencilShader();
screenspace = new ScreenspaceShader();
rayBlackHole = new RayBlackHoleShader();

Shader.prependVertexCode = prevVert;
Shader.prependFragmentCode = prevFrag;
Expand Down
25 changes: 19 additions & 6 deletions src/confictura/graphics/shaders/BlackHoleShader.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,51 @@
package confictura.graphics.shaders;

import arc.graphics.Cubemap.*;
import arc.graphics.g3d.*;
import arc.math.geom.*;
import arc.util.*;
import confictura.graphics.gl.*;
import confictura.world.celestial.*;

import static arc.Core.*;
import static confictura.graphics.CShaders.*;

public class BlackHoleShader extends Gl30Shader{
private static final Mat3D mat = new Mat3D();

public Camera3D camera;
public Mat3D[] cubemapView = new Mat3D[CubemapSide.values().length];
public BlackHole planet;

public BlackHoleShader(){
super(file("black-hole.vert"), file("black-hole.frag"));
for(int i = 0; i < cubemapView.length; i++){
cubemapView[i] = new Mat3D();
}
}

@Override
public void apply(){
setUniformMatrix4("u_proj", camera.combined.val);
setUniformMatrix4("u_invProj", camera.invProjectionView.val);
setUniformf("u_far", camera.far);
setUniformMatrix4("u_trans", planet.getTransform(mat).val);

setUniformf("u_camPos", camera.position);
setUniformf("u_viewport", graphics.getWidth(), graphics.getHeight());
setUniformf("u_relCamPos", Tmp.v31.set(camera.position).sub(planet.position));
setUniformf("u_center", planet.position);

setUniformf("u_radius", planet.radius);
setUniformf("u_horizon", planet.horizon);

//planet.ref.getTexture().bind(0);
for(int i = 0; i < cubemapView.length; i++){
setUniformMatrix4("u_cubeView[" + i + "]", cubemapView[i].val);
setUniformMatrix4("u_cubeInvView[" + i + "]", mat.set(cubemapView[i]).inv().val);
}

setUniformMatrix4("u_proj", camera.combined.val);
setUniformMatrix4("u_invProj", camera.invProjectionView.val);
setUniformf("u_depthRange", camera.near, camera.far);

planet.pov.getDepthTexture().bind(1);
planet.pov.getTexture().bind(0);
setUniformi("u_depth", 1);
setUniformi("u_ref", 0);
}
}
3 changes: 1 addition & 2 deletions src/confictura/graphics/shaders/BlackHoleStencilShader.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package confictura.graphics.shaders;

import confictura.graphics.gl.*;
import mindustry.graphics.*;

import static confictura.graphics.CShaders.*;

public class BlackHoleStencilShader extends Gl30Shader{
public CFrameBuffer src, ref;

public BlackHoleStencilShader(){
super(Shaders.getShaderFi("screenspace.vert"), file("black-hole-stencil.frag"));
super(file("black-hole-stencil.vert"), file("black-hole-stencil.frag"));
}

@Override
Expand Down
Loading

0 comments on commit 0318f24

Please sign in to comment.