Skip to content

Commit 1987d15

Browse files
committed
Big update
Terrain shadows and DDS support.
1 parent 8314557 commit 1987d15

File tree

9 files changed

+125
-37
lines changed

9 files changed

+125
-37
lines changed

GameData/KerbalVisualEnhancements/Settings.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ KERBAL_VISUAL_ENHANCEMENTS
44
volumeSegmentDiv = 3
55
recursionLevel = 4
66
resursionLevelBody = 6
7+
terrainShadows = True
78
}

Sources/Clouds/Clouds.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ protected void Update()
221221
useEditor = !useEditor;
222222
}
223223
}
224+
224225
if (HighLogic.LoadedScene == GameScenes.FLIGHT)
225226
{
226227
if (FlightGlobals.ActiveVessel != null && CloudLayer.BodyDatabase.ContainsKey(FlightGlobals.currentMainBody.name))
@@ -229,26 +230,27 @@ protected void Update()
229230
foreach (CloudLayer layer in CloudLayer.BodyDatabase[FlightGlobals.currentMainBody.name])
230231
{
231232
layer.UpdateParticleClouds(COM);
232-
layer.CloudMaterial.renderQueue = 3001;
233+
234+
layer.CloudMaterial.renderQueue = 3000;
235+
236+
if (!MapView.MapIsEnabled)
237+
layer.CloudMaterial.renderQueue = 3001;
233238
}
234239
}
235240
}
236241
else if (HighLogic.LoadedScene == GameScenes.SPACECENTER)
237242
{
238243
if (CloudLayer.BodyDatabase.ContainsKey(FlightGlobals.currentMainBody.name))
239244
{
240-
foreach (CloudLayer cl in CloudLayer.BodyDatabase[FlightGlobals.currentMainBody.name])
245+
foreach (CloudLayer layer in CloudLayer.BodyDatabase[FlightGlobals.currentMainBody.name])
241246
{
242-
cl.UpdateParticleClouds(GameObject.Find("KSC").transform.position);
243-
cl.CloudMaterial.renderQueue = 3001;
247+
layer.UpdateParticleClouds(GameObject.Find("KSC").transform.position);
248+
layer.CloudMaterial.renderQueue = 3001;
244249
}
245250
}
246251
}
247252
}
248-
catch
249-
{
250-
251-
}
253+
catch (NullReferenceException) { }
252254
}
253255

254256

Sources/Clouds/VolumeSection.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,17 @@ public void Update(Texture2D tex)
7373
Vector3 point = particle.transform.parent.parent.InverseTransformPoint(particle.transform.position).normalized;
7474
float u = (float)(.5 + (Mathf.Atan2(point.z, point.x) / (2f * Mathf.PI)));
7575
float v = Mathf.Acos(-point.y) / Mathf.PI;
76+
7677
Color color = tex.GetPixelBilinear(u, v);
78+
79+
if (color.a == 0)
80+
{
81+
particle.SetActive(false);
82+
return;
83+
}
84+
else
85+
particle.SetActive(true);
86+
7787
MeshFilter filter = particle.GetComponent<MeshFilter>();
7888
Mesh mesh = filter.mesh;
7989
mesh.colors = new Color[4]
@@ -87,6 +97,14 @@ public void Update(Texture2D tex)
8797

