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
0
Question by Epicwolf · Aug 01, 2013 at 08:26 PM · meshproceduralvertices

Deleting/restoring mesh vertices

I'm sorry if a similar question has been asked already...

I have recently been trying to create a script that can turn a cube into separate planes. (not whole as new gameObjects, just isolated faces of the cube.) For use in a voxel engine. For example, it creates a plane where the top of the cube would be, giving the illusion that only the top of the block is being rendered. But I can't figure out how to create multiple faces at once. Like creating the top,left, and bottom sides all at the same time. Can anybody give me any tips on how this is done?

Here is my code to create the top side of the box:

 import System.Collections.Generic;
 private var width: float = 1;
 private var height: float = 1;
 var verts: Vector3[] = new Vector3[4];
 var normals: Vector3[] = null;
 var uv: Vector2[] = null;
 var tri: int[] = null;
 var mf: MeshFilter = null;
 var basex : float = 0.0;
 var basey : float = 0.0;
 var basez : float = 0.0;
 var mesh: Mesh = null;
 
 verts = new Vector3[4];
 normals = new Vector3[4];
 uv = new Vector2[4];
 tri = new int[6];
 mf = GetComponent(MeshFilter);
 basex = transform.position.x - height / 2;
 basey = transform.position.y + height / 2;
 basez = transform.position.z - width / 2;
 verts[0] = new Vector3(basex, basey, basez);
 verts[1] = new Vector3(basex + width, basey, basez);
 verts[2] = new Vector3(basex, basey, basez + height);
 verts[3] = new Vector3(basex + width, basey, basez + height);
 for (i = 0; i < normals.Length; i++) {
     normals[i] = Vector3.up;
 }
 uv[0] = new Vector2(0, 0);
 uv[1] = new Vector2(1, 0);
 uv[2] = new Vector2(0, 1);
 uv[3] = new Vector2(1, 1);
 tri[0] = 0;
 tri[1] = 2;
 tri[2] = 3;
 tri[3] = 0;
 tri[4] = 3;
 tri[5] = 1;
 mesh = new Mesh();
 mesh.vertices = verts;
 mesh.triangles = tri;
 mesh.uv = uv;
 mesh.normals = normals;
 mf.mesh = mesh;
 mesh.RecalculateNormals();
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

3 Replies

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

Answer by robertbu · Aug 01, 2013 at 08:30 PM

There is code in this answer that produces cubes out of six independent faces:

http://answers.unity3d.com/questions/391561/create-a-mesh-and-color-cubes.html

I don't do a lot of mesh work, so there might be better code posted on this list. You might Google "unity3d voxels" to see what else might help you.

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 Epicwolf · Aug 02, 2013 at 08:35 PM 0
Share

I've tried the link, but I just don't understand how the vertexes, triangles, and uv's are mapped out in a cube. Plus I'm using JS, so I have no idea what the code in the link does. :/ I've also tried Google, but couldn't find anything.

avatar image robertbu · Aug 02, 2013 at 10:26 PM 0
Share

Below is the $$anonymous$$akeCube() portion of the script translated to Javascript with the color stuff stripped out. Attach it to any visible game object with a mesh and run. Right now it does not work with an empty game object because there is no code to create and setup a $$anonymous$$eshRenderer. Without a Renderer, nothing shows. You will see one line defining the four vertices for each side of the cube. You could leave out any of the lines defining the vertices for a side, and the code would still run and produce a partial block without that side. The 'for' loop defines the two triangles for each side:

 #pragma strict
 
 function Start () {
     var mf = GetComponent($$anonymous$$eshFilter);
     if (mf == null)
         mf = gameObject.AddComponent($$anonymous$$eshFilter);
     
     var mesh = $$anonymous$$akeCube(1.0f);
     mf.mesh = mesh;
 }
 
 function $$anonymous$$akeCube(fSize : float) : $$anonymous$$esh
     {
        var fHS = fSize / 2.0f;
        var mesh = new $$anonymous$$esh();   
        mesh.vertices  = [
          Vector3(-fHS,  fHS, -fHS), new Vector3( fHS,  fHS, -fHS), new Vector3( fHS, -fHS, -fHS), new Vector3(-fHS, -fHS, -fHS),  // Front
          Vector3(-fHS,  fHS,  fHS), new Vector3( fHS,  fHS,  fHS), new Vector3( fHS,  fHS, -fHS), new Vector3(-fHS,  fHS, -fHS),  // Top
          Vector3(-fHS, -fHS,  fHS), new Vector3( fHS, -fHS,  fHS), new Vector3( fHS,  fHS,  fHS), new Vector3(-fHS,  fHS,  fHS),  // Back
          Vector3(-fHS, -fHS, -fHS), new Vector3( fHS, -fHS, -fHS), new Vector3( fHS, -fHS,  fHS), new Vector3(-fHS, -fHS,  fHS),  // Bottom
          Vector3(-fHS,  fHS,  fHS), new Vector3(-fHS,  fHS, -fHS), new Vector3(-fHS, -fHS, -fHS), new Vector3(-fHS, -fHS,  fHS),  // Left
          Vector3( fHS,  fHS, -fHS), new Vector3( fHS,  fHS,  fHS), new Vector3( fHS, -fHS,  fHS), new Vector3( fHS, -fHS, -fHS)]; // right
  
        var triangles : int[]  = new int[mesh.vertices.Length / 4 * 2 * 3];
        var iPos = 0;
        for (var i = 0; i < mesh.vertices.Length; i = i + 4) {
          triangles[iPos++] = i;
          triangles[iPos++] = i+1;
          triangles[iPos++] = i+2;
          triangles[iPos++] = i;
          triangles[iPos++] = i+2;
          triangles[iPos++] = i+3;
        }
  
        mesh.triangles = triangles;
     
        mesh.RecalculateNormals();
        return mesh;
     }
avatar image Epicwolf · Aug 07, 2013 at 01:03 AM 0
Share

Thanks a bunch! This is exactly what I needed. Now I know what to do. :) And it's fine that it doesn't set up a renderer, since I will be putting this script on a cube.

avatar image
1

Answer by IgorAherne · Aug 01, 2013 at 08:30 PM

Hello! I am yet to begin studying this topic, yet here is a very good article, that will hopefully be useful. http://catlikecoding.com/unity/tutorials/star/

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

Answer by Dave-Carlile · Aug 01, 2013 at 08:30 PM

You just need to add more vertices and triangles to the mesh. Instead of arrays of 4, create arrays of 24 (6 faces * 4 vertices each).

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 Epicwolf · Aug 02, 2013 at 08:32 PM 0
Share

(This is a big comment, sorry.) Thanks for responding so quickly, guys. :)

Dave, I've done what you said and created bigger arrays, and I am now able to create multiple faces at once. But I have run into a small problem. I can't quite figure out how to recreate the sides of the cube exactly how they were. Like, I'm not sure how to get the new planes to have the same vertexes, triangles, and uv's as the cube would normally have on that side. I've tried printing out the values from a normal cube's mesh and pasting them into my code, but the faces get deformed for some reason. This is important because if I try to make my own face from scratch, the texture is usually rotated. Do you know where I could find the values I need to make the faces look normal? (I've tried looking at the above links, but I don't understand C#...)

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

16 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

Related Questions

Selecting a single polygon / face at runtime 1 Answer

UV-ing a sphere procedurally 1 Answer

Highmap on a cube. 0 Answers

mesh-morph scaling badly 1 Answer

reading vertices from a mesh always returns Vector3.zero 2 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