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
0
Question by RobotRocker · Jun 12, 2014 at 08:00 PM · lightingshadersnormals

Can I retrieve per face normals instead of per vertex normals in vertex shader?

I'm learning shaders and want to render a cube with each face lit based on the normal of the face rather than the normal of the vertex.

The cube I am using is a mesh I generate in code that only has 1 vertex per corner of the cube which is why the per vertex normals will face outward from the center of the cube, rather than normal of the face. One solution is to of course have 4 vertices per face but that would be unnecessary cost if I can just retrieve the normal of the face.

I'm currently using this shader:

 Shader "Custom/AmbientLambertColorShader" {
     Properties {
     }
     SubShader {
         Tags{ "LightMode"="ForwardBase"}
         Pass {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             
             //unity defined variable
             uniform float4 _LightColor0;
 
             //input structs
             struct vertexInput {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
                 float4 col : COLOR;
             };
             
             struct vertexOutput {
                 float4 pos : SV_POSITION;
                 float4 col : COLOR;
             };
             
             //vertex function
             vertexOutput vert(vertexInput v) {
                 vertexOutput o;
                 
                 float3 normalDirection = normalize(mul(float4(v.normal, 0.0), _World2Object).xyz);
                 float3 lightDirection;
                 float atten = 1.0;
                 
                 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
                 
                 float3 diffuseReflection = atten * _LightColor0.xyz * max( 0.0, dot(normalDirection, lightDirection));
                 float3 lightFinal = diffuseReflection + UNITY_LIGHTMODEL_AMBIENT.xyz;
             
                 
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
 //                o.col = float4(lightFinal * v.col.rgb,1.0);
                 o.col = float4(normalDirection,1.0);
                 
                 return o;
             }
             
             //Fragment function
             float4 frag(vertexOutput i) : COLOR {
                 return i.col;
             }
             
             ENDCG
         }
     } 
 //    FallBack "Diffuse"
 }


Thank you for any thoughts.

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
0

Answer by RobotRocker · Jun 29, 2014 at 06:31 AM

For posterity I will say I did not find an answer to this so I assume it's no.

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

2 People are following this question.

avatar image avatar image

Related Questions

Reflections not seamless 1 Answer

I'm computing my surface normals in the fragment program - so why does Unity still require per-vertex normals/tangents? 1 Answer

How to write unlit surface shader? 6 Answers

How to calculate light from behind a quad? (and other light positions) 1 Answer

Double sided geometry. 3 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