Skip to content

Commit

Permalink
Fix using constructor with monobehaviours and remove ReferenceEquals,…
Browse files Browse the repository at this point in the history
… need to test it better
  • Loading branch information
LGhassen committed Aug 20, 2022
1 parent 161a660 commit 50a2f8e
Show file tree
Hide file tree
Showing 42 changed files with 242 additions and 368 deletions.
13 changes: 0 additions & 13 deletions scatterer/Effects/AntiAliasing/GenericAntiAliasing.cs
@@ -1,15 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Runtime;
using KSP;
using KSP.IO;
using UnityEngine;
using UnityEngine.Rendering;

namespace Scatterer
{
Expand All @@ -18,8 +7,6 @@ public abstract class GenericAntiAliasing : MonoBehaviour
public GenericAntiAliasing ()
{
}

public abstract void Cleanup();
}
}

Expand Up @@ -42,7 +42,7 @@ public SubpixelMorphologicalAntialiasing()

int width, height;

if (!ReferenceEquals (targetCamera.activeTexture, null))
if (targetCamera.activeTexture)
{
width = targetCamera.activeTexture.width;
height = targetCamera.activeTexture.height;
Expand Down Expand Up @@ -75,9 +75,9 @@ public SubpixelMorphologicalAntialiasing()

SMAAMaterial = new Material(ShaderReplacer.Instance.LoadedShaders[("Scatterer/SubpixelMorphologicalAntialiasing")]);

if (ReferenceEquals(areaTex,null))
if (areaTex == null)
areaTex = (Texture2D) ShaderReplacer.Instance.LoadedTextures ["AreaTex"];
if (ReferenceEquals (searchTex, null))
if (searchTex == null)
searchTex = (Texture2D)ShaderReplacer.Instance.LoadedTextures ["SearchTex"];

SMAAMaterial.SetTexture("_AreaTex" , areaTex);
Expand Down Expand Up @@ -117,20 +117,20 @@ public void OnPreCull()
}
}

public override void Cleanup()
public void OnDestroy()
{
SMAAMaterial = null;

if (!ReferenceEquals(SMAACommandBuffer,null))
if (SMAACommandBuffer !=null)
{
targetCamera.RemoveCommandBuffer (CameraEvent.AfterForwardAlpha, SMAACommandBuffer);
SMAACommandBuffer.Clear();
}

if (!ReferenceEquals (flip, null))
if (flip)
flip.Release ();

if (!ReferenceEquals (flop, null))
if (flop)
flop.Release ();
}
}
Expand Down
7 changes: 3 additions & 4 deletions scatterer/Effects/AntiAliasing/TemporalAntiAliasing.cs
Expand Up @@ -3,7 +3,6 @@

