Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing 3D polygon on unity 3d

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

like image 452
user2760642 Avatar asked Nov 09 '15 15:11

user2760642


People also ask

How to build a 3D model in Unity?

To use all the functionality of Unity, you need an asset, a 3D model. However, to build a 3D model you need to use the appropriate software. There are 3 major tools on the market which you can choose from: All three can be used to create 3D models for Unity.

How do I create a poly shape in Unity?

Open the ProBuilder window (in Unity's top menu: Tools > ProBuilder window). The Edit Mode toolbar and the ProBuilder toolbar appear. From the ProBuilder toolbar, click New Poly Shape (). Tip: You can also access this tool from the ProBuilder menu (Tools > ProBuilder > Editors > New Poly Shape).

How do I create a custom 3D mesh in Unity?

To create a custom 3-dimensional Mesh, you can use the Poly Shape tool to create a custom 2-dimensional shape, and then extrude that shape to make it 3D. To define a custom Mesh: Open the ProBuilder window (in Unity's top menu: Tools > ProBuilder window ).

Why use unity for game development?

The engine has a broad community that helps creators to learn and solve problems which arise in the process of app development. Unity is used for scripting, scene creation, animation, app architecture development, level design, motion design, and physics implementation. Check out: “How Good is Unity for Game Development?”


1 Answers

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;
  }
}
like image 132
cjmarsh Avatar answered Oct 05 '22 09:10

cjmarsh