Drawing 3D polygon on unity 3d

丶灬走出姿态 提交于 2019-12-14 01:10:41

问题


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


回答1:


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;
  }
}


来源:https://stackoverflow.com/questions/33612439/drawing-3d-polygon-on-unity-3d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!