namespace Scatterer
{
// Not recommended in KSP, setting a custom camera projection matrix in Unity causes shadowmaps to flicker with high far/near clipplanes ratio
// Temporal anti-aliasing from Unity post-processing stack V2
// Modified to not blur the ocean, removed dependency on other postprocessing classes I don't need here so we can initialize and make everything work by just adding this to a camera
// Generating motion vectors for the ocean is both expensive and unnecessary so we should just not apply TAA on the ocean, otherwise it would just blur it without proper motion vectors
Expand Down Expand Up @@ -148,7 +147,7 @@ RenderTexture CheckHistory(int id, CommandBuffer cmd)

int width, height;

if (!ReferenceEquals (targetCamera.activeTexture, null))
if (targetCamera.activeTexture)
{
width = targetCamera.activeTexture.width;
height = targetCamera.activeTexture.height;
Expand Down Expand Up @@ -217,9 +216,9 @@ public void ResetProjection()
targetCamera.nonJitteredProjectionMatrix = targetCamera.projectionMatrix;
}

public override void Cleanup()
public void OnDestroy()
{
if (!ReferenceEquals(temporalAACommandBuffer,null))
if (temporalAACommandBuffer != null)
targetCamera.RemoveCommandBuffer (CameraEvent.AfterForwardAlpha, temporalAACommandBuffer);

targetCamera.depthTextureMode = originalDepthTextureMode;
Expand Down
23 changes: 8 additions & 15 deletions scatterer/Effects/PlanetShine/PlanetshineManager.cs
@@ -1,27 +1,16 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Runtime;
using KSP;
using KSP.IO;
using UnityEngine;

namespace Scatterer
{
public class PlanetshineManager : MonoBehaviour
public class PlanetshineManager
{
List<PlanetShineLight> celestialLightSources=new List<PlanetShineLight> {};
Cubemap planetShineCookieCubeMap;

public PlanetshineManager ()
{
}

public void Init ()
{
//load planetshine "cookie" cubemap
planetShineCookieCubeMap = new Cubemap (512, TextureFormat.ARGB32, true);
Expand Down Expand Up @@ -54,8 +43,12 @@ public void Init ()
aPsLight.source = celBody;
if (!_aSource.isSun)
aPsLight.sunCelestialBody = FlightGlobals.Bodies.SingleOrDefault (_cb => _cb.GetName () == _aSource.mainSunCelestialBody);
GameObject ScaledPlanetShineLight = (UnityEngine.GameObject)Instantiate (Scatterer.Instance.scaledSpaceSunLight.gameObject);
GameObject LocalPlanetShineLight = (UnityEngine.GameObject)Instantiate (Scatterer.Instance.scaledSpaceSunLight.gameObject);
// GameObject ScaledPlanetShineLight = (UnityEngine.GameObject)Instantiate (Scatterer.Instance.scaledSpaceSunLight.gameObject);
// GameObject LocalPlanetShineLight = (UnityEngine.GameObject)Instantiate (Scatterer.Instance.scaledSpaceSunLight.gameObject);

//TODO: fix this if I ever come back to it
GameObject ScaledPlanetShineLight = new GameObject();
GameObject LocalPlanetShineLight = new GameObject();
ScaledPlanetShineLight.GetComponent<Light> ().type = LightType.Point;
if (!_aSource.isSun)
ScaledPlanetShineLight.GetComponent<Light> ().cookie = planetShineCookieCubeMap;
Expand Down Expand Up @@ -90,7 +83,7 @@ public void UpdatePlanetshine ()
}
}

public void CleanUp()
public void Cleanup()
{
foreach (PlanetShineLight _aLight in celestialLightSources)
{
Expand Down
16 changes: 8 additions & 8 deletions scatterer/Effects/Proland/Atmosphere/Godrays/GodraysRenderer.cs
Expand Up @@ -61,7 +61,7 @@ public bool Init(Light inputLight, SkyNode inputParentSkyNode)
return false;
}

if (ReferenceEquals (inputLight, null))
if (!inputLight)
{
Utils.LogError("Godrays light is null, godrays can't be added");
return false;
Expand Down Expand Up @@ -245,7 +245,7 @@ public void RenderingDone()

void OnPreCull()
{
if (!ReferenceEquals(parentSkyNode,null) && !parentSkyNode.inScaledSpace)
if (parentSkyNode && !parentSkyNode.inScaledSpace)
{
EnableRenderingForFrame();

Expand Down Expand Up @@ -323,32 +323,32 @@ void OnPreCull()

public void OnPostRender()
{
if (!ReferenceEquals(parentSkyNode,null))
if (parentSkyNode)
{
RenderingDone ();
}
}

public void Cleanup()
public void OnDestroy()
{
RenderingDone ();

if (!ReferenceEquals (inverseShadowMatricesBuffer, null))
if (inverseShadowMatricesBuffer != null)
{
inverseShadowMatricesBuffer.Dispose();
}

if (!ReferenceEquals (volumeDepthTexture, null))
if (volumeDepthTexture)
{
volumeDepthTexture.Release();
}

if (!ReferenceEquals (volumeDepthGO, null))
if (volumeDepthGO)
{
DestroyImmediate(volumeDepthGO);
}

// if (!ReferenceEquals (cloudShadowGO, null))
// if (cloudShadowGO)
// {
// DestroyImmediate(cloudShadowGO);
// }
Expand Down
Expand Up @@ -38,7 +38,7 @@ public static AtmoPreprocessor Instance
{
get
{
if (ReferenceEquals(instance,null))
if (instance == null)
{
instance = new AtmoPreprocessor();
instance.InitMaterials();
Expand Down Expand Up @@ -235,15 +235,7 @@ public void Generate(float inRG,float inRT, Vector4 inBETA_R, Vector4 inBETA_MSc

Preprocess(assetPath);

m_transmittanceT.Release();
m_irradianceT[0].Release();
m_irradianceT[1].Release();
m_inscatterT[0].Release();
m_inscatterT[1].Release();
m_deltaET.Release();
m_deltaSRT.Release();
m_deltaSMT.Release();
m_deltaJT.Release();
ReleaseTextures();
}

void SetParameters(Material mat)
Expand Down Expand Up @@ -442,7 +434,12 @@ void ProcessInTiles(Action<int, int> process)
}
}

void OnDestroy()
public void OnDestroy()
{
ReleaseTextures();
}

void ReleaseTextures()
{
m_transmittanceT.Release();
m_irradianceT[0].Release();
Expand Down

0 comments on commit 50a2f8e

Please sign in to comment.