8898
internal void Update(Color color)
8999
{
100+
if (color.a == 0)
101+
{
102+
particle.SetActive(false);
103+
return;
104+
}
105+
else
106+
particle.SetActive(true);
107+
90108
MeshFilter filter = particle.GetComponent<MeshFilter>();
91109
Mesh mesh = filter.mesh;
92110
mesh.colors = new Color[4]

Sources/GlobalAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
[assembly: AssemblyProduct("Kerbal Visual Enhancements")]
2222
[assembly: AssemblyCopyright("Copyright © 2013-2016 Ryan Bray, RangeMachine")]
2323

24-
[assembly: AssemblyVersion("1.0.0.0")]
24+
[assembly: AssemblyVersion("1.1.0.0")]
2525

26-
[assembly: KSPAssembly("Kerbal Visual Enhancements", 1, 0)]
26+
[assembly: KSPAssembly("Kerbal Visual Enhancements", 1, 1)]

Sources/Utilities/GuiUtils.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,43 @@ private void initTexture()
203203
else if (!isBump && !TextureDictionary.ContainsKey(textureFile))
204204
{
205205
Texture2D tex = GameDatabase.Instance.GetTexture(textureFile, isBump);
206+
207+
//////////////////////////
208+
/// DDS BUG WORKAROUND ///
209+
//////////////////////////
210+
try
211+
{
212+
tex.GetPixel(0, 0);
213+
}
214+
catch (UnityException)
215+
{
216+
tex.filterMode = FilterMode.Point;
217+
218+
RenderTexture rt = RenderTexture.GetTemporary(tex.width, tex.height);
219+
rt.filterMode = FilterMode.Point;
220+
221+
RenderTexture.active = rt;
222+
Graphics.Blit(tex, rt);
223+
224+
Texture2D img2 = new Texture2D(tex.width, tex.height);
225+
img2.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
226+
img2.Apply();
227+
228+
RenderTexture.active = null;
229+
230+
tex = img2;
231+
}
232+
//////////////////////////
233+
206234
try { AddMipMaps(tex); }
207235
catch { }
236+
208237
if (tex.format != TextureFormat.DXT1 && tex.format != TextureFormat.DXT5)
209238
{
210239
try { tex.GetPixel(0, 0); tex.Compress(true); }
211240
catch { }
212241
}
242+
213243
TextureDictionary.Add(textureFile, tex);
214244
}
215245
string textureName = isBump ? textureFile + "_BUMP" : textureFile;

Sources/Utilities/OverlayMgr.cs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@ public static void LoadBundle()
8181
using (WWW www = new WWW("file://" + KSPUtil.ApplicationRootPath + "GameData/KerbalVisualEnhancements/Shaders/" + bundlePath))
8282
{
8383
Log("Bundle '" + bundlePath + "' loaded.");
84-
85-
Log("Bundle '" + bundlePath + "' loaded.");
84+
BundleLoaded = true;
8685

8786
AssetBundle bundle = www.assetBundle;
88-
8987
Shader[] shaders = bundle.LoadAllAssets<Shader>();
9088

9189
foreach (Shader shader in shaders)
@@ -96,8 +94,6 @@ public static void LoadBundle()
9694

9795
bundle.Unload(false);
9896
www.Dispose();
99-
100-
BundleLoaded = true;
10197
}
10298
}
10399

@@ -168,7 +164,11 @@ public static void Init()
168164

169165
protected void Start()
170166
{
171-
167+
if (HighLogic.LoadedScene == GameScenes.SPACECENTER || HighLogic.LoadedScene == GameScenes.FLIGHT)
168+
{
169+
SetTerrainShadows();
170+
}
171+
172172
if (HighLogic.LoadedScene == GameScenes.MAINMENU)
173173
{
174174
EnableMainOverlay();
@@ -179,6 +179,37 @@ protected void Start()
179179
}
180180
}
181181

