简体   繁体   English

如何在unity3d中围绕对象绘制圆圈?

[英]How to draw circle around an object in unity3d?

I want to draw a cube on scene. 我想在场景上绘制一个立方体。 I create a project on unity3d. 我在unity3d上创建一个项目。 It has main camera and directional light. 它具有主摄像头和定向灯。 I add an empty gameobject using unity gui. 我使用unity gui添加一个空的gameobject。 I create a .cs file and attached to gameobject. 我创建一个.cs文件并附加到gameobject。 Content of C# file is : C#文件的内容为:

using UnityEngine; 
using System.Collections;
using System.Collections.Generic;

/**
 * Simple example of creating a procedural 6 sided cube
 */
[RequireComponent (typeof (MeshFilter))] 
[RequireComponent (typeof (MeshRenderer))]
public class test : MonoBehaviour {

void Start () {
    MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
    Mesh mesh = new Mesh ();
    meshFilter.mesh = mesh;

    mesh.vertices = new Vector3[]{
        // face 1 (xy plane, z=0)
        new Vector3(0,0,0), 
        new Vector3(1,0,0), 
        new Vector3(1,1,0), 
        new Vector3(0,1,0), 
        // face 2 (zy plane, x=1)
        new Vector3(1,0,0), 
        new Vector3(1,0,1), 
        new Vector3(1,1,1), 
        new Vector3(1,1,0), 
        // face 3 (xy plane, z=1)
        new Vector3(1,0,1), 
        new Vector3(0,0,1), 
        new Vector3(0,1,1), 
        new Vector3(1,1,1), 
        // face 4 (zy plane, x=0)
        new Vector3(0,0,1), 
        new Vector3(0,0,0), 
        new Vector3(0,1,0), 
        new Vector3(0,1,1), 
        // face 5  (zx plane, y=1)
        new Vector3(0,1,0), 
        new Vector3(1,1,0), 
        new Vector3(1,1,1), 
        new Vector3(0,1,1), 
        // face 6 (zx plane, y=0)
        new Vector3(0,0,0), 
        new Vector3(0,0,1), 
        new Vector3(1,0,1), 
        new Vector3(1,0,0), 
    };

    int faces = 6; // here a face = 2 triangles

    List<int> triangles = new List<int>();
    List<Vector2> uvs = new List<Vector2>();

    for (int i = 0; i < faces; i++) {
        int triangleOffset = i*4;
        triangles.Add(0+triangleOffset);
        triangles.Add(2+triangleOffset);
        triangles.Add(1+triangleOffset);

        triangles.Add(0+triangleOffset);
        triangles.Add(3+triangleOffset);
        triangles.Add(2+triangleOffset);

        // same uvs for all faces
        uvs.Add(new Vector2(0,0));
        uvs.Add(new Vector2(1,0));
        uvs.Add(new Vector2(1,1));
        uvs.Add(new Vector2(0,1));
    }

    mesh.triangles = triangles.ToArray();

    mesh.uv = uvs.ToArray();

    GetComponent<Renderer>().material = new Material(Shader.Find("Diffuse"));

    mesh.RecalculateNormals(); 
    mesh.RecalculateBounds (); 
    mesh.Optimize();
} 
}

This code works. 此代码有效。 Now, I want to draw circle that has perspective effects around this cube using SetPixel function. 现在,我想使用SetPixel函数在这个立方体周围绘制具有透视效果的圆。 How can I do this work? 我该怎么做? I want to create a view as below 我想创建一个如下视图

在此处输入图片说明

1 - Instead of creating a hand maded cube, why don't you user a primitive box and just set the size? 1-为什么不创建一个手工制作的多维数据集,为什么不使用原始框并设置大小呢?

2 - One of the problems is that your "D" can be different depends on the box rotation. 2-问题之一是您的“ D”可以不同,具体取决于盒子的旋转角度。

For example, if your box is 0º, the D in 0º direction will be 0.5 (from center to boundary of a 1 unit cube). 例如,如果您的框为0º,则0º方向上的D将为0.5(从1立方的中心到边界)。 The sabe box, if you calculate the D in 45º direction will be 0.7 (hipotenuse). 如果您在45º方向上计算D,则sabe框将为0.7(斜边)。

Even if you trie to calculate the boundaries first, this boundaries will be different depens on rotation. 即使您先尝试计算边界,此边界在旋转时也会有所不同。 0º = 0.5, 45º = 0.7 and so on (the same problem) 0º= 0.5、45º = 0.7等等(同样的问题)

The simple way to aprouch (that I can think now) is: 达到足够的简单方法(我现在可以想到)是:

Create a primitive cube and set the desired scale. 创建一个基本立方体并设置所需的比例。 Create a plane that will represent the circle and add a transparent texture of a circle. 创建一个将代表该圆的平面,并添加一个圆的透明纹理。 Add the plane (circle) as a child of the box, while you resize the box, the circle will resize together. 将平面(圆)添加为框的子级,同时调整框的大小时,圆将一起调整大小。

Sorry for the grammar, english is not my native language. 抱歉,语法不是我的母语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM