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 whydoidoit · Oct 17, 2012 at 05:56 AM · shaderdirectional lightvertexlit

Shader lighting is too dark

Cross posted on the Unity Forums here.

Ok, let me preface this with: this is the first vertex/fragment shader I have written. I have a surface shader version of this that is working well - but I need it to work in the Vertex pipeline. The shader basically uses a reference map to recolour various parts of a combined mesh with different colours. That works fine. The problem is that it's too dark! The shader is written to handle one directional light, but I've also tried calling ShadeVertexLights and it's has the same effect.

 Shader "Custom/ColorRemappingVertexLit" {
     Properties {
         _Color ("Main Color", Color) = (1,1,1,1)
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _Color1Tex ("Color1 (RGB)", 2D) = "white" {}
         _Color2Tex ("Color2 (RGB)", 2D) = "white" {}
         _Color3Tex ("Color3 (RGB)", 2D) = "white" {}
     }
     SubShader {
             Lighting On
         
         Tags { "RenderType"="Opaque" "IgnoreProjector"="True" "LightMode" = "Vertex" }
         Pass {
         
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #define USING_DIRECTIONAL_LIGHT
             #include "UnityCG.cginc"
             
     
         
             sampler2D _MainTex;
             sampler2D _Color1Tex;
             sampler2D _Color2Tex;
             sampler2D _Color3Tex;
             float4 _Color; 
             float4 _MainTex_ST;
             float4 _Color1Tex_ST;
             
             struct a2v
             {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
                 float4 texcoord : TEXCOORD0;
                 float4 texcoord1 : TEXCOORD1;
             };
             
             struct v2f
             {
                 float4 pos : SV_POSITION;
                 float2 uv  : TEXCOORD0;
                 float2 uv2 : TEXCOORD1;
                 float4 color : COLOR;
             };
 
             v2f vert (a2v v)
             {
                 v2f o;
                 o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
                 o.uv = TRANSFORM_TEX (v.texcoord, _MainTex); 
                 o.uv2 = TRANSFORM_TEX (v.texcoord1, _Color1Tex);
                 float3 n = mul(UNITY_MATRIX_IT_MV, float4 ( v.normal, 1));
                 float LdotN = saturate (dot (normalize(unity_LightPosition[0]), n));
 
                 o.color = UNITY_LIGHTMODEL_AMBIENT * _Color;
 
                 o.color += LdotN * unity_LightColor[0];
                 return o;
             }
             
             half4 frag(v2f i) : COLOR
             {
                 half4 c1 = tex2D (_Color1Tex, i.uv2);
             
                 half4 c = tex2D (_MainTex, i.uv);
                 
                 if(c1.r==1 && c1.g==1 && c1.b==1)
                 {
                     return c * i.color;
                 }
                 else
                 {
                     half4 c2 = tex2D (_Color2Tex, i.uv2);
                     half4 c3 = tex2D (_Color3Tex, i.uv2);
                     half3 f = saturate(((c1.rgb * c.r) + (c2.rgb * c.g) + (c3.rgb * c.b))) * i.color;
                     return half4(f, c.a);
     
                 }
                 
             }
             ENDCG
         }
     } 
     FallBack "Diffuse"
 }

alt textalt text

screen shot 2012-10-17 at 06.57.20.png (31.0 kB)
screen shot 2012-10-17 at 06.56.59.png (34.2 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 whydoidoit · Oct 17, 2012 at 08:34 AM

Thanks to help on the forum, it was pointed out to me that the lighting needs to be doubled, this is a fudge factor to have a smoother lit scene.

The new code looks like this and works fine:

 Shader "Custom/ColorRemappingVertexLit" {
     Properties {
         _Color ("Main Color", Color) = (1,1,1,1)
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _Color1Tex ("Color1 (RGB)", 2D) = "white" {}
         _Color2Tex ("Color2 (RGB)", 2D) = "white" {}
         _Color3Tex ("Color3 (RGB)", 2D) = "white" {}
     }
     SubShader {
             Lighting On
         
         Tags { "RenderType"="Opaque" "IgnoreProjector"="True" "LightMode" = "Vertex" }
         Pass {
         
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #define USING_DIRECTIONAL_LIGHT
             #include "UnityCG.cginc"
             
     
         
             sampler2D _MainTex;
             sampler2D _Color1Tex;
             sampler2D _Color2Tex;
             sampler2D _Color3Tex;
             float4 _Color; 
             float4 _MainTex_ST;
             float4 _Color1Tex_ST;
             
             struct a2v
             {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
                 float4 texcoord : TEXCOORD0;
                 float4 texcoord1 : TEXCOORD1;
             };
             
             struct v2f
             {
                 float4 pos : SV_POSITION;
                 float2 uv  : TEXCOORD0;
                 float2 uv2 : TEXCOORD1;
                 float4 color : COLOR;
             };
 
             v2f vert (a2v v)
             {
                 v2f o;
                 o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
                 o.uv = TRANSFORM_TEX (v.texcoord, _MainTex); 
                 o.uv2 = TRANSFORM_TEX (v.texcoord1, _Color1Tex);
                 float3 n = normalize(mul(UNITY_MATRIX_IT_MV, float4 ( v.normal, 1)));
                 float LdotN =  dot (unity_LightPosition[0], n);
 
                 o.color = UNITY_LIGHTMODEL_AMBIENT * _Color * 2    ;
 
                 o.color += (LdotN * unity_LightColor[0]) * _Color * 2;
                 return o;
             }
             
             half4 frag(v2f i) : COLOR
             {
                 half4 c1 = tex2D (_Color1Tex, i.uv2);
             
                 half4 c = tex2D (_MainTex, i.uv);
                 
                 if(c1.r==1 && c1.g==1 && c1.b==1)
                 {
                     return c * i.color * 2;
                 }
                 else
                 {
                     half4 c2 = tex2D (_Color2Tex, i.uv2);
                     half4 c3 = tex2D (_Color3Tex, i.uv2);
                     half3 f = saturate(((c1.rgb * c.r) + (c2.rgb * c.g) + (c3.rgb * c.b))) * i.color;
                     return half4(f, c.a) * 2;
     
                 }
                 
             }
             ENDCG
         }
     } 
     FallBack "Diffuse"
 }
Comment
Add comment · 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

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vertex Lit Shaders 1 Answer

Realtime Shadow Glitch [Unity Indie: Directional Light] 2 Answers

i have some prefabs in the scene that are not affected by the dark directional light (with images) 1 Answer

Is a Vertex Lit Soft Particle shader possible? 0 Answers

Lens Flare & Auto Hide 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