如何在统一3D中制作动态模型? [英] How to make a dynamic model in unity 3D?

查看:100
本文介绍了如何在统一3D中制作动态模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例代码,它在其中创建了鼠标单击的动态墙。

I have a sample code that where it's creating dynamic walls to a mouse clicks.

using UnityEngine;
using System.Collections;

public class CreateWalls : MonoBehaviour {

    bool creating;
    public GameObject start;
    public GameObject end;

    public GameObject wallPrehab;
    GameObject wall;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        getinput();
    }

    /*
     *this is the method for getting mouse click inputs
     */
    void getinput(){
        if (Input.GetMouseButtonDown (0)) {
            setStart ();
        } else if (Input.GetMouseButtonUp (0)) {
            setEnd ();
        } else {
            if(creating){
                adjust();
            }
        }
    }
    /*
     * getting the mouse clicked position coordinate 
     */
    void setStart(){
        creating = true;
        start.transform.position = getWorldPoint ();
        Debug.Log("Start = " + start.transform.position);
        wall = (GameObject) Instantiate (wallPrehab, start.transform.position, Quaternion.identity);

    }

    /*
     * getting the mouse click over position coordinate
     */
    void setEnd(){
        creating = false;
        end.transform.position = getWorldPoint ();
        Debug.Log("End = " + end.transform.position);
    }

    /*
     * invoking the wall building method
     */
    void adjust(){
        end.transform.position = getWorldPoint ();
        adjustWall ();
    }

    /*
     * build the wall in between start point and the end point
     */
    void adjustWall(){
        start.transform.LookAt (end.transform.position);
        end.transform.LookAt (start.transform.position);
        float distance = Vector3.Distance (start.transform.position, end.transform.position);
        wall.transform.position = start.transform.position + distance / 2 * start.transform.forward;
        wall.transform.rotation = start.transform.rotation;
        wall.transform.localScale = new Vector3 (wall.transform.localScale.x, wall.transform.localScale.y, distance);
    }

    /*
     * method where the coordinate we taken from mouse click
     */
    Vector3 getWorldPoint(){
        Ray ray = GetComponent<Camera>().ScreenPointToRay (Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast (ray, out hit)) {
            return hit.point;
        }
        return Vector3.zero;
    }
}

我有一些坐标,下图的每个角。

I have some coordinates, each corner of below image.

a=(55, 297)
b=(300, 297)
c=(55, 297)
d=(55, 52)

上面的坐标是该图像的

现在我想为这些图像建造墙坐标。像一个盒子。我不知道如何放置它们(坐标)。基本上一个墙的坐标将是a(x,y)和b(x,y)。另一个将是b(x,y)和c(x,y)。有人可以帮我吗,我会很棒的

now I want to build walls for those coordinates. like a box. I don't know how to put them(coordinates). basically one walls coordinates will be a(x,y) and b(x,y). another will be b(x,y) and c(x,y). can someone help me on this I'll be great

推荐答案

您可以使用基本体来建造这堵墙,但这可以使您默认10x10顶点平面。这不是很有效,因为您花了很多无用的飞机。

You can build this walls with primitives, but this get you a default 10x10 vertice plane. This is not so much efficient because you spent a lot of unneded planes.

GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.localScale = Vector3 (xsize, ysize, zsize);
cube.transform.position = Vector3(xpos, ypos, zpos);

最好的解决方案和更复杂的方法是完全制作自己的网格。
为此,您需要做的事情很少,这里有一个示例来制作一个简单的平面:

The best solution and more complex is to make fully your own Mesh. To do it you need few things, there you have an example to do a simple plane:

GameObject plane = new GameObject("Plane");
MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));

Mesh mymesh = new Mesh();
mymesh.name = "MyCustomMesh";
mymesh.vertices = new Vector3[] {
     new Vector3(xpos, ypos, zpos),
     new Vector3(xpos, ypos, zpos),
     new Vector3(xpos, ypos, zpos),
     new Vector3(xpos, ypos, zpos),
 };
 mymesh.uv = new Vector2[] {
     new Vector2 (0, 0),
     new Vector2 (0, 1),
     new Vector2(1, 1),
     new Vector2 (1, 0)
 };
 mymesh.triangles = new int[] { 0, 1, 2, 0, 2, 3};
 mymesh.RecalculateNormals();

meshFilter.mesh = mymesh;
MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;

这篇关于如何在统一3D中制作动态模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