- Home /
iOS Shader with 8 lerpable textures via Vertex Colour/Painting
Hello, I wish to do Vertex painting using 8 textures for my Single mesh/material environment. I have made a very big Strumpy Shader Graph for this and it works perfectly on desktop... but wont let me : 1: Have any more than 8 textures (it starts throwing a UV error beyond that) 2: Wont let me lerp more than twice (Else the shader goes all Black when applied!)
I am at a loss here... help?
Additional Info... BTW, I am setting the textures using a condition where : if R = 0-0.5 then render Texture1 if R = 0.5-1 then render Texture2 if G = 0-0.5 then render Texture3 if G = 0.5-1 then render Texture4 if B = 0-0.5 then render Texture5 if B = 0.5-1 then render Texture6 if A = 0-0.5 then render Texture7 if A = 0.5-1 then render Texture8
Here is my Strumpy Shader Graph https://www.dropbox.com/s/vejgqbz2cn0wng9/GodShader.sgraph
Here is the Unity Package containing The Shader and the Materials with some dummy textures. https://www.dropbox.com/s/h69mbyc7xe7ls86/GodShaderV3.unitypackage
Works fine on PC, goes all black on iOS.
And This is the syntax to the compiled shader.
Shader "GodShaderV3"
{
Properties
{
_Red("_Red", 2D) = "black" {}
_Red_R("_Red_R", 2D) = "black" {}
_Blue("_Blue", 2D) = "black" {}
_Blue_R("_Blue_R", 2D) = "black" {}
_Green("_Green", 2D) = "black" {}
_Green_R("_Green_R", 2D) = "black" {}
_Alpha("_Alpha", 2D) = "black" {}
_Alpha_R("_Alpha_R", 2D) = "black" {}
}
SubShader
{
Tags
{
"Queue"="Geometry"
"IgnoreProjector"="False"
"RenderType"="Opaque"
}
Cull Back
ZWrite On
ZTest LEqual
ColorMask RGBA
Fog{
}
CGPROGRAM
#pragma surface surf BlinnPhongEditor vertex:vert
#pragma target 3.0
sampler2D _Red;
sampler2D _Red_R;
sampler2D _Blue;
sampler2D _Blue_R;
sampler2D _Green;
sampler2D _Green_R;
sampler2D _Alpha;
sampler2D _Alpha_R;
struct EditorSurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half3 Gloss;
half Specular;
half Alpha;
half4 Custom;
};
inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
{
half3 spec = light.a * s.Gloss;
half4 c;
c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
c.a = s.Alpha;
return c;
}
inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
{
half3 h = normalize (lightDir + viewDir);
half diff = max (0, dot ( lightDir, s.Normal ));
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, s.Specular*128.0);
half4 res;
res.rgb = _LightColor0.rgb * diff;
res.w = spec * Luminance (_LightColor0.rgb);
res *= atten * 2.0;
return LightingBlinnPhongEditor_PrePass( s, res );
}
inline half4 LightingBlinnPhongEditor_DirLightmap (EditorSurfaceOutput s, fixed4 color, fixed4 scale, half3 viewDir, bool surfFuncWritesNormal, out half3 specColor)
{
UNITY_DIRBASIS
half3 scalePerBasisVector;
half3 lm = DirLightmapDiffuse (unity_DirBasis, color, scale, s.Normal, surfFuncWritesNormal, scalePerBasisVector);
half3 lightDir = normalize (scalePerBasisVector.x * unity_DirBasis[0] + scalePerBasisVector.y * unity_DirBasis[1] + scalePerBasisVector.z * unity_DirBasis[2]);
half3 h = normalize (lightDir + viewDir);
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, s.Specular * 128.0);
// specColor used outside in the forward path, compiled out in prepass
specColor = lm * _SpecColor.rgb * s.Gloss * spec;
// spec from the alpha component is used to calculate specular
// in the Lighting*_Prepass function, it's not used in forward
return half4(lm, spec);
}
struct Input {
float4 color : COLOR;
float2 uv_Red;
float2 uv_Red_R;
float2 uv_Green;
float2 uv_Green_R;
float2 uv_Blue;
float2 uv_Blue_R;
float2 uv_Alpha;
float2 uv_Alpha_R;
};
void vert (inout appdata_full v, out Input o) {
float4 VertexOutputMaster0_0_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_1_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_2_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_3_NoInput = float4(0,0,0,0);
}
void surf (Input IN, inout EditorSurfaceOutput o) {
o.Normal = float3(0.0,0.0,1.0);
o.Alpha = 1.0;
o.Albedo = 0.0;
o.Emission = 0.0;
o.Gloss = 0.0;
o.Specular = 0.0;
o.Custom = 0.0;
float4 Split0=IN.color;
float4 Step0=step(float4( Split0.x, Split0.x, Split0.x, Split0.x),float4( 0.5,0.5,0.5,0.5 ));
float4 Subtract0=float4( 1.0, 1.0, 1.0, 1.0 ) - Step0;
float4 Abs0=abs(Subtract0);
float4 Sampled2D1=tex2D(_Red,IN.uv_Red.xy);
float4 Multiply0=Abs0 * Sampled2D1;
float4 Subtract1=float4( 0.0, 0.0, 0.0, 0.0 ) - Step0;
float4 Abs1=abs(Subtract1);
float4 Sampled2D0=tex2D(_Red_R,IN.uv_Red_R.xy);
float4 Multiply1=Abs1 * Sampled2D0;
float4 Add0=Multiply0 + Multiply1;
float4 Lerp0=lerp(float4( 0,0,0,0),Add0,float4( Split0.x, Split0.x, Split0.x, Split0.x));
float4 Split1=IN.color;
float4 Step2=step(float4( Split1.y, Split1.y, Split1.y, Split1.y),float4( 0.5,0.5,0.5,0.5 ));
float4 Subtract4=float4( 1.0, 1.0, 1.0, 1.0 ) - Step2;
float4 Abs4=abs(Subtract4);
float4 Sampled2D5=tex2D(_Green,IN.uv_Green.xy);
float4 Multiply4=Abs4 * Sampled2D5;
float4 Subtract5=float4( 0.0, 0.0, 0.0, 0.0 ) - Step2;
float4 Abs5=abs(Subtract5);
float4 Sampled2D4=tex2D(_Green_R,IN.uv_Green_R.xy);
float4 Multiply5=Abs5 * Sampled2D4;
float4 Add2=Multiply4 + Multiply5;
float4 Lerp2=lerp(Lerp0,Add2,float4( Split1.y, Split1.y, Split1.y, Split1.y));
float4 Split2=IN.color;
float4 Step3=step(float4( Split2.z, Split2.z, Split2.z, Split2.z),float4( 0.5,0.5,0.5,0.5 ));
float4 Subtract6=float4( 1.0, 1.0, 1.0, 1.0 ) - Step3;
float4 Abs6=abs(Subtract6);
float4 Sampled2D7=tex2D(_Blue,IN.uv_Blue.xy);
float4 Multiply6=Abs6 * Sampled2D7;
float4 Subtract7=float4( 0.0, 0.0, 0.0, 0.0 ) - Step3;
float4 Abs7=abs(Subtract7);
float4 Sampled2D6=tex2D(_Blue_R,IN.uv_Blue_R.xy);
float4 Multiply7=Abs7 * Sampled2D6;
float4 Add3=Multiply6 + Multiply7;
float4 Lerp1=lerp(Lerp2,Add3,float4( Split2.z, Split2.z, Split2.z, Split2.z));
float4 Split3=IN.color;
float4 Step1=step(float4( Split3.w, Split3.w, Split3.w, Split3.w),float4( 0.5,0.5,0.5,0.5 ));
float4 Subtract2=float4( 1.0, 1.0, 1.0, 1.0 ) - Step1;
float4 Abs2=abs(Subtract2);
float4 Sampled2D8=tex2D(_Alpha,IN.uv_Alpha.xy);
float4 Multiply2=Abs2 * Sampled2D8;
float4 Subtract3=float4( 0.0, 0.0, 0.0, 0.0 ) - Step1;
float4 Abs3=abs(Subtract3);
float4 Sampled2D3=tex2D(_Alpha_R,IN.uv_Alpha_R.xy);
float4 Multiply3=Abs3 * Sampled2D3;
float4 Add1=Multiply2 + Multiply3;
float4 Lerp3=lerp(Lerp1,Add1,float4( Split3.w, Split3.w, Split3.w, Split3.w));
float4 Master0_1_NoInput = float4(0,0,1,1);
float4 Master0_2_NoInput = float4(0,0,0,0);
float4 Master0_3_NoInput = float4(0,0,0,0);
float4 Master0_4_NoInput = float4(0,0,0,0);
float4 Master0_5_NoInput = float4(1,1,1,1);
float4 Master0_7_NoInput = float4(0,0,0,0);
float4 Master0_6_NoInput = float4(1,1,1,1);
o.Albedo = Lerp3;
o.Normal = normalize(o.Normal);
}
ENDCG
}
Fallback "Diffuse"
}
Hey, I have added the links to the shader and the code as well, kindly have a look. Am I running out of instruction counts because I had to make my own "if" statement? I read somewhere that you can override the limit. How much should be my budget for such a mobile shader? Thanks.
Thanks for the slimmer shader code. You could test it using Graphics Emulation. However, I shall verify this tomorrow morning and let you know how it goes. :)
yes i know about emulation, but i have no 3d-model with such mixed vertex colors to see how this shader works (or don't works...) 8)
these warning are about flash only. adding
#pragma exclude_renderers flash
after CGPROGRA$$anonymous$$, will disable it.
btw, all this looks like you can't use in iPad2 more than 6 textures.
so, here is possible ways to go:
use 6 textures ins$$anonymous$$d of 8
if you just need to smooth change of textures and no more then 2 textures are used at one time, better to use (in performance reason) shader with 2 textures and just change it on smooth needed
change shader from surface to vertex/fragment. this will disable built-in lighting and free up some slots for additional textures.
find a OpenGL ES 2.0 master, but possibly he will say the same thing 8) not sure
Answer by ScroodgeM · Aug 07, 2012 at 10:12 PM
your shader makes a lot of unneeded calculations 8) try this one - much less GPU load and possible it will work on iOS too (can't check it cause have no device)
waiting for feedback here
Shader "GodShaderV3" { Properties { _Red("_Red (+offset/tiling)", 2D) = "black" {} _Red_R("_Red_R", 2D) = "black" {} _Blue("_Blue", 2D) = "black" {} _Blue_R("_Blue_R", 2D) = "black" {} _Green("_Green", 2D) = "black" {} _Green_R("_Green_R", 2D) = "black" {} _Alpha("_Alpha", 2D) = "black" {} _Alpha_R("_Alpha_R", 2D) = "black" {} } SubShader { Tags { "Queue"="Geometry" "IgnoreProjector"="False" "RenderType"="Opaque" } Cull Back ZWrite On ZTest LEqual ColorMask RGBA Fog { Mode Off }
CGPROGRAM
#pragma surface surf BlinnPhong nodirlightmap
sampler2D _Red;
sampler2D _Red_R;
sampler2D _Blue;
sampler2D _Blue_R;
sampler2D _Green;
sampler2D _Green_R;
sampler2D _Alpha;
sampler2D _Alpha_R;
struct Input
{
float4 color : COLOR;
float2 uv_Red;
};
void surf (Input IN, inout SurfaceOutput o)
{
o.Albedo = 0.0;
if (IN.color.a < 1)
{
if (IN.color.b < 1)
{
if (IN.color.g < 1)
{
o.Albedo = lerp(o.Albedo, (IN.color.r > 0.5) ? tex2D(_Red, IN.uv_Red.xy).rgb : tex2D(_Red_R,IN.uv_Red.xy).rgb, IN.color.r);
}
o.Albedo = lerp(o.Albedo, (IN.color.g > 0.5) ? tex2D(_Green, IN.uv_Red.xy).rgb : tex2D(_Green_R,IN.uv_Red.xy).rgb, IN.color.g);
}
o.Albedo = lerp(o.Albedo, (IN.color.b > 0.5) ? tex2D(_Blue,IN.uv_Red.xy).rgb : tex2D(_Blue_R,IN.uv_Red.xy).rgb, IN.color.b);
}
o.Albedo = lerp(o.Albedo, (IN.color.a > 0.5) ? tex2D(_Alpha,IN.uv_Red.xy).rgb : tex2D(_Alpha_R,IN.uv_Red.xy).rgb, IN.color.a);
o.Normal = half3(0,0,1);
}
ENDCG
}
Fallback "Diffuse"
}
That is also the cause of his problem, the whole rendering black thing is writing 0 in his fragment pass because it reaches its instruction limit before completing. Removing the needless instructions would fix it.
Any idea how many instructions am I allowed to use? It would be of great help to know the exact limitations of OpenGL ES 2.0 on iOS.
Your answer
Follow this Question
Related Questions
Effects of Graphics.Blit to rendertexture are temporary 2 Answers
Cubemaps Lerp 0 Answers
poor performance is slowing down the rate 1 Answer
Fill Gradient color in the image portion 0 Answers