182+
private void SetTerrainShadows()
183+
{
184+
bool terrainShadows = false;
185+
186+
ConfigNode volumeConfig = GameDatabase.Instance.GetConfigNodes("KERBAL_VISUAL_ENHANCEMENTS")[0];
187+
bool.TryParse(volumeConfig.GetValue("terrainShadows"), out terrainShadows);
188+
189+
if (terrainShadows)
190+
{
191+
foreach (CelestialBody body in FindObjectsOfType(typeof(CelestialBody)))
192+
{
193+
if (body.pqsController)
194+
{
195+
body.pqsController.meshCastShadows = true;
196+
body.pqsController.meshRecieveShadows = true;
197+
198+
QualitySettings.shadowDistance = 100000;
199+
200+
foreach (Light light in FindObjectsOfType(typeof(Light)))
201+
{
202+
if ((light.gameObject.name == "Scaledspace SunLight") || (light.gameObject.name == "SunLight"))
203+
{
204+
// light.shadowNormalBias = 0.4f;
205+
// light.shadowBias = 0.125f;
206+
}
207+
}
208+
}
209+
}
210+
}
211+
}
212+
182213
private void OnDominantBodyChangeCallback(GameEvents.FromToAction<CelestialBody, CelestialBody> data)
183214
{
184215
UpdateCurrentBody(data.to.bodyName);
@@ -394,7 +425,7 @@ public Overlay(string planet, float altitude, Material scaledMaterial, Material
394425
var mr = OverlayGameObject.AddComponent<MeshRenderer>();
395426
mr.sharedMaterial = scaledMaterial;
396427
mr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
397-
mr.receiveShadows = false;
428+
mr.receiveShadows = true;
398429
//mr.enabled = mainMenu;
399430
mr.enabled = true;
400431

@@ -437,6 +468,7 @@ public static Overlay GeneratePlanetOverlay(string planet, float altitude, Mater
437468
Rotation.x += .25f;
438469

439470
Transform celestialTransform = PSystemManager.Instance.scaledBodies.Single(t => t.name == planet).transform;
471+
440472
Overlay overlay = new Overlay(planet, altitude, scaledMaterial, macroMaterial, Rotation, OverlayMgr.MAP_LAYER, celestialTransform, mainMenu, matchTerrain);
441473
if (!mainMenu)
442474
{

Unity/Assets/Scenes/Shaders.unity

-64 Bytes
Binary file not shown.

Unity/Assets/Shaders/CloudParticle.shader

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,15 @@ Properties {
1111
_MinLight ("Minimum Light", Range(0,1)) = .5
1212
_Color ("Color Tint", Color) = (1,1,1,1)
1313
}
14-
15-
Category {
1614

17-
Tags { "Queue"="Transparent+1" "IgnoreProjector"="True" "RenderType"="Transparent" }
15+
SubShader {
16+
17+
Tags { "Queue" = "Transparent+1" "IgnoreProjector"="True" "RenderType"="Transparent" }
1818
Blend SrcAlpha OneMinusSrcAlpha
1919
Cull Off ZWrite Off
2020
Offset -1,-1
21-
22-
SubShader {
21+
2322
Pass {
24-
Lighting On
2523
Tags { "LightMode"="ForwardBase"}
2624

2725
CGPROGRAM
@@ -164,5 +162,4 @@ Category {
164162

165163
}
166164

167-
}
168165
}

Unity/Assets/Shaders/SphereCloud.shader

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@ Shader "Sphere/Cloud" {
1515
_FadeScale ("Fade Scale", Range(0,1)) = .002
1616
_RimDist ("Rim Distance", Range(0,1)) = 1
1717
}
18-
19-
Category {
20-
21-
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
22-
Blend SrcAlpha OneMinusSrcAlpha
23-
Cull Off ZWrite Off
24-
Offset -1,-1
2518

2619
SubShader {
20+
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
21+
Blend SrcAlpha OneMinusSrcAlpha
22+
Cull Off ZWrite Off
23+
Offset -1,-1
24+
2725
Pass {
28-
Lighting On
29-
Tags { "LightMode"="ForwardBase"}
30-
26+
Tags{ "LightMode" = "ForwardBase" }
27+
3128
CGPROGRAM
3229

3330
#include "UnityCG.cginc"
@@ -41,6 +38,12 @@ SubShader {
4138
#define INV_PI (1.0/PI)
4239
#define TWOPI (2.0*PI)
4340
#define INV_2PI (1.0/TWOPI)
41+
42+
// compile shader into multiple variants, with and without shadows
43+
// (we don't care about any lightmaps yet, so skip these variants)
44+
#pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
45+
// shadow helper functions and macros
46+
#include "AutoLight.cginc"
4447

4548
sampler2D _MainTex;
4649
sampler2D _DetailTex;
@@ -69,6 +72,7 @@ SubShader {
6972
float3 worldNormal : TEXCOORD3;
7073
float3 objNormal : TEXCOORD4;
7174
float3 viewDir : TEXCOORD5;
75+
SHADOW_COORDS(6)
7276
};
7377

7478

@@ -85,6 +89,8 @@ SubShader {
8589
o.worldNormal = normalize(vertexPos-origin);
8690
o.objNormal = normalize( v.vertex);
8791
o.viewDir = normalize(WorldSpaceViewDir(v.vertex));
92+
TRANSFER_SHADOW(o)
93+
8894
return o;
8995
}
9096

@@ -136,13 +142,15 @@ SubShader {
136142
half lightIntensity = saturate(_LightColor0.a * diff * 4);
137143
color.rgb *= saturate(ambientLighting + ((_MinLight + _LightColor0.rgb) * lightIntensity));
138144

145+
fixed shadow = SHADOW_ATTENUATION(IN);
146+
color.rgb *= shadow;
147+
139148
return color;
140149
}
141150
ENDCG
142151

143-
}
152+
}
144153

145154
}
146155

147-
}
148-
}
156+
}

0 commit comments

Comments
 (0)