diff --git a/src/shaders/atmosphericScatteringFragment.glsl b/src/shaders/atmosphericScatteringFragment.glsl index 5a6a42647..de46efe4b 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 @@ -39,8 +39,6 @@ uniform sampler2D atmosphereLUT; #include "./utils/atmosphere.glsl"; -#include "./utils/remap.glsl"; - #include "./utils/worldFromUV.glsl"; #include "./utils/rayIntersectSphere.glsl"; @@ -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; 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 f8bd4600b..6a755c7af 100644 --- a/src/shaders/juliaSet.glsl +++ b/src/shaders/juliaSet.glsl @@ -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))) { diff --git a/src/shaders/lensflare.glsl b/src/shaders/lensflare.glsl index 33189cdcc..41f2afc73 100644 --- a/src/shaders/lensflare.glsl +++ b/src/shaders/lensflare.glsl @@ -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); diff --git a/src/shaders/mandelbulb.glsl b/src/shaders/mandelbulb.glsl index 7555a5a6c..b558661d9 100644 --- a/src/shaders/mandelbulb.glsl +++ b/src/shaders/mandelbulb.glsl @@ -34,8 +34,6 @@ uniform sampler2D depthSampler; #include "./utils/camera.glsl"; -#include "./utils/remap.glsl"; - #include "./utils/worldFromUV.glsl"; #include "./utils/rayIntersectSphere.glsl"; @@ -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))) { diff --git a/src/shaders/matterjet.glsl b/src/shaders/matterjet.glsl index 42b9ed115..d0f41cf88 100644 --- a/src/shaders/matterjet.glsl +++ b/src/shaders/matterjet.glsl @@ -30,8 +30,6 @@ uniform float time; #include "./utils/rotateAround.glsl"; -#include "./utils/remap.glsl"; - #include "./utils/worldFromUV.glsl"; #include "./utils/removeAxialTilt.glsl"; @@ -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; 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 fafe61e99..86c5e1882 100644 --- a/src/shaders/shadowFragment.glsl +++ b/src/shaders/shadowFragment.glsl @@ -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; diff --git a/src/shaders/sierpinski.glsl b/src/shaders/sierpinski.glsl index 800b07b06..cdaf398a7 100644 --- a/src/shaders/sierpinski.glsl +++ b/src/shaders/sierpinski.glsl @@ -36,8 +36,6 @@ uniform sampler2D depthSampler; #include "./utils/camera.glsl"; -#include "./utils/remap.glsl"; - #include "./utils/worldFromUV.glsl"; #include "./utils/rayIntersectSphere.glsl"; @@ -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))) { 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/blackHoleDemo.ts b/src/ts/blackHoleDemo.ts index 6d9afb062..6f483755b 100644 --- a/src/ts/blackHoleDemo.ts +++ b/src/ts/blackHoleDemo.ts @@ -34,7 +34,7 @@ const scene = starSystemView.scene; const blackHoleModel = newSeededBlackHoleModel(42, "Gargantua", []); const millerPlanetModel = newSeededTelluricPlanetModel(47, "Miller", [blackHoleModel]); -millerPlanetModel.orbit.radius = blackHoleModel.physics.accretionDiskRadius * 4; +millerPlanetModel.orbit.radius = blackHoleModel.physics.accretionDiskRadius * 10; millerPlanetModel.orbit.normalToPlane.x += 0.2; const starSystemModel: StarSystemModel = { diff --git a/src/ts/cosmosJourneyer.ts b/src/ts/cosmosJourneyer.ts index c6b456689..424d55327 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 { SaveFileData } from "./saveFile/saveFileData"; import { Vector3 } from "@babylonjs/core/Maths/math.vector"; @@ -45,6 +43,7 @@ import { GeneralInputs } from "./inputs/generalInputs"; import { createNotification } from "./utils/notification"; import { LoadingScreen } from "./uberCore/loadingScreen"; import i18n from "./i18n"; +import { WebGPUEngine } from "@babylonjs/core/Engines/webgpuEngine"; import { AbstractEngine } from "@babylonjs/core/Engines/abstractEngine"; import { Sounds } from "./assets/sounds"; import { TutorialLayer } from "./ui/tutorial/tutorialLayer"; @@ -193,20 +192,22 @@ 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, + 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.loadingScreen = new LoadingScreen(canvas); diff --git a/src/ts/planets/gasPlanet/gasPlanetModel.ts b/src/ts/planets/gasPlanet/gasPlanetModel.ts index 309351676..070ac6442 100644 --- a/src/ts/planets/gasPlanet/gasPlanetModel.ts +++ b/src/ts/planets/gasPlanet/gasPlanetModel.ts @@ -41,7 +41,7 @@ export function newSeededGasPlanetModel(seed: number, name: string, parentBodies 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 = 2e9 + rng(GenerationSteps.ORBIT) * 300e9; const orbitalP = clamp(0.7, 3.0, normalRandom(2.0, 0.3, rng, GenerationSteps.ORBIT + 80)); orbitRadius += orbitRadius - getPeriapsis(orbitRadius, orbitalP); diff --git a/src/ts/planets/telluricPlanet/telluricPlanetModel.ts b/src/ts/planets/telluricPlanet/telluricPlanetModel.ts index 0330f037e..69c1ff8f4 100644 --- a/src/ts/planets/telluricPlanet/telluricPlanetModel.ts +++ b/src/ts/planets/telluricPlanet/telluricPlanetModel.ts @@ -79,7 +79,7 @@ export function newSeededTelluricPlanetModel(seed: number, name: string, parentB 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 = 12e9 + rng(GenerationSteps.ORBIT) * 150e9 + parentMaxRadius * 1.5; const orbitalP = 2; //clamp(normalRandom(2.0, 0.3, this.rng, GenerationSteps.Orbit + 80), 0.7, 3.0); diff --git a/src/ts/postProcesses/postProcessManager.ts b/src/ts/postProcesses/postProcessManager.ts index af5b69041..6992855ce 100644 --- a/src/ts/postProcesses/postProcessManager.ts +++ b/src/ts/postProcesses/postProcessManager.ts @@ -442,6 +442,7 @@ export class PostProcessManager { const lensFlareRenderEffect = new PostProcessRenderEffect(this.engine, "LensFlareRenderEffect", () => this.lensFlares); this.renderingPipeline.addEffect(shadowRenderEffect); + this.renderingPipeline.addEffect(this.bloomRenderEffect); // other objects are viewed in their space configuration for (const postProcessType of spaceRenderingOrder) { @@ -524,7 +525,6 @@ export class PostProcessManager { this.renderingPipeline.addEffect(this.bloomRenderEffect); this.renderingPipeline.addEffect(lensFlareRenderEffect); this.renderingPipeline.addEffect(this.fxaaRenderEffect); - //this.renderingPipeline.addEffect(this.bloomRenderEffect); this.renderingPipeline.addEffect(this.colorCorrectionRenderEffect); this.renderingPipelineManager.addPipeline(this.renderingPipeline); 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 bf4379ad3..83420bac4 100644 --- a/src/ts/settings.ts +++ b/src/ts/settings.ts @@ -22,9 +22,9 @@ import { getRngFromSeed } from "./utils/getRngFromSeed"; export const Settings = { UNIVERSE_SEED: Math.PI, - POWER_PLAY_SEED: 77, + EARTH_RADIUS: 6000e3, // target is 6000e3 - EARTH_RADIUS: 1000e3, // target is 6000e3 + POWER_PLAY_SEED: 77, EARTH_MASS: 5.972e24, diff --git a/src/ts/spaceStationGenerator.ts b/src/ts/spaceStationGenerator.ts index 24d57c860..efd273d3c 100644 --- a/src/ts/spaceStationGenerator.ts +++ b/src/ts/spaceStationGenerator.ts @@ -17,7 +17,6 @@ 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"; @@ -34,6 +33,7 @@ import { HavokPlugin } from "@babylonjs/core/Physics/v2/Plugins/havokPlugin"; import { Star } from "./stellarObjects/star/star"; import { Settings } from "./settings"; import { StarFieldBox } from "./starSystem/starFieldBox"; +import { WebGPUEngine } from "@babylonjs/core/Engines/webgpuEngine"; import { newSeededStarModel } from "./stellarObjects/star/starModel"; import { newSeededSpaceStationModel } from "./spacestation/spacestationModel"; @@ -41,7 +41,8 @@ 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); +await engine.initAsync(); engine.useReverseDepthBuffer = true; engine.displayLoadingUI(); diff --git a/src/ts/starSystem/starSystemView.ts b/src/ts/starSystem/starSystemView.ts index 1beeede51..503bbb2f1 100644 --- a/src/ts/starSystem/starSystemView.ts +++ b/src/ts/starSystem/starSystemView.ts @@ -504,18 +504,21 @@ export class StarSystemView implements View { public async initAssets() { await Assets.Init(this.scene); - 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) this.defaultControls = new DefaultControls(this.scene); this.defaultControls.speed = 0.2 * Settings.EARTH_RADIUS; this.defaultControls.getActiveCameras().forEach((camera) => (camera.maxZ = maxZ)); + this.defaultControls.getActiveCameras().forEach((camera) => (camera.minZ = 0.1)); this.spaceshipControls = new ShipControls(this.scene); this.spaceshipControls.getActiveCameras().forEach((camera) => (camera.maxZ = maxZ)); + this.spaceshipControls.getActiveCameras().forEach((camera) => (camera.minZ = 0.1)); this.characterControls = new CharacterControls(this.scene); this.characterControls.getTransform().setEnabled(false); this.characterControls.getActiveCameras().forEach((camera) => (camera.maxZ = maxZ)); + this.characterControls.getActiveCameras().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 f30201122..689d81ca8 100644 --- a/src/ts/uberCore/uberScene.ts +++ b/src/ts/uberCore/uberScene.ts @@ -45,8 +45,10 @@ export class UberScene extends Scene { this.clearColor = new Color4(0, 0, 0, 0); this.onNewCameraAddedObservable.add((camera) => { - if (this.depthRenderer === null) { - this.depthRenderer = this.enableDepthRenderer(camera, false, true); + if(this.depthRenderer === null) { + this.depthRenderer = this.enableDepthRenderer(camera, true, true); + this.depthRenderer.getDepthMap().activeCamera = camera; + this.depthRenderer.clearColor = new Color4(0, 0, 0, 1); } });