Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 danman43 · Dec 07, 2016 at 12:40 PM · rotationvertex shaderopengl es2.0

Opengl es 2.0 Shader Vertex Rotation Issue

I have a simple custom shader that I wrote that takes in two 2D textures, rotates one of them and blends the result creating a mask.

Everything works fine until I ran the game on the 1st Gen iPad Mini which uses opengl es2.0. I am a beginner in writing shaders (this is the first one I've written), and was able to determine it had something to do with the way I was doing the rotation - it seemed like on my ipad that code was ignored completely.

Also, when running the shader in unity and simulating opengl es2.0, it works as expected. It's only when it is run on the device that things don't work correctly.

See the code below - the rotation is in the vert function and happens in the if statement block. Any help is appreciated!

Shader code:

 Shader "Sprites/CullingMask" {
     Properties{
         _MainTex("Texture", 2D) = "white" {}
         _Mask("Culling Mask", 2D) = "white" {}
         _MaskRotationDegrees("Mask Rotation Degrees", float) = 0.0
     }
     
     SubShader {
         Tags { 
             "RenderType" = "Transparent" 
             "Queue" = "Transparent"
             "IgnoreProjector" = "True"
         }
 
         LOD 100
         ZWrite Off
         Blend SrcAlpha OneMinusSrcAlpha
 
         Pass
         {
             CGPROGRAM
             #pragma exclude_renderers ps3 xbox360 flash
             #pragma fragmentoption ARB_precision_hint_fastest
             #pragma vertex vert
             #pragma fragment frag
                         
             #include "UnityCG.cginc"
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
             sampler2D _Mask;
             float4 _Mask_ST;
             float _MaskRotationDegrees;
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float2 uv2 : TEXCOORD1;
                 float4 vertex : SV_POSITION;
             };
             
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                 if (_MaskRotationDegrees != 0.0 && abs(_MaskRotationDegrees / 360.0) != 1.0) {
                     float radians = _MaskRotationDegrees * (UNITY_PI * 2.0) / 360.0;
                     float s = sin(radians);
                     float c = cos(radians);
 
                     float2x2 rotationMatrix = float2x2(c, -s, s, c);
                     v.uv -= 0.5;  // offset UV so we rotate around 0.5 and not 0.0
                     v.uv = mul(v.uv, rotationMatrix);
                     v.uv += 0.5; // offset UV again so UVs are in the correct location
                 }
 
                 o.uv2 = TRANSFORM_TEX(v.uv, _Mask);
                 return o;
             }
             
             fixed4 frag (v2f i) : SV_Target {
                 return tex2D(_MainTex, i.uv) * tex2D(_Mask, i.uv2).a;
             }
             ENDCG
         }
     }
 
     FallBack "Diffuse"
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AndyJenkins30 · Dec 21, 2016 at 02:13 PM

Hi Dan,

I would advise removing the 'if' statement from your vertex function. There are a few threads you can read on why creating 'branches' in shader code is a bad idea: https://forum.unity3d.com/threads/branches-how-expensive-are-they.152411/ http://bps10.idav.ucdavis.edu/talks/03-fatahalian_gpuArchTeraflop_BPS_SIGGRAPH2010.pdf

There are a few things you could do instead:

  1. Change '_MaskRotationDegrees' type from float to range, so you can guarantee it appears within the boundaries you set.

  2. Take the modulo of your value to ensure it is between 1-360 ( _MaskRotationDegrees %= 360.0; )

  3. Just let any value pass into your rotation code. You don't seem to ever be dividing by zero so this shouldn't ever compute an error.

I've tested your shader on an iPhone SE running OpenGL ES 2.0 and it rotates/looks as expected. Let me know if your issue still occurs on your iPad mini after removing the if statement.

Best Regards, Andy

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate a vertex about "u" Axis 1 Answer

Quaternion Multiplication in a shader. 1 Answer

Rotate a mesh on world y axis using a CG vertex shader? 1 Answer

Flip over an object (smooth transition) 3 Answers

rotation of the object to a given angle 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