diff --git a/Assets/Shaders/EVEUtils.cginc b/Assets/Shaders/EVEUtils.cginc index d38280e1..241d3ebc 100644 --- a/Assets/Shaders/EVEUtils.cginc +++ b/Assets/Shaders/EVEUtils.cginc @@ -164,9 +164,14 @@ return terminator; } + //V is worldVertex + //R is radius of the sun + //r is radius of body + //P is sunPos + //p is pos of body + //Original EVE eclipse function, looks less natural than EclipseShadow below inline half BodyShadow(float3 v, float R, float r, float3 P, float3 p) - { - + { float3 D = P - v; float a = PI*(r*r); @@ -185,12 +190,87 @@ return lerp(1, saturate((A - (s*a)) / A), step(r, tc)*saturate(a)); } - inline half MultiBodyShadow(float3 v, float R, float3 P, float4x4 m) + //Same eclipse function from scatterer + half EclipseShadow(float3 worldPos, float lightSourceRadius, float occluderSphereRadius, float3 worldLightPos,float3 occluderSpherePosition) + { + float3 lightDirection = float3(worldLightPos - worldPos); + float3 lightDistance = length(lightDirection); + lightDirection = lightDirection / lightDistance; + + // computation of level of shadowing w + float3 sphereDirection = float3(occluderSpherePosition - worldPos); //occluder planet + float sphereDistance = length(sphereDirection); + sphereDirection = sphereDirection / sphereDistance; + + float dd = lightDistance * (asin(min(1.0, length(cross(lightDirection, sphereDirection)))) + - asin(min(1.0, occluderSphereRadius / sphereDistance))); + + float w = smoothstep(-1.0, 1.0, -dd / lightSourceRadius); + w = w * smoothstep(0.0, 0.2, dot(lightDirection, sphereDirection)); + + return (1-w); + } + + inline half MultiBodyShadow(float3 worldPos, float sunRadius, float3 sunPos, float4x4 m) { - half a = BodyShadow(v, R, m[0].w, P, m[0].xyz); - half b = BodyShadow(v, R, m[1].w, P, m[1].xyz); - half c = BodyShadow(v, R, m[2].w, P, m[2].xyz); - half d = BodyShadow(v, R, m[3].w, P, m[3].xyz); - return min(min(a, b), min(c, d)); + half shadowTerm = 1.0; + + for (int i=0; i<4;i++) + { + if (m[i].w == 0.0) break; + + shadowTerm*= EclipseShadow(worldPos, sunRadius, m[i].w, sunPos, m[i].xyz); + } + + return shadowTerm; + } + + //hybrid method using the depth from the ray method and the direction from the invprojection method + //from scatterer + float3 getPreciseWorldPosFromDepth(float2 uv, float zdepth, float4x4 CameraToWorld) + { + float depth = Linear01Depth(zdepth); + + #ifdef SHADER_API_D3D11 + zdepth = 1 - zdepth; + #endif + + float4 clipPos = float4(uv, zdepth, 1.0); + clipPos.xyz = 2.0f * clipPos.xyz - 1.0f; + float4 camPos = mul(unity_CameraInvProjection, clipPos); + camPos.xyz /= camPos.w; + + float3 rayDirection = normalize(camPos.xyz); + + float3 cameraForwardDir = float3(0,0,-1); + float aa = dot(rayDirection, cameraForwardDir); + + camPos.xyz = rayDirection * depth/aa * _ProjectionParams.z; + + float4 worldPos = mul(CameraToWorld,float4(camPos.xyz,1.0)); + return (worldPos.xyz/worldPos.w); + } + + float3 getPreciseViewPosFromDepth(float2 uv, float zdepth) + { + float depth = Linear01Depth(zdepth); + + #ifdef SHADER_API_D3D11 + zdepth = 1 - zdepth; + #endif + + float4 clipPos = float4(uv, zdepth, 1.0); + clipPos.xyz = 2.0f * clipPos.xyz - 1.0f; + float4 camPos = mul(unity_CameraInvProjection, clipPos); + camPos.xyz /= camPos.w; + + float3 rayDirection = normalize(camPos.xyz); + + float3 cameraForwardDir = float3(0,0,-1); + float aa = dot(rayDirection, cameraForwardDir); + + camPos.xyz = rayDirection * depth/aa * _ProjectionParams.z; + + return camPos.xyz; } #endif \ No newline at end of file diff --git a/Assets/Shaders/ScreenSpaceCloudShadow.shader b/Assets/Shaders/ScreenSpaceCloudShadow.shader index 65de5dcb..719432a1 100644 --- a/Assets/Shaders/ScreenSpaceCloudShadow.shader +++ b/Assets/Shaders/ScreenSpaceCloudShadow.shader @@ -92,19 +92,15 @@ Shader "EVE/ScreenSpaceCloudShadow" { { float zdepth = tex2Dlod(_CameraDepthTexture, float4(IN.uv,0,0)); + #if SHADER_API_D3D11 + if (zdepth == 0.0) {discard;} + #else + if (zdepth == 1.0) {discard;} + #endif -#ifdef SHADER_API_D3D11 //#if defined(UNITY_REVERSED_Z) - zdepth = 1 - zdepth; -#endif - - float4 clipPos = float4(IN.uv, zdepth, 1.0); - clipPos.xyz = 2.0f * clipPos.xyz - 1.0f; - float4 camPos = mul(unity_CameraInvProjection, clipPos); - - float4 worldPos = mul(CameraToWorld,camPos); - worldPos/=worldPos.w; + float3 worldPos = getPreciseWorldPosFromDepth(IN.uv, zdepth, CameraToWorld); - float4 vertexPos = worldPos; + float4 vertexPos = float4(worldPos,1.0); float3 worldOrigin = _PlanetOrigin; float3 L = worldOrigin - vertexPos.xyz; @@ -133,20 +129,14 @@ Shader "EVE/ScreenSpaceCloudShadow" { half4 detail = GetCubeDetailMap(_DetailTex, detailPos, _DetailScale); - float viewDist = distance(worldPos.xyz,_WorldSpaceCameraPos); + float viewDist = distance(worldPos,_WorldSpaceCameraPos); half detailLevel = saturate(2 * _DetailDist*viewDist); fixed4 color = _Color * main.rgba * lerp(detail.rgba, 1, detailLevel); color.rgb = saturate(color.rgb * (1- color.a)); color.rgb = lerp(1, color.rgb, _ShadowFactor*color.a); - float fadeout = 1.0; -#ifdef SHADER_API_D3D11 - float camDistance = length(camPos.xyz/camPos.w); - fadeout = 1.0 - smoothstep (40000.0, 60000.0, camDistance); //fade out the shadows to hide artifacts from insufficient depth precision in dx11 -#else - fadeout = (zdepth == 1.0) ? 0.0 : 1.0; //don't render anything at or near clipping planes on ogl since we have 2 cameras -#endif + float fadeout = clamp(0.01 * (sphereRadius - originDist), 0.0, 1.0); return lerp(1, color, shadowCheck*fadeout); } diff --git a/Assets/Shaders/ScreenSpaceSpherePlanetLighting.shader b/Assets/Shaders/ScreenSpaceSpherePlanetLighting.shader index e982373c..63109fb1 100644 --- a/Assets/Shaders/ScreenSpaceSpherePlanetLighting.shader +++ b/Assets/Shaders/ScreenSpaceSpherePlanetLighting.shader @@ -80,25 +80,16 @@ Shader "EVE/ScreenSpacePlanetLight" { float zdepth = tex2Dlod(_CameraDepthTexture, float4(IN.uv,0,0)); + #if SHADER_API_D3D11 + if (zdepth == 0.0) {discard;} + #else + if (zdepth == 1.0) {discard;} + #endif -#ifdef SHADER_API_D3D11 //#if defined(UNITY_REVERSED_Z) - zdepth = 1 - zdepth; -#endif - - float4 clipPos = float4(IN.uv, zdepth, 1.0); - clipPos.xyz = 2.0f * clipPos.xyz - 1.0f; - float4 camPos = mul(unity_CameraInvProjection, clipPos); - - float4 worldPos = mul(CameraToWorld,camPos); - worldPos/=worldPos.w; + float3 worldPos = getPreciseWorldPosFromDepth(IN.uv, zdepth, CameraToWorld); color.rgb = MultiBodyShadow(worldPos.xyz, _SunRadius, _SunPos, _ShadowBodies); - - float fadeout = (zdepth == 1.0) ? 0.0 : 1.0; //don't render anything at or near clipping planes - - color.rgb = lerp(1.0,color.rgb,fadeout); - return color; } ENDCG diff --git a/Assets/Shaders/SphereCloud.shader b/Assets/Shaders/SphereCloud.shader index 284d833c..cc868970 100644 --- a/Assets/Shaders/SphereCloud.shader +++ b/Assets/Shaders/SphereCloud.shader @@ -187,7 +187,6 @@ Shader "EVE/Cloud" { color.a = lerp(0, color.a, distAlpha); - #ifdef WORLD_SPACE_ON half3 worldDir = normalize(IN.worldVert - _WorldSpaceCameraPos.xyz); float tc = dot(IN.L, worldDir); @@ -210,10 +209,21 @@ Shader "EVE/Cloud" { scolor *= Terminator(normalize(_WorldSpaceLightPos0), worldNormal); scolor.a = transparency; #ifdef SOFT_DEPTH_ON - float depth = UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.projPos))); - depth = LinearEyeDepth(depth); float partZ = IN.projPos.z; - float fade = saturate(_InvFade * (depth - partZ)); + + float2 depthUV = IN.projPos.xy/IN.projPos.w; + float zdepth = tex2Dlod(_CameraDepthTexture, float4(depthUV,0,0)); + + float3 terrainViewPos = getPreciseViewPosFromDepth(depthUV, zdepth); + float terrainLength = length(terrainViewPos); + + float4 viewPos = mul(UNITY_MATRIX_V, float4(IN.worldVert,1.0)); + float viewLength = length(viewPos.xyz/viewPos.w); + + float fade = saturate(_InvFade * 0.1 * (terrainLength - viewLength)); + + fade = lerp(fade,1.0, smoothstep (50000.0, 100000.0, viewLength)); //fade out soft depth at large distances + scolor.a *= fade; #endif scolor.rgb *= MultiBodyShadow(IN.worldVert, _SunRadius, _SunPos, _ShadowBodies); @@ -238,4 +248,4 @@ Shader "EVE/Cloud" { } } -} +} \ No newline at end of file diff --git a/Atmosphere/CloudsPQS.cs b/Atmosphere/CloudsPQS.cs index 5cde1eb6..9c27a460 100644 --- a/Atmosphere/CloudsPQS.cs +++ b/Atmosphere/CloudsPQS.cs @@ -377,8 +377,22 @@ private void SceneLoaded(GameScenes scene) } } + if (scene == GameScenes.FLIGHT) + { + StartCoroutine(DelayedCheckForSphereInactive()); + } + } + + IEnumerator DelayedCheckForSphereInactive() + { + yield return new WaitForFixedUpdate(); + + if (!sphere.isActive && !layer2D.Scaled) + { + OnSphereInactive(); + } } - + public void Remove() { if (layer2D != null) diff --git a/Atmosphere/DeferredVolumetricCloudsRenderer.cs b/Atmosphere/DeferredVolumetricCloudsRenderer.cs index 54384c56..b8d1f782 100644 --- a/Atmosphere/DeferredVolumetricCloudsRenderer.cs +++ b/Atmosphere/DeferredVolumetricCloudsRenderer.cs @@ -186,6 +186,11 @@ public class DeferredRendererNotifier : MonoBehaviour public Material mat; public DeferredRendererNotifier() + { + + } + + public void Init() { mr = gameObject.GetComponent(); } diff --git a/Atmosphere/VolumeSection.cs b/Atmosphere/VolumeSection.cs index fe638597..7fc5cf8b 100644 --- a/Atmosphere/VolumeSection.cs +++ b/Atmosphere/VolumeSection.cs @@ -94,6 +94,7 @@ public CloudMesh(Material cloudParticleMaterial, Vector2 size, Transform parent, mr.sharedMaterial = new Material(InvisibleShader); DeferredRendererNotifier notifier = cloudMesh.AddComponent(); notifier.mat = cloudParticleMaterial; + notifier.Init(); } else { diff --git a/CelestialShadows/ShadowObject.cs b/CelestialShadows/ShadowObject.cs index 4bb8ed7a..9da0e0b0 100644 --- a/CelestialShadows/ShadowObject.cs +++ b/CelestialShadows/ShadowObject.cs @@ -30,13 +30,18 @@ internal void OnWillRenderObject() { if (HighLogic.LoadedScene != GameScenes.MAINMENU) { - Matrix4x4 bodies = new Matrix4x4(); + Matrix4x4 bodies = Matrix4x4.zero; int i = 0; foreach (CelestialBody cb in shadowList) { - bodies.SetRow(i, cb.scaledBody.transform.position); - bodies[i, 3] = (float)(ScaledSpace.InverseScaleFactor * cb.Radius); - i++; + if (cb != null && cb.transform != null) + { + bodies.SetRow(i, cb.scaledBody.transform.position); + bodies[i, 3] = (float)(ScaledSpace.InverseScaleFactor * cb.Radius); + i++; + if (i == 4) + break; + } } if (shadowMat != null) { @@ -80,16 +85,20 @@ internal void OnPreCull() { if (HighLogic.LoadedScene != GameScenes.MAINMENU && screenSpaceShadowGO != null && body.pqsController != null) { - Matrix4x4 bodies = new Matrix4x4(); + Matrix4x4 bodies = Matrix4x4.zero; int i = 0; foreach (CelestialBody cb in shadowList) { - bodies.SetRow(i, cb.transform.position); - bodies[i, 3] = (float)(cb.Radius); - i++; - if (i == 4) - break; + if (cb != null && cb.transform != null) + { + bodies.SetRow(i, cb.transform.position); + bodies[i, 3] = (float)(cb.Radius); + i++; + if (i == 4) + break; + } } + if (shadowMat != null) { shadowMat.SetVector(ShaderProperties._SunPos_PROPERTY, Sun.Instance.sun.transform.position); diff --git a/ContentEVE/GameData/EnvironmentalVisualEnhancements/EnvironmentalVisualEnhancements.version b/ContentEVE/GameData/EnvironmentalVisualEnhancements/EnvironmentalVisualEnhancements.version index 3857db50..5a438787 100644 --- a/ContentEVE/GameData/EnvironmentalVisualEnhancements/EnvironmentalVisualEnhancements.version +++ b/ContentEVE/GameData/EnvironmentalVisualEnhancements/EnvironmentalVisualEnhancements.version @@ -1,21 +1,21 @@ { - "NAME": "Environmental Visual Enhancements", - "URL": "https://github.com/R-T-B/EnvironmentalVisualEnhancements/raw/master/ContentEVE/GameData/EnvironmentalVisualEnhancements/EnvironmentalVisualEnhancements.version", - "DOWNLOAD" : "https://github.com/R-T-B/EnvironmentalVisualEnhancements/releases", + "NAME": "Environmental Visual Enhancements - Redux", + "URL": "https://github.com/LGhassen/EnvironmentalVisualEnhancements/raw/master/ContentEVE/GameData/EnvironmentalVisualEnhancements/EnvironmentalVisualEnhancements.version", + "DOWNLOAD" : "https://github.com/LGhassen/EnvironmentalVisualEnhancements/releases", "GITHUB": { - "USERNAME": "WazWaz", + "USERNAME": "LGhassen", "REPOSITORY": "EnvironmentalVisualEnhancements", "ALLOW_PRE_RELEASE": false }, "VERSION": { "MAJOR": 1, - "MINOR": 10, - "PATCH": 1, + "MINOR": 11, + "PATCH": 2, "BUILD": 1 }, "KSP_VERSION": { "MAJOR": 1, - "MINOR": 10, + "MINOR": 11, "PATCH": 1 }, "KSP_VERSION_MIN": { @@ -25,7 +25,7 @@ }, "KSP_VERSION_MAX": { "MAJOR": 1, - "MINOR": 10, + "MINOR": 11, "PATCH": 99 } } diff --git a/_BuildManager/Properties/AssemblyVersionInfo.cs b/_BuildManager/Properties/AssemblyVersionInfo.cs index 3bb12944..9164381c 100644 --- a/_BuildManager/Properties/AssemblyVersionInfo.cs +++ b/_BuildManager/Properties/AssemblyVersionInfo.cs @@ -13,4 +13,4 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.10.1.1")] // KSP version with EVE release number appended; see also CKAN version file. +[assembly: AssemblyVersion("1.11.2.1")] // KSP version with EVE release number appended; see also CKAN version file.