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 hulahoolgames · Jan 25, 2016 at 01:44 AM · errorshader programmingfloats

Shader float value weirdness

 Shader "Shader Tuts/Lambert" {
     Properties {
         _Color ("Color", Color) = (1.0,1.0,1.0,1.0)
         _MainTex ("Main Tex", 2D) = "white" {}
         _BumpMap ("Bump Map", 2D) = "bump" {}
         _BumpDepth ("Bump Depth", Range(-2.0, 2.0)) = 0.0
         _SpecColor ("Specular Color", Color) = (1.0, 1.0, 1.0, 1.0)
         _Shininess ("Shininess", float) = 0.0
     }
 
     SubShader {
         Pass {
             Tags {
                 "LightMode" = "ForwardBase"
             }
 
             CGPROGRAM
                 // pragmas
                 #pragma vertex vert
                 #pragma fragment frag
 
                 // 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 _Shininess;
                 uniform float _BumpDepth;
 
                 // inbuilt variables
                 uniform float4 _LightColor0;
 
                 // base input structs
                 struct vertexInput {
                     float4 vertex : POSITION;
                     float3 normal : NORMAL;
                     float4 texcoord : TEXCOORD0;
                     float4 tangent : TANGENT;
                 };
 
                 struct vertexOutput {
                     float4 pos : SV_POSITION;
                     float4 tex : TEXCOORD0;
                     float4 posWorld : TEXCOORD1;
                     float3 normalWorld : TEXCOORD2;
                     float3 tangentWorld : TEXCOORD3;
                     float3 binormalWorld : TEXCOORD4;
                 };
 
                 // vertex program
                 vertexOutput vert(vertexInput v) {
                     vertexOutput o;
 
                     o.posWorld = mul(_Object2World, v.vertex);
                     // convert normal from object space to world space and normalize
                     o.normalWorld = normalize(mul(float4(v.normal, 0.0f), _World2Object));
                     o.tangentWorld = normalize(mul(_Object2World, v.tangent));
                     o.binormalWorld = normalize(cross(o.normalWorld, o.tangentWorld) * v.tangent.w);
                     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                     o.tex = v.texcoord;
                     return o;
                 }
 
                 float4 frag(vertexOutput i) : COLOR {
                     // direction calculations
                     float3 lightDir = normalize(_WorldSpaceLightPos0).xyz;
                     float3 viewDir = normalize(_WorldSpaceCameraPos - i.posWorld);
                     float atten = 1.0f;
 
                     // texture calculations
                     float4 tex = tex2D(_MainTex, i.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw);
                     float4 encodedNormal = tex2D(_BumpMap, i.tex.xy * _BumpMap_ST.xy + _BumpMap_ST.zw);
 
                     // unpack normals, Unity has inbuild function but we will create our own since its more efficient
                     float3 localCoords = (2.0 * encodedNormal.ag - float2(1.0,1.0), 0.0);
                     localCoords.z = 1.0 - 0.5 * dot(localCoords, localCoords);
 
                     // normal transpose matrix
                     float3x3 local2WorldTranspose = float3x3(i.tangentWorld,
                                                             i.binormalWorld,
                                                             i.normalWorld);
 
                     float3 normalDir = normalize(mul(localCoords, local2WorldTranspose));
                     float3 reflectedDir = reflect(-lightDir, normalDir);
 
                     // light calculations
                     float3 diffuseReflection = atten * _LightColor0.xyz * max(0.0, dot(normalDir, lightDir));
                     float3 specularReflection = diffuseReflection * _SpecColor.rgb * pow(max(0.0, dot(reflectedDir, viewDir)), _Shininess);
                     float3 finalLight = diffuseReflection + specularReflection + UNITY_LIGHTMODEL_AMBIENT;
 
                     return float4(tex.xyz * finalLight * _Color.rgb , 1.0);
                 }
 
                 // fragment program
             ENDCG
         }
     }
 
     // Fallback "Diffuse"
 }

In the above shader, if I set the value of _Shininess to 0 from the editor I get this result alt text

Instead, if I replace the value of _Shininess to 0 manually in the shader at this line:

 float3 specularReflection = diffuseReflection * _SpecColor.rgb * pow(max(0.0, dot(reflectedDir, viewDir)), 0.0);
 

then I get this result: alt text

What exactly is going on here!? Why does it behave differently when the value is set in the editor and when its hardcoded! Any help would be appreciated. Thank you.

screen-shot-2016-01-24-at-83911-pm.png (193.5 kB)
screen-shot-2016-01-24-at-84158-pm.png (225.5 kB)
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
1
Best Answer

Answer by Teravisor · Jan 25, 2016 at 12:08 PM

It's result of pow(x,0) always being 1 and due to how pow(x,y) is compiled. If y is not known at compile time, it is compiled as exp(y*log(x))otherwise it's compiled as several multiplications. So if you exchange your pow(max(0.0,dot(reflectedDir,viewDir)), 0) to exp(0*log(max(0.0,dot(reflectedDir,viewDir)))) you will get same effect as when you use pow(x,y) with y not being constant. It gets black when that max(...) statement returns 0 and log(0) is defined as -INF if I remember right (shader default, not mathematical).

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 hulahoolgames · Jan 27, 2016 at 08:14 PM 0
Share

Thanks for sharing this knowledge. I had no idea about this.

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

39 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 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

why there is a parsing error at the END ? 0 Answers

[Need Help of a expert shader user] How to make following shaders to Fallback to "Diffuse"? 1 Answer

Shader error: incorrect number of arguments to numeric-type constructor 2 Answers

Image effect Shader giving weird error. 0 Answers

pink screen and hdr rendering not supported 0 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