Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by VivienS · Jan 28, 2011 at 05:16 PM · shaderperformancecgbestpracticescustom-shader

Which way to write a shader in Unity is the best?

So, I've come across three different ways to write shaders in Unity.

First, there's the fixed function pipeline, where you use only ShaderLab code like

... Pass {
    SetTexture [_MainTex] {
            constantColor [_Color]
            Combine texture * constant
        }
} 
...

next, there is the custom surface and lighting function using Cg, looking like this

...
#pragma surface mySurf myLight
struct Input { ... };
void mySurf (Input IN, inout SurfaceOutput o) { ... }
half4 LightingMyLight (SurfaceOutput s, half3 lightDir, half atten) { ... } 
...

and finally, you can write your very own vertex and fragment programs with

...
#pragma vertex vert
#pragma fragment frag
struct v2f { ... };
v2f vert (appdata_base v) { ... }
half4 frag (v2f i) : COLOR { ... }
...

If I understand the documentation right, you can combine them in different ways but also use them (more or less) exclusively to achieve a certain effect.

My question now is: If I'm only interested in simple shaders that can possibly be achieved by either of the above methods (simple light models or unlit, various texture combines, and so on), which one should I choose?

Most of the time, I've found myself using the surface and lighting function method, because I've had previous experience with Cg. But I was wondering if there is maybe a performance gain with the fixed function pipeline or such.

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
7
Best Answer

Answer by Peter G · Mar 20, 2011 at 02:05 PM

Sorry to bring back an old question. I just noticed it looking for something else. Benefits of each:

FixedFunction: This is the fastest method. It provides vertex-lighting and simple texture combiners at the cheapest cost. If you are worried about performance, then you should use fixed-function shaders. They are also the easiest to write since Unity handles the transformations and math for you.

Surface Shaders: Surface shaders are really just vertex and fragment programs at a different abstraction level. AFAIK, Unity compiles that into standard CG code. It lets you determine the look of the surface without having to mess with the low-level stuff. A simple example, think how often you would put this into a CG program float4 vert = mul(UNITY_MATRIX_MVP, input.vertex); This is only one line of code, but it gets the point across. It must be typed in 99% of shaders to position the object properly. That raises the question:why can't I have the compiler put it in for me? This problem is emphasized even more with shadow mapping. So, as Aras points out in his blog posts 1 and 2, the current abstraction level requires programmers to write many of the same statements repeatedly.

Surface shaders handle that stuff including transforming coordinates so that the programmer can focus on the look, not the repetitive chunks of code. It should be compiled down into identical (if not more optimized) code than you would have written by hand.

CG programs: CG programs provide lower level control than surface shaders. With the creation of surface shaders though, they are really only needed for special effects. It is usually a hassle to create lighting and shadowing when Unity could do that for you with the above technique. You only want to use vertex and fragment programs if you are creating a special effect, possibly some sort of GUI.

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

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

No one has followed this question yet.

Related Questions

Get access to previous pass from CG 2 Answers

Modify custom shader to add bump and normal maps 2 Answers

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

Overlay Effect Shader - Very slow in mobile device 1 Answer

ShaderLab on Unity3d Indie 2 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