Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Michael-Thomas · Mar 28, 2015 at 11:08 PM · shadermeshunity5scalevertex shader

Vertex Shader and Non-uniformly scaled meshes

I'm having an issue in Unity 5 with a vertex shader where if I have a non-uniformly scaled mesh it will (usually) incorrectly calculate the position in the vertex shader. The shader is intended to offset all the vertices of the object outwards by a set distance (In this example, 0.5 meters). I've seen some questions/answers suggesting a need in U5 to normalize the normals before using them in the vertex shader, but I've done that to no avail.

It seems like this behavior must be related to the batching of objects since it seems to only occur when another object is on screen using the same shader. And given the changes to non-uniform mesh scaling in U5, I assume it's related to that. But I cannot for the life of me see what is happening incorrectly here. Does anyone see a way to fix the shader?

I have simplified my shader down to the following which still exhibits my error:

 Shader "Custom/VertexOffsetter" {
     Properties {
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         
         Pass {
             Name "FORWARD"
             Tags {
                 "LightMode"="ForwardBase"
             }
         CGPROGRAM
         #include "UnityCG.cginc"
         #pragma fragment frag
         #pragma vertex vert
 
         #define UNITY_PASS_FORWARDBASE
 
         // Use shader model 3.0 target, to get nicer looking lighting
         #pragma target 3.0
 
         struct VertexOutput {
             float4 pos : SV_POSITION;
         };
 
         VertexOutput vert(appdata_base v) {
             VertexOutput o = (VertexOutput)0;
             v.vertex.xyz += normalize(v.normal) * 0.5;
             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
             return o;
         }
                 
         float4 frag (VertexOutput i) : COLOR {
             return float4(0.5, 0.5, 0, 0);
         }
         ENDCG
         }
         
     }
     FallBack "Diffuse"
 }

The incorrectly rendered mesh. Note that the wireframe appears to be correct: Incorrect Rendering of the Mesh

The correctly rendered mesh, with the only change that the object which was previously on the left side of the screen is now moved entirely offscreen.

Correct Rendering of the Mesh after extra object using same shader removed

Thanks in advance.

mesh-incorrect.png (66.3 kB)
mesh-correct.png (61.4 kB)
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 DuneWalker · Mar 29, 2015 at 02:55 AM

Could it be related to using the ModelViewProjection matrix. Try substituting out it for the ModelView or the Model matrix (the exact names should be found on the Unity documentation under Shader Variables or something similar) and see what happens. If you use the Projection matrix with the ModelView matrix, it may be causing the object to become double-projected, so to speak.

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 Michael-Thomas · Mar 30, 2015 at 01:11 AM 0
Share

Given that the object draws correctly when it is the only object in the scene, I don't believe the projection matrix could be at fault.

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

21 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

Related Questions

Changes to Mesh Vertex Colors Reset on Build 1 Answer

Shader Outline scale problem since U5 1 Answer

Create a Wireframe Shader with Shader Forge 1 Answer

How to export .OBJ from Editor with rescaled mesh? 1 Answer

Clarification on static batching a shader-animated mesh 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