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 Stampyturtle · Oct 31, 2013 at 01:05 AM · shadernormalsviewartifactsfragment

Why is my shader showing hard edge artifacts on mesh planes?

My shader is being used on all of the objects in my scene and all of them with one exception are showing up fine. However, if the light is small enough that its range can be escaped from view, the ground (a unity mesh plane, no textures applied) has hard edges depicted where the light's range ends. I've verified that this issue is singular to a flat plane by importing a plane from maya (same issue) and replacing the plane with a terrain (uses different terrain shader, no more hard edge). I'm at a loss why this issue only happens for flat planes.

I have a pictures below with two point lights with small range; as you can see the buildings and environmental props are unaffected by the same edge maligning the ground. The shader code is also included below.

alt text

 Shader "UnityQuestionShader"{
     Properties {
         _Color ("Color Tint", Color) = (1.0,1.0,1.0,1.0)
         _MainTex ("Diffuse Texture", 2D) = "white" {}
         _BumpMap ("Normal Texture", 2D) = "bump" {}
         _BumpDepth ("Bump Depth", Range(-2.0,2.0)) = 1
         _SpecColor ("Specular Color", Color) = (1.0,1.0,1.0,1.0)
     }
     SubShader {
         Pass {
             Tags {"LightMode" = "ForwardBase"}
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma target 4.0
             #pragma exclude_renderers flash
             #include "UnityCG.cginc"
             
             //user defined variables
             uniform sampler2D _MainTex;
             uniform float4 _MainTex_ST;
             uniform sampler2D _BumpMap;
             uniform float4 _BumpMap_ST;
             uniform float4 _Color;
             uniform float4 _SpecColor;
             uniform float _BumpDepth;
             
             //unity defined variables
             uniform float4 _LightColor0;
             
             struct vertexOutput{
                 float4 pos : SV_POSITION;
                 float4 tex : TEXCOORD0;
                 float4 posWorld : TEXCOORD1;
                 float3 normalWorld : TEXCOORD2;
                 float3 tangentWorld : TEXCOORD3;
                 float3 binormalWorld : TEXCOORD4;
             };
             
             //vertex Function
             
             vertexOutput vert(appdata_tan v){
                 vertexOutput o;
                 
                 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 );
                 
                 o.posWorld = mul(_Object2World, v.vertex);
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.tex = v.texcoord;
                 
                 return o;
             }
             
             //fragment function
             
             float4 frag(vertexOutput IN) : COLOR
             {
                 float3 viewDirection = normalize( _WorldSpaceCameraPos.xyz - IN.posWorld.xyz );
                 float3 lightDirection;
                 float atten;
                 
                 if(_WorldSpaceLightPos0.w == 0.0){ //directional light
                     atten = 1.0;
                     lightDirection = normalize(_WorldSpaceLightPos0.xyz);
                 }
                 else{
                     float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - IN.posWorld.xyz;
                     float distance = length(fragmentToLightSource);
                     atten = 1.0/distance;
                     lightDirection = normalize(fragmentToLightSource);
                 }
                 //atten = 1.0 / (1.0 + 25.0*distance*distance);
                 
                 //Texture Maps
                 float4 tex = tex2D(_MainTex, IN.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw);
                 float4 texN = tex2D(_BumpMap, IN.tex.xy * _BumpMap_ST.xy + _BumpMap_ST.zw);
                 
                 //unpackNormal function
                 float3 localCoords = float3(2.0 * texN.ag - float2(1.0, 1.0), 0.0);
                 localCoords.z = _BumpDepth;
                 
                 //normal transpose matrix
                 float3x3 local2WorldTranspose = float3x3(
                     IN.tangentWorld,
                     IN.binormalWorld,
                     IN.normalWorld
                 );
                 
                 //calculate normal direction
                 float3 normalDirection = normalize( mul( localCoords, local2WorldTranspose ) );
                 
                 //Lighting
                 float3 ambientLighting = UNITY_LIGHTMODEL_AMBIENT.xyz * _Color.rgb;
                 float3 diffuseReflection = atten * _LightColor0.xyz * saturate(dot(normalDirection, lightDirection));
                 float3 specularReflection = diffuseReflection * _SpecColor.xyz * pow(saturate(dot(reflect(-lightDirection, normalDirection), viewDirection)) , tex.a*128.0);
                 float3 lightFinal = ambientLighting + diffuseReflection + specularReflection; //UNITY_LIGHTMODEL_AMBIENT.xyz
                 
                 float4 litColor;
                 litColor.rgb = tex.rgb * lightFinal * _Color.rgb;
                 litColor.a   = 1.0;
                 
                 return litColor;
             }
             
             ENDCG
             
         }
         Pass {
             Tags {"LightMode" = "ForwardAdd"}
             Blend One One
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma target 4.0
             #pragma exclude_renderers flash
             #include "UnityCG.cginc"
             
             //user defined variables
             uniform sampler2D _MainTex;
             uniform float4 _MainTex_ST;
             uniform sampler2D _BumpMap;
             uniform float4 _BumpMap_ST;
             uniform float4 _Color;
             uniform float4 _SpecColor;
             uniform float _BumpDepth;
             
             //unity defined variables
             uniform float4 _LightColor0;
             
             struct vertexOutput{
                 float4 pos : SV_POSITION;
                 float4 tex : TEXCOORD0;
                 float4 posWorld : TEXCOORD1;
                 float3 normalWorld : TEXCOORD2;
                 float3 tangentWorld : TEXCOORD3;
                 float3 binormalWorld : TEXCOORD4;
             };
             
             //vertex Function
             
             vertexOutput vert(appdata_tan v){
                 vertexOutput o;
                 
                 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 );
                 
                 o.posWorld = mul(_Object2World, v.vertex);
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.tex = v.texcoord;
                 
                 return o;
             }
             
             //fragment function
             
             float4 frag(vertexOutput IN) : COLOR
             {
                 float3 viewDirection = normalize( _WorldSpaceCameraPos.xyz - IN.posWorld.xyz );
                 float3 lightDirection;
                 float atten;
                 
                 if(_WorldSpaceLightPos0.w == 0.0){ //directional light
                     atten = 1.0;
                     lightDirection = normalize(_WorldSpaceLightPos0.xyz);
                 }
                 else{
                     float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - IN.posWorld.xyz;
                     float distance = length(fragmentToLightSource);
                     atten = 1.0/distance;
                     lightDirection = normalize(fragmentToLightSource);
                 }
                 //atten = 1.0 / (1.0 + 25.0*distance*distance);
                 
                 //Texture Maps
                 float4 tex = tex2D(_MainTex, IN.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw);
                 float4 texN = tex2D(_BumpMap, IN.tex.xy * _BumpMap_ST.xy + _BumpMap_ST.zw);
                 
                 //unpackNormal function
                 float3 localCoords = float3(2.0 * texN.ag - float2(1.0, 1.0), 0.0);
                 localCoords.z = _BumpDepth;
                 
                 //normal transpose matrix
                 float3x3 local2WorldTranspose = float3x3(
                     IN.tangentWorld,
                     IN.binormalWorld,
                     IN.normalWorld
                 );
                 
                 //calculate normal direction
                 float3 normalDirection = normalize( mul( localCoords, local2WorldTranspose ) );
                 
                 //Lighting
                 float3 ambientLighting = UNITY_LIGHTMODEL_AMBIENT.xyz * _Color.rgb;
                 float3 diffuseReflection = atten * _LightColor0.xyz * saturate(dot(normalDirection, lightDirection));
                 float3 specularReflection = diffuseReflection * _SpecColor.xyz * pow(saturate(dot(reflect(-lightDirection, normalDirection), viewDirection)) , tex.a*128.0);
                 float3 lightFinal = ambientLighting + diffuseReflection + specularReflection; //UNITY_LIGHTMODEL_AMBIENT.xyz
                 
                 float4 litColor;
                 litColor.rgb = tex.rgb * lightFinal * _Color.rgb;
                 litColor.a   = 1.0;
                 
                 return litColor;
             }
             
             ENDCG
             
         }
 
     }
     //Fallback "Specular"
 }


hard edge error2.png (183.9 kB)
Comment
Add comment · Show 1
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 deadlyGolum · Mar 27, 2014 at 09:00 AM 0
Share

Did you found any solution for that, cause i am having the same problem.

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

What are normals? 1 Answer

Getting a pixel's screen-space coordinate in a fragment shader 1 Answer

Normal from _CameraDepthNormals to screen space angle 1 Answer

Normals for Displacement Shader 0 Answers

Add a slider to bumped specular shader for normal strength? 3 Answers


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