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 tkoknordic · May 27, 2015 at 09:34 AM · shadermaterialtransformation

Does Unity modify mesh Normals or Vertex before rendering?

Or do I just have a bug in my own code?

I have a special mesh where my vertices are in groups of four. I would like to have a vertex shader that linearly scales so that with scale value of 1.0 all vertices would have orginal positions and with scale value of 0.0 all vertices would be in the center of their groups of four.

To solve this task I have calculated all centers and save offsets to them in Normals array. Then in shader I add them lineary multiplied with [0-1] scaling value.

Problem is that when I scale mesh with Material Property "_Scale" vertices scales toward the center as ment but also toward origo of the mesh. So the whole mesh scales down with _Scale property and it's not intended.

Mesh code:

 //tiles:
 for (int x = 0; x < currentTileCount; x++)
 {
     float newT = Mathf.Min(t / offsetOfAnimations.Evaluate(x / (float)currentTileCount), 1f);
     tiles[x].CopyVerticesToArray(newT, vertices, x * 4);
     tiles[x].CopyTrianglesToArray(4 + x * 4, triangles, x * 6);
     tiles[x].CopyColorsToArray(newT, vertexColors, x * 4);
 }
 
 //centers as normals
 for(int x = 0; x < currentTileCount * 4; x += 4)
 {
     Vector3 normalAsCenter = (vertices[x + 0] + vertices[x + 1] + vertices[x + 2] + vertices[x + 3]) * 0.25f;
     normals[x + 0] = normalAsCenter - vertices[x + 0];
     normals[x + 1] = normalAsCenter - vertices[x + 1];
     normals[x + 2] = normalAsCenter - vertices[x + 2];
     normals[x + 3] = normalAsCenter - vertices[x + 3];
 }
             
 mesh.Clear();
 mesh.vertices = vertices;
 mesh.colors32 = vertexColors;
 mesh.triangles = triangles;
 mesh.RecalculateNormals();
 mesh.normals = normals;

Shader code: (Problem is in vert function)

 Shader "NeduTileTales/OpaqueVertexColor" 
 {
     Properties{
         _Scale("Scale", Range(0.0,1.0)) = 1.0
     }
 
     SubShader
     {
         Tags{ "RenderType" = "Opaque"}
 
         Pass
         {
             CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
             //#include "UnityCG.cginc"
 
             uniform float _Scale;
 
             struct v2f {
                 float4 pos : SV_POSITION;
                 fixed4 color : COLOR;
             };
 
             struct appdata {
                 float4 vertex : POSITION;
                 float4 color : COLOR;
                 float4 normal : NORMAL;
             };
 
             v2f vert(appdata v)
             {
                 v2f o;
                 //pos calculations doesn't work as wanted
                 float4 pos = (v.vertex + v.normal * (1 - _Scale));
                 o.pos = mul(UNITY_MATRIX_MVP, pos);
 
                 o.color = v.color;
                 return o;
             }
 
             fixed4 frag(v2f i) : SV_Target
             { 
                 return i.color; 
             }
 
             ENDCG
         }
 
     }
 }
Comment
Add comment · Show 3
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 Owen-Reynolds · May 27, 2015 at 01:32 PM 0
Share

Didn't read it all, but: Unity 5 claims to pre-rotate normals to correct them for non-uniformly scaled objects.

avatar image _dns_ · May 27, 2015 at 01:51 PM 0
Share

Hi, didn't check the code in detail but: if the meshes are small, dynamic batching may merge multiple meshes into one, then changing all the meshes' origin. You can disable this in the Player's settings.

avatar image tkoknordic · May 27, 2015 at 02:19 PM 0
Share

Thank you for your input. Own Reynolds: All objects with this material/shader are uniformly scaled. dns: some of those meshes are small (<100 vertices) and others are kind of big (9000 vertices and 4000 triangles). Actually Unity was batching couple of them and I turned batchin off and it didn't affect on this issue.

I have checked my own code so many times now that I'm pretty sure that my logic should work. So may be Unity does some modifications to my data at some point :( Any work arounds on this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by tkoknordic · May 27, 2015 at 02:27 PM

My friend looked this issue and had a lucky quess that my vertex positions W-components will go wild when I add a normal to it.

The fix:

Old:

 float4 pos = (v.vertex + v.normal * (1 - _Scale));

New:

 float4 pos = float4((v.vertex + v.normal * (1 - _Scale)).xyz, v.vertex.w);
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

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

Material doesn't have a color property '_Color' 4 Answers

Assign Texture To Material Unity 4.6 1 Answer

Updating custom material properties for RawImage 2 Answers

One outline material for multiple objects? 2 Answers

Changing material color´s alpha in self iluminated 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