在统一3D上绘制3D多边形 [英] Drawing 3D polygon on unity 3d
问题描述
我正在为我正在进行的项目尝试团结一致.
I am trying out unity for a project that i am on.
我正在尝试从一组坐标中绘制3D多边形.
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
推荐答案
您可以制作基元并对其进行操作,但是如果将来需要扩展或更改需求,这将对您有很大的限制.我建议根据需要使用过程网格来创建所需的几何.基础并不是太难,只是在给定一些顶点的情况下从其基础组件构造Mesh对象的问题.这是构造3d四边形的示例:
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;
}
}
这篇关于在统一3D上绘制3D多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!