- Home /
Fragment Shader not applyable at runtime?
I have the following scenrario: A Script generates ShaderCode and it is assigned to a virtual Material. The ShaderCode contains a fragment Shader and is working, if I put the Code into a .shader file and assign it to a created Material file.
If I try to assign the pure Code as a string to a new Material (Material m = new Material(Code)), the compiler produces:
Shader error in 'mcfShaders/Diffuse': Parse error: syntax error at line 15
So something is wrong with the CGPROGRAM. Maybe the Mapping in vertexInput or fragmentInput? This errormessage is not really helping...
Shader "mcfShaders/Diffuse" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
}
SubShader {
Tags { "RenderType"="Transparent" "Queue"="Transparent" "LightMode"="ForwardBase" }
LOD 200
ZWrite On
Lighting On
Blend SrcAlpha OneMinusSrcAlpha
Cull Back
Pass {
CGPROGRAM
#pragma exclude_renderers ps3 xbox360 flash
#pragma fragmentoption ARB_precision_hint_fastest
#pragma vertex vert addshadow fullforwardshadows
#pragma fragment frag
uniform float4 _Color;
struct vertInput {
float4 vertex : POSITION;
float4 color : COLOR;
};
struct fragInput {
float4 pos : SV_POSITION;
float4 diff : COLOR0;
};
fragInput vert(vertInput i) {
fragInput r;
r.pos = mul(UNITY_MATRIX_MVP, i.vertex);
r.diff = _Color;
return r;
}
half4 frag( fragInput i ) : COLOR {
return half4(i.diff);
}
ENDCG
}
}
Fallback "Diffuse"
}
Hope someone with knowledge finds this :)
I'm not an expert but I assume the Cg compiler isn't part of the run-time.
Shaders can't really be applied or changed at run-time without expensive, extensive, and difficult swapping. Consider subsituting by instantiating objects ins$$anonymous$$d.
Answer by Jessespike · Feb 23, 2015 at 02:27 PM
http://docs.unity3d.com/ScriptReference/Material-ctor.html
From that page:
NOTE: Creating materials this way supports only simple shaders (fixed function ones). If you need a surface shader, or vertex/pixel shaders, you'll need to create shader asset in the editor and use that.
Thx for advice. I managed to do a work-around for implementing fragment shaders at runtime. $$anonymous$$y script now writes the code into a file which is loaded by Resources.Load. Then I call the $$anonymous$$aterial constructor with the Name of the shader. Works fine for what I am needing.
That works? Did you test in a build or only in editor?
I was a bit preoccupied about the solution. Seems like the file is written correctly, but the shader update happens only at the 2nd start. So i tried to use AssetDatabase, requiring UnityEditor. Still everything happens in Editor, not inside the build. Seems like there is no way to manually reimport an external file without using the Editor. Or is there?
Your answer
![](https://koobas.hobune.stream/wayback/20220613184105im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Use multiple materials? 1 Answer
Create Staining effect 0 Answers
What keyword string should I add to enable/disable when changing shader properties ? 1 Answer
Multiple Cars not working 1 Answer