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
1
Question by benbenben · Aug 14, 2013 at 05:09 PM · shaderlightingcg

Shader behaves different in other scene / project

So I worked on a shader in a test environment and when I imported it into my real project, it produced different results. Both scenes have the same models, use the same textures, the same shader and have exactly one Point Light with same color and range. I seems as if the specular part is almost zero in the second scene. Here is one image from each scene (ignore the floor on the second one, that's a different material). The shader is applied to the material of the brick walls. alt text alt text

The second one is missing those specular highlights.

Shader code:

 Shader "Shadershadershader"
 {
     Properties
     {
         _MainTex( "Diffuse Texture", 2D ) = "white" {}
         _Shininess ( "Shininess", Float ) = 10
     }
     SubShader
     {
         Pass
         {
             Tags { "LightMode" = "ForwardBase" }
             CGPROGRAM
 
             #pragma vertex vert  
             #pragma fragment frag 
  
             // user-specified uniforms
             uniform sampler2D _MainTex;
             uniform float _Shininess;
 
             // built-in uniforms
             uniform float4 _LightColor0;
             
             struct vertexInput
             {
                 float4 position : POSITION;
                 float3 normal : NORMAL;
                 float4 color : COLOR;
                 float4 texcoord : TEXCOORD0;
             };
             
             struct vertexOutput
             {
                 float4 position : SV_POSITION;
                 float4 posWorld : TEXCOORD0;
                 float3 normal : TEXCOORD1;
                 float4 color : COLOR;
                 float4 tex : TEXCOORD2;
             };
 
             vertexOutput vert( vertexInput input )
             {
                 vertexOutput output;
 
                 float4x4 modelMatrix = _Object2World;
                 float4x4 modelMatrixInverse = _World2Object;
 
                 output.position = mul( UNITY_MATRIX_MVP, input.position );
                 
                 output.posWorld = mul( modelMatrix, input.position );
                 
                 output.normal = normalize( float3(
                     mul( float4( input.normal, 0.0 ), modelMatrixInverse ) ) );
                 // vertex color set to white for testing    
                 output.color = float4(1.0);
 
                 output.tex = input.texcoord;
 
                 return output;
             }
 
             struct fragmentOut
             {
                 float4 color : COLOR;
             };
 
             fragmentOut frag( vertexOutput input )
             {
                 fragmentOut output;
 
                 float3 normalDirection = normalize( input.normal );
                 
                 float3 viewDirection = normalize(
                     _WorldSpaceCameraPos - float3( input.posWorld ) );
                 float3 lightDirection;
                 float attenuation;
 
                 if ( 0.0 == _WorldSpaceLightPos0.w ) // directional light?
                 {
                     attenuation = 1.0;
                     lightDirection = normalize( float3(_WorldSpaceLightPos0) );
                 }
                 else // point or spot light
                 {
                     float3 vertexToLightSource =
                         float3( _WorldSpaceLightPos0 - input.posWorld );
                     float distance = length( vertexToLightSource );
                     attenuation = 1.0 / distance;
                     lightDirection = normalize( vertexToLightSource );
                 }
 
                 float3 ambientLighting = float3( 0.1, 0.1, 0.1 );
 
                 float3 diffuseReflection =
                     attenuation * float3( _LightColor0 ) * float3( 1.0 ) *
                     max( 0.0, dot( normalDirection, lightDirection ) ) *
                     tex2D( _MainTex, float2( input.tex ) );
 
                 float3 specularReflection;
                 if ( dot( normalDirection, lightDirection ) < 0.0 ) // light on other side
                 {
                     specularReflection = float3( 0.0 );
                 }
                 else
                 {
                     // use diffuse texture as specular map
                     specularReflection = attenuation * float3( 1.0 ) *
                         float3(1.0) * pow( max( 0.0,
                         dot( reflect( -lightDirection, normalDirection ), viewDirection ) ),
                         _Shininess ) *
                         tex2D( _MainTex, float2( input.tex ) );
                 }
                 
                 float shadowing = input.color.r;
                                 
                 output.color = float4( ambientLighting+shadowing*(diffuseReflection+specularReflection), 1.0 );
                 
 
                 //output.color = input.color;
                 return output;
             }
 
             ENDCG
         }
 
         Pass
         {
             Tags { "LightMode" = "ForwardAdd" }
 
             Blend One One
 
             CGPROGRAM
 
             
             #pragma vertex vert  
             #pragma fragment frag 
  
             // user-specified uniforms
             uniform sampler2D _MainTex;
             uniform float _Shininess;
 
             // built-in uniforms
             uniform float4 _LightColor0;
             
             struct vertexInput
             {
                 float4 position : POSITION;
                 float3 normal : NORMAL;
                 float4 color : COLOR;
                 float4 texcoord : TEXCOORD0;
             };
             
             struct vertexOutput
             {
                 float4 position : SV_POSITION;
                 float4 posWorld : TEXCOORD0;
                 float3 normal : TEXCOORD1;
                 float4 color : COLOR;
                 float4 tex : TEXCOORD2;
             };
 
             vertexOutput vert( vertexInput input )
             {
                 vertexOutput output;
 
                 float4x4 modelMatrix = _Object2World;
                 float4x4 modelMatrixInverse = _World2Object;
 
                 output.position = mul( UNITY_MATRIX_MVP, input.position );
                 
                 output.posWorld = mul( modelMatrix, input.position );
                 
                 output.normal = normalize( float3(
                     mul( float4( input.normal, 0.0 ), modelMatrixInverse ) ) );
                 // vertex color set to white for testing    
                 output.color = float4(1.0);
 
                 output.tex = input.texcoord;
 
                 return output;
             }
 
             struct fragmentOut
             {
                 float4 color : COLOR;
             };
 
             fragmentOut frag( vertexOutput input )
             {
                 fragmentOut output;
 
                 float3 normalDirection = normalize( input.normal );
                 
                 float3 viewDirection = normalize(
                     _WorldSpaceCameraPos - float3( input.posWorld ) );
                 float3 lightDirection;
                 float attenuation;
 
                 if ( 0.0 == _WorldSpaceLightPos0.w ) // directional light?
                 {
                     attenuation = 1.0;
                     lightDirection = normalize( float3(_WorldSpaceLightPos0) );
                 }
                 else // point or spot light
                 {
                     float3 vertexToLightSource =
                         float3( _WorldSpaceLightPos0 - input.posWorld );
                     float distance = length( vertexToLightSource );
                     attenuation = 1.0 / distance;
                     lightDirection = normalize( vertexToLightSource );
                 }
 
                 float3 diffuseReflection =
                     attenuation * float3( _LightColor0 ) * float3( 1.0 ) *
                     max( 0.0, dot( normalDirection, lightDirection ) ) *
                     tex2D( _MainTex, float2( input.tex ) );
 
                 float3 specularReflection;
                 if ( dot( normalDirection, lightDirection ) < 0.0 ) // light on other side
                 {
                     specularReflection = float3( 0.0 );
                 }
                 else
                 {
                     // use diffuse texture as specular map
                     specularReflection = attenuation * float3( 1.0 ) *
                         float3(1.0) * pow( max( 0.0,
                         dot( reflect( -lightDirection, normalDirection ), viewDirection ) ),
                         _Shininess ) *
                         tex2D( _MainTex, float2( input.tex ) );;
                 }
                 
                 float shadowing = input.color.r;
                                 
                 output.color = float4( shadowing*(diffuseReflection+specularReflection), 1.0 );
                 
 
                 //output.color = input.color;
                 return output;
             }
 
             ENDCG
         }
     }
     //FallBack "Diffuse"
 }

The shader is a modified version of this tutorial: http://en.wikibooks.org/wiki/Cg_Programming/Unity/Smooth_Specular_Highlights

I want the walls to look like they do in the first image. What could cause the differences?

shaderproblem1.png (31.9 kB)
shaderproblem2.png (93.6 kB)
Comment
Add comment · Show 2
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 Teh_Bucket · Aug 14, 2013 at 05:58 PM 0
Share

Just in case, check your render settings for both scenes.

avatar image benbenben · Aug 14, 2013 at 06:02 PM 0
Share

Render settings are identical except for the ambient light property, which is not used by the shader and playing with it causes no difference.

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

16 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

Related Questions

Shader: get nearest point light's color, direction and general custom lighting questions 0 Answers

Accessing light's range value within a fragment shader? 0 Answers

Problem with lighting in CG Shader 1 Answer

Custom ambient lighting color in a Cg shader 2 Answers

How can I get correct lighting on a low poly water shader? 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