Skip to content

Commit

Permalink
Added space helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocelot5836 committed Jan 9, 2025
1 parent 48e71df commit 13e7397
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
vec3 rgb2hsv(vec3 c) {
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = c.g < c.b ? vec4(c.bg, K.wz) : vec4(c.gb, K.xy);
vec4 q = c.r < p.x ? vec4(p.xyw, c.r) : vec4(c.r, p.yzx);

float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
#veil:buffer veil:camera

float getFov() {
return 2.0 * atan(1.0 / VeilCamera.ProjMat[1][1]);
}

float getAspectRatio() {
return VeilCamera.ProjMat[1][1] / VeilCamera.ProjMat[0][0];
}
#veil:buffer veil:camera VeilCamera

vec3 viewPosFromDepth(float depth, vec2 uv) {
float z = depth * 2.0 - 1.0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#veil:buffer veil:camera VeilCamera

float getFov() {
return 2.0 * atan(1.0 / VeilCamera.ProjMat[1][1]);
}

float getAspectRatio() {
return VeilCamera.ProjMat[1][1] / VeilCamera.ProjMat[0][0];
}

// World Space

vec4 worldToViewSpace(vec4 pos) {
vec4 viewSpacePos = VeilCamera.ViewMat * (pos - vec4(VeilCamera.CameraPosition, 0.0));
return viewSpacePos / viewSpacePos.w;
}

vec4 worldToClipSpace(vec4 pos) {
vec4 viewSpacePos = VeilCamera.ViewMat * (pos - vec4(VeilCamera.CameraPosition, 0.0));
return VeilCamera.ProjMat * (viewSpacePos / viewSpacePos.w);
}

vec3 worldToScreenSpace(vec4 pos) {
vec4 viewSpacePos = VeilCamera.ViewMat * (pos - vec4(VeilCamera.CameraPosition, 0.0));
vec4 clipSpace = VeilCamera.ProjMat * (viewSpacePos / viewSpacePos.w);
clipSpace.xyz /= clipSpace.w;
return vec3((clipSpace.xy + vec2(1.0)) / 2.0, clipSpace.z);
}

#define worldToViewSpacePosition(pos) worldToViewSpace(vec4(pos, 1.0)).xyz
#define worldToClipSpacePosition(pos) worldToClipSpace(vec4(pos, 1.0)).xyz
#define worldToScreenSpacePosition(pos) worldToScreenSpace(vec4(pos, 1.0))
#define worldToViewSpaceDirection(pos) worldToViewSpace(vec4(pos, 0.0)).xyz
#define worldToClipSpaceDirection(pos) worldToClipSpace(vec4(pos, 0.0)).xyz
#define worldToScreenSpaceDirection(pos) worldToScreenSpace(vec4(pos, 0.0))

// View Space

vec4 viewToWorldSpace(vec4 pos) {
return vec4(VeilCamera.CameraPosition, 0.0) + VeilCamera.IViewMat * pos;
}

vec4 viewToClipSpace(vec4 pos) {
return VeilCamera.ProjMat * pos;
}

vec3 viewToScreenSpace(vec4 pos) {
vec4 clipSpace = VeilCamera.ProjMat * pos;
clipSpace.xyz /= clipSpace.w;
return vec3((clipSpace.xy + vec2(1.0)) / 2.0, clipSpace.z);
}

#define viewToWorldSpacePosition(pos) viewToWorldSpace(vec4(pos, 1.0)).xyz
#define viewToClipSpacePosition(pos) viewToClipSpace(vec4(pos, 1.0)).xyz
#define viewToScreenSpacePosition(pos) viewToScreenSpace(vec4(pos, 1.0))
#define viewToWorldSpaceDirection(pos) viewToWorldSpace(vec4(pos, 0.0)).xyz
#define viewToClipSpaceDirection(pos) viewToClipSpace(vec4(pos, 0.0)).xyz
#define viewToScreenSpaceDirection(pos) viewToScreenSpace(vec4(pos, 0.0))

// Clip Space

vec4 clipToWorldSpace(vec4 pos) {
return vec4(VeilCamera.CameraPosition, 0.0) + VeilCamera.IViewMat * VeilCamera.IProjMat * pos;
}

vec4 clipToViewSpace(vec4 pos) {
return VeilCamera.IProjMat * pos;
}

vec3 clipToScreenSpace(vec4 pos) {
pos.xyz /= pos.w;
return vec3((pos.xy + vec2(1.0)) / 2.0, pos.z);
}

#define clipToWorldSpacePosition(pos) clipToWorldSpace(vec4(pos, 1.0)).xyz
#define clipToViewSpacePosition(pos) clipToViewSpace(vec4(pos, 1.0)).xyz
#define clipToScreenSpacePosition(pos) clipToScreenSpace(vec4(pos, 1.0))
#define clipToWorldSpaceDirection(pos) clipToWorldSpace(vec4(pos, 0.0)).xyz
#define clipToViewSpaceDirection(pos) clipToViewSpace(vec4(pos, 0.0)).xyz
#define clipToScreenSpaceDirection(pos) clipToScreenSpace(vec4(pos, 0.0))

// Screen Space

vec4 screenToWorldSpace(vec3 pos) {
vec4 viewSpacePos = VeilCamera.IProjMat * (vec4(pos.xy, pos.z, 1.0) * 2.0 - 1.0);
return vec4(VeilCamera.CameraPosition, 0.0) + VeilCamera.IViewMat * (viewSpacePos / viewSpacePos.w);
}

vec4 screenToViewSpace(vec3 pos) {
vec4 viewSpacePos = VeilCamera.IProjMat * (vec4(pos.xy, pos.z, 1.0) * 2.0 - 1.0);
return viewSpacePos / viewSpacePos.w;
}

vec4 screenToClipSpace(vec3 pos) {
return vec4(pos.xy, pos.z, 1.0) * 2.0 - 1.0;
}

vec4 screenToWorldSpace(vec2 uv, float depth) {
vec4 viewSpacePos = VeilCamera.IProjMat * (vec4(uv, depth, 1.0) * 2.0 - 1.0);
return vec4(VeilCamera.CameraPosition, 0.0) + VeilCamera.IViewMat * (viewSpacePos / viewSpacePos.w);
}

vec4 screenToViewSpace(vec2 uv, float depth) {
vec4 viewSpacePos = VeilCamera.IProjMat * (vec4(uv, depth, 1.0) * 2.0 - 1.0);
return viewSpacePos / viewSpacePos.w;
}

vec4 screenToClipSpace(vec2 uv, float depth) {
return vec4(uv, depth, 1.0) * 2.0 - 1.0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"stages": [
{
"type": "veil:blit",
"shader": "veil:space_test",
"in": "minecraft:main"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include veil:space_helper

uniform sampler2D DiffuseSampler0;
uniform sampler2D DiffuseDepthSampler;

in vec2 texCoord;

out vec4 fragColor;

void main() {
vec4 baseColor = texture(DiffuseSampler0, texCoord);
float depthSample = texture(DiffuseDepthSampler, texCoord).r;
vec4 pos = screenToWorldSpace(texCoord, depthSample);

fragColor = vec4(pos.xyz, 1.0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"vertex": "veil:blit_screen",
"fragment": "veil:space_test"
}

0 comments on commit 13e7397

Please sign in to comment.