using System; using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEngine.UIElements; public class RuntimeTransformGizmo : RuntimeGizmo { protected Material redMaterial; protected Material greenMaterial; protected Material blueMaterial; protected Material yellowMaterial; protected Material whiteMaterial; //the vector projected by Gizmo XYZ axis on the screen protected Vector2 gizmoXYZScreenStart; protected Vector2 gizmoXYZScreenDirection; protected enum Axis { X, Y, Z } public override void Initialize() { //Material Initialize yellowMaterial = Resources.Load("Materials/Yellow"); whiteMaterial = Resources.Load("Materials/White"); } public override void Update() { } protected override void RenderUpdate() { base.RenderUpdate(); } protected override void FollowUpdate() { } public override void EnableGizmo(GameObject target) { base.EnableGizmo(target); } public override void DisableGizmo() { base.DisableGizmo(); } public override void OnSelect(string handleName) { } public override void OnUnSelect() { } protected override void OnSelecting() { } protected override void ChangeRenderOnSelect() { } protected override void ChangeRenderOnUnSelect() { } protected Vector3? GetIntersectionPlaneAndLine(Axis axis, Vector3 lineStart, Vector3 lineDirection) { Vector4 plane = GetPlane(axis); Vector3 planeNormal = new Vector3(plane.x, plane.y, plane.z); Vector3? intersectionPoint = GetIntersectionPlaneAndLine(planeNormal, plane.w, lineStart, lineDirection); return intersectionPoint; } protected Vector3? GetIntersectionPlaneAndLine(Vector3 planeNormal, float d, Vector3 lineStart, Vector3 lineDirection) { // Plane: Ax + By + Cz + D = 0 // Line: P(t) = P0 + tD float a = planeNormal.x; float b = planeNormal.y; float c = planeNormal.z; float dx = lineDirection.x; float dy = lineDirection.y; float dz = lineDirection.z; float denominator = a * dx + b * dy + c * dz; if (Math.Abs(denominator) < 0.0001f) return null; float t = -(a * lineStart.x + b * lineStart.y + c * lineStart.z + d) / denominator; Vector3 intersectionPoint = lineStart + t * lineDirection; return intersectionPoint; } protected Vector4 GetPlane(Axis axis) { Vector4 plane = new Vector4(); Vector3 normal = new Vector3(); switch (axis) { case Axis.X: normal = gizmo.GetComponent().right; break; case Axis.Y: normal = gizmo.GetComponent().up; break; case Axis.Z: normal = gizmo.GetComponent().forward; break; } plane.x = normal.x; plane.y = normal.y; plane.z = normal.z; Vector3 position = gizmo.GetComponent().position; plane.w = -Vector3.Dot(position,normal); return plane; } }