- Home /
How to draw triangles with coordinates
Hello. I have set up a 3 dimensional array, which is used to "cover" the whole map with a 3d grid. The array is Boolean, so each coordinate is turned on or off.
I want to make it so whenever an area of coordinates are turned "on", they create a mesh around them self, that the player collides with and can walk around on. Whenever I turn on another coordinate near it, the mesh adjusts accordingly. Whenever I turn off a coordinate inside the mesh, the mesh creates a hole accordingly. This way, the player can "dig" into a set coordinates, with a realistic mesh that appears to have a newly dug hole.
I am not sure where to start off, because I am not familiar with working with meshes.
SO to sum things up, im asking for help creating code that will draw a mesh around given coordinates. (x,y,z)
(For example, if I turn on one coordinate, i will see a cube. If i turn on 2 next to each other, I will see a rectangle. But if i turn on coordinates in the shape of a sphere, I will see a smooth sphere, not a blocky one).
Even if you don't know how the mesh generateing around the coordinates works, any help on creating a triangle given 3 coordinates will be helpful.
the length, width and depth are going to be changeable, so I can adjust the grid to a reasonable frame rate. (I worried that the millions of coordinates will cause lots of lag.) Here is my starting code for the grid:
var threeWay : boolean[,,] = new boolean[6,6,6];
var cube : GameObject;
function Start(){
for(var z=0;z<6;z++) {
for(var y=0;y<6;y++) {
for(var x=0;x<6;x++) {
threeWay[x,y,z] = false;
var temp = Instantiate(cube,Vector3(x,y,z),Quaternion.identity);
temp.name = ""+x+y+z;
}}}}
As you can see, im only using a grid that is 6 grid squares wide, just for testing purposes. You will also notice that each square is set to false, because the player has yet to turn some on to create a shape, and thus, a mesh.
Answer by robertbu · Mar 17, 2013 at 05:35 PM
There are too many unknowns in your project for a clear answer at this point. I suggest you educate yourself in two areas for this project. The first is building meshes. Since you will be building cube-like or lattice structures, create a procedural cube. I suggest using this post:
http://answers.unity3d.com/questions/355193/making-a-procedural-cube-mesh.html
There is a bug in the code posted with the question (as you can see by the image). There are working examples of procedural cubes, but understanding and fixing the bug will help you understand triangle winding and normals, which is very important to your project.
Once you have the cube up and working, take a look at uv coordinates. This is likely how you will define and change the texture of all the faces of the cubes.
Ok, I can not figure out how to draw anything. I paste the code from from your example, and from many other examples I find online, and it always has an error before I can test it out. I tried editing the one you mentioned, but I can't seem to get rid of the errors unity gives me. Could someone please show me an editable example of manually drawing a triangle from 3 given coordinates. Then hopefully, it won't have an error.
This link is to a procedural plane script written by @aldonaletto. I pasted it into a javascript file, attached it to an empty game object and ran it just to make sure it worked.
http://answers.unity3d.com/questions/219689/simple-plane-generation.html
Thank you very much. The code you lead me to works great. However, it creates a new object for each time I want a mesh. Is it possible to have one object spawn all the meshes in my game, and access each mess individually? And if so, will thousands of individual meshes overload the game?
Any help would be appreciated. I started off with my plan above by trying to figure out how to create an array of meshes like this:
function Start(){
for(var z=0;z<6;z++) {
for(var y=0;y<6;y++) {
for(var x=0;x<6;x++) {
var mesh : $$anonymous$$esh[,,] = new $$anonymous$$esh [6,6,6];
}}}}
(notice how I am limiting the game to a 6 by 6 by 6 amount off cubes, just for testing purposes.)
After this, I wasn't sure how to use aldonaletto's code. Also, all of this is under the assumption that one object can have multiple meshes, and that multiple meshes won't overload the game.
Any help with the code I have started so far would help greatly.
How many triangles you can have with acceptable performance will depend on a whole bunch of different factors...thing like target platform, shader used, how many submeshes and how many materials. For most platforms, and with reasonable choices, you will get 10's of thousands at least.
Sounds like you might be better off having a Skinned$$anonymous$$eshRenderer (or more than one if you breach the 65000 vertex limit) and attaching each of your objects to a bone in the renderer.
Or using the coordinates to create multiple discrete objects in a mesh every frame.
I do the latter in an mobile tree/rock impostor system and it works well. I've done the former for light weight models (one draw call, thousands of tree models - works well on many platforms).
Answer by Monko · Mar 21, 2013 at 01:14 AM
Ok, I am going to start from scratch. Can anyone help lead me to a magical function that basically does this:
DrawATriangle(x1,y1,z1,x2,y2,z2,x3,y3,z3)
NO unnecessary objects. NO meshes to be "attached" to the objects.
I know this probably doesn't exists, but if it does, please let me know.
I noticed that there is a game called minecraft that people are cloning. I can tell that there are thousands of cubes being drawn. Im sure there isn't one object per cube, and it is probably controlled by one 3 dimensional array like my game is. Can anyone tell me how they draw the cubes in there game? Hopefully, I can take this knowledge and draw a customizable triangles instead of a cube
$$anonymous$$eshes are not a beginner concept. Nothing like the function you describes exists. You can draw a mesh without a game object, but the performance of individual draws of a mesh can be substantially worse than a group of meshes attached to game objects (individual mesh draws don't batch). Plus you only save a few lines of code over having the mesh attached to a game object.
The big wins in performance are when a complex object or design is constructed out a single mesh with a single material. A mesh starts out with vertices. Vertices are Vector3 positions, and by themselves don't draw anything. Using these vertices, triangles are formed. Triangles are what is visible. So the plane @aldonaletto's code constructs has 4 vertices, two triangles and one game object. To draw a cube would take 8 vertices, and 12 triangles (two for each side) and only one game object. I have a baseball bat in the game I was working on with my daughter this afternoon. It has 576 vertices, 1148 triangles, and is still only one game object.
So for your game, assu$$anonymous$$g you want adequate performance, you will need to build a lattice of vertices and then create triangles for all the positions. You will have to figure out how to manipulate the mesh to "turn on" and "turn off" various triangle pairs. Unity has a 65,534 vertices limit per mesh. So if you were going to build your game out of a single mesh the Unity limit would allow a cube 39 blocks on a side. That would be a total of around 59,000 blocks and 700,000 triangles.
This is for a single mesh. If your target platform will support it, you can have multiple meshes of this complexity.
Thank you. The tips for saving performance helps a lot. So just to verify before I start working for several days figuring this out, From what I understand, Would having every coordinate in my 3 dimensional array act as a vertices be the most efficient method for drawing the triangles? (and for every 39 block cube, start a new mesh)
Yes, every vertex of your cube structure could go into the mesh. Then you could build triangles. But before you start building such large structures, figure out how to build just a single cube from scratch just understanding the concepts. It took me awhile the first time, and I had to really pay attention to getting everything right.
And there is another "gotcha" that I've not written about. Triangles are one sided. Which way they face is depending on the order you walk the vertices. If you take the standard plane and move the camera below it and look up, you will not see it. For a cube, all the triangles face outward. For the way I visualize your project, all the triangles at every cube must face inward to make collision and textures work, so you really need to understand triangle winding.
Once you have one cube up and running, figure out how to turn on and off each side. It can be done by modifying but not rebuilding the triangle array.
Thanks robertbu, you have helped so much. I have enough information that I can actually start this project that I knew nothing about just a few days ago.
Answer by Banglemoose · Mar 22, 2015 at 07:49 PM
I know this is an old thread but for anyone trying to do a similar thing who isn't up to building meshes in script you can just create a cube prefab and instantiate the whole cube in whenever a block is cleared.
Of course it will be less efficient since you'll have the whole cube even when some of the faces are hidden, but it's a simpler method and will allow you to move onto mesh building later once you've made some progress.
Answer by huse · Nov 17, 2019 at 04:20 PM
Here you can learn step by step how to draw a triangle just by code (Turn on English subtitles)
https://www.youtube.com/watch?v=ThWyvoqWjhE&list=PLp2zpxnnEFuemzgoUFYGkKQWwygDFXiIt