Jump to content

Is there a way to build a shader without Unity IDE?


Recommended Posts

I have a very simple shader written in CG that takes a param though. As I understand, in order to use a shader in KSP I need it to be wrapped with ShaderLab stuff which is done by Unity SL compiler but I would like to bypass downloading, installing and getting into several Gs of Unity dev tools just for the sake of a couple lines of code. Is there a way to make my shader SL compatible without the need to use Unity IDE itself?

My plan was to compile my shader with cg compiler (as it is much, much lighter than Unity's) and then just write the proper shader lab lines around the resulting assembly code. But while viewing other shaders already compiled with ShaderLab I found that's not that obvious. May be there's a standalone SL compiler or someone could explain how the cg-compiled assembly code could be properly wrapped with ShaderLab instructions?

Edited by Ser
Link to comment
Share on other sites

I think it's not exactly possible. I've got shaders in a couple mods, but I'm using the Unity Cg compiler wrapper since Unity adds a number of #defines - I edit the Cg text in Notepad, and I use a batch file to run the Unity Cg compiler, but it's still entailed a Unity download (which, since I also work on IVAs, I needed anyway). You're welcome to look at the compiler output I've got on GitHub (RasterPropMonitor project) to see if the Unity wrapper on the Cg output is something you can generate manually.

Link to comment
Share on other sites

MOARdV, thanks, I'll look at your sources, but at the date I've discovered that it's probably better to develop shaders with Unity, especially for beginners, for several reasons: it has means of quick editing, debugging and instant visualization plus the compiled code contains multiple profiles and different assembly under each that would be a little pain to reproduce manually.

Buy the way, I've ran into some trouble with my shader: I have a material in Unity with a texture and my shader assigned to it. I'm struggling with the fragment function. The vertex output struct matches the fragment input. The trouble is when I try to take the input color and pass it through, I get a totally white output, no matter what I do with it. On the other hand, if I return a constant (or parameter-specified) color, the mesh is properly colored. Seems like vertex function's output color is messed up for some reason.

What I'm trying to achieve is to create a shader that will alter screen colors in KSP so I expect that the colors of the texture of that test mesh should be affected properly first.

May be you can help with that?

Edited by Ser
Link to comment
Share on other sites

  • 2 weeks later...
Buy the way, I've ran into some trouble with my shader: I have a material in Unity with a texture and my shader assigned to it. I'm struggling with the fragment function. The vertex output struct matches the fragment input. The trouble is when I try to take the input color and pass it through, I get a totally white output, no matter what I do with it. On the other hand, if I return a constant (or parameter-specified) color, the mesh is properly colored. Seems like vertex function's output color is messed up for some reason.

What I'm trying to achieve is to create a shader that will alter screen colors in KSP so I expect that the colors of the texture of that test mesh should be affected properly first.

May be you can help with that?

Sorry - I didn't have this thread watched, and I forgot about it. Did you sort out this problem? If not, do you have the shader fragments somewhere so I can look at them?

Link to comment
Share on other sites

MOARdV, I've stopped trying and concentrated on other things but would be glad if you could help.

Once again: I have a mesh with a material. There's a texture assigned to that material and my shader. The shader is supposed to pass through all the underlying texture's colors but instead it makes the mesh totally white.

Seems like I can't get rid of some fundamental misconception.


Shader "My-shader" {
Properties {
_Magnitude ("Magnitude", Float) = 0.0
}
SubShader {
Pass {
CGPROGRAM
#pragma target 3.0
#pragma vertex vertexFunction
#pragma fragment fragmentFunction
#include "UnityCG.cginc"

uniform fixed _Magnitude;

struct vertexInput {
float4 pos : POSITION;
float4 color : COLOR;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 color : COLOR;
};
struct pvv2f {
float4 color : COLOR;
};

vertexOutput vertexFunction(vertexInput vert) {
vertexOutput o;
o.pos = mul(UNITY_MATRIX_MVP, vert.pos);
o.color = vert.color;
return o;
}

pvv2f fragmentFunction(vertexOutput inp) {
float4 outcolor;
outcolor[0] = inp.color[0];
outcolor[1] = inp.color[1];
outcolor[2] = inp.color[2];
outcolor[3] = inp.color[3];

pvv2f o;
o.color = outcolor;
return o;
}
ENDCG
}
}
}

Link to comment
Share on other sites

MOARdV, I've stopped trying and concentrated on other things but would be glad if you could help.

Once again: I have a mesh with a material. There's a texture assigned to that material and my shader. The shader is supposed to pass through all the underlying texture's colors but instead it makes the mesh totally white.

Seems like I can't get rid of some fundamental misconception.


Shader "My-shader" {
Properties {
_Magnitude ("Magnitude", Float) = 0.0
}
SubShader {
Pass {
CGPROGRAM
#pragma target 3.0
#pragma vertex vertexFunction
#pragma fragment fragmentFunction
#include "UnityCG.cginc"

uniform fixed _Magnitude;

struct vertexInput {
float4 pos : POSITION;
float4 color : COLOR;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 color : COLOR;
};
struct pvv2f {
float4 color : COLOR;
};

vertexOutput vertexFunction(vertexInput vert) {
vertexOutput o;
o.pos = mul(UNITY_MATRIX_MVP, vert.pos);
o.color = vert.color;
return o;
}

pvv2f fragmentFunction(vertexOutput inp) {
float4 outcolor;
outcolor[0] = inp.color[0];
outcolor[1] = inp.color[1];
outcolor[2] = inp.color[2];
outcolor[3] = inp.color[3];

pvv2f o;
o.color = outcolor;
return o;
}
ENDCG
}
}
}

This shader doesn't do any texture fetches. All it's doing is taking the color parameter from the vertex buffer (vertexInput) and passing it through unchanged. You have to include information about the texture in this shader in order to read from it. This shader from RasterPropMonitor is probably the simplest way to do that - you can ignore the first line of code in the vertex shader (multiplying alpha), but notice the variable _MainTex and the texture coordinate parameters. Those are what tell the shader there's a texture, and where in the texture you want to read.

All the COLOR parameter does is allow for coloring the vertices (and doing smooth color shading across the fragments). It can be used to apply tinting effects to your texture like you can see in the font shader I linked to, but it has nothing to do with the texture by itself.

Link to comment
Share on other sites

So if I want to take KSP's screen rendering and just alter its colors I have to use TEXCOORD even though I don't apply any additional texture in my shader?

If you want to change what's drawn on the screen, it gets complicated. There are ways to apply post-processing effects in Unity, but I don't know Unity well enough to guide you through those steps. But, in short, yes, you'd still need texture coordinates, but you'd also have to bind the screen as a texture.

Link to comment
Share on other sites

Pixel shaders deals with texture pixels.

Then I still don't get it. We have a vertex program that takes POSITION and COLOR as input and output. Then we have the exactly same input structure for a fragment(pixel) program. It takes POSITION and COLOR and outputs COLOR. Aren't they the position and the color of every pixel of some image? And the result will be a color of what?

Edited by Ser
Link to comment
Share on other sites

Googling for how the "Blit" function is called in Unity I've suddenly ran into an example of what exactly I needed so despite of some questions remaining the case is closed.

Thank you very much for your help.

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...