Skip to content

Commit

Permalink
Merge branch 'experiments'
Browse files Browse the repository at this point in the history
  • Loading branch information
LGhassen committed Jan 31, 2021
2 parents 23d7ac2 + 29aabf2 commit 816e24c
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 67 deletions.
96 changes: 88 additions & 8 deletions Assets/Shaders/EVEUtils.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
28 changes: 9 additions & 19 deletions Assets/Shaders/ScreenSpaceCloudShadow.shader
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
21 changes: 6 additions & 15 deletions Assets/Shaders/ScreenSpaceSpherePlanetLighting.shader
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 15 additions & 5 deletions Assets/Shaders/SphereCloud.shader
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -238,4 +248,4 @@ Shader "EVE/Cloud" {
}

}
}
}
16 changes: 15 additions & 1 deletion Atmosphere/CloudsPQS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions Atmosphere/DeferredVolumetricCloudsRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ public class DeferredRendererNotifier : MonoBehaviour
public Material mat;

public DeferredRendererNotifier()
{

}

public void Init()
{
mr = gameObject.GetComponent<MeshRenderer>();
}
Expand Down
1 change: 1 addition & 0 deletions Atmosphere/VolumeSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public CloudMesh(Material cloudParticleMaterial, Vector2 size, Transform parent,
mr.sharedMaterial = new Material(InvisibleShader);
DeferredRendererNotifier notifier = cloudMesh.AddComponent<DeferredRendererNotifier>();
notifier.mat = cloudParticleMaterial;
notifier.Init();
}
else
{
Expand Down
29 changes: 19 additions & 10 deletions CelestialShadows/ShadowObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -25,7 +25,7 @@
},
"KSP_VERSION_MAX": {
"MAJOR": 1,
"MINOR": 10,
"MINOR": 11,
"PATCH": 99
}
}
2 changes: 1 addition & 1 deletion _BuildManager/Properties/AssemblyVersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 816e24c

Please sign in to comment.