Skip to content

Commit

Permalink
Remove all traces of material
Browse files Browse the repository at this point in the history
`material` allocates a new instance of the material, which removes
Kopernicus custom material wrapper and breaks Kittopia. Also, it
doesn't make much sense to allocate materials permanently.
StollD committed Jul 20, 2018
1 parent 009e24f commit 98913a3
Showing 9 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Kopernicus.Components/KSC.cs
Original file line number Diff line number Diff line change
@@ -293,7 +293,7 @@ void Update()
// Loop through all Materials and change their settings
try
{
foreach (Material material in Resources.FindObjectsOfTypeAll<Material>().Where(m => m.color.ToString() == new Color(0.640f, 0.728f, 0.171f, 0.729f).ToString()))
foreach (Material material in Resources.FindObjectsOfTypeAll<Material>().Where(m => m.HasProperty("_Color") && m.color.ToString() == new Color(0.640f, 0.728f, 0.171f, 0.729f).ToString()))
{
// Patch the texture
if (ksc.mainTexture != null)
4 changes: 2 additions & 2 deletions src/Kopernicus.Components/PlanetaryParticle.cs
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ void Awake()
if (!GetComponent<ParticleRenderer>())
{
renderer = gameObject.AddComponent<ParticleRenderer>();
renderer.material = new Material(Shader.Find("Particles/Alpha Blended"));
renderer.sharedMaterial = new Material(Shader.Find("Particles/Alpha Blended"));
}
else
{
@@ -130,7 +130,7 @@ void Update()
emitter.maxSize = maxSize;
animator.sizeGrow = sizeGrow;
animator.colorAnimation = colorAnimation;
renderer.material.mainTexture = mainTexture;
renderer.sharedMaterial.mainTexture = mainTexture;
filter.mesh = filter.sharedMesh = mesh ?? transform.parent.GetComponent<MeshFilter>().sharedMesh;
animator.force = force;

30 changes: 15 additions & 15 deletions src/Kopernicus.Components/Ring.cs
Original file line number Diff line number Diff line change
@@ -193,22 +193,22 @@ public void BuildRing()
// Set texture
ringMR = gameObject.AddComponent<MeshRenderer>();
Renderer parentRenderer = parent.GetComponent<Renderer>();
ringMR.material = new Material(getShader());
ringMR.material.SetTexture("_MainTex", texture);
ringMR.sharedMaterial = new Material(getShader());
ringMR.sharedMaterial.SetTexture("_MainTex", texture);

ringMR.material.SetFloat("innerRadius", innerRadius * parent.transform.localScale.x);
ringMR.material.SetFloat("outerRadius", outerRadius * parent.transform.localScale.x);
ringMR.sharedMaterial.SetFloat("innerRadius", innerRadius * parent.transform.localScale.x);
ringMR.sharedMaterial.SetFloat("outerRadius", outerRadius * parent.transform.localScale.x);

if (useNewShader)
{
ringMR.material.SetFloat("planetRadius", planetRadius);
ringMR.material.SetFloat("penumbraMultiplier", penumbraMultiplier);
ringMR.sharedMaterial.SetFloat("planetRadius", planetRadius);
ringMR.sharedMaterial.SetFloat("penumbraMultiplier", penumbraMultiplier);

if (innerShadeTexture != null) {
ringMR.material.SetTexture("_InnerShadeTexture", innerShadeTexture);
ringMR.sharedMaterial.SetTexture("_InnerShadeTexture", innerShadeTexture);
}
if (innerShadeTiles > 0) {
ringMR.material.SetFloat("innerShadeTiles", tiles / innerShadeTiles);
ringMR.sharedMaterial.SetFloat("innerShadeTiles", tiles / innerShadeTiles);
}
if (innerShadeRotationPeriod > 0 && rotationPeriod > 0) {
innerShadeOffsetRate = innerShadeTiles * (
@@ -217,11 +217,11 @@ public void BuildRing()
}
}

ringMR.material.color = color;
ringMR.material.renderQueue = 3010;
ringMR.sharedMaterial.color = color;
ringMR.sharedMaterial.renderQueue = 3010;
if (parent.GetChild("Atmosphere") != null)
{
parent.GetChild("Atmosphere").GetComponent<Renderer>().material.renderQueue = 3020;
parent.GetChild("Atmosphere").GetComponent<Renderer>().sharedMaterial.renderQueue = 3020;
}

// Call the modules
@@ -514,15 +514,15 @@ void Update()
transform.localScale = transform.parent.localScale;
SetRotation();

if (useNewShader && ringMR?.material != null
if (useNewShader && ringMR?.sharedMaterial != null
&& KopernicusStar.Current?.sun?.transform != null)
{
ringMR.material.SetFloat("sunRadius",
ringMR.sharedMaterial.SetFloat("sunRadius",
(Single) KopernicusStar.Current.sun.Radius);
ringMR.material.SetVector("sunPosRelativeToPlanet",
ringMR.sharedMaterial.SetVector("sunPosRelativeToPlanet",
(Vector3) (KopernicusStar.Current.sun.transform.position -
ScaledSpace.ScaledToLocalSpace(transform.position)));
ringMR.material.SetFloat("innerShadeOffset",
ringMR.sharedMaterial.SetFloat("innerShadeOffset",
(Single) (Planetarium.GetUniversalTime() * innerShadeOffsetRate));
}

8 changes: 4 additions & 4 deletions src/Kopernicus.OnDemand/ScaledSpaceOnDemand.cs
Original file line number Diff line number Diff line change
@@ -105,14 +105,14 @@ public void LoadTextures()
// Load Diffuse
if (OnDemandStorage.TextureExists(texture))
{
scaledRenderer.material.SetTexture("_MainTex",
scaledRenderer.sharedMaterial.SetTexture("_MainTex",
OnDemandStorage.LoadTexture(texture, false, true, true));
}

// Load Normals
if (OnDemandStorage.TextureExists(normals))
{
scaledRenderer.material.SetTexture("_BumpMap",
scaledRenderer.sharedMaterial.SetTexture("_BumpMap",
OnDemandStorage.LoadTexture(normals, false, true, false));
}

@@ -130,13 +130,13 @@ public void UnloadTextures()
// Kill Diffuse
if (OnDemandStorage.TextureExists(texture))
{
DestroyImmediate(scaledRenderer.material.GetTexture("_MainTex"));
DestroyImmediate(scaledRenderer.sharedMaterial.GetTexture("_MainTex"));
}

// Kill Normals
if (OnDemandStorage.TextureExists(normals))
{
DestroyImmediate(scaledRenderer.material.GetTexture("_BumpMap"));
DestroyImmediate(scaledRenderer.sharedMaterial.GetTexture("_BumpMap"));
}

// Events
8 changes: 4 additions & 4 deletions src/Kopernicus/Configuration/CoronaLoader.cs
Original file line number Diff line number Diff line change
@@ -139,7 +139,7 @@ public CoronaLoader()
Value = corona.GetComponent<SunCoronas> ();

// Setup the material loader
material = new ParticleAddSmoothLoader (corona.GetComponent<Renderer>().material);
material = new ParticleAddSmoothLoader (corona.GetComponent<Renderer>().sharedMaterial);
material.name = Guid.NewGuid().ToString();
}

@@ -177,7 +177,7 @@ public CoronaLoader(CelestialBody body)
Value = corona.GetComponent<SunCoronas> ();

// Setup the material loader
material = new ParticleAddSmoothLoader (corona.GetComponent<Renderer>().material);
material = new ParticleAddSmoothLoader (corona.GetComponent<Renderer>().sharedMaterial);
material.name = Guid.NewGuid().ToString();
}

@@ -187,9 +187,9 @@ public CoronaLoader(CelestialBody body)
public CoronaLoader(SunCoronas component)
{
Value = component;
if (!(Value.GetComponent<Renderer>().material is ParticleAddSmoothLoader))
if (!(Value.GetComponent<Renderer>().sharedMaterial is ParticleAddSmoothLoader))
{
material = new ParticleAddSmoothLoader(Value.GetComponent<Renderer>().material);
material = new ParticleAddSmoothLoader(Value.GetComponent<Renderer>().sharedMaterial);
}
}
}
6 changes: 3 additions & 3 deletions src/Kopernicus/Configuration/ScaledVersionLoader.cs
Original file line number Diff line number Diff line change
@@ -298,9 +298,9 @@ void IParserEventSubscriber.PostApply(ConfigNode node)
if (OnDemandStorage.useOnDemand && type.Value != BodyType.Star)
{
Texture2D texture =
Value.scaledBody.GetComponent<Renderer>().material.GetTexture("_MainTex") as Texture2D;
Value.scaledBody.GetComponent<Renderer>().sharedMaterial.GetTexture("_MainTex") as Texture2D;
Texture2D normals =
Value.scaledBody.GetComponent<Renderer>().material.GetTexture("_BumpMap") as Texture2D;
Value.scaledBody.GetComponent<Renderer>().sharedMaterial.GetTexture("_BumpMap") as Texture2D;
ScaledSpaceOnDemand onDemand = Value.scaledBody.AddComponent<ScaledSpaceOnDemand>();
if (texture != null)
onDemand.texture = texture.name;
@@ -338,7 +338,7 @@ public ScaledVersionLoader()
if (Value.scaledBody.GetComponent<MeshRenderer>() == null)
{
Value.scaledBody.AddComponent<MeshRenderer>();
Value.scaledBody.GetComponent<Renderer>().material = null;
Value.scaledBody.GetComponent<Renderer>().sharedMaterial = null;
}

if (options == null)
2 changes: 1 addition & 1 deletion src/Kopernicus/RuntimeUtility/RuntimeUtility.cs
Original file line number Diff line number Diff line change
@@ -381,7 +381,7 @@ void LateUpdate()
GameObject star_ = KopernicusStar.GetNearest(body).gameObject;
Vector3 planet2cam = body.scaledBody.transform.position - body.afg.mainCamera.transform.position;
body.afg.lightDot = Mathf.Clamp01(Vector3.Dot(planet2cam, body.afg.mainCamera.transform.position - star_.transform.position) * body.afg.dawnFactor);
body.afg.GetComponent<Renderer>().material.SetFloat("_lightDot", body.afg.lightDot);
body.afg.GetComponent<Renderer>().sharedMaterial.SetFloat("_lightDot", body.afg.lightDot);
}

// Update the names of the presets in the settings dialog
4 changes: 2 additions & 2 deletions src/Kopernicus/RuntimeUtility/StarLightSwitcher.cs
Original file line number Diff line number Diff line change
@@ -187,15 +187,15 @@ public static void DebugSunScaledSpace(GameObject scaledVersion)
{
// Debug the scaled space size of the star
Utility.PrintTransform (scaledVersion.transform, " " + scaledVersion.name + " Transform ");
Utility.DumpObjectProperties (scaledVersion.GetComponent<Renderer>().material);
Utility.DumpObjectProperties (scaledVersion.GetComponent<Renderer>().sharedMaterial);

// Get the sun corona objects in scaled space
foreach (SunCoronas corona in scaledVersion.GetComponentsInChildren<SunCoronas>(true))
{
Logger.Active.Log ("---- Sun Corona ----");
Utility.PrintTransform (corona.transform);
Utility.DumpObjectProperties (corona);
Utility.DumpObjectProperties (corona.GetComponent<Renderer>().material);
Utility.DumpObjectProperties (corona.GetComponent<Renderer>().sharedMaterial);
Logger.Active.Log ("--------------------");
}
}
4 changes: 2 additions & 2 deletions src/Kopernicus/UI/PlanetTextureExporter.cs
Original file line number Diff line number Diff line change
@@ -216,7 +216,7 @@ public static IEnumerator UpdateTextures(CelestialBody celestialBody, TextureOpt
if (options.ApplyToScaled)
{
colorMap.Apply();
celestialBody.scaledBody.GetComponent<MeshRenderer>().material.SetTexture("_MainTex", colorMap);
celestialBody.scaledBody.GetComponent<MeshRenderer>().sharedMaterial.SetTexture("_MainTex", colorMap);
}
}

@@ -250,7 +250,7 @@ public static IEnumerator UpdateTextures(CelestialBody celestialBody, TextureOpt
if (options.ApplyToScaled)
{
normalMap.Apply();
celestialBody.scaledBody.GetComponent<MeshRenderer>().material
celestialBody.scaledBody.GetComponent<MeshRenderer>().sharedMaterial
.SetTexture("_BumpMap", normalMap);
}
}

0 comments on commit 98913a3

Please sign in to comment.