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
2
Question by highpockets · Oct 05, 2013 at 08:33 AM · shadershadersiphoneopenglnormalmap

normal maps on iOS with a custom shader.. Works, but not the way I need it to

Hi,

I'm trying to get a normal map effect on an iPhone (4G to be exact) with a custom shader. The built in mobile normal mapped shaders have an extra texture that I do not need and this is why I have made my own.

In the Unity editor, the effect looks great, but on the iPhone, the scripts work without producing the desired effect. The only detail of the normal map texture on the iPhone seems to be near the edges/corners of the objects. Below are screen shots:

In the Unity editor:

alt text

On the iPhone:

alt text

As you can see, the normal mapped texture is significantly more detailed on the editor pic.

Here is my shader:

 Shader "My Shaders/Normal Maps/OptimizedNormal"
 {
     
     Properties
     {
     
         _Color( "ColorTint", Color ) = ( 1.0, 1.0, 1.0, 1.0 )
         _BumpMap( "NormalTexture", 2D ) = "bump" {}
         _BumpDepth( "BumpDepth", Range( 0.0, 1.0 ) ) = 1
         _SpecColor( "SpecularColor", Color ) = ( 1.0, 1.0, 1.0, 1.0 )
         _Shininess( "Shininess", float ) = 10
     }
     
     SubShader
     {
         
         Pass
         {
         
             Tags { "LightMode" = "ForwardBase" }
             
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma fragmentoption ARB_precision_hint_fastest
             
             //user defined variables
             uniform fixed4 _Color;
             uniform sampler2D _BumpMap;
             uniform fixed _BumpDepth;
             uniform half4 _BumpMap_ST;
             uniform fixed4 _SpecColor;
             uniform half _Shininess;
             
             //unity defined variables
             uniform half4 _LightColor0;
             
             //base input structs
             struct vertexInput
             {
             
                 half4 vertex : POSITION;
                 half3 normal : NORMAL;
                 half4 texcoord : TEXCOORD0;
                 half4 tangent : TANGENT;
                 fixed4 color : COLOR;
             };
             
             struct vertexOutput
             {
             
                 half4 pos : SV_POSITION;
                 half4 tex : TEXCOORD0;
                 fixed4 lightDirection : TEXCOORD1;
                 fixed3 viewDirection : TEXCOORD2;
                 fixed3 normalWorld : TEXCOORD3;
                 fixed3 tangentWorld : TEXCOORD4;
                 fixed3 binormalWorld : TEXCOORD5;
                 fixed4 color : COLOR;
             };
             
             
             //vertex function
             vertexOutput vert( vertexInput v )
             {
             
                 vertexOutput o;
                 o.normalWorld = normalize( mul( half4( v.normal, 0.0 ), _World2Object ).xyz );
                 o.tangentWorld = normalize( mul( _Object2World, v.tangent ).xyz );
                 o.binormalWorld = normalize( cross( o.normalWorld, o.tangentWorld ) * v.tangent.w );
                 
                 half4 posWorld = mul( _Object2World, v.vertex );
                 o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
                 o.tex = v.texcoord;
                 o.color = v.color;
                 
                 o.viewDirection = normalize( _WorldSpaceCameraPos.xyz - posWorld.xyz );
                 
                 half3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - posWorld.xyz;
                 
                 o.lightDirection = fixed4( 
                     normalize( lerp( _WorldSpaceLightPos0.xyz, fragmentToLightSource, _WorldSpaceLightPos0.w ) ),
                     lerp( 1.0, 1.0/length( fragmentToLightSource ), _WorldSpaceLightPos0.w ) );
                 
                 return o;
             }
             
             //fragment function
             fixed4 frag( vertexOutput i ) : COLOR
             {
                 
                 //texture Maps
                 fixed4 texN = tex2D( _BumpMap, i.tex.xy * _BumpMap_ST.xy + _BumpMap_ST.zw );
                 
                 //unPackNormal function
                 fixed3 localCoords = fixed3( 2.0 * texN.ag - float2( 1.0, 1.0 ), _BumpDepth );
                 
                 //normal transpose matrix
                 fixed3x3 local2WorldTranspose = fixed3x3(
                     i.tangentWorld,
                     i.binormalWorld,
                     i.normalWorld
                 );
                 
                 //calculate normal direction
                 fixed3 normalDirection = normalize( mul( localCoords, local2WorldTranspose ) );
                 
                 //Lighting
                 //dot product
                 fixed nDotL = saturate( dot( normalDirection, i.lightDirection.xyz ) );
                 
                 fixed3 diffuseReflection = i.lightDirection.w * _LightColor0.xyz * nDotL;
                 fixed3 specularReflection = diffuseReflection * _SpecColor.xyz * pow( saturate( dot( reflect( -i.lightDirection.xyz, normalDirection ), i.viewDirection ) ), _Shininess);
                 
                 fixed3 lightFinal = UNITY_LIGHTMODEL_AMBIENT.xyz + diffuseReflection + ( specularReflection );
                 
                 return fixed4( lightFinal * i.color, 1.0 );
             }
             
             ENDCG
         }
     }
     //Fallback "Diffuse"
 }
 

Am I missing some important commands for the shader to communicate more effectively with the iPhone?

screen shot 2013-10-05 at 1.10.09 am.png (148.4 kB)
screenshotgame.png (91.0 kB)
Comment
Add comment · Show 3
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 meat5000 ♦ · Oct 05, 2013 at 07:10 PM 0
Share

I think what you are observing there is a lack of shadowing on the iPhone

avatar image highpockets · Oct 05, 2013 at 10:08 PM 0
Share

Are you certain?? If this is true, it's very disappointing. I'm crushed!! I guess it's my own fault for not knowing the limitations. Does anybody have a work around other than a more detailed/high poly mesh? Do you think I can over-exaggerate the depth/height of my normal map?

avatar image reefwirrax · Nov 04, 2013 at 07:57 PM 0
Share

I don# t know if you could say that is optimised, it does most of the ambient lighting calculations in fragment shader for each pixel, you can do them for each vertex. I don t know if it makes much difference given the bump map tho

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

Shader and normal map working when editing, but sprite turns black when running with animations 2 Answers

ES2.0 Diffuse Shader? 0 Answers

Why does this shader show up black on iOS? 1 Answer

Shading with cube maps on iPhone 3GS for Unity 3 beta 1 Answer

Mobile Bumped Diffuse problem 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