简体   繁体   中英

How to draw circle around an object in unity3d?

I want to draw a cube on scene. I create a project on unity3d. It has main camera and directional light. I add an empty gameobject using unity gui. I create a .cs file and attached to gameobject. Content of C# file is :

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. 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?

2 - One of the problems is that your "D" can be different depends on the box rotation.

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). The sabe box, if you calculate the D in 45º direction will be 0.7 (hipotenuse).

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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