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 /
avatar image
0
Question by laurG · Jul 03, 2017 at 09:20 PM · shadersvertex shaderhlsl

How to reuse HLSL shader code?

Hello!

I needed a set of shaders that curve based on the distance from camera. Basically, I took some of the default HLSL shaders Unity provides and changed the code in the vertex shader to adjust the position of the vertices.

It works great, but I would like to know if there is a way to centralize the code in the vertex shader, as it is the same in all cases, and just pass that code throughout all shaders I need, because only the fragment shader is different.

This is because if I need to change anything in the vertex shader function, I need to change it in 6 different places.

Thank you!

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 Namey5 · Jul 04, 2017 at 10:19 AM

They're called include files. In fact, by default all surface shaders in Unity use include files and you'll find most vert/frag shaders do too. Basically, create an empty text file and call it whatever you want. Then, change the file suffix/type to '.cginc'. Then, inside this file, you don't have to add any special information, you can literally just type the lines you want to be included. For example, if you wanted to reuse just a vertex function, you could simply have the following by itself in an include file (let's call it 'Resources.cginc');

 struct appdata
 {
     float4 vertex : POSITION;
 };
 
 struct v2f 
 {
     float4 pos : SV_POSITION;
 };
 
 v2f vert (appdata v)
 {
     v2f o;
     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
     return o;
 }

Then, in each of your shaders, at the beginning you just need to have the following;

 ...
 #pragma vertex vert
 
 #include "Resources.cginc"
 ...

And by default, the shader will use the vertex program from the include file we created, and therefore you don't even need to type it in this shader (so long as you initialise the vertex function pragma with the same name).

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 Bunny83 · Jul 04, 2017 at 11:09 AM 2
Share

I strongly suggest to have a look at Unity's default include files which are located at:

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

at least on windows10. The main include file is called "UnityCG.cginc". It includes most other includes. It should give you enough examples how you can actually create "define" macros or inline methods.

In theory you could place only a code fragment into a seperate file and include it at the right spot. The #include statement literally includes the code from the specified file at the point where the statement is located.

It is common practise to wrap the include code in an include file into an #ifndef pre-processor tag to avoid that the file might be included several times in which case you would be flooded with errors.

avatar image laurG Bunny83 · Jul 04, 2017 at 12:20 PM 0
Share

@Bunny83 Thank you for the information! Really helps!

avatar image laurG · Jul 04, 2017 at 12:20 PM 0
Share

Thank you for the answer, exactly what I was looking for.

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

71 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

Related Questions

Mobile performance of splat map shader with distance blending 0 Answers

What is the meaning of a vertex shader for a post-processing shader? 1 Answer

2D sprite - jelly, squeeze, squash 0 Answers

How to Mask Vertex Animation? 0 Answers

Has Unity 2018 removed DX11 quad tessellation? 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