Skip to content

Commit

Permalink
load shaders via new asset loader
Browse files Browse the repository at this point in the history
  • Loading branch information
BahamutoD committed Apr 4, 2016
1 parent 6f64121 commit f35cfea
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 5 deletions.
67 changes: 65 additions & 2 deletions BahaTurret/BDAShaderLoader.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,71 @@
using System;
using UnityEngine;
using System.Collections;
using System.Reflection;
using System.IO;
using UnityEngine;
using KSPAssets.Loaders;
using KSPAssets;
namespace BahaTurret
{
public static class BDAShaderLoader
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
public class BDAShaderLoader : MonoBehaviour
{
private static bool loaded = false;
public static Shader GrayscaleEffectShader;
public static Shader UnlitBlackShader;
public static Shader BulletShader;

void Start()
{
if(!loaded)
{
StartCoroutine(LoadRoutine());
loaded = true;
}
}

IEnumerator LoadRoutine()
{
while(!AssetLoader.Ready)
{
yield return null;
}


AssetDefinition bulletDef = AssetLoader.GetAssetDefinitionWithName("BDArmory/AssetBundles/bdabulletshader", "BDArmory/Bullet");
AssetDefinition unlitBlackDef = AssetLoader.GetAssetDefinitionWithName("BDArmory/AssetBundles/bdaunlitblack", "BDArmory/Unlit Black");
AssetDefinition grayscaleDef = AssetLoader.GetAssetDefinitionWithName("BDArmory/AssetBundles/bdagrayscaleshader", "BDArmory/Grayscale Effect");
AssetDefinition[] assetDefs = new AssetDefinition[]{ bulletDef, unlitBlackDef, grayscaleDef };
AssetLoader.LoadAssets(AssetLoaded, assetDefs);
}


void AssetLoaded(AssetLoader.Loader loader)
{
Debug.Log("BDArmory loaded shaders: ");
for(int i = 0; i < loader.objects.Length; i++)
{
Shader s = (Shader)loader.objects[i];
if(s == null) continue;

Debug.Log("- " + s.name);

if(s.name == "BDArmory/Bullet")
{
BulletShader = s;
}
else if(s.name == "BDArmory/Unlit Black")
{
UnlitBlackShader = s;
}
else if(s.name == "BDArmory/Grayscale Effect")
{
GrayscaleEffectShader = s;
}
}
}

/*
public static Shader LoadManifestShader(string manifestResource)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Expand All @@ -23,6 +83,9 @@ public static Shader LoadManifestShader(string manifestResource)
}
return null;
}
*/
}


}

3 changes: 3 additions & 0 deletions BahaTurret/BahaTurret.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
<Reference Include="UnityEngine.UI">
<HintPath>..\..\..\Program Files\Steam\steamapps\common\Kerbal Space Program\KSP_x64_Data\Managed\UnityEngine.UI.dll</HintPath>
</Reference>
<Reference Include="KSPAssets">
<HintPath>..\..\..\Program Files\Steam\SteamApps\common\Kerbal Space Program\KSP_x64_Data\Managed\KSPAssets.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CFEnable.cs" />
Expand Down
2 changes: 1 addition & 1 deletion BahaTurret/PooledBullet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void OnEnable()
if(!shaderInitialized)
{
shaderInitialized = true;
bulletShader = BDAShaderLoader.LoadManifestShader("BahaTurret.BulletShader.shader");
bulletShader = BDAShaderLoader.BulletShader;//.LoadManifestShader("BahaTurret.BulletShader.shader");
}

if(!wasInitiated)
Expand Down
2 changes: 1 addition & 1 deletion BahaTurret/RadarUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void SetupRadarCamera()
//setup shader first
if(!radarShader)
{
radarShader = BDAShaderLoader.LoadManifestShader("BahaTurret.UnlitBlack.shader");
radarShader = BDAShaderLoader.UnlitBlackShader;//.LoadManifestShader("BahaTurret.UnlitBlack.shader");
}

//then setup textures
Expand Down
2 changes: 1 addition & 1 deletion BahaTurret/TGPCameraEffects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void Awake()
{
if(!grayscaleMaterial)
{
grayscaleMaterial = new Material(BDAShaderLoader.LoadManifestShader("BahaTurret.GrayscaleEffectShader.shader"));
grayscaleMaterial = new Material(BDAShaderLoader.GrayscaleEffectShader);//LoadManifestShader("BahaTurret.GrayscaleEffectShader.shader"));
grayscaleMaterial.SetTexture("_RampTex", textureRamp);
grayscaleMaterial.SetFloat("_RampOffset", rampOffset);
}
Expand Down
Binary file modified BahaTurret/bin/Release/BahaTurret.dll
Binary file not shown.
Binary file not shown.
Binary file modified BahaTurret/obj/x86/Release/BahaTurret.dll
Binary file not shown.

0 comments on commit f35cfea

Please sign in to comment.