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 /
avatar image
0
Question by ry511 · Jun 24, 2017 at 08:22 PM · c#meshnormalsvoxelcard

Getting meshes to render properly, and coloring triangles of a mesh

I am wondering how to ensure that all sides of a mesh are visible always.

My mesh is a "card" made of voxels that is basically a 2D array of voxels where cardWidth = 63 and cardHeight = 88. After generating the mesh if I rotate it certain ways certain sides of the mesh do not render.

I understand from looking around that I might have to calculate my normal vectors differently to get my desired result but I am confused on how that all works.

I also am looking to move on to coloring each cube individually which, unless I have no idea what I'm talking about, should be "easier" since I am not sharing vertices between my triangles. Ideally, in my mind, I would call a method colorSquare(int x, int y, color) and then be able to extend it from there for whatever purposes.

Whatever tips I could get on either Issue would be greatly appreciated. I like to think I have a solid grasp on programming but 3D and meshes is something I have yet to be able to wrap my head around, and I still would consider myself a new to Unity.

Thanks in advance! Below I have included my scripts for Card, Voxel, and MeshData if it helps at all.

Card:

 using UnityEngine;
 using System.Collections;
 [RequireComponent(typeof(MeshFilter))]
 [RequireComponent(typeof(MeshRenderer))]
 [RequireComponent(typeof(MeshCollider))]
 public class Card : MonoBehaviour {

 Voxel[,] cardGraphics;

 public static int cardHeight = 88;
 public static int cardWidth = 63;

 public bool update = true;

 bool faceUp;
 string name;
 int cost;
 int health;
 int attack;

 public Card(string name, int cost, int health, int attack)
 {
     this.name = name;
     this.cost = cost;
     this.health = health;
     this.attack = attack;
 }

 MeshFilter filter;
 MeshCollider coll;
 public void Start()
 {
     filter = gameObject.GetComponent<MeshFilter>();
     coll = gameObject.GetComponent<MeshCollider>();
     cardGraphics = new Voxel[cardWidth, cardHeight];
     for (int x = 0; x < cardWidth; x++)
     {
         for (int y = 0; y < cardHeight; y++)
         {
             cardGraphics[x, y] = new Voxel();
         }
     }
     //cardGraphics[3, 5] = new Voxel();
     UpdateCard();
 }

 public Voxel GetVoxel(int x, int y)
 {
     return cardGraphics[x, y];
 }

 void UpdateCard()
 {
     MeshData meshData = new MeshData();
     for (int x = 0; x < cardWidth; x++)
     {
         for (int y = 0; y < cardHeight; y++)
         {
             meshData = cardGraphics[x, y].Voxeldata(this, x, y, meshData);
         }
     }
     RenderMesh(meshData);
 }

 void RenderMesh(MeshData meshData)
 {
     filter.mesh.Clear();
     filter.mesh.vertices = meshData.vertices.ToArray();
     filter.mesh.triangles = meshData.triangles.ToArray();

     filter.mesh.uv = meshData.uv.ToArray();
     filter.mesh.RecalculateNormals();
 }
 }
 

Voxel

 public class Voxel
 {

 public enum Direction { north, east, south, west, up, down };

 public struct Colors { public int x; public int y; }
 const float colorSize = 0.25f;
 public Voxel()
 {
 }

 public virtual bool IsSolid(Direction direction)
 {
     switch (direction)
     {
         case Direction.north:
             return true;
         case Direction.east:
             return true;
         case Direction.south:
             return true;
         case Direction.west:
             return true;
         case Direction.up:
             return true;
         case Direction.down:
             return true;
     }
     return false;
 }

 public virtual MeshData Voxeldata(Card card, int x, int y, MeshData meshData)
 {
     if (x == 0)
     {
         meshData = FaceDataWest(card, x, y, meshData);
     }
     if (y == 0)
     {
         meshData = FaceDataDown(card, x, y, meshData);
     }
     else if (x == Card.cardWidth)
     {
         meshData = FaceDataEast(card, x, y, meshData);
     }
     else if (y == Card.cardHeight)
     {
         meshData = FaceDataUp(card, x, y, meshData);
     }
     meshData = FaceDataNorth(card, x, y, meshData);
     meshData = FaceDataSouth(card, x, y, meshData);

     return meshData;
 }

 protected virtual MeshData FaceDataUp(Card card, int x, int y, MeshData meshData)
 {
     meshData.vertices.Add(new Vector3(x - 0.5f, y + 0.5f, 0 + 0.5f));
     meshData.vertices.Add(new Vector3(x + 0.5f, y + 0.5f, 0 + 0.5f));
     meshData.vertices.Add(new Vector3(x + 0.5f, y + 0.5f, 0 - 0.5f));
     meshData.vertices.Add(new Vector3(x - 0.5f, y + 0.5f, 0 - 0.5f));
     meshData.AddQuadTriangles();
     return meshData;
 }

 protected virtual MeshData FaceDataDown(Card card, int x, int y, MeshData meshData)
 {
     meshData.vertices.Add(new Vector3(x - 0.5f, y - 0.5f, 0 - 0.5f));
     meshData.vertices.Add(new Vector3(x + 0.5f, y - 0.5f, 0 - 0.5f));
     meshData.vertices.Add(new Vector3(x + 0.5f, y - 0.5f, 0 + 0.5f));
     meshData.vertices.Add(new Vector3(x - 0.5f, y - 0.5f, 0 + 0.5f));

     meshData.AddQuadTriangles();
     return meshData;
 }
 //remaining faces of voxel created in same way 
 }

MeshData

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class MeshData
 {
 public List<Vector3> vertices = new List<Vector3>();
 public List<int> triangles = new List<int>();
 public List<Vector2> uv = new List<Vector2>();
 public List<Vector3> colVertices = new List<Vector3>();
 public List<int> colTriangles = new List<int>();
 public MeshData() { }

 public void AddQuadTriangles()
 {
     triangles.Add(vertices.Count - 4);
     triangles.Add(vertices.Count - 3);
     triangles.Add(vertices.Count - 2);
     triangles.Add(vertices.Count - 4);
     triangles.Add(vertices.Count - 2);
     triangles.Add(vertices.Count - 1);
 }
 }
Comment
Add comment · Show 1
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 Cherno · Jun 25, 2017 at 01:19 AM 0
Share

Two things to help you along:

  1. The direction of a triangle's normal is deter$$anonymous$$ed by the order of it's vertices. If you have a triangle of three vertices:

    0 = lower left, 1 = upper left, 2 = upper right

then the normal direction will be "towards you" if you see these vertices before you laid out. Basically, a clockwise vertex assignment. Counter-cockwise would face "away from you".

  1. The easiest way to color triangles is probably to use a shader that supports vertex colors and use the mesh's colors array to give each vertex a color.

0 Replies

· Add your reply
  • Sort: 

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

338 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 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 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

How to create a custom cuboids at runtime? 1 Answer

How to snap a Prefab to the normal of an irregular mesh 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Cube Voxel not working. Help please 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