diff --git a/src/shaders/atmosphericScatteringFragment.glsl b/src/shaders/atmosphericScatteringFragment.glsl index 708f094e2..d66fa5269 100644 --- a/src/shaders/atmosphericScatteringFragment.glsl +++ b/src/shaders/atmosphericScatteringFragment.glsl @@ -15,7 +15,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -precision lowp float; +precision highp float; #define DISABLE_UNIFORMITY_ANALYSIS @@ -37,8 +37,6 @@ uniform sampler2D depthSampler;// the depth map of the camera #include "./utils/atmosphere.glsl"; -#include "./utils/remap.glsl"; - #include "./utils/worldFromUV.glsl"; #include "./utils/rayIntersectSphere.glsl"; @@ -160,12 +158,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; diff --git a/src/shaders/blackhole.glsl b/src/shaders/blackhole.glsl index 2e35c6715..e4e57b73f 100644 --- a/src/shaders/blackhole.glsl +++ b/src/shaders/blackhole.glsl @@ -32,8 +32,6 @@ uniform mat4 starfieldRotation; #include "./utils/camera.glsl"; -#include "./utils/remap.glsl"; - #include "./utils/worldFromUV.glsl"; #include "./utils/uvFromWorld.glsl"; @@ -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); @@ -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); diff --git a/src/shaders/flatCloudsFragment.glsl b/src/shaders/flatCloudsFragment.glsl index 5659e2e71..89af4ef03 100644 --- a/src/shaders/flatCloudsFragment.glsl +++ b/src/shaders/flatCloudsFragment.glsl @@ -49,8 +49,6 @@ uniform float time; #include "./utils/saturate.glsl"; -#include "./utils/remap.glsl"; - #include "./utils/worldFromUV.glsl"; #include "./utils/rayIntersectSphere.glsl"; @@ -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; diff --git a/src/shaders/grassMaterial/grassVertex.glsl b/src/shaders/grassMaterial/grassVertex.glsl index 92218e86b..2cc5705a7 100644 --- a/src/shaders/grassMaterial/grassVertex.glsl +++ b/src/shaders/grassMaterial/grassVertex.glsl @@ -37,7 +37,6 @@ uniform sampler2D perlinNoise; varying vec3 vPosition; varying vec3 vPositionW; -varying mat4 normalMatrix; varying vec3 vNormalW; varying float vPlanetNdl; diff --git a/src/shaders/juliaSet.glsl b/src/shaders/juliaSet.glsl index fc55c094e..4c2ebee3f 100644 --- a/src/shaders/juliaSet.glsl +++ b/src/shaders/juliaSet.glsl @@ -147,12 +147,14 @@ float getShadow(vec3 rayOrigin, vec3 rayDir, vec3 starPosition) { 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))) { diff --git a/src/shaders/lensflare.glsl b/src/shaders/lensflare.glsl index 158818abb..9c8ae22c6 100644 --- a/src/shaders/lensflare.glsl +++ b/src/shaders/lensflare.glsl @@ -114,8 +114,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); diff --git a/src/shaders/mandelbox.glsl b/src/shaders/mandelbox.glsl index 45f67e7af..d04f79a58 100644 --- a/src/shaders/mandelbox.glsl +++ b/src/shaders/mandelbox.glsl @@ -70,13 +70,15 @@ float distanceEstimator(vec3 position) { 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))) { gl_FragColor = screenColor; diff --git a/src/shaders/mandelbulb.glsl b/src/shaders/mandelbulb.glsl index 822ea7613..0e1c271b7 100644 --- a/src/shaders/mandelbulb.glsl +++ b/src/shaders/mandelbulb.glsl @@ -35,8 +35,6 @@ uniform sampler2D depthSampler; #include "./utils/camera.glsl"; -#include "./utils/remap.glsl"; - #include "./utils/worldFromUV.glsl"; #include "./utils/rayIntersectSphere.glsl"; @@ -180,12 +178,14 @@ float getShadow(vec3 rayOrigin, vec3 rayDir, vec3 starPosition) { 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))) { diff --git a/src/shaders/matterjet.glsl b/src/shaders/matterjet.glsl index a4a4f0e31..b67f3d844 100644 --- a/src/shaders/matterjet.glsl +++ b/src/shaders/matterjet.glsl @@ -34,8 +34,6 @@ uniform float dipoleTilt; #include "./utils/rotateAround.glsl"; -#include "./utils/remap.glsl"; - #include "./utils/worldFromUV.glsl"; #include "./utils/removeAxialTilt.glsl"; @@ -230,12 +228,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 float scaling_factor = object_radius * 10000.0; diff --git a/src/shaders/mengerSponge.glsl b/src/shaders/mengerSponge.glsl index 6682f81c1..2cd887e69 100644 --- a/src/shaders/mengerSponge.glsl +++ b/src/shaders/mengerSponge.glsl @@ -88,13 +88,15 @@ float distanceEstimator( in vec3 p ) { 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))) { gl_FragColor = screenColor; diff --git a/src/shaders/metalSectionMaterial/fragment.glsl b/src/shaders/metalSectionMaterial/fragment.glsl index 72f7e87d7..89082fb19 100644 --- a/src/shaders/metalSectionMaterial/fragment.glsl +++ b/src/shaders/metalSectionMaterial/fragment.glsl @@ -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; @@ -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); } \ No newline at end of file diff --git a/src/shaders/metalSectionMaterial/vertex.glsl b/src/shaders/metalSectionMaterial/vertex.glsl index af1c0ed43..01598da07 100644 --- a/src/shaders/metalSectionMaterial/vertex.glsl +++ b/src/shaders/metalSectionMaterial/vertex.glsl @@ -22,7 +22,6 @@ attribute vec3 normal; attribute vec2 uv; uniform mat4 world; -uniform mat4 view; uniform mat4 worldViewProjection; varying vec3 vPositionW; diff --git a/src/shaders/oceanFragment.glsl b/src/shaders/oceanFragment.glsl index 15ec2b5e4..0f74f45bc 100644 --- a/src/shaders/oceanFragment.glsl +++ b/src/shaders/oceanFragment.glsl @@ -43,8 +43,6 @@ uniform float ocean_waveBlendingSharpness; uniform float time; -#include "./utils/remap.glsl"; - #include "./utils/worldFromUV.glsl"; #include "./utils/rayIntersectSphere.glsl"; @@ -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; @@ -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); diff --git a/src/shaders/ringsFragment.glsl b/src/shaders/ringsFragment.glsl index 0e1cc5cc2..4805cd648 100644 --- a/src/shaders/ringsFragment.glsl +++ b/src/shaders/ringsFragment.glsl @@ -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; diff --git a/src/shaders/shadowFragment.glsl b/src/shaders/shadowFragment.glsl index 47da19aa4..b261929f5 100644 --- a/src/shaders/shadowFragment.glsl +++ b/src/shaders/shadowFragment.glsl @@ -88,12 +88,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; diff --git a/src/shaders/sierpinski.glsl b/src/shaders/sierpinski.glsl index f0fbdd1b5..163194257 100644 --- a/src/shaders/sierpinski.glsl +++ b/src/shaders/sierpinski.glsl @@ -32,8 +32,6 @@ uniform sampler2D depthSampler; #include "./utils/camera.glsl"; -#include "./utils/remap.glsl"; - #include "./utils/worldFromUV.glsl"; #include "./utils/rayIntersectSphere.glsl"; @@ -78,13 +76,15 @@ float distanceEstimator(vec3 z) { 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))) { gl_FragColor = screenColor; diff --git a/src/shaders/solarPanelMaterial/fragment.glsl b/src/shaders/solarPanelMaterial/fragment.glsl index 6c60e87d6..4e61e0278 100644 --- a/src/shaders/solarPanelMaterial/fragment.glsl +++ b/src/shaders/solarPanelMaterial/fragment.glsl @@ -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"; diff --git a/src/shaders/solarPanelMaterial/vertex.glsl b/src/shaders/solarPanelMaterial/vertex.glsl index 0e5a45e37..510df1d2a 100644 --- a/src/shaders/solarPanelMaterial/vertex.glsl +++ b/src/shaders/solarPanelMaterial/vertex.glsl @@ -21,7 +21,6 @@ attribute vec3 position; attribute vec3 normal; uniform mat4 world; -uniform mat4 view; uniform mat4 worldViewProjection; varying vec3 vPositionW; @@ -29,9 +28,9 @@ 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); } \ No newline at end of file diff --git a/src/shaders/utils/camera.glsl b/src/shaders/utils/camera.glsl index ff592dc8f..276f1ce4d 100644 --- a/src/shaders/utils/camera.glsl +++ b/src/shaders/utils/camera.glsl @@ -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; \ No newline at end of file diff --git a/src/shaders/utils/worldFromUV.glsl b/src/shaders/utils/worldFromUV.glsl index 0c0ac33af..dd55fb4fa 100644 --- a/src/shaders/utils/worldFromUV.glsl +++ b/src/shaders/utils/worldFromUV.glsl @@ -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; } diff --git a/src/shaders/volumetricCloudsFragment.glsl b/src/shaders/volumetricCloudsFragment.glsl index b4863bd66..a8eadb8c2 100644 --- a/src/shaders/volumetricCloudsFragment.glsl +++ b/src/shaders/volumetricCloudsFragment.glsl @@ -36,8 +36,6 @@ uniform sampler2D depthSampler;// the depth map of the camera uniform float cloudLayerMaxHeight;// atmosphere radius (calculate from planet center) uniform float cloudLayerMinHeight; -#include "./utils/remap.glsl"; - #include "./utils/saturate.glsl"; #include "./utils/noise.glsl"; @@ -175,12 +173,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 vec3 finalColor = scatter(screenColor, camera_position, rayDir, maximumDistance);// the color to be displayed on the screen diff --git a/src/ts/anomalies/julia/juliaSetModelGenerator.ts b/src/ts/anomalies/julia/juliaSetModelGenerator.ts index 151960b4f..24a260e55 100644 --- a/src/ts/anomalies/julia/juliaSetModelGenerator.ts +++ b/src/ts/anomalies/julia/juliaSetModelGenerator.ts @@ -37,7 +37,7 @@ export function newSeededJuliaSetModel(seed: number, name: string): JuliaSetMode ); // Todo: do not hardcode - const orbitRadius = rng(GenerationSteps.ORBIT) * 15e9; + const orbitRadius = rng(GenerationSteps.ORBIT) * 90e9; const orbitalP = clamp(0.5, 3.0, normalRandom(1.0, 0.3, rng, GenerationSteps.ORBIT + 80)); diff --git a/src/ts/anomalies/mandelbox/mandelboxModelGenerator.ts b/src/ts/anomalies/mandelbox/mandelboxModelGenerator.ts index 60f48d989..31a1834b6 100644 --- a/src/ts/anomalies/mandelbox/mandelboxModelGenerator.ts +++ b/src/ts/anomalies/mandelbox/mandelboxModelGenerator.ts @@ -39,7 +39,7 @@ export function newSeededMandelboxModel(seed: number, name: string): MandelboxMo ); // Todo: do not hardcode - const orbitRadius = rng(GenerationSteps.ORBIT) * 15e9; + const orbitRadius = rng(GenerationSteps.ORBIT) * 90e9; const orbitalP = clamp(0.5, 3.0, normalRandom(1.0, 0.3, rng, GenerationSteps.ORBIT + 80)); diff --git a/src/ts/anomalies/mandelbulb/mandelbulbModelGenerator.ts b/src/ts/anomalies/mandelbulb/mandelbulbModelGenerator.ts index 071a6f667..9d0e18b95 100644 --- a/src/ts/anomalies/mandelbulb/mandelbulbModelGenerator.ts +++ b/src/ts/anomalies/mandelbulb/mandelbulbModelGenerator.ts @@ -38,7 +38,7 @@ export function newSeededMandelbulbModel(seed: number, name: string): Mandelbulb ); // Todo: do not hardcode - const orbitRadius = rng(GenerationSteps.ORBIT) * 15e9; + const orbitRadius = rng(GenerationSteps.ORBIT) * 90e9; const orbitalP = clamp(0.5, 3.0, normalRandom(1.0, 0.3, rng, GenerationSteps.ORBIT + 80)); diff --git a/src/ts/anomalies/mengerSponge/mengerSpongeModelGenerator.ts b/src/ts/anomalies/mengerSponge/mengerSpongeModelGenerator.ts index a9d7ed455..65865b16a 100644 --- a/src/ts/anomalies/mengerSponge/mengerSpongeModelGenerator.ts +++ b/src/ts/anomalies/mengerSponge/mengerSpongeModelGenerator.ts @@ -37,7 +37,7 @@ export function newSeededMengerSpongeModel(seed: number, name: string): MengerSp ); // Todo: do not hardcode - const orbitRadius = rng(GenerationSteps.ORBIT) * 15e9; + const orbitRadius = rng(GenerationSteps.ORBIT) * 90e9; const orbitalP = clamp(0.5, 3.0, normalRandom(1.0, 0.3, rng, GenerationSteps.ORBIT + 80)); diff --git a/src/ts/anomalies/sierpinskiPyramid/sierpinskiPyramidModelGenerator.ts b/src/ts/anomalies/sierpinskiPyramid/sierpinskiPyramidModelGenerator.ts index b712dcb4c..2a4f4bf83 100644 --- a/src/ts/anomalies/sierpinskiPyramid/sierpinskiPyramidModelGenerator.ts +++ b/src/ts/anomalies/sierpinskiPyramid/sierpinskiPyramidModelGenerator.ts @@ -37,7 +37,7 @@ export function newSeededSierpinskiPyramidModel(seed: number, name: string): Sie ); // Todo: do not hardcode - const orbitRadius = rng(GenerationSteps.ORBIT) * 15e9; + const orbitRadius = rng(GenerationSteps.ORBIT) * 90e9; const orbitalP = clamp(0.5, 3.0, normalRandom(1.0, 0.3, rng, GenerationSteps.ORBIT + 80)); diff --git a/src/ts/cosmosJourneyer.ts b/src/ts/cosmosJourneyer.ts index c74d05ea7..75d2b05b4 100644 --- a/src/ts/cosmosJourneyer.ts +++ b/src/ts/cosmosJourneyer.ts @@ -17,7 +17,6 @@ import projectInfo from "../../package.json"; -import { Engine } from "@babylonjs/core/Engines/engine"; import { Tools } from "@babylonjs/core/Misc/tools"; import { VideoRecorder } from "@babylonjs/core/Misc/videoRecorder"; import "@babylonjs/core/Misc/screenshotTools"; @@ -29,7 +28,6 @@ import HavokPhysics from "@babylonjs/havok"; import "@babylonjs/core/Engines/WebGPU/Extensions/"; import { PauseMenu } from "./ui/pauseMenu"; import { StarSystemView } from "./starSystem/starSystemView"; -import { EngineFactory } from "@babylonjs/core/Engines/engineFactory"; import { MainMenu } from "./ui/mainMenu"; import { getSavesFromLocalStorage, SaveFileData, writeSavesToLocalStorage } from "./saveFile/saveFileData"; import { Vector3 } from "@babylonjs/core/Maths/math.vector"; @@ -62,6 +60,7 @@ import { EncyclopaediaGalacticaLocal } from "./society/encyclopaediaGalacticaLoc import { MusicConductor } from "./audio/musicConductor"; import { StarSystemDatabase } from "./starSystem/starSystemDatabase"; import { registerCustomSystems } from "./starSystem/customSystems/registerCustomSystems"; +import { WebGPUEngine } from "@babylonjs/core/Engines/webgpuEngine"; const enum EngineState { UNINITIALIZED, @@ -351,20 +350,23 @@ export class CosmosJourneyer { canvas.width = window.innerWidth; canvas.height = window.innerHeight; - // Init BabylonJS engine (use webgpu if ?webgpu is in the url) - const engine = window.location.search.includes("webgpu") - ? await EngineFactory.CreateAsync(canvas, { - twgslOptions: { - wasmPath: new URL("./utils/TWGSL/twgsl.wasm", import.meta.url).href, - jsPath: new URL("./utils/TWGSL/twgsl.js", import.meta.url).href - } - }) - : new Engine(canvas, true, { - // the preserveDrawingBuffer option is required for the screenshot feature to work - preserveDrawingBuffer: true, - useHighPrecisionMatrix: true, - doNotHandleContextLost: true - }); + if (!(await WebGPUEngine.IsSupportedAsync)) { + alert( + "WebGPU is not supported in your browser. Please check the compatibility here: https://github.com/gpuweb/gpuweb/wiki/Implementation-Status#implementation-status" + ); + } + + // Init BabylonJS engine + const engine = new WebGPUEngine(canvas, { + antialias: true, + useHighPrecisionMatrix: true, + doNotHandleContextLost: true, + audioEngine: true + }); + await engine.initAsync(undefined, { + wasmPath: new URL("./utils/TWGSL/twgsl.wasm", import.meta.url).href, + jsPath: new URL("./utils/TWGSL/twgsl.js", import.meta.url).href + }); engine.useReverseDepthBuffer = true; engine.loadingScreen = new LoadingScreen(canvas); diff --git a/src/ts/planets/gasPlanet/gasPlanetModelGenerator.ts b/src/ts/planets/gasPlanet/gasPlanetModelGenerator.ts index 61291faf0..ab6a0ea76 100644 --- a/src/ts/planets/gasPlanet/gasPlanetModelGenerator.ts +++ b/src/ts/planets/gasPlanet/gasPlanetModelGenerator.ts @@ -36,7 +36,7 @@ export function newSeededGasPlanetModel( const radius = randRangeInt(Settings.EARTH_RADIUS * 4, Settings.EARTH_RADIUS * 20, rng, GenerationSteps.RADIUS); // Todo: do not hardcode - let orbitRadius = rng(GenerationSteps.ORBIT) * 15e9; + let orbitRadius = rng(GenerationSteps.ORBIT) * 90e9; let parentAverageInclination = 0; let parentAverageAxialTilt = 0; diff --git a/src/ts/planets/telluricPlanet/telluricPlanetModelGenerator.ts b/src/ts/planets/telluricPlanet/telluricPlanetModelGenerator.ts index 8e6031a40..30c5f4231 100644 --- a/src/ts/planets/telluricPlanet/telluricPlanetModelGenerator.ts +++ b/src/ts/planets/telluricPlanet/telluricPlanetModelGenerator.ts @@ -87,7 +87,7 @@ export function newSeededTelluricPlanetModel( const parentMaxRadius = parentBodies.reduce((max, body) => Math.max(max, body.radius), 0); // Todo: do not hardcode - const orbitRadius = 2e9 + rng(GenerationSteps.ORBIT) * 15e9 + parentMaxRadius * 1.5; + const orbitRadius = 2e9 + rng(GenerationSteps.ORBIT) * 90e9 + parentMaxRadius * 1.5; let parentAverageInclination = 0; let parentAverageAxialTilt = 0; diff --git a/src/ts/playground.ts b/src/ts/playground.ts index 5cb22863e..3090bdf25 100644 --- a/src/ts/playground.ts +++ b/src/ts/playground.ts @@ -17,12 +17,7 @@ import "../styles/index.scss"; -import { Engine } from "@babylonjs/core/Engines/engine"; -import "@babylonjs/core/Materials/standardMaterial"; -import "@babylonjs/core/Loading/loadingScreen"; -import "@babylonjs/core/Misc/screenshotTools"; -import "@babylonjs/core/Meshes/thinInstanceMesh"; -import { Scene, Tools } from "@babylonjs/core"; +import { Scene, Tools, WebGPUEngine } from "@babylonjs/core"; import { createOrbitalDemoScene } from "./playgrounds/orbitalDemo"; import { createAutomaticLandingScene } from "./playgrounds/automaticLanding"; import { createHyperspaceTunnelDemo } from "./playgrounds/hyperspaceTunnel"; @@ -36,7 +31,18 @@ const canvas = document.getElementById("renderer") as HTMLCanvasElement; canvas.width = window.innerWidth; canvas.height = window.innerHeight; -const engine = new Engine(canvas, true); +const engine = new WebGPUEngine(canvas, { + antialias: true, + audioEngine: true, + useHighPrecisionMatrix: true, + doNotHandleContextLost: true +}); + +await engine.initAsync(undefined, { + wasmPath: new URL("./utils/TWGSL/twgsl.wasm", import.meta.url).href, + jsPath: new URL("./utils/TWGSL/twgsl.js", import.meta.url).href +}); + engine.useReverseDepthBuffer = true; engine.displayLoadingUI(); diff --git a/src/ts/playgrounds/hyperspaceTunnel.ts b/src/ts/playgrounds/hyperspaceTunnel.ts index 07aaa6528..053b5bcbe 100644 --- a/src/ts/playgrounds/hyperspaceTunnel.ts +++ b/src/ts/playgrounds/hyperspaceTunnel.ts @@ -36,8 +36,6 @@ export async function createHyperspaceTunnelDemo(engine: AbstractEngine) { const camera = defaultControls.getActiveCamera(); camera.attachControl(); - scene.enableDepthRenderer(camera, false, true); - await Assets.Init(scene); const directionalLight = new DirectionalLight("sun", new Vector3(1, -1, 0), scene); diff --git a/src/ts/playgrounds/neutronStar.ts b/src/ts/playgrounds/neutronStar.ts index 8aabd4086..ca9e67839 100644 --- a/src/ts/playgrounds/neutronStar.ts +++ b/src/ts/playgrounds/neutronStar.ts @@ -26,7 +26,7 @@ import { MatterJetPostProcess } from "../postProcesses/matterJetPostProcess"; import { VolumetricLight } from "../volumetricLight/volumetricLight"; import { translate } from "../uberCore/transforms/basicTransform"; import { Textures } from "../assets/textures"; -import { AssetsManager, Axis } from "@babylonjs/core"; +import { AssetsManager, Axis, Color4 } from "@babylonjs/core"; import { LensFlarePostProcess } from "../postProcesses/lensFlarePostProcess"; import { getRgbFromTemperature } from "../utils/specrend"; @@ -45,10 +45,12 @@ export async function createNeutronStarScene(engine: AbstractEngine): Promise { camera.wheelPrecision *= 100; camera.minZ = 0.01; - const depthRenderer = scene.enableDepthRenderer(null, false, true); + const depthRenderer = scene.enableDepthRenderer(null, true, true); + depthRenderer.clearColor = new Color4(0, 0, 0, 1); function createMandelbulb(): TransformNode { const mandelBulbModel = newSeededMandelbulbModel(Math.random() * 100_000, "XR Anomaly"); diff --git a/src/ts/postProcesses/uniforms/cameraUniforms.ts b/src/ts/postProcesses/uniforms/cameraUniforms.ts index fe03b091e..a026ad14e 100644 --- a/src/ts/postProcesses/uniforms/cameraUniforms.ts +++ b/src/ts/postProcesses/uniforms/cameraUniforms.ts @@ -21,20 +21,22 @@ import { Camera } from "@babylonjs/core/Cameras/camera"; export const CameraUniformNames = { CAMERA_POSITION: "camera_position", CAMERA_PROJECTION: "camera_projection", - CAMERA_INVERSE_PROJECTION: "camera_inverseProjection", CAMERA_VIEW: "camera_view", - CAMERA_INVERSE_VIEW: "camera_inverseView", + CAMERA_INVERSE_PROJECTION_VIEW: "camera_inverseProjectionView", CAMERA_NEAR: "camera_near", CAMERA_FAR: "camera_far", CAMERA_FOV: "camera_fov" }; export function setCameraUniforms(effect: Effect, camera: Camera): void { + const projection = camera.getProjectionMatrix(); + const view = camera.getViewMatrix(); + effect.setVector3(CameraUniformNames.CAMERA_POSITION, camera.globalPosition); - effect.setMatrix(CameraUniformNames.CAMERA_PROJECTION, camera.getProjectionMatrix()); - effect.setMatrix(CameraUniformNames.CAMERA_INVERSE_PROJECTION, camera.getProjectionMatrix().clone().invert()); + effect.setMatrix(CameraUniformNames.CAMERA_PROJECTION, projection); effect.setMatrix(CameraUniformNames.CAMERA_VIEW, camera.getViewMatrix()); - effect.setMatrix(CameraUniformNames.CAMERA_INVERSE_VIEW, camera.getViewMatrix().clone().invert()); + effect.setMatrix(CameraUniformNames.CAMERA_INVERSE_PROJECTION_VIEW, view.multiply(projection).clone().invert()); + effect.setFloat(CameraUniformNames.CAMERA_NEAR, camera.minZ); effect.setFloat(CameraUniformNames.CAMERA_FAR, camera.maxZ); effect.setFloat(CameraUniformNames.CAMERA_FOV, camera.fov); diff --git a/src/ts/settings.ts b/src/ts/settings.ts index 7003001cd..28a9e8392 100644 --- a/src/ts/settings.ts +++ b/src/ts/settings.ts @@ -15,16 +15,14 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -import { makeNoise3D } from "fast-simplex-noise"; import { Tools } from "@babylonjs/core/Misc/tools"; -import { getRngFromSeed } from "./utils/getRngFromSeed"; export const Settings = { UNIVERSE_SEED: Math.PI, POWER_PLAY_SEED: 77, - EARTH_RADIUS: 1000e3, // target is 6000e3 + EARTH_RADIUS: 6000e3, // target is 6000e3 EARTH_MASS: 5.972e24, diff --git a/src/ts/starSystem/starSystemView.ts b/src/ts/starSystem/starSystemView.ts index c48ad9394..1af860810 100644 --- a/src/ts/starSystem/starSystemView.ts +++ b/src/ts/starSystem/starSystemView.ts @@ -656,12 +656,13 @@ export class StarSystemView implements View { public resetPlayer() { this.postProcessManager.reset(); - const maxZ = Settings.EARTH_RADIUS * 1e5; + const maxZ = 0; // infinite far plane mode (see https://forum.babylonjs.com/t/how-to-show-objects-20km-away/25462/6) if (this.defaultControls === null) { this.defaultControls = new DefaultControls(this.scene); this.defaultControls.speed = 0.2 * Settings.EARTH_RADIUS; this.defaultControls.getCameras().forEach((camera) => (camera.maxZ = maxZ)); + this.defaultControls.getCameras().forEach((camera) => (camera.minZ = 0.1)); } const spaceshipSerialized = this.player.serializedSpaceships.shift(); @@ -673,6 +674,7 @@ export class StarSystemView implements View { if (this.spaceshipControls === null) { this.spaceshipControls = new ShipControls(spaceship, this.scene); this.spaceshipControls.getCameras().forEach((camera) => (camera.maxZ = maxZ)); + this.spaceshipControls.getCameras().forEach((camera) => (camera.minZ = 0.1)); } else { const oldSpaceship = this.spaceshipControls.getSpaceship(); this.spaceshipControls.reset(); @@ -684,6 +686,7 @@ export class StarSystemView implements View { this.characterControls = new CharacterControls(this.scene); this.characterControls.getTransform().setEnabled(false); this.characterControls.getCameras().forEach((camera) => (camera.maxZ = maxZ)); + this.characterControls.getCameras().forEach((camera) => (camera.minZ = 0.1)); } this.scene.setActiveControls(this.spaceshipControls); diff --git a/src/ts/uberCore/uberScene.ts b/src/ts/uberCore/uberScene.ts index 61ae9f502..df2a5eefe 100644 --- a/src/ts/uberCore/uberScene.ts +++ b/src/ts/uberCore/uberScene.ts @@ -46,7 +46,8 @@ export class UberScene extends Scene { this.onNewCameraAddedObservable.add((camera) => { if (this.depthRenderer === null) { - this.depthRenderer = this.enableDepthRenderer(camera, false, true); + this.depthRenderer = this.enableDepthRenderer(camera, true, true); + this.depthRenderer.clearColor = new Color4(0, 0, 0, 1); } });