Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Feb 16, 2018 at 11:15 PM by ModLunar for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ModLunar · Feb 16, 2018 at 08:20 PM · shadersshader programmingpreprocessor

Shader preprocessor macros - define custom vector?

Hey so I've seen lines like this in shaders, where you can define basically compilation constants:

 #define MY_CUSTOM_CONSTANT 2.940386


I was wondering if I could do the same, but with a vector? Maybe something like...

 #define MY_CUSTOM_FLOAT4 float4(0.2, 0.4, 0.6, 1)


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

  • Sort: 
avatar image
2
Best Answer

Answer by ModLunar · Feb 16, 2018 at 11:14 PM

Ah silly me! I was right -- I tested it and it actually worked! There's just not a lot of documentation on some shader topics, so hopefully this helps someone else too. My example from above works:

 #define MY_CUSTOM_FLOAT4 float4(0.2, 0.4, 0.6, 1)


And also here are some other examples that also worked -- it's good with basically any simple Cg/HLSL data type like booleans, integers, and matrices! (I didn't need to try defining any samplers/textures cause those would usually not be constant, or I would want to be able to set from a material via C#/inspector anyway)

 #define DEFAULT_NORMAL float3(0, 0, 1)
 #define DEFAULT_NORMAL_COLOR float4(0.5, 0.5, 1, 1)
 #define TEST_BOOLEANS bool2(true, true)
 #define SOME_UNSIGNED_INTEGER uint(49)
 #define JUST_A_SYMBOL
Comment
Add comment · Show 2 · 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 Bunny83 · Feb 17, 2018 at 07:06 AM 1
Share

Cg / HLSL uses pretty much the standard C perprocessor directives. The documentation even mentions slight differences in the HLSL compiler because HLSL has of course different language tokens. Just look at the examples.


Also keep in $$anonymous$$d that Unity provides many include files for their own shaders and your shader code. Just look at your install folder of Unity. On windows you'll find the cg include folder at

 C:\Program Files\Unity\Editor\Data\CGIncludes\

unless you have installed Unity elsewhere. Unity uses a lot of built-in macros which makes it actually quite difficult to follow what a line of code actually means / does ^^.

avatar image ModLunar Bunny83 · Feb 17, 2018 at 11:08 PM 0
Share

Ah hey Bunny83, great thanks for the help :) I'll take a look at that.

Haha yeah right it's like a wild goose chase sometimes! I wonder if Visual Studio will be able to support intellisense/better syntax highlighting for Unity shaders and Cg/HLSL to make it easier for us to interpret when we're writing shaders :o

Follow this Question

Answers Answers and Comments

83 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

Related Questions

Control light intensity via shader script 0 Answers

LineRenderer Shader to color different parts of the line based on value 0 Answers

Reading and Writing to the same texture 0 Answers

Why are there stretch marks using my triplanar shader? 2 Answers

Unity Shadows Show Through Double Sided shader 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