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
2
Question by Enigme · Jun 01, 2013 at 03:28 PM · meshvertex

Modifying vertices values in a mesh

I've been trying to apply brushes as heightmap on a sphere but there's some weird stuff going on that I can't find an answer to.

What is happening is that when I apply the heightmap on a vertex, it seems like it's not affecting the good vertex. I'm checking if from the center in the direction of the vertex there's a collision with a brush and if so, I'll add a vector3 in the same direction as the raycast used but as you can see in the image below, it affects the vertex on the left side instead of on the top of the sphere.

The code is straightforward but I can't seem to understand what's happening and why.

alt text

Also, if the brush is on x axis, it appears fine but otherwise, the z and y are invert. alt text

The code I used is:

 void Start () {
         
         Mesh mesh = GetComponent<MeshFilter>().mesh;
         Vector3[] vertices = mesh.vertices;
         Vector3[] sphereVertices = new Vector3[mesh.vertexCount];
         
         for(int i=0; i<vertices.Length; ++i){
             Vector3 vertex = vertices[i].normalized * 3;
             
             RaycastHit info;
             if(Physics.Raycast(Vector3.zero, vertex, out info)){
                 Vector3 addHeight = vertex.normalized;
                 addHeight *= GetGrayscale(info.collider.transform.renderer.material.GetTexture("_MainTex") as Texture2D,info.textureCoord);
                 vertex += addHeight;
             }
             
             sphereVertices[i] = vertex;
         }
         
         mesh.vertices = sphereVertices;
         mesh.RecalculateNormals();
     }


vertex.png (142.9 kB)
vertex2.png (95.3 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
3
Best Answer

Answer by robertbu · Jun 01, 2013 at 05:57 PM

I don't know if this is the issue without more study, but your vertices are in local space and your brush is in world space. You may need to do a Transform.TransformPoint(). If you sphere is not at the origin, you also have to take that into account:

 RaycastHit info;
 Vector3 v3 = transform.TransformPoint(vertex) - transform.position;
 if(Physics.Raycast(Vector3.zero, v3, out info)){

  
Comment
Add comment · Show 3 · 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 Enigme · Jun 02, 2013 at 02:08 AM 0
Share

Great! It's working :) Thanks!

If you can create an answer, I can mark it as the solution

avatar image Bunny83 · Jun 02, 2013 at 02:56 AM 1
Share

I've converted the comment into an answer.

btw. $$anonymous$$eep in $$anonymous$$d that your collider will stay the same and will not reflect the new shape unless:

  • You use a $$anonymous$$eshCollider ins$$anonymous$$d of a SphereCollider

  • You update the mesh of the collider as well

Using the .mesh property will duplicate the mesh. You might want to use shared$$anonymous$$esh ins$$anonymous$$d.

avatar image Bunny83 · Jun 02, 2013 at 02:58 AM 1
Share

ps: The Collider class has a dedicated Raycast function which will only test for this collider. This is quite useful for such cases.

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

15 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to use vertex groups in Unity? 0 Answers

Ignore/Destroy then re-enable Mesh Colliders 3 Answers

Mesh vertex count differs on Mac/Windows 1 Answer

Mesh from Vertices (Polygon from Spline) 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