Cubes! I said cubes!

I don’t means minecraft-like game. Just i want to know how to build a cube or sth using scripts. As example this code:

var material : Material;

 var xe : int = 255;
 var ye : int = 255;
 var size = Vector3(255, 0, 255);

 function Update ()
{
         CreateMeABox();
}

 function CreateMeABox()
{
         gameObject.AddComponent(MeshFilter);
         gameObject.AddComponent("MeshRenderer");
         
         renderer.material = material;
         
         var mesh : Mesh = GetComponent(MeshFilter).mesh;
         
         var vertices = new Vector3[xe * ye];
         var uv = new Vector2[xe * ye];
         var tangents = new Vector4[ye * xe];
         
         var uvScale = Vector2 (1.0 / (xe - 1), 1.0 / (ye - 1));
         
         var sizeScale = Vector3 (size.x / (xe - 1), size.y, size.z / (ye - 1));
         
         for(var y = 0; y < ye; y++)
         {
                 for(var x = 0; x < xe; x++)
                 {
                         var vertex = Vector3(x, Mathf.PerlinNoise(x*Time.time*0.0001,y*Time.time*0.0001)*500, y);
                         vertices[y*xe + x] = Vector3.Scale(Vector3(1.0,1.0,1.0), vertex);
                         uv[y*xe + x] = Vector2.Scale(Vector2 (x, y), uvScale);
                         var vertexL = Vector3( x-1, x*y*Time.time*0.0001, y );
                         var vertexR = Vector3( x+1, x*y*Time.time*0.0001, y );
                         var tan = Vector3.Scale( sizeScale, vertexR - vertexL ).normalized;
                         tangents[y*xe + x] = Vector4( tan.x, tan.y, tan.z, -1.0 );      
                 }
         }
         mesh.vertices = vertices;
         mesh.uv = uv;
         var triangles = new int[(ye - 1) * (xe - 1) * 6];
         var index = 0;
         for (y=0;y<ye-1;y++)
         {
                 for (x=0;x<xe-1;x++)
                 {

                         triangles[index++] = (y     * xe) + x;
                         triangles[index++] = ((y+1) * xe) + x;
                         triangles[index++] = (y     * xe) + x + 1;

                         triangles[index++] = ((y+1) * xe) + x;
                         triangles[index++] = ((y+1) * xe) + x + 1;
                         triangles[index++] = (y     * xe) + x + 1;
                 }
         }

         mesh.triangles = triangles;
                 
         mesh.RecalculateNormals();
         
         mesh.tangents = tangents;
}

It isn’t cube, it’s a plane modifying by Perlin Noise.
This code isn’t best, but it works. For these who want to use it:

  • Attach to empty game object
  • Set size (not bigger than 255x255) and material.
  • … and don’t use to many of them. It is not the best code (slow).

And i ask again:
How to build an object using coordinates?

If you see that post firstly, please go down a little.

I hope in the year 2100 people learned to use informative thread titles.

why not: Unity - Scripting API: PrimitiveType.Cube

otherwise if you really wanna procedural build your mesh, he explains it quite well: Procedural generated mesh in Unity part 2 with UV mapping | Morten Nobel's Blog

Not only cube.

Dude…?

Yep, a little…

#define WRONG!!! Procedural generated mesh in Unity part 2 with UV mapping | Morten Nobel's Blog

NOT a uv.

And that smug reply of yours prolly killed the thread, remember you are here to ask for help, you rely on the kindness of others, acting like that will get you no help. The link provided is a 2 part tutorial on how to procedurally create a mesh (tetrahedron) and how to UV map it.

The only difference between your request and the one provided is the shape, should be childsplay to change it from tetra to box with some basic math knowledge.

Also your original post is confusing and poorly explains what you need, I’d recommend rewritting it for better help and not putting stuff in bold as if you’re demanding help at gunpoint.

If you were to write whole sentences you might actually get the response you were expecting. If you keep on posting one-liners you will be out of luck, sorry dude.

It seems that you have pissed off some people here.

I believe that you should step back and re-ask this question or, have a better look at the docementation.

Using the documentation, you can see that you build a mesh using vertices, triangles and UV’s. vertices are the points in space (local) that you use in building triangles. UV’s are the points in a 2d texture where those vertices lay. (this is relative to the bottom left)

So, people are giving you the right information, you are not really taking time to read through the material and understand.

If you must ask questions, don’t start off like everyone here is just being an ass to you. Ask questions in a clear and easy to follow manner, and you WILL get the right answers.

Have a look at this posting

Loading…
Loading…
Loading…
Runtime error in 0x00DAMNIT.
Why you didn’t gave me that link first? Procedural generated mesh in Unity | Morten Nobel's Blog
RAID me in SATA, why i so blind…
Sorry for that, just i’m little crazy last times…

There is only one shape for four (LOL) vertices… But, again, how about a cube.

:face_with_spiral_eyes:

:face_with_spiral_eyes:

Some own thinking is required, nobody is going to write the code for you.
Think of what the coordinates would be to construct a cube.

You can construct a cube in various ways, one would be a center point (vector3) and a size variable (float), then calculate the 8 coordinates from there.

That link explains the way to generate meshes, now apply it for your own need.