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
1
Question by thenachotech1113 · Dec 19, 2014 at 02:40 AM · meshmovingvertexminecraft

when I try to move the vertices of a cube a face disapears though the vertices stay intact

Hello, so I just started getting into the world of creating meshes through script, with the intention of creating a minecraft style block placing mechanic and adding an interesting touch allowing the player to change the position of the vertices and hopefully the edges in the future. here is the code i used

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Create_Mesh : MonoBehaviour {
 
     Mesh mesh;
     MeshFilter mF;
     MeshRenderer mR;
 
     public float width;
     public float height;
     public float depth;
 
     public Material mat;
 
     Vector3[] vertices = new Vector3[24];
     int[] tri = new int[36];
     Vector3[] normals = new Vector3[24];
     Vector2[] uv = new Vector2[24];
 
     public List<GameObject> vertexHandles = new List<GameObject>();
     public List<HandleScript> handleScripts = new List<HandleScript>();
     public Vector3[] repVerts = new Vector3[8];
     public int[] realVertLink = new int[24];
 
     public Color[] handleColors = new Color[2];
 
 
     void Start () {
         mF = gameObject.AddComponent<MeshFilter>();
         mR = gameObject.AddComponent<MeshRenderer>();
         mesh = new Mesh();
 
         mF.mesh = mesh;
         mR.material = mat;
 //vertices
         //front face
         vertices[0] = new Vector3 (0, 0, 0);
         vertices[1] = new Vector3 (width, 0, 0);
         vertices[2] = new Vector3(0, height, 0);
         vertices[3] = new Vector3 (width, height, 0);
         //Right Face
         vertices[4] = new Vector3(width, 0, 0);
         vertices[5] = new Vector3(width, 0, depth);
         vertices[6] = new Vector3(width, height, 0);
         vertices[7] = new Vector3(width, height, depth);
         //back face 
         vertices[8] = new Vector3 (width, 0, depth);
         vertices[9] = new Vector3 (0, 0, depth);
         vertices[10] = new Vector3 (width, height, depth);
         vertices[11] = new Vector3 (0, height, depth);
         //left face
         vertices[12] = new Vector3 (0, 0, depth);
         vertices[13] = new Vector3 (0, 0, 0);
         vertices[14] = new Vector3 (0, height, depth);
         vertices[15] = new Vector3 (0, height, 0);
         //top face
         vertices[16] = new Vector3 (0, height, 0);
         vertices[17] = new Vector3 (width, height, 0);
         vertices[18] = new Vector3 (0, height, depth);
         vertices[19] = new Vector3 (width, height, depth);
         //bot face 
         vertices[20] = new Vector3 (0, 0, depth);
         vertices[21] = new Vector3 (width, 0, depth);
         vertices[22] = new Vector3 (0, 0, 0);
         vertices[23] = new Vector3 (width, 0, 0);
 
         mesh.vertices = vertices;
 
         //lower triangle front
         tri[0] = 2;
         tri[1] = 1;
         tri[2] = 0;
 
         //upper Triangle front
         tri[3] = 2;
         tri[4] = 3;
         tri[5] = 1;
 
         //Upper triangle right
         tri[6] = 6;
         tri[7] = 7;
         tri[8] = 5;
 
         //lower right tri
         tri[9] = 6;
         tri[10] = 5;
         tri[11] = 4;
 
         //upper back tri
         tri [12] = 10;
         tri [13] = 9;
         tri [14] = 8;
 
         //lower back tri
         tri [15] = 10;
         tri [16] = 11;
         tri [17] = 9;
 
         //upper triangle left
         tri [18] = 14;
         tri [19] = 15;
         tri [20] = 13;
 
         //lower triangle left 
         tri [21] = 14;
         tri [22] = 13;
         tri [23] = 12;
 
         //upper top face 
         tri[24] = 18;
         tri[25] = 19;
         tri[26] = 17;
 
         //lower top face
         tri[27] = 18;
         tri[28] = 17;
         tri[29] = 16;
 
         //upper bot tri
         tri [30] = 22;
         tri [31] = 23;
         tri [32] = 21;
 
         //lower bot tri
         tri [33] = 22;
         tri [34] = 21;
         tri [35] = 20;
 
         mesh.triangles = tri;
 
         //normals
 
         normals[0] = -Vector3.forward;
         normals[1] = -Vector3.forward;
         normals[2] = -Vector3.forward;
         normals[3] = -Vector3.forward;
 
         normals[4] = Vector3.right;
         normals[5] = Vector3.right;
         normals[6] = Vector3.right;
         normals[7] = Vector3.right;
 
         normals[8] = Vector3.forward;
         normals[9] = Vector3.forward;
         normals[10] = Vector3.forward;
         normals[11] = Vector3.forward;
 
         normals[12] = -Vector3.right;
         normals[13] = -Vector3.right;
         normals[14] = -Vector3.right;
         normals[15] = -Vector3.right;
 
         normals[16] = Vector3.up;
         normals[17] = Vector3.up;
         normals[18] = Vector3.up;
         normals[19] = Vector3.up;
 
         normals [20] = -Vector3.up;
         normals [21] = -Vector3.up;
         normals [22] = -Vector3.up;
         normals [23] = -Vector3.up;
 
         mesh.normals = normals;
 
         //uv
         uv[0] = new Vector2 (0, 0);
         uv[1] = new Vector2 (1, 0);
         uv[2] = new Vector2 (0, 1);
         uv[3] = new Vector2 (1, 1);
 
         uv[4] = new Vector2 (0, 0);
         uv[5] = new Vector2 (1, 0);
         uv[6] = new Vector2 (0, 1);
         uv[7] = new Vector2 (1, 1);
 
         uv[8] = new Vector2 (0, 0);
         uv[9] = new Vector2 (1, 0);
         uv[10] = new Vector2 (0, 1);
         uv[11] = new Vector2 (1, 1);
 
         uv[12] = new Vector2 (0, 0);
         uv[13] = new Vector2 (1, 0);
         uv[14] = new Vector2 (0, 1);
         uv[15] = new Vector2 (1, 1);
 
         uv[16] = new Vector2 (0, 0);
         uv[17] = new Vector2 (1, 0);
         uv[18] = new Vector2 (0, 1);
         uv[19] = new Vector2 (1, 1);
 
         uv[20] = new Vector2 (0, 0);
         uv[21] = new Vector2 (1, 0);
         uv[22] = new Vector2 (0, 1);
         uv[23] = new Vector2 (1, 1);
 
         mesh.uv = uv;
 
         //set representative vertices ***basicaly the corners, i just forgor the word***
         repVerts[0] = new Vector3 (0, 0, 0);
         repVerts[1] = new Vector3 (width, 0, 0);
         repVerts[2] = new Vector3 (0, height, 0);
         repVerts[3] = new Vector3 (width, height, 0);
 
         repVerts[4] = new Vector3 (0, 0, depth);
         repVerts[5] = new Vector3 (width, 0, depth);
         repVerts[6] = new Vector3 (0, height, depth);
         repVerts[7] = new Vector3 (width, height, depth);
 
         //link real verts to ref ***organizes vertices so every 3 represent a corner
         realVertLink[0] = 0;
         realVertLink[1] = 13;
         realVertLink[2] = 22;
 
         realVertLink[3] = 1;
         realVertLink[4] = 4;
         realVertLink[5] = 23;
 
         realVertLink[6] = 2;
         realVertLink[7] = 15;
         realVertLink[8] = 16;
 
         realVertLink[9] = 3;
         realVertLink[10] = 6;
         realVertLink[11] = 17;
 
         realVertLink[12] = 9;
         realVertLink[13] = 12;
         realVertLink[14] = 20;
 
         realVertLink[15] = 5;
         realVertLink[16] = 8;
         realVertLink[17] = 21;
 
         realVertLink[18] = 11;
         realVertLink[19] = 14;
         realVertLink[20] = 18;
 
         realVertLink[21] = 7;
         realVertLink[22] = 10;
         realVertLink[23] = 19;
 
 
         //spawn controll handles 
         SpawnControlHandles(repVerts);
     }
 //spawns control handles
     void SpawnControlHandles (Vector3[] verticesPos) {
         for (int i = 0; i < verticesPos.Length; i++){
             GameObject clone = GameObject.CreatePrimitive(PrimitiveType.Cube);
             HandleScript hS = clone.AddComponent<HandleScript>();
             hS.id = i;
             hS.vertex = true;
             hS.cM = gameObject.GetComponent<Create_Mesh>();
             hS.materlialColors = handleColors;
             clone.name = "Vertex" + i;
             clone.transform.position = verticesPos[i];
             vertexHandles.Add(clone);
             handleScripts.Add(hS);
         }
     }
 //moves controlHandles
     public void MoveVertexHandle (int id, Vector3 position) {
         int actualID = id * 3;
 
         vertices[actualID] = position;
         vertices[actualID +1] = position;
         vertices[actualID +2] = position;
         mesh.vertices = vertices;
         mesh.RecalculateNormals();
     }
 
     void Update () {
     }
 }
 

