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
0
Question by avoorhees · Jul 16, 2014 at 09:32 PM · shadersnormal map

Shader has uv seams

Hey there, I'm working on a vertex fragment shader which has a diffuse, normal and specular texture. Everything is working except that the normal map doesn't look correct in the shader. There are seams at all the uv edges and the rest doesn't look right either. I believe the issue is in how I'm doing the normal mapping in the fragment program. It almost looks as if on of the channels in the normal map is flipped, but I tried flipping channels in Photoshop to no avail. Also, I know the problem is not the normal map because I tested it on a builtin Unity shader and it looks fine.

I'm stumped and need to figure this out so any help would really be appreciated.

 Shader "Simple" {    
     Properties {
         _Color ("Lit Color", Color) = (1,1,1,1)
         _SpecColor ("Specular Color", Color) = (1,1,1,1)
         _Shininess ("Specular Polish", Range(0.1,10)) = 1
         _MainTex ("Diffuse Map", 2D) = "white" {}
         _BumpMap ("Normal Map", 2D) = "Bump" {}
     }
 
     SubShader {
         Pass {
             Tags {"LightMode" = "ForwardBase"}
             CGPROGRAM
             #pragma vertex vert 
             #pragma fragment frag
             #include "UnityCG.cginc"
             #pragma target 3.0 
 
             //User defined variables
             uniform fixed4 _SpecColor;
             uniform float _Shininess;
             uniform sampler _MainTex;
             uniform float4 _MainTex_ST;
             uniform sampler2D _BumpMap;
             uniform float4 _BumpMap_ST;
 
             //Structs
             struct vertexInput{
                 float4 vertex : POSITION;
                 float4 texcoord : TEXCOORD0;
                 float3 normal : NORMAL;
                 float4 tangent : TANGENT;
             };
 
             struct vertexOutput{
                 float4 pos : SV_POSITION;
                 float2 uvs : TEXCOORD0;
                 float4 posWorld : TEXCOORD1;
                 float3 normalWorld : TEXCOORD2;
                 float3 tangentWorld : TEXCOORD3;
                 float3 binormalWorld : TEXCOORD4;
             };
 
             //Vertex function
             vertexOutput vert(vertexInput v){
                 vertexOutput o;
                 
                    //Adding normal stuff
                    o.normalWorld = normalize(mul(float4(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);
 
                    //Unity transform position
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
 
                 //World position
                 o.posWorld = mul(_Object2World, v.vertex);
 
                 //UV coordinates
                 o.uvs = v.texcoord;  
 
                 return o;  
             }
 
             //Fragment function
             fixed4 frag(vertexOutput i) : COLOR {
                 
                 //Basic parameters
                 fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
                 float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
                 
                 //Textures
                 float4 difTex = tex2D(_MainTex, i.uvs.xy * _MainTex_ST.xy + _MainTex_ST.zw);
                 float4 norTex = tex2D(_BumpMap, _BumpMap_ST.xy * i.uvs.xy + _BumpMap_ST.zw);
                 
                 //Normal map
                 float3 localCoords = float3(2.0 * norTex.ag - float2(1.0), 0.0); 
                 localCoords.z = sqrt(1.0 - dot(localCoords, localCoords));
                 
                 //Normal matrix
                 float3x3 local2WorldTranspose = float3x3(i.tangentWorld, i.binormalWorld, i.normalWorld);
                 float3 normalDir = normalize(mul(localCoords, local2WorldTranspose));
 
                 //Specular shading
                 float4 specularShading = _SpecColor * pow(saturate(dot(reflect(-lightDir, normalDir), viewDir)), _Shininess);
 
                 //Output
                 return difTex + specularShading; 
 
             }
             ENDCG
         }
     }   
 }

 
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

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

23 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

Related Questions

Making Secondary Map Texture Animation 1 Answer

Why does my bumped diffuse material get lit in a weird way? 1 Answer

Surface Shader apply normal map to object normal without pluggin it into o.Normal 1 Answer

Specular and normal map artefacts problem 3 Answers

Problem with Normal Maps 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