• 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
Question by Wolfram · Sep 26, 2011 at 05:15 PM · shaderviewnormalprojection

Shader: actual view direction/normal?

Hiho.

I only have basic knowledge about shaders, ShaderLab, and CG, and I didn't find my answer in any docs, examples, or by trial&error, so my hope is one of you could help me out.

I am looking for the actual view direction, i.e. the vector from the camera position through the currently rendered pixel, as I am trying to find the angle between that and the normal of the object at that point/along that ray. Or to put it differently, I am looking for the actual orientation of an object/vertex/pixel with respect to an arbitrary screen location, not with respect to the camera plane (see the following example).

I can use this which gives me some view-dependent object space(?) normal:

 float3 viewN = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);

but this vector remains constant, no matter where on the screen the object is (i.e., it uses the constant normal of the camera plane (=the equivalent of transform.forward) for all pixels). And there is something called "viewDir" in surface shaders, if I understand it correctly, but I am not sure what it does precisely.

I don't care if I need to use a fragment shader or a surface shader for that, I am happy with any solution that works.

Thanks! Wolfram

Comment
equalsequals
unityuser94

People who like this

2 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 equalsequals · Jan 31, 2012 at 05:14 PM 0
Share

Did you ever solve this? I am also looking to for the view direction for use in a v2f program

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Julien-Lynge · Jan 31, 2012 at 05:22 PM

I believe that you can get the view direction vector within the surface shader function like so:

struct Input { float3 viewDir; //whatever else you need to define for Input }

void surf (Input IN, inout EditorSurfaceOutput o) { float3 temp = IN.viewDir; //gives the view direction as a float3, so you can access e.g. IN.viewDir.x }

Note that you may have to use a lighting model that takes into account view direction - that's beyond my expertise.

half4 LightingName (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten);
Comment
equalsequals
Steven-1
metaleap
unityuser94
pld
minchio
nomadic
chris-nolet

People who like this

4 Show 2 · 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 equalsequals · Jan 31, 2012 at 07:23 PM 0
Share

getting the view direction in a surface shader is straight-forward like that, yes. But we, or at least I am talking about vertex-to-fragment programs and NOT surface shaders. Surface Shaders do unnecessary calculations for a lot of effects and is often more effective and more economical to write the v2f functions yourself instead of letting the surface shader generate it for you.

avatar image Julien-Lynge · Feb 01, 2012 at 04:57 PM 4
Share

I certainly understand that you don't want a surface shader, but that's what the initial question asked for, so that's what I gave. I don't appreciate you giving me -1 karma for answering the question as asked - you may want to start your own post if your question is markedly different.

avatar image

Answer by equalsequals · Jan 31, 2012 at 07:28 PM

I was able to figure it out here:

 struct v2f {
     float4 pos : SV_POSITION;
     float3 normal : TEXCOORD0; //you don't need these semantics except for XBox360
     float3 viewT : TEXCOORD1; //you don't need these semantics except for XBox360
 };

 v2f vert (appdata_base v)
 {
     v2f o;
     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
     o.normal = normalize(v.normal);
     o.viewT = normalize(WorldSpaceViewDir(v.vertex));//ObjSpaceViewDir is similar, but localspace.
     return o;
 }

Just make sure you have #include "UnityCG.cginc" in there as well.

Cheers

==

Comment
Paulius-Liekis
sanmn19
unityuser94
hbraun87
onanchew

People who like this

5 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Shader using View Projection matrix in Scene View 1 Answer

Previous Model*View*Projection Matrix 0 Answers

Custom Shader: Using Normal, Diffuse, Gloss, and Specular Maps 0 Answers

Project a shape using a mesh 0 Answers

How to not require normals in a surface shader 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