As the question says it all goes well until I try to move the handles and one of the faces totaly disapears (im prety sure since i dont see the triangle gizmo) and the other faces don´t have the vertex moved nor they disapear. The mesh is created fine, it create the handle fine(all they do is if draged it will call a fucntion in the Create Mesh script to change the position of the vertices its linked to). I am absolutely clueless on to why this is not working and I realy apreciate your time. sorry for the long question and thanks a lot in advanced.

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 Bunny83 · Dec 19, 2014 at 03:53 AM

Uhm, you don't seem to transform the vertex positions into worldspace and when done back to localspace. Is your gameobject located at 0,0,0 without any rotation? In this case it won't matter, if not your handles will be way off.

Another way is of course to parent your handles to the actual mesh object and use localPosition to read and write the position once it's a child of the same Transform. Using localPosition is also usually slightly faster since position has to figure out the worldspace position first.

edit
Additionally i just realized you forgot to use realVertLink inside your MoveVertexHandle method. It should be something like:

 public void MoveVertexHandle (int id, Vector3 position)
 {
     int actualID = id * 3;
     vertices[realVertLink[actualID]] = position;
     vertices[realVertLink[actualID +1]] = position;
     vertices[realVertLink[actualID +2]] = position;
     mesh.vertices = vertices;
     mesh.RecalculateNormals();
     mesh.RecalculateBounds();
 }

I've also added RecalculateBounds. If you don't call RecalculateBounds the object might be clipped to early (or too late but that's not visible. It's just a performance thing).

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 thenachotech1113 · Dec 19, 2014 at 04:22 AM 0
Share

good observation, actualy it is at 0, 0, 0 but eventualy ill have to change it. thanks a lot.

avatar image Bunny83 · Dec 19, 2014 at 04:24 AM 1
Share

@thenachotech1113: Just made an edit ^^

avatar image thenachotech1113 · Dec 22, 2014 at 11:03 PM 0
Share

@Bunny83 cant thank you enough. its working great now. sprry for not marking your answer as correct before, thought i did and i just noticed i hadn`t.

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

28 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

Related Questions

Using compute shader, how to take every vertex of a mesh and make a cube spawn and move to each one? 0 Answers

Any script to 'fix' normals/winding order? 3 Answers

multiple textures on one mesh + different Albedos 1 Answer

Mesh.vertices are all 0? 1 Answer

What is going on with the graphics here... 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