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
1
Question by Pauls · Apr 17, 2013 at 03:04 AM · shaderinputcg

What is struct Input (Shader)?

Hi,

I was wondering what "struct Input" is ("tex2D" just needs for some reason to store the uvs, and it does it automatically with _MainTex in the first parameter?), and how do we know the "name" of what we define in the struct input (uv_MainTex etc.)? Is it an information we have to find in the Lambert shader?

For example :

 SubShader {
       Tags { "RenderType" = "Opaque" }
       CGPROGRAM
       #pragma surface surf Lambert
       struct Input {
           float2 uv_MainTex;
       };
       sampler2D _MainTex;
       void surf (Input IN, inout SurfaceOutput o) {
           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
       }
       ENDCG
     } 



EDIT :

in this example, viewDir takes the viewing angle from Unity... so what's inside "Input" is also directly connected to our properties outside the shader? I am a bit confused between the two examples :

     //...
     struct Input {
         float2 uv_MainTex;
         float2 uv_BumpTex;
         float3 viewDir; //We want to get the viewing angle
     };
     sampler2D _MainTex;
     sampler2D _BumpTex;
     float4 _RimColor;
     float _RimPower;
     void surf (Input IN, inout SurfaceOutput o) {
         o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
         o.Normal = UnpackNormal (tex2D (_BumpTex, IN.uv_BumpTex));
         half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
         //...

(source : http://cgcookie.com/unity/2012/11/26/introduction-to-surface-shaders-in-unity-part-0/)

Thanks

Comment
Add comment · Show 2
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 whydoidoit · Apr 17, 2013 at 02:31 PM 1
Share

$$anonymous$$y Noobs guide to shaders on Unity Gems attempts to demystify some of the "magic" things that happen when you defined variables in the input of a surface shader.

avatar image Pauls · Apr 17, 2013 at 06:54 PM 0
Share

@whydoitdoit thanks whydoitdoit! Your website looks great, thanks for the link!

1 Reply

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

Answer by Owen-Reynolds · Apr 17, 2013 at 02:23 PM

Shaders are very specialized and strange animals. The Input you're looking at is magically interpolated from the vertex data of whichever face the pixel came from. Look up "renderer pipeline" (or GPU or shader pipeline) for an explanation. It's not a Unity thing. OpenGL sources often do the best job explaining it.

For ViewDir, each pixel could figure out the angle to the light itself, but that might be a lot of work. So, the person who wrote your code decided to have each vertex compute ViewDir. Then the pixels "guess" their viewDir, based on surrounding verts.

Suppose you're a pixel near the bottom of a "tip up" triangle. The system will magically produce a weighted average of the three verts for everything -- mostly UVs, and including ViewDir. For a middle bottom pixel, it will average the left and right vert values, with a hint of the top one (it Lerps them -- Lerp means linear interpolation. Non-linear is better, like a spline, but that's too much math.)

If you've ever weight-painted a model you can see this. You paint the verts, and the faces have the interpolated red/blue washing over them.

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 Pauls · Apr 17, 2013 at 06:53 PM 0
Share

@OwenReynolds thanks Owen! It is actually the same as the normal map in purple that we can get with Gimp, and calculated each frame with the camera's view? If it is so, I think I got it. Thanks!

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

13 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

Related Questions

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

Blinn-Phong shader (vertex lit) from scratch. 1 Answer

TEXCOORD 0 1 2 3 for cg shader scripting? 1 Answer

Accessing Light in Cg Shader 1 Answer

Overlay Effect Shader - Very slow in mobile device 1 Answer


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