Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Trithilon · Aug 06, 2012 at 08:36 PM · shaderlerptexturespaintingsse

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"
 }
Comment
Add comment · Show 22
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ScroodgeM · Aug 06, 2012 at 09:07 PM 0
Share

post a syntax of your condition selecting / lerping

avatar image Trithilon · Aug 07, 2012 at 06:26 AM 0
Share

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.

avatar image Trithilon · Aug 07, 2012 at 10:17 PM 0
Share

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

avatar image ScroodgeM · Aug 07, 2012 at 10:19 PM 0
Share

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)

avatar image ScroodgeM · Aug 16, 2012 at 12:56 PM 1
Share

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

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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"

}

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image equalsequals · Aug 16, 2012 at 04:00 PM 0
Share

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.

avatar image Trithilon · Aug 16, 2012 at 04:03 PM 0
Share

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.

avatar image ScroodgeM · Aug 16, 2012 at 04:04 PM 0
Share

not iOS, but hardware GPU limits this.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Anyone have shader that supports two UV channels? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges