Circle and unity3d scenes in unity3d scenes

Source: Internet
Author: User

Circle and unity3d scenes in unity3d scenes

Sun Guangdong: 2015-2-6/2: 28 reprint please indicate the source: http://blog.csdn.net/u010019717

More comprehensive content please see my game pretty cool address: http://www.unitymanual.com/space-uid-18602.html

First look at the effect:

The difference is a 2d 3d.

2d is not introduced. It is relatively simple. for 3d content, let's take a look at a question and answer in mathematics, so that we can easily understand the program.

This is easy to handle! Let's look at the following scripts.

using UnityEngine;using System.Collections;using SunGuangDong.Math;[ExecuteInEditMode]public class DrawCircleMono : MonoBehaviour {private Vector3 m_Normal ;// Use this for initializationvoid Start () {m_Normal = new Vector3 (0, 1, 0);}void OnDrawGizmos(){Circle2d circle2 = new Circle2d(transform.position, 5);DrawCircle(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);DrawCircle (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);prev = 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);prev = curr;}Gizmos.color = tempColor;}protected void DrawCircle(Circle3d circle, Color color, int count = 20){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);prev = curr;}Gizmos.color = tempColor;}}

The above script is mounted to any object in the scenario, 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 * this.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)));        }        public bool Contains(ref Vector2 point)        {            Vector2 vector = point - this.Center;return (vector.magnitude <= (this.Radius * this.Radius));        }        public bool Contains(Vector2 point)        {            Vector2 vector = point - this.Center;return (vector.magnitude <= (this.Radius * this.Radius));        }        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 this.Axis0, out 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 this.Axis0, out this.Axis1, ref this.Normal);            this.Radius = radius;        }        public float CalcPerimeter()        {            return (6.283185f * this.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;        }    }}


Attach some other images.






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.