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 Waliactico · May 27, 2014 at 03:27 PM · shadershading

Flatten mesh shader

Hi, I want to flat an object in its z with respect to the camera. I'll explain it a bit more: What I want is that a 3D mesh appears as a "plane" in the x-y plane (z axis) in whatever rotation it is. Think of it as if you smash a 3D object against the screen to get a squashed plane out of it. What I tried is setting its Z to something constant in modelview coords, and then convert it to projection, but it isn't working... What am I doing wrong?

 Shader "Test/FlattenMesh" {
 SubShader {
     Pass {
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #include "UnityCG.cginc"
 
         struct v2f {
             float4 pos : SV_POSITION;
             float3 color : COLOR0;
 
         };
 
         v2f vert (appdata_base v)
         {
            float4 origin = mul (UNITY_MATRIX_MV, float4(0,0,0,1));
             v2f o;
             o.pos = mul (UNITY_MATRIX_MV, v.vertex);
             o.pos.z = origin.z;
             o.pos = mul (UNITY_MATRIX_P, o.pos);
             return o;
         }
 
         half4 frag (v2f i) : COLOR
         {
             return half4 (i.color, 1);
         }
         ENDCG
         }
     }
 }
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
1
Best Answer

Answer by DMGregory · May 27, 2014 at 05:39 PM

You're applying the flattening in view space, so this will always flatten the object in the camera's z axis (ie. it will become a flat billboard facing the camera). Is that what you want?

If you'd prefer to flatten in world space, replace UNITY_MATRIX_MV with _Object2World and UNITY_MATRIX_P with UNITY_MATRIX_VP

If you want to flatten in the object's local z axis, do:

 o.pos = mul(UNITY_MATRIX_MVP, v.vertex * float4(1, 1, 0, 1));

to zero out the local z offset of each vertex.

Comment
Add comment · 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 Waliactico · May 27, 2014 at 06:08 PM 0
Share

Nice! It worked flatten it in world space, that is exactly the behaviour I was expecting! Thank you so much!

avatar image Gcs_deV · Sep 01, 2014 at 10:15 PM 0
Share

I'm trying to do the exact same thing but in a surface shader and am having little success.

D$$anonymous$$G, could you please go through the "view space" solution you touched upon?

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vertex lit vs diffuse shading with no shadows? 0 Answers

Foliage shader advices ? 0 Answers

Grab texture from Vertex Shader 0 Answers

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

Shining 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