Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 rleguen · Jul 14, 2015 at 09:23 AM · shader programming

How does this shader calculation result in different colors?

I'm reading "Unity Shaders and Effects Cookbook" by Kenny Lammers, and I'm already puzzled on one of the very first examples which uses Properties. Given that _EmissiveColor, _AmbientColor and _MySliderValue are properties set in the Unity Editor, I don't understand how this calculation:

     void surf (Input IN, inout SurfaceOutput o) {
         fixed4 c = pow((_EmissiveColor + _AmbientColor), _MySliderValue);
         o.Albedo = c.rgb;
         o.Alpha = c.a;
     }

... gives an object whose coloration varies depending on lighting/shading. Since the inputs to that pow(...) calculation are all constant values, how can the resulting visible color change from pixel to pixel? Wouldn't the pixel or its position have to one of the inputs?

Comment
Add comment · Show 1
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 Dudledok · Jul 14, 2015 at 09:41 AM 0
Share

Look into the inout variable o. inout means it is both an input and an output, so o.Albedo and o.Alpha change the diffuse colour and transparency values respectively but of o but not the rest. See: http://docs.unity3d.com/$$anonymous$$anual/SL-SurfaceShaders.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Jul 14, 2015 at 09:41 AM

I haven't read the book, so I'm surprised this isn't explained somewhere, but you're creating a surface shader. This is a kind of shader template/helper/wizard, specific to Unity, which automates a lot of the tedious, complicated stuff about lighting, shadows etc. that you'd normally have to write by hand.

A bit earlier on in your code, you probably have a line that looks a bit like this:

 #pragma surface surf Lambert

The "Lambert" specified here tells Unity which lighting model you want to use, and when it compiles your surface shader into "standard" vert/frag shaders it generates the light/shadow code automatically for you based on that model. So this will vary the fragment colour based on the direction, intensity, and colour of incoming lightsources, lightmaps etc. (The Lambert model comes with Unity and is defined in Lighting.cginc, but you can define any custom lighting model you want). More examples at http://docs.unity3d.com/Manual/SL-SurfaceShaderLightingExamples.html

Although, in some senses, surface shaders are easier to use for beginning writing shaders, the fact that they abstract away some of what's going on behind the scenes can make it harder to understand what's really going on. I found this an excellent starter guide to learning "standard" (i.e. vert/frag) shaders in Unity: https://en.wikibooks.org/wiki/Cg_Programming/Unity

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Shader graph equivelent of Unreal's StaticBool? 2 Answers

Making a rather simple shader.shader 0 Answers

Why doesn't my new shader property show up in the inspector? 0 Answers

How to write an additive HDRP shader without glow 0 Answers

Get shadowmap in HDRP with commandBuffer 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