Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by cdrandin · Dec 29, 2014 at 08:53 AM · shader

How to write shaders for mobile?

I have a very basic monochroming shader as follows...

 Shader "Custom/Monochrome" 
 {
     Properties 
     {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _DimIntensity ("Monochrome Value", range(0.5, 5.0)) = 1.0
     }
     
     SubShader 
     {
         Tags { "Queue" = "Transparent" }
         Pass 
         {    
             LOD 100
             Cull Back // now render the front faces
             ZWrite Off // don't write to depth buffer 
             // in order not to occlude other objects
             Blend SrcAlpha OneMinusSrcAlpha 
             // blend based on the fragment's alpha value
 
             CGPROGRAM
 
             #pragma vertex vert  
             #pragma fragment frag 
             #include "UnityCG.cginc"
 
             uniform sampler2D _MainTex; 
             uniform half _DimIntensity;
             uniform float _StartTime = 0.0;
             uniform float _isPlaying;
             
             struct vertexInput 
             {
                 float4 vertex   : POSITION;
                 float4 texcoord : TEXCOORD0;
             };
             struct vertexOutput 
             {
                 float4 pos : SV_POSITION;
                 float4 tex : TEXCOORD0;
             };
 
             vertexOutput vert(vertexInput input) 
             {
                 vertexOutput output;
 
                 output.tex = input.texcoord;
                 output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
                 return output;
             }
 
             fixed4 frag(v2f_img input) : COLOR
             {
                 fixed4 rgba = tex2D(_MainTex, input.uv.xy);
                 return fixed4(rgba.rgb/_DimIntensity, rgba.a);  
             }
             
             ENDCG
         }
     } 
     //FallBack "Transparent/Cutout/VertixLit"
 }
 

My guess why this isn't working is because I need to then use methods that are available on the GPU of the mobile device. I haven't seen anything clear from some google searches.

Anyone have any references?

Comment
Add comment · Show 4
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 Paulius-Liekis · Dec 29, 2014 at 02:43 PM 0
Share

What's the indication that it doesn't? How does it look? Do you get any warnings when compiling for platform or running? If you select the shader does it show any warnings in the inspector?

Generally there are no differences between writing shaders for Editor or $$anonymous$$obile - Unity takes care of cross compiling for you. Although there are situation where there are hardware limitations (which is not your case) or shaders work differently (like something doesn't get initialised or something like that)

avatar image cdrandin · Dec 29, 2014 at 07:49 PM 0
Share

The shader involved used on a mobile device comes out as pink. So, that means that the sub shader apparently didn't work correctly. I probably should use Unity Remote to see what errors pop up.

avatar image tanoshimi · Dec 29, 2014 at 08:10 PM 0
Share

What "mobile device" in particular doesn't work? Android? Windows Phone? I don't see anything that wouldn't be supported (you've got some unused _StartTime and _isPlaying variables, but that shouldn't be it)

avatar image cdrandin · Dec 29, 2014 at 08:50 PM 0
Share

Android device. So far S5. I have another device which is not working at all, but I think it is a different issue.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by tanoshimi · Dec 29, 2014 at 10:02 PM

Oh, hang on - I've just seen your error...

Your vertex shader is outputting a custom vertexOutput structure. (line43) However, your fragment shader is expecting to receive a v2f_img input structure. (line52)

You need to change your fragment shader to:

 fixed4 frag(vertexOutput input) : COLOR
 {
   fixed4 rgba = tex2D(_MainTex, input.tex.xy);
   return fixed4(rgba.rgb/_DimIntensity, rgba.a);
 }

I don't see how this is related to Android, however - it should never have even worked in the editor!

Comment
Add comment · Show 1 · 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 cdrandin · Dec 29, 2014 at 10:57 PM 0
Share

Awesome thank you so much! That fixed the issue. Weird how it still worked in the editor.

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

26 People are following this question.

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

Related Questions

How to force the compilation of a shader in Unity? 5 Answers

Shader compilation fails for Wii-U 2 Answers

Unity 3 Problem: Legacy Lightmap Shaders don't seem to work. 1 Answer

Shader issue with a 170 upper angle 0 Answers

Exclude outline only, from fog, in custom toon shader. 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