6

I am trying out unity for a project that i am on.

I am attempting to draw 3D polygon from a set of coordinate that I have.

So what i am doing now is to build a row of cube btw the two points. I plan to build these points into either a solid shape or just "walls" to form a room. However, it doesn't seem to work as expected. Please advise.

drawCube( Vector3(10,0,14),Vector3(70,0,14)); 
drawCube( Vector3(90,0,14),Vector3(60,87,45));   

function drawCube(v1,v2) { 



pA = v1; 
pB = v2;
var plane : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);

 var between:Vector3 = pB - pA;
    var distance:float = between.magnitude;
plane.transform.localScale.x = distance;
plane.transform.localScale.y=10;
plane.transform.position = pA + (between / 2.0);
plane.transform.LookAt(pB);

}

updated: I have also tried using a mesh but all i got was the below image. What am i doing wrong?

I am trying to achieve something like this

enter image description here

Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26
user2760642
  • 137
  • 1
  • 9
  • Wouldn't it be better to build a mesh with these points? Btw. why are you using Cubes to create triangles? Or am I getting something wrong? – AntiHeadshot Nov 09 '15 at 15:31
  • 1
    I tried using a mesh, but all I get is a flat image. i have no idea how to make it into a 3D object. please give me a directly that i can look into. And for mesh i have to define both the vector3 points and triangles in order to from the shape right? – user2760642 Nov 09 '15 at 15:35
  • Your drawing is showing a 2D image, what kind of shape do you want to get? In theory you only have to define the triangles, but in order to create them you need the points. – AntiHeadshot Nov 09 '15 at 15:40
  • The coordinates that are like "spaces" on a floor map. I am trying to build them into blocks (*as the pic above) in order for users to visually see the "spaces" after partition wall has been build. – user2760642 Nov 09 '15 at 15:46
  • I think its impossible to create something like an apple without the triangles given. – AntiHeadshot Nov 09 '15 at 15:52
  • Just to double clarify, the coordinates i have is like the black outline, all i am trying to achieve is the build up the blue part. Is that not possible? – user2760642 Nov 09 '15 at 15:58
  • Ok this is possible, if you define the thicknes, then you can take the crossproduct of two points set the length to the thicknes and use this as an offset for every other point – AntiHeadshot Nov 09 '15 at 16:53
  • is there any sample code i can look at? – user2760642 Nov 09 '15 at 17:18
  • can anybody help please :( – user2760642 Nov 10 '15 at 03:13
  • I'm sorry but Fallout4 :( – AntiHeadshot Nov 10 '15 at 07:24
  • Your first image doesn't look like its working for me, what does it look like that you have now? – NotAGenie Nov 11 '15 at 14:29

1 Answers1

8

You could make primitives and manipulate them but that would limit you very much if you needed to scale or change your requirements in the future. I would recommend using a procedural mesh to create the geometry you need as you need it. The basics aren't too hard, it's just a matter of constructing the Mesh object from it's base components given some vertices. Here's an example of constructing a 3d quadrilateral:

using UnityEngine;
using System.Collections.Generic;

public class SquareMaker : MonoBehaviour {

  public List<Vector3> points;

  void Start()
  {
    GameObject threeDSquare = new GameObject("3DSquare");
    threeDSquare.AddComponent<MeshRenderer>();
    threeDSquare.AddComponent<MeshFilter>();
    threeDSquare.GetComponent<MeshFilter>().mesh = CreateMesh(points);
  }

  private Mesh CreateMesh(List<Vector3> points)
  {
    List<int> tris = new List<int>(); // Every 3 ints represents a triangle
    List<Vector2> uvs = new List<Vector2>(); // Vertex position in 0-1 UV space
    /* 4 points in the list for the square made of two triangles:
    0 *--* 1
      | /|
      |/ |
    3 *--* 2
    */
    tris.Add(1); 
    tris.Add(2);
    tris.Add(3);
    tris.Add(3);
    tris.Add(0);
    tris.Add(1);
    // uvs determine vert (point) coordinates in uv space
    uvs.Add(new Vector2(0f, 1f));
    uvs.Add(new Vector2(1f, 1f));
    uvs.Add(new Vector2(1f, 0f));
    uvs.Add(new Vector2(0f, 0f));

    Mesh mesh = new Mesh();
    mesh.vertices = points.ToArray();
    mesh.uv = uvs.ToArray();
    mesh.triangles = tris.ToArray();
    mesh.RecalculateNormals();
    return mesh;
  }
}
cjmarsh
  • 292
  • 2
  • 12