Unity3d Graphics Bar in the scene of the circle

Source: Internet
Author: User
Tags abs cos sin

??

Sun Guangdong: 2015-2-6/2:28 reprint Please specify the Source: http://blog.csdn.net/u010019717

More full content Please see my game Bull address: http://www.unitymanual.com/space-uid-18602.html

First look at the effect:

The difference is a 2d a 3d.

2d is not introduced, relatively simple, for the 3d content, we first look at a mathematical question and answer, so it is easy to understand the program.

That would be a good thing! Just look at the following script.

Using unityengine;using system.collections;using Sunguangdong.math; [Executeineditmode]public class Drawcirclemono:monobehaviour {private Vector3 m_normal;//Use this for INITIALIZATIONVO ID Start () {m_normal = new Vector3 (0, 1, 0);} void Ondrawgizmos () {circle2d circle2 = new Circle2d (transform.position, 5);D rawcircle (ref circle2, color.red); Vector3 Center = new Vector3 (transform.position.x, TRANSFORM.POSITION.Y + 0.2f, transform.position.z); Circle3d circle3 = new Circle3d (ref Center,ref M_normal, 5);D rawcircle (Circle3, color.red, 40);} protected void drawcircle (ref circle2d circle, color color) {int count = 40;float Delta = 2f * MATHF.PI/COUNT; Vector3 prev = circle. Eval (0); Color tempcolor = Gizmos.color; Gizmos.color = color;for (int i = 1; I <= count; ++i) {Vector3 Curr = circle. Eval (i * delta); Gizmos.drawline (prev, curr);p rev = Curr;} Gizmos.color = Tempcolor;} protected void Drawcircle (Vector2 center, float radius, color color) {Circle2d circle = new Circle2d (ref center, RADIUS);int count = 40;float Delta = 2f * MATHF.PI/COUNT; Vector3 prev = circle. Eval (0); Color tempcolor = Gizmos.color; Gizmos.color = color;for (int i = 1; I <= count; ++i) {Vector3 Curr = circle. Eval (i * delta); Gizmos.drawline (prev, curr);p rev = Curr;} Gizmos.color = Tempcolor;} protected void Drawcircle (Circle3d circle, color color, int count =) {Float Delta = 2f * MATHF.PI/COUNT; Vector3 prev = circle. Eval (0); Color tempcolor = Gizmos.color; Gizmos.color = color;for (int i = 1; I <= count; ++i) {Vector3 Curr = circle. Eval (i * delta); Gizmos.drawline (prev, curr);p rev = Curr;} Gizmos.color = Tempcolor;}}

The above script is attached to any object in the scene, such as Cube


namespace sunguangdong.math{using System;    Using System.Collections.Generic;    Using System.Runtime.InteropServices;    Using Unityengine;        [StructLayout (layoutkind.sequential)] public struct Circle2d {public Vector2 Center;        public float Radius; Public circle2d (ref Vector2 Center, float radius) {this.            Center = center; This.        radius = radius; } Public circle2d (Vector2 Center, float radius) {this.            Center = center; This.        radius = radius; public float Calcperimeter () {return (6.283185f *).        Radius); } public float CalcArea () {return (3.141593f * this. Radius) * this.        Radius); Public Vector2 Eval (float t) {return new Vector2 (this. Center.x + (this. Radius * Mathf.cos (t)), this. Center.y + (this.        Radius * Mathf.sin (t)));  } public Vector2 Eval (float T, float radius) {          return new Vector2 (this. Center.x + (RADIUS * Mathf.cos (t)), this.        CENTER.Y + (RADIUS * Mathf.sin (t))); The public bool Contains (ref Vector2 point) {Vector2 vector = point-this. Center;return (Vector.magnitude <= (this. Radius * this.        Radius)); The public bool Contains (Vector2 point) {Vector2 vector = point-this. Center;return (Vector.magnitude <= (this. Radius * this.        Radius)); The public void Include (ref circle2d circle) {Vector2 vector = circle. Center-this.            Center;float num = vector.magnitude; float num2 = circle. Radius-this.            Radius;            float num3 = num2 * NUM2;                if (num3 >= num) {if (num2 >= 0f) {this = circle;                }} else {float NUM4 = mathf.sqrt (num);                if (Num4 > 1e-05f) {    float NUM5 = (num4 + num2)/(2f * num4); This.                Center + = (VECTOR2) (NUM5 * vector); } this. Radius = 0.5f * ((NUM4 + this. Radius) + circle.            Radius); }} public void Include (Circle2d circle) {this.        Include (ref circle); }    }}


namespace sunguangdong.math{using System;    Using System.Runtime.InteropServices;    Using Unityengine;        [StructLayout (layoutkind.sequential)] public struct Circle3d {public Vector3 Center;        Public Vector3 Axis0;        Public Vector3 Axis1;        Public Vector3 Normal;        public float Radius; Public Circle3d (ref Vector3 Center, ref Vector3 Normal, float radius) {this.            Center = center; This.            normal = normal; Vector3ex.createorthonormalbasis (out). Axis0, out of this. AXIS1, ref this.            Normal); This.        radius = radius; } Public Circle3d (Vector3 Center, Vector3 Normal, float radius) {this.            Center = center; This.            normal = normal; Vector3ex.createorthonormalbasis (out). Axis0, out of this. AXIS1, ref this.            Normal); This.        radius = radius; public float Calcperimeter () {return (6.283185f *).        Radius);       } public float CalcArea () {return (3.141593f * this. Radius) * this.        Radius); Public Vector3 Eval (float t) {return (this. Center + ((Vector3) (this. Radius * ((Mathf.cos (t) * this. AXIS0) + (Mathf.sin (t) * this.        AXIS1))); Public Vector3 Eval (float T, float radius) {return (this. Center + ((VECTOR3) (RADIUS * (Mathf.cos (t) * this. AXIS0) + (Mathf.sin (t) * this.        AXIS1))); }    }}


namespace sunguangdong.math{using System;    Using System.Collections.Generic;    Using System.Runtime.CompilerServices;    Using System.Runtime.InteropServices;    Using Unityengine;        public static class Vector3ex {public static readonly Vector3 Zero = new Vector3 (0f, 0f, 0f); public static void Createorthonormalbasis (out Vector3 u, out Vector3 V, ref Vector3 W) {if (Mathf.abs (w                . x) >= Mathf.abs (w.y)) {Float num = mathfex.invsqrt ((w.x * w.x) + (W.Z * w.z));                u.x =-w.z * NUM;                U.y = 0f;                U.Z = w.x * NUM;                v.x = W.y * U.Z;                V.Y = (W.Z * u.x)-(w.x * u.z);            v.z =-w.y * u.x;                } else {Float num2 = mathfex.invsqrt ((W.Y * w.y) + (W.Z * w.z));                u.x = 0f;                U.Y = w.z * NUM2;                u.z =-w.y * NUM2;         v.x = (W.Y * u.z)-(W.Z * u.y);       V.Y =-w.x * U.Z;            v.z = w.x * U.Y; }        }}}


namespace sunguangdong.math{    using System;    Using System.Runtime.InteropServices;    Using Unityengine;    public static class Mathfex    {public        const float Pi = 3.141593f;        public static float invsqrt (float value)        {            if (value! = 0f)            {                return (1f/mathf.sqrt (value));            }            return 0f;}}    }


Finally, attach some other pictures.






Unity3d Graphics Bar in the scene of the circle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.