Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.0 - WebGPU Only #98

Draft
wants to merge 47 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
890bba7
changed depth calculation
BarthPaleologue Mar 24, 2024
3981cc7
Working WebGPU depth
BarthPaleologue Mar 25, 2024
ad2bc92
enable infinite far plane
BarthPaleologue Mar 25, 2024
124f780
giving up on supporting WebGL #61
BarthPaleologue Mar 25, 2024
4ba795a
Merge branch 'main' into WebGPU
BarthPaleologue May 12, 2024
57982a6
merge main with webgpu
BarthPaleologue May 12, 2024
a2dcc94
Update fragment.glsl
BarthPaleologue May 12, 2024
19dd963
Merge branch 'main' into WebGPU
BarthPaleologue May 12, 2024
c835ca7
directly use WebGPUEngine instead of EngineFactory
BarthPaleologue May 12, 2024
3019076
Merge branch 'main' into WebGPU
BarthPaleologue Jun 1, 2024
048eb15
Merge branch 'main' into WebGPU
BarthPaleologue Jun 1, 2024
aceedd3
fix depth renderer for webgpu
BarthPaleologue Jun 1, 2024
3e2ac34
Merge branch 'main' into WebGPU
BarthPaleologue Jun 1, 2024
0289631
Merge branch 'main' into WebGPU
BarthPaleologue Jun 14, 2024
99b9d56
Merge branch 'main' into WebGPU
BarthPaleologue Jun 14, 2024
ab50e8a
fix non linear depth
BarthPaleologue Jun 14, 2024
6591fdc
Merge branch 'main' into WebGPU
BarthPaleologue Jun 23, 2024
ee48531
Merge branch 'main' into WebGPU
BarthPaleologue Jun 23, 2024
f41ab57
Merge branch 'main' into WebGPU
BarthPaleologue Jul 10, 2024
c35cfc9
Merge branch 'main' into WebGPU
BarthPaleologue Jul 14, 2024
cbce797
trying to fix materials for WebGPU
BarthPaleologue Jul 14, 2024
30b5b21
removing errors
BarthPaleologue Jul 14, 2024
d7fa90a
Merge branch 'main' into WebGPU
BarthPaleologue Jul 14, 2024
8210795
space stations are fixed for webgpu
BarthPaleologue Jul 14, 2024
68d078f
Merge branch 'main' into WebGPU
BarthPaleologue Jul 15, 2024
a23bafe
Merge branch 'main' into WebGPU
BarthPaleologue Jul 16, 2024
75009af
Merge branch 'main' into WebGPU
BarthPaleologue Jul 16, 2024
b1a0ed6
Merge branch 'main' into WebGPU
BarthPaleologue Jul 23, 2024
f6c403f
fix uniform definition error
BarthPaleologue Jul 28, 2024
d9e7591
Merge branch 'main' into WebGPU
BarthPaleologue Jul 28, 2024
f439a90
increase universe scale to 1:1
BarthPaleologue Jul 28, 2024
5e84c14
Move near plane closer
BarthPaleologue Jul 28, 2024
6bee48d
Merge branch 'main' into WebGPU
BarthPaleologue Aug 2, 2024
4d13573
improve atmosphere look
BarthPaleologue Aug 2, 2024
d1fe072
fixed glsl compilation error
BarthPaleologue Aug 2, 2024
ab0c1e4
Merge branch 'main' into WebGPU
BarthPaleologue Aug 6, 2024
c28d14d
Update grassVertex.glsl
BarthPaleologue Aug 7, 2024
a53ef60
Merge branch 'main' into WebGPU
BarthPaleologue Aug 20, 2024
eae080f
denser atmospheres
BarthPaleologue Aug 20, 2024
29d4809
added small bloom (before post processes)
BarthPaleologue Aug 20, 2024
91d7e2c
make bloom more subtle
BarthPaleologue Aug 20, 2024
7f5c2ac
Merge branch 'main' into WebGPU
BarthPaleologue Sep 4, 2024
0c22a69
Merge branch 'main' into WebGPU
BarthPaleologue Oct 15, 2024
a5c9a4e
Merge branch 'main' into WebGPU
BarthPaleologue Oct 15, 2024
6ba0586
Update blackHoleDemo.ts
BarthPaleologue Oct 15, 2024
30e1f03
Merge branch 'main' into WebGPU
BarthPaleologue Oct 15, 2024
3df8148
Merge branch 'main' into WebGPU
BarthPaleologue Oct 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/shaders/atmosphericScatteringFragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

