• 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 /
  • Help Room /
avatar image
0
Question by Canhuo233 · Aug 22, 2018 at 08:44 AM · shaderlab

undeclared identifier 'unity_WorldToLight' in Unity2018

In Unity2018, although i have included "AutoLight.cginc",there's still a error when i used the matrix "unity_WorldToLight". Sometimes I will get the right rendering.But if i change code in somewhere,such as add a fallback specular,the rendering may be wrong,and if I restart Unity2018,the result becomes right again.

here is the code

 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
 
 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
 
 // Upgrade NOTE: replaced '_LightMatrix0' with 'unity_WorldToLight'
 
 // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
 
 Shader "Unlit/ForwardRendering"
 {
     Properties
     {
         m_diffuse("Diffuse",Color) = (1,1,1,1)
         m_specular("Specular",Color) = (1,1,1,1)
         m_gloss("Gloss",Range(1,10)) = 1
     }
     SubShader
     {
         Pass{
             Tags{"LightMode" = "ForwardBase"}
             CGPROGRAM
             #include "UnityCG.cginc"
             #include "Lighting.cginc"
             #pragma multi_compile_fwdbase
             #pragma vertex vert
             #pragma fragment frag
 
             fixed3 m_diffuse;
             fixed3 m_specular;
             float m_gloss;
             
             struct v2f{
                 float4 pos:SV_POSITION;
                 float3 worldVertex:TEXCOORD1;
                 float3 worldNormal:TEXCOORD2;
             };
 
             v2f vert(appdata_full v){
                 v2f o;
                 o.pos = UnityObjectToClipPos(v.vertex);
                 o.worldNormal = UnityObjectToWorldNormal(v.normal);
                 o.worldVertex = mul(unity_ObjectToWorld,v.vertex).xyz;
                 return o;
             }
 
             fixed4 frag(v2f i):SV_TARGET{
                 float3 worldNormal = normalize(i.worldNormal);
                 float3 worldView = normalize(UnityWorldSpaceViewDir(i.worldVertex));
                 float3 worldLight = normalize(UnityWorldSpaceLightDir(i.worldVertex));
 
                 fixed3 diffuse = _LightColor0.rgb * m_diffuse.rgb*saturate(dot(worldNormal,worldLight));
                 fixed3 specular = _LightColor0.rgb * m_specular.rgb * pow(saturate(dot(worldNormal,normalize(worldView+worldLight))),m_gloss);
                 return fixed4(diffuse+specular*0.5,1);
             }
 
             ENDCG
         }
 
         Pass{
             Tags{"LightMode" = "ForwardAdd"}
             blend one one
             CGPROGRAM
             
             #pragma multi_compile_fwdadd
             #pragma vertex vert
             #pragma fragment frag
             #include "AutoLight.cginc"
             #include "UnityCG.cginc"
             #include "Lighting.cginc"
 
             fixed3 m_diffuse;
             fixed3 m_specular;
             float m_gloss;
             
             struct v2f{
                 float4 pos:SV_POSITION;
                 float3 worldVertex:TEXCOORD1;
                 float3 worldNormal:TEXCOORD2;
             };
 
             v2f vert(appdata_full v){
                 v2f o;
                 o.pos = UnityObjectToClipPos(v.vertex);
                 o.worldNormal = UnityObjectToWorldNormal(v.normal);
                 o.worldVertex = mul(unity_ObjectToWorld, v.vertex).xyz;
                 return o;
             }
 
             fixed4 frag(v2f i):SV_TARGET{   
                 float3 worldNormal = normalize(i.worldNormal);
                 float3 worldView = normalize(UnityWorldSpaceViewDir(i.worldVertex));
                 float3 worldLight = normalize(UnityWorldSpaceLightDir(i.worldVertex));
 
                 fixed3 diffuse = _LightColor0.rgb * m_diffuse.rgb*saturate(dot(worldNormal,worldLight));
                 fixed3 specular = _LightColor0.rgb * m_specular.rgb * pow(saturate(dot(worldNormal,normalize(worldView+worldLight))),m_gloss);
 
                 float3 lightVertex = mul(unity_WorldToLight,float4(i.worldVertex,1)).rgb;
                 float atten = tex2D(_LightTexture0,dot(lightVertex,lightVertex).xx).UNITY_ATTEN_CHANNEL;
                 return fixed4((diffuse+specular)*atten,1);
             }
 
             ENDCG
         }
     }
     
 }
 
qq截图20180822163458.png (179.4 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

Answer by ReCoding · Apr 25, 2019 at 04:08 PM

I found a way that might be correct. Use "if defined ()" to surround the area using "unity_WorldToLight". like this:

  #if defined (POINT)
         float3 lightCoord = mul(unity_WorldToLight, float4(i.worldPos, 1)).xyz;
         fixed atten = tex2D(_LightTexture0, dot(lightCoord, lightCoord).rr).UNITY_ATTEN_CHANNEL;
     #elif defined (SPOT)
         float4 lightCoord = mul(unity_WorldToLight, float4(i.worldPos, 1));
         fixed atten = (lightCoord.z > 0) * tex2D(_LightTexture0, lightCoord.xy / lightCoord.w + 0.5).w * tex2D(_LightTextureB0, dot(lightCoord, lightCoord).rr).UNITY_ATTEN_CHANNEL;
     #else
         fixed atten = 1.0;
     #endif
 
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Shadowcaster Fragment Shader Discard 0 Answers

Operate the shader, rotate the material 90 degrees 0 Answers

Why is it necessary to transform vertex coordinate in vertex shader? 1 Answer

How to affect Shader _Color by the Tint Color of Button (Through Shader) 0 Answers

Is there any way to do multiple passes on a texture / in a fragment 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