Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
7
Question by Jens Fursund · Oct 21, 2009 at 12:29 PM · shadergraphicscgglsl

How can I make my own shader include file (.cginc or .glslinc)?

I am wondering how I can create my own shader include file (.cginc or .glslinc), I want to reuse my shader code in many different shaders, and a personalized include file could help me in this regard.

  • Should I write anything special in the file except from the structs/functions/variables I want to include in my shaders?
  • Where should I place it in order for Unity to see it?
  • What should I write in the shader in order for it to pick it up?
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

2 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by Jens Fursund · Oct 21, 2009 at 05:40 PM

This is what I found out:

  • Should I write anything special in the file except from the structs/functions/variables I want to include in my shaders?

Generally you probably want to put include guards (#ifndef) in your file, else it might not work in more than one file, which sort of beats the purpose.

  • Where should I place it in order for Unity to see it?
  • What should I write in the shader in order for it to pick it up?

You can place it anywhere in your Assets folder. Accesing it from the shader can the be done by writing either of the two lines of code below:

#include "Assets/path/to/your/shader.cginc"
#include "Assets/path/to/your/shader.glslinc"
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
avatar image
2

Answer by Aras · Oct 27, 2009 at 05:34 AM

The content of the "included" file is just pasted into the original file. So you can write pretty much anything in there, as long as it's still meaningful when pasted into original shaders.

Where to put it: best probably is next to the shaders (in the same folder). If it's in a different folder, use relative path to include it.

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 thallippoli · Sep 25, 2013 at 12:43 PM 2
Share

Interestingly #pragma's don't seem to work when included from a .cginc file. When I put the #pragma surface directive inside a .cginc file Unity complained that it could not find a surface function reference (not exact wording). $$anonymous$$y guess is that a pass for #pragma's is done before processing #include I find this inconsistent and was also inconvenient in my case.

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

1 Person is following this question.

avatar image

Related Questions

Send data from a inner CG variable back to a Shaderlab property, is it possible? 1 Answer

Shardelab/CG shaders not working on iOS 1 Answer

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

Cg Language incompatibility Desktop VS GLSL Android 0 Answers

Grabpass refraction masking 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