precision lowp float;
precision highp float;

#define DISABLE_UNIFORMITY_ANALYSIS

Expand All @@ -39,8 +39,6 @@ uniform sampler2D atmosphereLUT;

#include "./utils/atmosphere.glsl";

#include "./utils/remap.glsl";

#include "./utils/worldFromUV.glsl";

#include "./utils/rayIntersectSphere.glsl";
Expand Down Expand Up @@ -162,12 +160,12 @@ void main() {

float depth = texture2D(depthSampler, vUV).r;// the depth corresponding to the pixel in the depth map

vec3 pixelWorldPosition = worldFromUV(vUV, camera_inverseProjection, camera_inverseView);// the pixel position in world space (near plane)

vec3 rayDir = normalize(pixelWorldPosition - camera_position);// normalized direction of the ray
vec3 pixelWorldPosition = worldFromUV(vUV, depth, camera_inverseProjectionView);// the pixel position in world space (near plane)

// actual depth of the scene
float maximumDistance = length(pixelWorldPosition - camera_position) * remap(depth, 0.0, 1.0, camera_near, camera_far);
float maximumDistance = length(pixelWorldPosition - camera_position);

vec3 rayDir = normalize(worldFromUV(vUV, 1.0, camera_inverseProjectionView) - camera_position);

// Cohabitation avec le shader d'océan (un jour je merge)
float waterImpact, waterEscape;
Expand Down
17 changes: 7 additions & 10 deletions src/shaders/blackhole.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ uniform mat4 starfieldRotation;

#include "./utils/camera.glsl";

#include "./utils/remap.glsl";

#include "./utils/worldFromUV.glsl";

#include "./utils/uvFromWorld.glsl";
Expand Down Expand Up @@ -156,14 +154,13 @@ float customLength(vec3 v) {

void main() {
vec4 screenColor = texture2D(textureSampler, vUV);// the current screen color

vec3 pixelWorldPosition = worldFromUV(vUV, camera_inverseProjection, camera_inverseView);// the pixel position in world space (near plane)
vec3 rayDir = normalize(pixelWorldPosition - camera_position);// normalized direction of the ray

float depth = texture2D(depthSampler, vUV).r;// the depth corresponding to the pixel in the depth map

vec3 pixelWorldPosition = worldFromUV(vUV, depth, camera_inverseProjectionView);// the pixel position in world space (near plane)
vec3 rayDir = normalize(worldFromUV(vUV, 1.0, camera_inverseProjectionView) - camera_position);// normalized direction of the ray

// actual depth of the scene
float maximumDistance = length(pixelWorldPosition - camera_position) * remap(depth, 0.0, 1.0, camera_near, camera_far);
float maximumDistance = length(pixelWorldPosition - camera_position);

float maxBendDistance = max(accretionDiskRadius * 3.0, schwarzschildRadius * 15.0);

Expand Down Expand Up @@ -255,17 +252,17 @@ void main() {

// getting the screen coordinate of the end of the bended ray
vec2 uv = uvFromWorld(rayPositionBlackHoleSpace + object_position, camera_projection, camera_view);
float depthEndRay = texture2D(depthSampler, uv).r;// the depth corresponding to the pixel in the depth map
// check if there is an object occlusion
vec3 pixelWorldPositionEndRay = worldFromUV(uv, camera_inverseProjection, camera_inverseView);// the pixel position in world space (near plane)
vec3 pixelWorldPositionEndRay = worldFromUV(uv, depthEndRay, camera_inverseProjectionView);// the pixel position in world space (near plane)
vec3 rayDirToEndRay = normalize(pixelWorldPositionEndRay - camera_position);// normalized direction of the ray

float depthEndRay = texture2D(depthSampler, uv).r;// the depth corresponding to the pixel in the depth map
for(int i = 0; i < 10; i++) {
vec2 offset = (vec2(hash(float(i)), hash(float(i + 1))) - 0.5) * 0.01;
depthEndRay = min(depthEndRay, texture2D(depthSampler, uv + offset).r);
}
// closest physical point from the camera in the direction of the pixel (occlusion)
vec3 closestPointEndRay = (pixelWorldPositionEndRay - camera_position) * remap(depthEndRay, 0.0, 1.0, camera_near, camera_far);
vec3 closestPointEndRay = pixelWorldPositionEndRay - camera_position;
float maximumDistanceEndRay = length(closestPointEndRay);// the maxium ray length due to occlusion
float BHDistance = length(camera_position - object_position);

Expand Down
8 changes: 3 additions & 5 deletions src/shaders/flatCloudsFragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ uniform float time;

#include "./utils/saturate.glsl";

#include "./utils/remap.glsl";

#include "./utils/worldFromUV.glsl";

#include "./utils/rayIntersectSphere.glsl";
Expand Down Expand Up @@ -164,12 +162,12 @@ void main() {

float depth = texture2D(depthSampler, vUV).r;// the depth corresponding to the pixel in the depth map

vec3 pixelWorldPosition = worldFromUV(vUV, camera_inverseProjection, camera_inverseView);// the pixel position in world space (near plane)
vec3 pixelWorldPosition = worldFromUV(vUV, depth, camera_inverseProjectionView);// the pixel position in world space (near plane)

// closest physical point from the camera in the direction of the pixel (occlusion)
float maximumDistance = length(pixelWorldPosition - camera_position) * remap(depth, 0.0, 1.0, camera_near, camera_far);
float maximumDistance = length(pixelWorldPosition - camera_position);

vec3 rayDir = normalize(pixelWorldPosition - camera_position);// normalized direction of the ray
vec3 rayDir = normalize(worldFromUV(vUV, 1.0, camera_inverseProjectionView) - camera_position);// normalized direction of the ray

vec3 closestPoint = camera_position + rayDir * maximumDistance;
float t0, t1;
Expand Down
1 change: 0 additions & 1 deletion src/shaders/grassMaterial/grassVertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ uniform sampler2D perlinNoise;
varying vec3 vPosition;
varying vec3 vPositionW;

varying mat4 normalMatrix;
varying vec3 vNormalW;

varying float vPlanetNdl;
Expand Down
10 changes: 6 additions & 4 deletions src/shaders/juliaSet.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,14 @@ float contrast(float val, float contrast_offset, float contrast_mid_level)
void main() {
vec4 screenColor = texture2D(textureSampler, vUV);// the current screen color

vec3 pixelWorldPosition = worldFromUV(vUV, camera_inverseProjection, camera_inverseView);// the pixel position in world space (near plane)
vec3 rayDir = normalize(pixelWorldPosition - camera_position);// normalized direction of the ray

float depth = texture2D(depthSampler, vUV).r;// the depth corresponding to the pixel in the depth map

vec3 pixelWorldPosition = worldFromUV(vUV, depth, camera_inverseProjectionView);// the pixel position in world space (near plane)

// actual depth of the scene
float maximumDistance = length(pixelWorldPosition - camera_position) * remap(depth, 0.0, 1.0, camera_near, camera_far);
float maximumDistance = length(pixelWorldPosition - camera_position);

vec3 rayDir = normalize(worldFromUV(vUV, 1.0, camera_inverseProjectionView) - camera_position);

float impactPoint, escapePoint;
if (!(rayIntersectSphere(camera_position, rayDir, object_position, object_radius * object_scaling_determinant, impactPoint, escapePoint))) {
Expand Down
4 changes: 2 additions & 2 deletions src/shaders/lensflare.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ void main() {
return;
}

vec3 pixelWorldPosition = worldFromUV(vUV, camera_inverseProjection, camera_inverseView);// the pixel position in world space (near plane)
vec3 rayDir = normalize(pixelWorldPosition - camera_position);
vec3 pixelWorldPosition = worldFromUV(vUV, 1.0, camera_inverseProjectionView);// the pixel position in world space (near plane)
vec3 rayDir = normalize(worldFromUV(vUV, 1.0, camera_inverseProjectionView) - camera_position);

vec3 objectDirection = normalize(object_position - camera_position);

Expand Down
12 changes: 6 additions & 6 deletions src/shaders/mandelbulb.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ uniform sampler2D depthSampler;

#include "./utils/camera.glsl";

#include "./utils/remap.glsl";

#include "./utils/worldFromUV.glsl";

#include "./utils/rayIntersectSphere.glsl";
Expand Down Expand Up @@ -120,12 +118,14 @@ float contrast(float val, float contrast_offset, float contrast_mid_level)
void main() {
vec4 screenColor = texture2D(textureSampler, vUV);// the current screen color

vec3 pixelWorldPosition = worldFromUV(vUV, camera_inverseProjection, camera_inverseView);// the pixel position in world space (near plane)
vec3 rayDir = normalize(pixelWorldPosition - camera_position);// normalized direction of the ray

float depth = texture2D(depthSampler, vUV).r;// the depth corresponding to the pixel in the depth map

vec3 pixelWorldPosition = worldFromUV(vUV, depth, camera_inverseProjectionView);// the pixel position in world space (near plane)

// actual depth of the scene
float maximumDistance = length(pixelWorldPosition - camera_position) * remap(depth, 0.0, 1.0, camera_near, camera_far);
float maximumDistance = length(pixelWorldPosition - camera_position);

vec3 rayDir = normalize(worldFromUV(vUV, 1.0, camera_inverseProjectionView) - camera_position);

float impactPoint, escapePoint;
if (!(rayIntersectSphere(camera_position, rayDir, object_position, object_radius * object_scaling_determinant, impactPoint, escapePoint))) {
Expand Down
8 changes: 3 additions & 5 deletions src/shaders/matterjet.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ uniform float time;

#include "./utils/rotateAround.glsl";

#include "./utils/remap.glsl";

#include "./utils/worldFromUV.glsl";

#include "./utils/removeAxialTilt.glsl";
Expand Down Expand Up @@ -116,12 +114,12 @@ void main() {

float depth = texture2D(depthSampler, vUV).r;// the depth corresponding to the pixel in the depth map

vec3 pixelWorldPosition = worldFromUV(vUV, camera_inverseProjection, camera_inverseView);// the pixel position in world space (near plane)
vec3 pixelWorldPosition = worldFromUV(vUV, depth, camera_inverseProjectionView);// the pixel position in world space (near plane)

// actual depth of the scene
float maximumDistance = length(pixelWorldPosition - camera_position) * remap(depth, 0.0, 1.0, camera_near, camera_far);
float maximumDistance = length(pixelWorldPosition - camera_position);

vec3 rayDir = normalize(pixelWorldPosition - camera_position);// normalized direction of the ray
vec3 rayDir = normalize(worldFromUV(vUV, 1.0, camera_inverseProjectionView) - camera_position);// normalized direction of the ray

vec4 finalColor = screenColor;

Expand Down
6 changes: 4 additions & 2 deletions src/shaders/metalSectionMaterial/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ void main() {

vec2 uv = vec2(fract(6.0 * vUV.x), fract(vPosition.y / 50.0));

float gamma = 2.2;

vec3 normalFromMap = texture(normalMap, uv).rgb;
normalFromMap.y = 1.0 - normalFromMap.y;
normalFromMap = normalFromMap * 2.0 - 1.0;
vec3 normalW = normalize(vTBN * normalFromMap);

vec3 albedo = pow(texture(albedoMap, uv).rgb, vec3(2.2));
vec3 albedo = pow(texture(albedoMap, uv).rgb, vec3(gamma));
float roughness = texture(roughnessMap, uv).r;
float metallic = texture(metallicMap, uv).r;

Expand All @@ -53,7 +55,7 @@ void main() {
Lo += calculateLight(albedo, normalW, roughness, metallic, lightDirectionW, viewDirectionW, star_colors[i]);
}

Lo = pow(Lo, vec3(1.0 / 2.2));
Lo = pow(Lo, vec3(1.0 / gamma));

gl_FragColor = vec4(Lo, 1.0);
}
1 change: 0 additions & 1 deletion src/shaders/metalSectionMaterial/vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ attribute vec3 normal;
attribute vec2 uv;

uniform mat4 world;
uniform mat4 view;
uniform mat4 worldViewProjection;

varying vec3 vPositionW;
Expand Down
10 changes: 4 additions & 6 deletions src/shaders/oceanFragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ uniform float ocean_waveBlendingSharpness;

uniform float time;

#include "./utils/remap.glsl";

#include "./utils/worldFromUV.glsl";

#include "./utils/rayIntersectSphere.glsl";
Expand All @@ -68,12 +66,12 @@ void main() {

float depth = texture2D(depthSampler, vUV).r;// the depth corresponding to the pixel in the depth map

vec3 pixelWorldPosition = worldFromUV(vUV, camera_inverseProjection, camera_inverseView);// the pixel position in world space (near plane)
vec3 pixelWorldPosition = worldFromUV(vUV, depth, camera_inverseProjectionView);// the pixel position in world space (near plane)

// actual depth of the scene
float maximumDistance = length(pixelWorldPosition - camera_position) * remap(depth, 0.0, 1.0, camera_near, camera_far);
float maximumDistance = length(pixelWorldPosition - camera_position);

vec3 rayDir = normalize(pixelWorldPosition - camera_position);// normalized direction of the ray
vec3 rayDir = normalize(worldFromUV(vUV, 1.0, camera_inverseProjectionView) - camera_position);

vec4 finalColor = screenColor;

Expand Down Expand Up @@ -109,7 +107,7 @@ void main() {
vec3 ambiant = mix(oceanColor, screenColor.rgb, alpha);

// if the camera is not inside the ocean
if(impactPoint > 0.0) {
if (impactPoint > 0.0) {
// color of the sky
vec3 reflectedSkyColor = vec3(0.6, 0.8, 0.95);

Expand Down
6 changes: 3 additions & 3 deletions src/shaders/ringsFragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ void main() {

float depth = texture2D(depthSampler, vUV).r;// the depth corresponding to the pixel in the depth map

vec3 pixelWorldPosition = worldFromUV(vUV, camera_inverseProjection, camera_inverseView);// the pixel position in world space (near plane)
vec3 pixelWorldPosition = worldFromUV(vUV, depth, camera_inverseProjectionView);// the pixel position in world space (near plane)

// actual depth of the scene
float maximumDistance = length(pixelWorldPosition - camera_position) * remap(depth, 0.0, 1.0, camera_near, camera_far);
float maximumDistance = length(pixelWorldPosition - camera_position);

vec3 rayDir = normalize(pixelWorldPosition - camera_position);// normalized direction of the ray
vec3 rayDir = normalize(worldFromUV(vUV, 1.0, camera_inverseProjectionView) - camera_position);// normalized direction of the ray

vec4 finalColor = screenColor;

Expand Down
6 changes: 3 additions & 3 deletions src/shaders/shadowFragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ void main() {

float depth = texture2D(depthSampler, vUV).r;// the depth corresponding to the pixel in the depth map

vec3 pixelWorldPosition = worldFromUV(vUV, camera_inverseProjection, camera_inverseView);// the pixel position in world space (near plane)
vec3 pixelWorldPosition = worldFromUV(vUV, depth, camera_inverseProjectionView);// the pixel position in world space (near plane)

// closest physical point from the camera in the direction of the pixel (occlusion)
float maximumDistance = length(pixelWorldPosition - camera_position) * remap(depth, 0.0, 1.0, camera_near, camera_far);
float maximumDistance = length(pixelWorldPosition - camera_position);

vec3 rayDir = normalize(pixelWorldPosition - camera_position);// normalized direction of the ray
vec3 rayDir = normalize(worldFromUV(vUV, 1.0, camera_inverseProjectionView) - camera_position);// normalized direction of the ray

vec4 finalColor = screenColor;

Expand Down
11 changes: 5 additions & 6 deletions src/shaders/sierpinski.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ uniform sampler2D depthSampler;

#include "./utils/camera.glsl";

#include "./utils/remap.glsl";

#include "./utils/worldFromUV.glsl";

#include "./utils/rayIntersectSphere.glsl";
Expand Down Expand Up @@ -127,12 +125,13 @@ vec3 estimate_normal(const vec3 p, const float delta)
void main() {
vec4 screenColor = texture2D(textureSampler, vUV);// the current screen color

vec3 pixelWorldPosition = worldFromUV(vUV, camera_inverseProjection, camera_inverseView);// the pixel position in world space (near plane)
vec3 rayDir = normalize(pixelWorldPosition - camera_position);// normalized direction of the ray

float depth = texture2D(depthSampler, vUV).r;// the depth corresponding to the pixel in the depth map

vec3 pixelWorldPosition = worldFromUV(vUV, depth, camera_inverseProjectionView);// the pixel position in world space (near plane)
vec3 rayDir = normalize(pixelWorldPosition - cameraPosition);// normalized direction of the ray

// actual depth of the scene
float maximumDistance = length(pixelWorldPosition - camera_position) * remap(depth, 0.0, 1.0, camera_near, camera_far);
float maximumDistance = length(pixelWorldPosition - camera_position);

float impactPoint, escapePoint;
if (!(rayIntersectSphere(camera_position, rayDir, object_position, planetRadius, impactPoint, escapePoint))) {
Expand Down
4 changes: 0 additions & 4 deletions src/shaders/solarPanelMaterial/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ uniform sampler2D roughnessMap;

uniform vec3 cameraPosition;

#include "../utils/pi.glsl";

#include "../utils/stars.glsl";

#include "../utils/remap.glsl";

#include "../utils/textureNoTile.glsl";

#include "../utils/pbr.glsl";
Expand Down
5 changes: 2 additions & 3 deletions src/shaders/solarPanelMaterial/vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ attribute vec3 position;
attribute vec3 normal;

uniform mat4 world;
uniform mat4 view;
uniform mat4 worldViewProjection;

varying vec3 vPositionW;
varying vec3 vNormalW;
varying vec3 vPosition;

void main() {
gl_Position = worldViewProjection * vec4(position, 1.0);

vPositionW = vec3(world * vec4(position, 1.0));
vNormalW = vec3(world * vec4(normal, 0.0));
vPosition = position;

gl_Position = worldViewProjection * vec4(position, 1.0);
}
3 changes: 1 addition & 2 deletions src/shaders/utils/camera.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
uniform vec3 camera_position;
uniform mat4 camera_projection;
uniform mat4 camera_view;
uniform mat4 camera_inverseProjection;
uniform mat4 camera_inverseView;
uniform mat4 camera_inverseProjectionView;
uniform float camera_near;
uniform float camera_far;
uniform float camera_fov;
15 changes: 10 additions & 5 deletions src/shaders/utils/worldFromUV.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
// This is an evolution from the code found here
// https://forum.babylonjs.com/t/pixel-position-in-world-space-from-fragment-postprocess-shader-issue/30232
// also see https://www.babylonjs-playground.com/#1PHYB0#318 for smaller scale testing
// also see https://forum.babylonjs.com/t/clip-space-to-world-space-with-non-linear-reverse-depth-buffer-with-webgpu/48892/5 for the ultimate version
// This is a revised version that works with the reverse depth buffer
vec3 worldFromUV(vec2 pos, mat4 inverseProjection, mat4 inverseView) {
vec4 ndc = vec4(pos.xy * 2.0 - 1.0, 1.0, 1.0); // get ndc position (z = 1 because the depth buffer is reversed)
vec4 posVS = inverseProjection * ndc; // unproject the ndc coordinates : we are now in view space if i understand correctly
vec4 posWS = inverseView * posVS; // then we use inverse view to get to world space, division by w to get actual coordinates
return posWS.xyz / posWS.w;
vec3 worldFromUV(vec2 pos, float depth, mat4 inverseProjectionView) {
vec4 ndc = vec4(
pos.xy * 2.0 - 1.0,
depth,
1.0
);

vec4 positionWorldSpace = inverseProjectionView * ndc;
return positionWorldSpace.xyz / positionWorldSpace.w;
}
Loading
Loading