• 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 TropicalTrevor · Jan 28, 2014 at 01:48 AM · shadercg

cginc errors

I am writing a shader and as it gets rather long intend to begin splitting it up in pieces.

When I put the code in a separate file an use CGINCLUDE #include "main.cginc" ENDCG where this one line simply replaces all code I just had in there I get a syntax error. When I then open the cginc file and add a blank line at the top of the file the error changes into the following:

Program 'frag', syntax error unexpected $undefined at token "" at line 0 Program 'frag', type name expected at token "" at line 0 Program 'vert', illegal character in shader file (compiling for d3d11) at line 0 Program 'vert', syntax error unexpected $undefined at token "" at line 0 Program 'verttype name expected at token "" at line 0 GLSL translate vertex: Failed to find entry function: 'vert' at line 20 GLSL vertex shader: 50: ERROR: '<': syntax error parse error at line 20 Shader program had errors at line 21

Cg code Shader "Custom/CgIncText" { Properties { _BumpMap("Bump map", 2D) = "bump" {} }

     CGINCLUDE
     #include "main.cginc"
     ENDCG
     
     SubShader
     {
         Tags { "RenderType"="Opaque" } 
         
         Pass
         {
             CGPROGRAM
             #pragma target 3.0
             #pragma vertex vert
             #pragma fragment frag
             #pragma exclude_renderers noprepass
             #pragma glsl_no_auto_normalization
             ENDCG
         }
     } 
     FallBack "Diffuse"
 }


Cginc code:

 #define NORMAL_MAP
 //#define OBJECT_SPACE_NORMALS
 //TODO: DERIVATIVE_NORMALS
 
 
 uniform sampler2D _BumpMap;
 
 
 struct a2pb
 {
     fixed4 vertex : POSITION;
     fixed3 normal : NORMAL; 
 #if !defined (OBJECT_SPACE_NORMALS)
     fixed3 tangent : TANGENT;
 #endif
     fixed2 uv : TEXCOORD0;
 };
 
 struct pb2pf
 {
     fixed4 vertex : SV_POSITION;
     fixed3 normal : TEXCOORD1; 
 #if !defined (OBJECT_SPACE_NORMALS)
     fixed3 tangent : TEXCOORD2;
 #endif
     fixed2 uv : TEXCOORD0;
 };
 
 
 pb2pf vert(a2pb v)
 { 
     pb2pf o;
     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);  
 #if defined(NORMAL_MAP)
     #if !defined (OBJECT_SPACE_NORMALS) 
     o.normal = mul(v.normal, (fixed3x3)_World2Object);
     o.tangent = mul((fixed3x3)_Object2World, v.tangent);  
     #endif 
 #else 
     o.normal = mul(v.normal, (fixed3x3)_World2Object);
     o.normal = o.normal * 0.5 + 0.5;
 #endif
     o.uv = v.uv;
     return o;
 } 
 
 fixed4 frag(pb2pf i) : COLOR
 {  
 #if defined(NORMAL_MAP)
     #if defined (OBJECT_SPACE_NORMALS)
     fixed3 n = tex2D(_OSBumpMap, i.uv).xyz;
     n = mul(n * 2 - 1, _World2Object);
     #else 
     fixed3 n;
     n.xy = tex2D(_BumpMap, i.uv).ar * 2 - 1;
     n.z = sqrt(1-dot(n.xy, n.xy));
     fixed3 bitangent = cross(i.normal, i.tangent);
     n = mul(n, fixed3x3(i.tangent, bitangent, i.normal));
     #endif 
     return fixed4(n, 1);
 #else 
     i.normal = normalize(i.normal);
     return fixed4(i.normal, 1); 
 #endif
 }
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
4
Best Answer

Answer by phaez · Feb 11, 2014 at 11:47 PM

Possible duplicate issue: http://stackoverflow.com/questions/10877386/opengl-shader-compilation-errors-unexpected-undefined-at-token-undefined

The error is occuring because you have a garbage character in your include file. Make a brand new text file and use notepad or notepad++ to paste your code into it and save it. Then try compiling again and see if it's fixed.

Comment
Add comment · Show 3 · 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 TropicalTrevor · Feb 12, 2014 at 07:36 AM 0
Share

Thanks a lot! Now I can finally cut up my shader files again... this is really a strange problem. Is it because Unity adds the garbage, or perhaps visual studio does it? I can't imagine anything else as I have Unity create the file and use VS to edit it. Though sometimes I duplicate a .shader in unity and rename it to .cginc, which I believe to remember was what I did here. Annoying little bug, thank you very much for finding this!

avatar image konsnos · Sep 17, 2014 at 01:54 PM 0
Share

Thanks. That worked for me too!

avatar image Witchunter · Nov 07, 2017 at 12:27 PM 0
Share

I had an error regarding not being able to include the Common.cginc file. Opened the file, deleted and rewrote one character to be able to save the file again, and it was fixed.

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

20 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

Related Questions

How to specify my CGIncludes directories? 3 Answers

How to force the compilation of a shader in Unity? 5 Answers

UnityObjectToClipPos is inverted? 0 Answers

Unity 5 upgrade broke lightmapped shader 1 Answer

How to get view space position of a pixel 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