Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ValakhP · Nov 19, 2017 at 08:49 PM · meshshadersbatching

Getting submesh center in batch

Hello community.

I'm trying to optimize rendering and use batching for a lot of meshes. But there is a shader that uses center of mesh and I need to store it somewhere. I believe that I can just store them in constant buffer, but I'd like to try storing them inside vertices colors.

So I wrote a little script that will set colors for all vertices:

 public void ResetVertexColors()
 {
     var vertices = meshFilter.sharedMesh.vertices;
     var colors = meshFilter.sharedMesh.colors;
 
     var squaredLength = 0f;
     for (var i = 0; i < vertices.Length; i++)
     {
         squaredLength += Mathf.Pow(vertices[i].magnitude, 2f);
     }
 
     var length = Mathf.Sqrt(squaredLength);
     for (var i = 0; i < vertices.Length; i++)
     {
         vertices[i] = vertices[i] / length * 0.5f + Vector3.one * 0.5f;
         colors[i] = new Color(vertices[i].x, vertices[i].y, vertices[i].z, 1f);
     }
 
     meshFilter.sharedMesh.SetVertices(vertices.ToList());
     meshFilter.sharedMesh.SetColors(colors.ToList());
 }  


I'm normalizing vertices here, so colors and positions will be in [0, 1] range.

After that I use this vertex shader to be sure that all submesh vertices will have the same center position:

 v2f vert (appdata v)
 {
     v2f o;
 
     o.vertex = UnityObjectToClipPos(v.vertex);
     o.color = (v.vertex - v.color) / 10;
     return o;
 }

 fixed4 frag (v2f i) : SV_Target
 {
     return i.color;
 }


I expect to see submeshes filled with one same color, but this is what I actually see: http://take.ms/uVRvG

These two submeshes have different colors on their edges. That means that each vertex "is pointing" on different center point and I can't use my shader properly.

Anyone knows what I'm doing wrong?

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 ValakhP · Nov 20, 2017 at 07:37 PM

Alright, I found a problem.
Actually, there were two mistakes:

  1. Transformation of the object on the scene. All scales and rotations should be reseted to default: Vector3.one and Vector3.zero;

  2. Don't change vertices and wrire all positions into colors as they are.

As a result, I have this script for changing colors of vertices:

 public void ResetVertexColors()
 {
     var vertices = meshFilter.sharedMesh.vertices;
     var colors = meshFilter.sharedMesh.colors;
 
     for (var i = 0; i < vertices.Length; i++)
     {
         colors[i] = new Color(vertices[i].x, vertices[i].y, vertices[i].z, 1f);
     }
 
     meshFilter.sharedMesh.SetColors(colors.ToList());
 }


And everything work fine from this point.

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

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

Related Questions

Tiling a noise texture 1:1 over each triangle without creating extra vertices 1 Answer

Vertex Displacement stretched when used in a particle emitter, how can I fix it? 1 Answer

Can I create a mesh from a terrain and get a similar texture quality ? Or this is not possible ? 0 Answers

Get an array or list of points and normals of mesh surface (Not vertecis) 1 Answer

Help with a NURBS / B-Spline / Bezier patch shader. 0 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