• 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
3
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

73 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

Related Questions

How to add fog to a "Fire" vertex shader 0 Answers

"Floor" function produces artifacts in shader 1 Answer

Chaining Multiple Shaders Together 0 Answers

Shader Variable Types 2 Answers

Mobile performance of splat map shader with distance blending 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges