Jump to content

Loading and using custom shaders


Recommended Posts

Hi

I found this thread here, which explained how to load custom shaders. I've written something I'd like to load and use, and would like to write some more, but I'm having trouble actually getting it assigned to a material in KSP;

I've tried various ways to load the compiled shader, the most recent of which being:


[KSPAddon(KSPAddon.Startup.Instantly, false)]
public class ShaderLoad : MonoBehaviour
{
public string shaderFileName = "KerbalFoundries/_Dev/KFSpec.shader";
Shader _shader;


public void Start()
{
Debug.Log("ShaderSwap OnInit");
string filename = KSPUtil.ApplicationRootPath + "GameData/" + shaderFileName;
if (shaderFileName != string.Empty && File.Exists(filename))
{
Debug.Log("Found shader");
try
{
TextReader shaderFile = new StreamReader(filename);
string shaderText = shaderFile.ReadToEnd();
_shader = new Material(shaderText).shader;
Debug.Log("Loaded shader");
}
catch (Exception e)
{
Debug.LogError("unable to load shader " + shaderFileName + " : " + e);
}
}
else
Debug.Log("Shader not found");
}
}

I've also tried as a PartModule. I can assign the shader in Unity, and part tools exports successfully with it applied to some objects, but they always seem to fall back to the Diffuse shader. I was assuming, having loaded the shader, I could then do Shader.Find("ShaderName"); to poke it into life and apply it, but that always returns null. The shader works perfectly in Unity.

What am I missing?

Many thanks!

Link to comment
Share on other sites

I don't think using shader.find works for shader objects you gotten from that loading method. Try just putting the shader object into a dictionary or something, and load it from there.

You should also not rely on the shader being loaded with the part loader, you should manually apply the new material to the part using a PartModule or something.

Link to comment
Share on other sites

Indeed; having done some more googling, that appears to be the case... How would I put the compiled shader into a dictionary?

I did try loading and applying it with a PartModule, but that didn't work either. Confused!

Edited by lo-fi
Link to comment
Share on other sites

Have you actually compiled the shader? You can't just load a plain shader, you have to compile it in unity first, there's a button for it in the shader inspector in the unity editor. (make sure you're using unity 4.6.1)

If you have, are you sure you're setting the shader correctly? You're supposed to set part.renderer.sharedMaterial to a new material, using the loaded shader as a parameter in the constructor, something like this:


[KSPAddon(KSPAddon.Startup.Instantly, false)]
public class ShaderLoad : MonoBehaviour
{
public string shaderFileName = "KerbalFoundries/_Dev/KFSpec.shader";
public static Shader KFSpecShader;

public void Start()
{
Debug.Log("ShaderSwap OnInit");
string filename = KSPUtil.ApplicationRootPath + "GameData/" + shaderFileName;
if (shaderFileName != string.Empty && File.Exists(filename))
{
Debug.Log("Found shader");
try
{
TextReader shaderFile = new StreamReader(filename);
string shaderText = shaderFile.ReadToEnd();
KFSpecShader = new Material(shaderText).shader;
Debug.Log("Loaded shader");
}
catch (Exception e)
{
Debug.LogError("unable to load shader " + shaderFileName + " : " + e);
}
}
else
Debug.Log("Shader not found");
}
}

...

public class CustomMaterialModule : PartModule
{
public Material PartMaterial;

public override void OnStart(StartState state)
{
PartMaterial = new Material(ShaderLoad.KFSpecShader);
//set other properties for the material, like textures and normal maps

part.renderer.sharedMaterial = PartMaterial;
}
}

Edited by MrHappyFace
formatting
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...