From 78304183fb59e243a2d6da4a00a4311ae21c9717 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Fri, 15 Mar 2024 21:03:07 +0800 Subject: *misc --- SetMainCamera.cs | 1 + _ActiveRagdoll/AnimationHandler.cs | 14 - _ActiveRagdoll/Balance.cs | 47 +- _ActiveRagdoll/Holding.cs | 1 + _ActiveRagdoll/MovementDataHandler.cs | 92 ---- _ActiveRagdoll/MovementHandler.cs | 85 ---- _ActiveRagdoll/PickupHandler.cs | 63 --- _ActiveRagdoll/Player/AnimationHandler.cs | 15 + _ActiveRagdoll/Player/MovementDataHandler.cs | 92 ++++ _ActiveRagdoll/Player/MovementHandler.cs | 86 ++++ _ActiveRagdoll/Player/PickupHandler.cs | 64 +++ _ActiveRagdoll/Standing.cs | 4 +- _Debug/DebugRigidBody.cs | 13 +- _Debug/Draw.cs | 612 +++++++++++++++++++++++++++ _Debug/REPL.cs | 55 ++- 15 files changed, 960 insertions(+), 284 deletions(-) delete mode 100644 _ActiveRagdoll/AnimationHandler.cs delete mode 100644 _ActiveRagdoll/MovementDataHandler.cs delete mode 100644 _ActiveRagdoll/MovementHandler.cs delete mode 100644 _ActiveRagdoll/PickupHandler.cs create mode 100644 _ActiveRagdoll/Player/AnimationHandler.cs create mode 100644 _ActiveRagdoll/Player/MovementDataHandler.cs create mode 100644 _ActiveRagdoll/Player/MovementHandler.cs create mode 100644 _ActiveRagdoll/Player/PickupHandler.cs create mode 100644 _Debug/Draw.cs diff --git a/SetMainCamera.cs b/SetMainCamera.cs index 82d3f5f..f6235bf 100644 --- a/SetMainCamera.cs +++ b/SetMainCamera.cs @@ -16,3 +16,4 @@ public class SetMainCamera : MonoBehaviour } } } + \ No newline at end of file diff --git a/_ActiveRagdoll/AnimationHandler.cs b/_ActiveRagdoll/AnimationHandler.cs deleted file mode 100644 index 6011e47..0000000 --- a/_ActiveRagdoll/AnimationHandler.cs +++ /dev/null @@ -1,14 +0,0 @@ -using UnityEngine; - -public class AnimationHandler : MonoBehaviour -{ - public int animationState; - - private void Start() - { - } - - private void Update() - { - } -} diff --git a/_ActiveRagdoll/Balance.cs b/_ActiveRagdoll/Balance.cs index dc9ae60..06953fa 100644 --- a/_ActiveRagdoll/Balance.cs +++ b/_ActiveRagdoll/Balance.cs @@ -1,32 +1,31 @@ using UnityEngine; +//Player Balance ¶¯×÷-¿ØÖÆÉíÌ寽ºâ public class Balance : MonoBehaviour { - private Rigidbody handLeft; - + #region rigs + private Rigidbody handLeft; private Rigidbody handRight; - - private Rigidbody footLeft; - - private Rigidbody footRight; - + private Rigidbody footLeft; // kneeLeft + private Rigidbody footRight; // kneeRight private Rigidbody hip; + #endregion - private Vector3 centerOfMass; + private Vector3 centerOfMass; // 5.0759, 0, -7.2038 - private Rigidbody[] allRigs; + private Rigidbody[] allRigs;//ËùÓÐ14¸öparts - public float[] balanceForce; + public float[] balanceForce; // 50 30 30 30 - public float[] footCenterForces; + public float[] footCenterForces; // 50 20 20 20 - private AnimationHandler animationHandler; + private AnimationHandler animationHandler; // µ±Ç°¶¯×÷±àºÅ private PlayerDeath death; private Strength str; - private float muscleMultiplier; + private float muscleMultiplier; // 1 private void Start() { @@ -90,11 +89,27 @@ public class Balance : MonoBehaviour Vector3 vector = footLeft.transform.position + footLeft.transform.forward * 0.5f; Vector3 vector2 = footRight.transform.position + footRight.transform.forward * 0.5f; Vector3 vector3 = (vector + vector2) / 2f; - if (!(vector3.y + 0.3f > hip.worldCenterOfMass.y)) + + Vector3 pos = vector3; + pos.y += 0.3f; + + if (!(vector3.y + 0.3f > hip.worldCenterOfMass.y)) { vector3.y = 0f; - Vector3 vector4 = centerOfMass - vector3; - footLeft.AddForceAtPosition(vector4 * muscleMultiplier * balanceForce[animationHandler.animationState], vector, ForceMode.Acceleration); + Vector3 vector4 = centerOfMass - vector3; // centerOfMassÊÇxozÆ½Ãæ£¬²»º¬y + REPL.footCenter.GetComponent().customDraw = () => + { + Draw.Sphere(vector, 0.05f); + Draw.Sphere(vector2, 0.05f); + Draw.Sphere(pos, 0.05f); + Draw.Sphere(vector3, 0.05f); + Draw.Sphere(hip.worldCenterOfMass, 0.2f); + + Draw.Sphere(centerOfMass, 0.05f); + Draw.Line3D(vector3 + new Vector3(0, footLeft.transform.position.y, 0), centerOfMass + new Vector3(0, footLeft.transform.position.y, 0)); + }; + + footLeft.AddForceAtPosition(vector4 * muscleMultiplier * balanceForce[animationHandler.animationState], vector, ForceMode.Acceleration); footRight.AddForceAtPosition(vector4 * muscleMultiplier * balanceForce[animationHandler.animationState], vector2, ForceMode.Acceleration); } } diff --git a/_ActiveRagdoll/Holding.cs b/_ActiveRagdoll/Holding.cs index dafc08b..eb27095 100644 --- a/_ActiveRagdoll/Holding.cs +++ b/_ActiveRagdoll/Holding.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +//Player Holding ¶¯×÷-ÄÃÎäÆ÷ public class Holding : MonoBehaviour { [HideInInspector] diff --git a/_ActiveRagdoll/MovementDataHandler.cs b/_ActiveRagdoll/MovementDataHandler.cs deleted file mode 100644 index 18d43f5..0000000 --- a/_ActiveRagdoll/MovementDataHandler.cs +++ /dev/null @@ -1,92 +0,0 @@ -using UnityEngine; - -public class MovementDataHandler : MonoBehaviour -{ - [HideInInspector] - public Vector3 groundedForward; - - [HideInInspector] - public Vector3 right; - - [HideInInspector] - public Vector3 left; - - [HideInInspector] - public Vector3 groundedBack; - - private Transform hip; - - private Transform torso; - - public Transform rotationTarget; - - [HideInInspector] - public float slopeStrenght; - - public float slopeVelocityStrenght; - - [HideInInspector] - public float sinceJump = 1f; - - [HideInInspector] - public Vector3 groundNormal; - - private InputHandler inputHandler; - - private Transform leftKnee; - - private Transform rightKnee; - - private void Start() - { - inputHandler = GetComponent(); - hip = GetComponentInChildren().transform; - torso = GetComponentInChildren().transform; - if (!rotationTarget) - { - rotationTarget = GetComponentInChildren().transform; - } - KneeLeft componentInChildren = GetComponentInChildren(); - if ((bool)componentInChildren) - { - leftKnee = componentInChildren.transform; - } - KneeRight componentInChildren2 = GetComponentInChildren(); - if ((bool)componentInChildren2) - { - rightKnee = componentInChildren2.transform; - } - } - - private void Update() - { - sinceJump += Time.deltaTime; - groundedForward = rotationTarget.forward; - groundedForward.y = slopeStrenght * 1f; - groundedForward = groundedForward.normalized; - groundedBack = -groundedForward; - right = rotationTarget.right; - left = -rotationTarget.right; - slopeStrenght = Mathf.Lerp(slopeStrenght, 0f, Time.deltaTime * 1f); - Debug.DrawLine(hip.position, hip.position + groundedForward * 10f); - } - - public void SetSlope(Vector3 normal) - { - groundNormal = normal; - slopeStrenght = Vector3.Cross(rotationTarget.right, normal).y; - Vector3 lhs = Vector3.Cross(Vector3.up, inputHandler.inputMovementDirection); - slopeVelocityStrenght = Vector3.Cross(lhs, normal).y; - } - - public float GetSmallestLegAngle() - { - float num = Vector3.Angle(leftKnee.forward, Vector3.down); - float num2 = Vector3.Angle(rightKnee.forward, Vector3.down); - if (num < num2) - { - return num; - } - return num2; - } -} diff --git a/_ActiveRagdoll/MovementHandler.cs b/_ActiveRagdoll/MovementHandler.cs deleted file mode 100644 index 22795f7..0000000 --- a/_ActiveRagdoll/MovementHandler.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using UnityEngine; - -public class MovementHandler : MonoBehaviour -{ - private InputHandler inputHandler; - - public float friction = 0.9f; - - public Vector3 movementVector; - - public float[] animationForceAmounts; - - private AnimationHandler animationHandler; - - private RigidbodyHolder allRigs; - - public AnimationCurve jumpCurve; - - public float jumpForce; - - private StandingDataHandler standingData; - - private MovementDataHandler data; - - private PlayerDeath death; - - [HideInInspector] - public float multiplier = 1f; - - private WobbleShake wobbleShake; - - private void Start() - { - wobbleShake = GetComponentInChildren(); - death = GetComponent(); - standingData = GetComponent(); - inputHandler = GetComponent(); - animationHandler = GetComponent(); - allRigs = GetComponent(); - data = GetComponent(); - } - - private void FixedUpdate() - { - if (!death.dead) - { - data.sinceJump += Time.fixedDeltaTime; - movementVector += inputHandler.inputMovementDirection * animationForceAmounts[animationHandler.animationState]; - movementVector *= friction; - for (int i = 0; i < allRigs.GetAllRigs().Length; i++) - { - allRigs.GetAllRigs()[i].AddForce(movementVector * multiplier, ForceMode.Acceleration); - } - } - } - - public void Jump() - { - if (!(data.sinceJump < 0.5f) && !(standingData.sinceGrounded > 0.3f)) - { - data.sinceJump = 0f; - StartCoroutine(AddJumpForce()); - wobbleShake.AddShake(Vector3.up * 2f, 0.8f); - } - } - - private IEnumerator AddJumpForce() - { - float counter = 0f; - for (int i = 0; i < allRigs.GetAllRigs().Length; i++) - { - allRigs.GetAllRigs()[i].velocity = new Vector3(allRigs.GetAllRigs()[i].velocity.x, 0f, allRigs.GetAllRigs()[i].velocity.z); - } - while (counter < jumpCurve.keys[jumpCurve.length - 1].time && !death.dead) - { - counter += Time.deltaTime; - for (int j = 0; j < allRigs.GetAllRigs().Length; j++) - { - allRigs.GetAllRigs()[j].AddForce(Vector3.up * multiplier * jumpForce * jumpCurve.Evaluate(counter) * Time.deltaTime, ForceMode.Acceleration); - } - yield return null; - } - } -} diff --git a/_ActiveRagdoll/PickupHandler.cs b/_ActiveRagdoll/PickupHandler.cs deleted file mode 100644 index 8bf1615..0000000 --- a/_ActiveRagdoll/PickupHandler.cs +++ /dev/null @@ -1,63 +0,0 @@ -using UnityEngine; - -public class PickupHandler : MonoBehaviour -{ - public Pickup setWeapon; - - public Pickup setWeapon2; - - private WeaponHandler weaponHandler; - - private Holding holding; - - private float counter; - - private void Start() - { - weaponHandler = GetComponent(); - holding = GetComponent(); - if ((bool)setWeapon) - { - PickUp(setWeapon); - } - if ((bool)setWeapon2) - { - PickUp2(setWeapon2); - } - } - - private void Update() - { - counter += Time.deltaTime; - } - - public void PickUp(Pickup objectToPickUp) - { - if (!(counter < 1f)) - { - counter = 0f; - holding.Drop(); - Weapon component = objectToPickUp.GetComponent(); - Gun component2 = component.GetComponent(); - weaponHandler.SetGun(component2, mainHand: true); - bool hasOffHand = false; - if ((bool)setWeapon2) - { - hasOffHand = true; - } - HoldableObject component3 = component.GetComponent(); - component3.holder = base.transform; - holding.StartHolding(component3, hasOffHand); - } - } - - public void PickUp2(Pickup objectToPickUp) - { - Weapon component = objectToPickUp.GetComponent(); - Gun component2 = component.GetComponent(); - weaponHandler.SetGun(component2, mainHand: false); - HoldableObject component3 = component.GetComponent(); - component3.holder = base.transform; - holding.StartHolding(component3, hasOffHand: true); - } -} diff --git a/_ActiveRagdoll/Player/AnimationHandler.cs b/_ActiveRagdoll/Player/AnimationHandler.cs new file mode 100644 index 0000000..382b3a5 --- /dev/null +++ b/_ActiveRagdoll/Player/AnimationHandler.cs @@ -0,0 +1,15 @@ +using UnityEngine; + +//Player AnimationHandler ¼Ç¼µ±Ç°¶¯×÷״̬±àºÅ +public class AnimationHandler : MonoBehaviour +{ + public int animationState; + + private void Start() + { + } + + private void Update() + { + } +} diff --git a/_ActiveRagdoll/Player/MovementDataHandler.cs b/_ActiveRagdoll/Player/MovementDataHandler.cs new file mode 100644 index 0000000..18d43f5 --- /dev/null +++ b/_ActiveRagdoll/Player/MovementDataHandler.cs @@ -0,0 +1,92 @@ +using UnityEngine; + +public class MovementDataHandler : MonoBehaviour +{ + [HideInInspector] + public Vector3 groundedForward; + + [HideInInspector] + public Vector3 right; + + [HideInInspector] + public Vector3 left; + + [HideInInspector] + public Vector3 groundedBack; + + private Transform hip; + + private Transform torso; + + public Transform rotationTarget; + + [HideInInspector] + public float slopeStrenght; + + public float slopeVelocityStrenght; + + [HideInInspector] + public float sinceJump = 1f; + + [HideInInspector] + public Vector3 groundNormal; + + private InputHandler inputHandler; + + private Transform leftKnee; + + private Transform rightKnee; + + private void Start() + { + inputHandler = GetComponent(); + hip = GetComponentInChildren().transform; + torso = GetComponentInChildren().transform; + if (!rotationTarget) + { + rotationTarget = GetComponentInChildren().transform; + } + KneeLeft componentInChildren = GetComponentInChildren(); + if ((bool)componentInChildren) + { + leftKnee = componentInChildren.transform; + } + KneeRight componentInChildren2 = GetComponentInChildren(); + if ((bool)componentInChildren2) + { + rightKnee = componentInChildren2.transform; + } + } + + private void Update() + { + sinceJump += Time.deltaTime; + groundedForward = rotationTarget.forward; + groundedForward.y = slopeStrenght * 1f; + groundedForward = groundedForward.normalized; + groundedBack = -groundedForward; + right = rotationTarget.right; + left = -rotationTarget.right; + slopeStrenght = Mathf.Lerp(slopeStrenght, 0f, Time.deltaTime * 1f); + Debug.DrawLine(hip.position, hip.position + groundedForward * 10f); + } + + public void SetSlope(Vector3 normal) + { + groundNormal = normal; + slopeStrenght = Vector3.Cross(rotationTarget.right, normal).y; + Vector3 lhs = Vector3.Cross(Vector3.up, inputHandler.inputMovementDirection); + slopeVelocityStrenght = Vector3.Cross(lhs, normal).y; + } + + public float GetSmallestLegAngle() + { + float num = Vector3.Angle(leftKnee.forward, Vector3.down); + float num2 = Vector3.Angle(rightKnee.forward, Vector3.down); + if (num < num2) + { + return num; + } + return num2; + } +} diff --git a/_ActiveRagdoll/Player/MovementHandler.cs b/_ActiveRagdoll/Player/MovementHandler.cs new file mode 100644 index 0000000..4579efc --- /dev/null +++ b/_ActiveRagdoll/Player/MovementHandler.cs @@ -0,0 +1,86 @@ +using System.Collections; +using UnityEngine; + +//Player MovementHandler ¿ØÖÆÒƶ¯¡¢ÌøÔ¾ +public class MovementHandler : MonoBehaviour +{ + private InputHandler inputHandler; + + public float friction = 0.9f; + + public Vector3 movementVector; + + public float[] animationForceAmounts; + + private AnimationHandler animationHandler; + + private RigidbodyHolder allRigs; + + public AnimationCurve jumpCurve; + + public float jumpForce; + + private StandingDataHandler standingData; + + private MovementDataHandler data; + + private PlayerDeath death; + + [HideInInspector] + public float multiplier = 1f; + + private WobbleShake wobbleShake; + + private void Start() + { + wobbleShake = GetComponentInChildren(); + death = GetComponent(); + standingData = GetComponent(); + inputHandler = GetComponent(); + animationHandler = GetComponent(); + allRigs = GetComponent(); + data = GetComponent(); + } + + private void FixedUpdate() + { + if (!death.dead) + { + data.sinceJump += Time.fixedDeltaTime; + movementVector += inputHandler.inputMovementDirection * animationForceAmounts[animationHandler.animationState]; + movementVector *= friction; + for (int i = 0; i < allRigs.GetAllRigs().Length; i++) + { + allRigs.GetAllRigs()[i].AddForce(movementVector * multiplier, ForceMode.Acceleration); + } + } + } + + public void Jump() + { + if (!(data.sinceJump < 0.5f) && !(standingData.sinceGrounded > 0.3f)) + { + data.sinceJump = 0f; + StartCoroutine(AddJumpForce()); + wobbleShake.AddShake(Vector3.up * 2f, 0.8f); + } + } + + private IEnumerator AddJumpForce() + { + float counter = 0f; + for (int i = 0; i < allRigs.GetAllRigs().Length; i++) + { + allRigs.GetAllRigs()[i].velocity = new Vector3(allRigs.GetAllRigs()[i].velocity.x, 0f, allRigs.GetAllRigs()[i].velocity.z); + } + while (counter < jumpCurve.keys[jumpCurve.length - 1].time && !death.dead) + { + counter += Time.deltaTime; + for (int j = 0; j < allRigs.GetAllRigs().Length; j++) + { + allRigs.GetAllRigs()[j].AddForce(Vector3.up * multiplier * jumpForce * jumpCurve.Evaluate(counter) * Time.deltaTime, ForceMode.Acceleration); + } + yield return null; + } + } +} diff --git a/_ActiveRagdoll/Player/PickupHandler.cs b/_ActiveRagdoll/Player/PickupHandler.cs new file mode 100644 index 0000000..6a3a249 --- /dev/null +++ b/_ActiveRagdoll/Player/PickupHandler.cs @@ -0,0 +1,64 @@ +using UnityEngine; + +//Player PickupHandler ¼ñÎäÆ÷ +public class PickupHandler : MonoBehaviour +{ + public Pickup setWeapon; + + public Pickup setWeapon2; + + private WeaponHandler weaponHandler; + + private Holding holding; // holding¶¯×÷ + + private float counter; + + private void Start() + { + weaponHandler = GetComponent(); + holding = GetComponent(); + if ((bool)setWeapon) + { + PickUp(setWeapon); + } + if ((bool)setWeapon2) + { + PickUp2(setWeapon2); + } + } + + private void Update() + { + counter += Time.deltaTime; + } + + public void PickUp(Pickup objectToPickUp) + { + if (!(counter < 1f)) + { + counter = 0f; + holding.Drop(); + Weapon component = objectToPickUp.GetComponent(); + Gun component2 = component.GetComponent(); + weaponHandler.SetGun(component2, mainHand: true); + bool hasOffHand = false; + if ((bool)setWeapon2) + { + hasOffHand = true; + } + HoldableObject component3 = component.GetComponent(); + component3.holder = base.transform; + holding.StartHolding(component3, hasOffHand); + } + } + + public void PickUp2(Pickup objectToPickUp) + { + Weapon component = objectToPickUp.GetComponent(); + Gun component2 = component.GetComponent(); + weaponHandler.SetGun(component2, mainHand: false); + HoldableObject component3 = component.GetComponent(); + component3.holder = base.transform; + holding.StartHolding(component3, hasOffHand: true); + } +} diff --git a/_ActiveRagdoll/Standing.cs b/_ActiveRagdoll/Standing.cs index afbfd8b..2af0e4b 100644 --- a/_ActiveRagdoll/Standing.cs +++ b/_ActiveRagdoll/Standing.cs @@ -1,8 +1,9 @@ using UnityEngine; +//Player Standing ¶¯×÷-Õ¾Á¢ public class Standing : MonoBehaviour { - public RigidbodyMovment[] rigsToLift; + public RigidbodyMovment[] rigsToLift; //Head, Torso public AnimationCurve[] animationLiftCurves; @@ -49,6 +50,7 @@ public class Standing : MonoBehaviour RigidbodyMovment[] array = rigsToLift; foreach (RigidbodyMovment rigidbodyMovment in array) { + // Ê©¼ÓÒ»¸öÏòÉϵÄÁ¦ rigidbodyMovment.rig.AddForce(Vector3.up * muscleMultiplier * rigidbodyMovment.force * legAngleMultiplier * curve.Evaluate(standingData.distanceToGround + offset + moveData.slopeVelocityStrenght * -0.2f), ForceMode.Acceleration); } } diff --git a/_Debug/DebugRigidBody.cs b/_Debug/DebugRigidBody.cs index 8267f50..a83d1ed 100644 --- a/_Debug/DebugRigidBody.cs +++ b/_Debug/DebugRigidBody.cs @@ -1,4 +1,5 @@  +using System; using UnityEngine; public class DebugRigidBody : MonoBehaviour @@ -9,6 +10,7 @@ public class DebugRigidBody : MonoBehaviour Axis, Cube, Axis_Cube, + Custom, } public EMode mode = EMode.Axis; @@ -21,6 +23,8 @@ public class DebugRigidBody : MonoBehaviour public float cubeLen = 0.05f; + public Action customDraw; + static Material lineMaterial; static void CreateLineMaterial() { @@ -87,7 +91,7 @@ public class DebugRigidBody : MonoBehaviour // match our transform GL.MultMatrix(transform.localToWorldMatrix); - float len = cubeLen; + float len = cubeLen/2.0f; GL.wireframe = true; @@ -135,6 +139,13 @@ public class DebugRigidBody : MonoBehaviour GL.PopMatrix(); } + if(mode == EMode.Custom) + { + if(customDraw != null) + { + customDraw.Invoke(); + } + } } } diff --git a/_Debug/Draw.cs b/_Debug/Draw.cs new file mode 100644 index 0000000..d3ed497 --- /dev/null +++ b/_Debug/Draw.cs @@ -0,0 +1,612 @@ +/// +/// Draw lines at runtime +/// by Nothke +/// unlicensed, aka do whatever you want with it +/// made during Stugan 2016 :) +/// ..(it's midnight, and Robbie clicks A LOT, LOUDLY!) +/// +/// Important: +/// - Should be called in OnPostRender() (after everything else has been drawn) +/// therefore the script that calls it must be attached to the camera +/// - Use someMaterial.SetPass(0) to set that material for line rendering +/// If no material is SetPass'd, Unity default material will be used +/// - Use Draw.color = someColor to set line color +/// - Most parameters are passed as screen 0-1 points (see function summaries) +/// To input as pixel coordinates use PixelsToScreen(screenV2Coordinate) +/// +/// See function summaries for what this script can do +/// and check DrawExaple.cs for examples +/// + +using UnityEngine; +using System.Collections.Generic; + +public static class Draw +{ + public static Color color = Color.white; + + #region Line + + /// + /// Draws line between 2 screen points. Use Draw.PixelToScreen to convert from pixels to screen points + /// + public static void Line(Vector2 p1, Vector2 p2) + { + Prepare(); + + // Vertices + GL.Vertex(p1); + GL.Vertex(p2); + + Postpare(); + } + + /// + /// Draws line between 2 world points + /// + public static void Line3D(Vector3 p1, Vector3 p2) + { + Prepare3D(); + + GL.Vertex(p1); + GL.Vertex(p2); + + Postpare(); + } + + #endregion + + + #region Circles and Ellipses + + /// + /// Draws circle using a screen point center, and pixel radius. Use Draw.ScreenToPixel to convert screen points to pixels + /// + public static void Circle(Vector2 center, float pixelRadius) + { + Vector2 size = new Vector2(pixelRadius / Screen.width, pixelRadius / Screen.height); + + Ellipse(center, size); + } + + /// + /// Draws dashed circle using a screen point center, and pixel radius. Use Draw.ScreenToPixel to convert screen points to pixels + /// The dashes are not very nice, just skipping every other point.. + /// + public static void CircleDashed(Vector2 center, float radius) + { + Prepare(); + + float radX = radius / Screen.width; + float radY = radius / Screen.height; + + // Vertices + for (float theta = 0.0f; theta < (2 * Mathf.PI); theta += 0.01f) + { + Vector2 ci = new Vector2(center.x + (Mathf.Cos(theta) * radX), center.y + (Mathf.Sin(theta) * radY)); + GL.Vertex(ci); + } + + Postpare(); + } + + /// + /// Draws an ellipse using a center, and size (in screen fractions). Use Draw.ScreenToPixel to convert screen points to pixels + /// + public static void Ellipse(Vector2 center, Vector2 size) + { + Prepare(); + + float radX = size.x; + float radY = size.y; + + // Vertices + for (float theta = 0.0f; theta < (2 * Mathf.PI); theta += 0.01f) + { + Vector2 ci = new Vector2(center.x + (Mathf.Cos(theta) * radX), center.y + (Mathf.Sin(theta) * radY)); + GL.Vertex(ci); + + if (theta != 0) + GL.Vertex(ci); + } + + Postpare(); + } + + /// + /// Draws a circle in world space + /// + public static void Circle3D(Vector3 center, float radius, Vector3 normal) + { + Prepare3D(); + + normal = normal.normalized; + Vector3 forward = normal == Vector3.up ? + Vector3.ProjectOnPlane(Vector3.forward, normal).normalized : + Vector3.ProjectOnPlane(Vector3.up, normal); + Vector3 right = Vector3.Cross(normal, forward); + + for (float theta = 0.0f; theta < (2 * Mathf.PI); theta += 0.01f) + { + Vector3 ci = center + forward * Mathf.Cos(theta) * radius + right * Mathf.Sin(theta) * radius; + GL.Vertex(ci); + + if (theta != 0) + GL.Vertex(ci); + } + + Postpare(); + } + + + /// + /// Draws an elliptic orbit using eccentricity and semi-major axis in pixels + /// + public static void Orbit(Vector2 center, float eccentricity, float semiMajorAxis, float dir = 0) + { + Prepare(); + + eccentricity = Mathf.Clamp01(eccentricity); + + Vector2 up = new Vector2(Mathf.Cos(dir), Mathf.Sin(dir)); + Vector2 right = Vector3.Cross(up, Vector3.forward); + + for (float theta = 0.0f; theta < (2 * Mathf.PI); theta += 0.01f) + { + float r = (semiMajorAxis * (1 - eccentricity * eccentricity)) / (1 + eccentricity * Mathf.Cos(theta)); + + Vector2 point = PixelToScreen((right * Mathf.Cos(theta) * r) + (up * Mathf.Sin(theta) * r)); + Vector2 ci = center + point; + + GL.Vertex(ci); + + if (theta != 0) + GL.Vertex(ci); + } + + Postpare(); + } + + /// + /// Draws an elliptic orbit using periapsis and apoapsis in pixels + /// + public static void OrbitApses(Vector2 center, float periapsis, float apoapsis, float dir = 0) + { + float a = (periapsis + apoapsis) / 2; + float e = (apoapsis - periapsis) / (apoapsis + periapsis); + + Orbit(center, e, a, dir); + } + + /// + /// Draws an elliptic orbit in world space using eccentricity and semi-major axis + /// + public static void Orbit3D(Vector3 center, float eccentricity, float semiMajorAxis, Vector3 normal, Vector3 forward) + { + Prepare3D(); + + eccentricity = Mathf.Clamp01(eccentricity); + + forward = Vector3.ProjectOnPlane(forward, normal).normalized; + Vector3 right = Vector3.Cross(forward, normal).normalized; + + for (float theta = 0.0f; theta < (2 * Mathf.PI); theta += 0.01f) + { + float r = (semiMajorAxis * (1 - eccentricity * eccentricity)) / (1 + eccentricity * Mathf.Cos(theta)); + + Vector3 point = (right * Mathf.Cos(theta) * r) + (forward * Mathf.Sin(theta) * r); + Vector3 ci = center + point; + + GL.Vertex(ci); + + if (theta != 0) + GL.Vertex(ci); + } + + Postpare(); + } + + /// + /// Draws an elliptic orbit in world space using periapsis and apoapsis + /// + public static void Orbit3DApses(Vector3 center, float periapsis, float apoapsis, Vector3 normal, Vector3 forward) + { + float a = (periapsis + apoapsis) / 2; + float e = (apoapsis - periapsis) / (apoapsis + periapsis); + + Orbit3D(center, e, a, normal, forward); + } + + #endregion + + + #region Rectangle + + /// + /// Draws rectangle on screen using a Rect (in pixels) + /// + public static void Rect(Rect rect) + { + Rect(rect.x, rect.y, rect.width, rect.height); + } + + /// + /// Draws rectangle on screen (in screen 0-1 fractions) + /// + public static void RectScreen(float x, float y, float width, float height) + { + Prepare(); + + GL.Vertex(new Vector3(x, y)); + GL.Vertex(new Vector3(x + width, y)); + + GL.Vertex(new Vector3(x + width, y)); + GL.Vertex(new Vector3(x + width, y + height)); + + GL.Vertex(new Vector3(x + width, y + height)); + GL.Vertex(new Vector3(x, y + height)); + + GL.Vertex(new Vector3(x, y + height)); + GL.Vertex(new Vector3(x, y)); + + Postpare(); + } + + /// + /// Draws rectangle on screen (in pixels) + /// + public static void Rect(float x, float y, float width, float height) + { + PreparePixel(); + + GL.Vertex(new Vector3(x, y)); + GL.Vertex(new Vector3(x + width, y)); + + GL.Vertex(new Vector3(x + width, y)); + GL.Vertex(new Vector3(x + width, y + height)); + + GL.Vertex(new Vector3(x + width, y + height)); + GL.Vertex(new Vector3(x, y + height)); + + GL.Vertex(new Vector3(x, y + height)); + GL.Vertex(new Vector3(x, y)); + + Postpare(); + } + + #endregion + + + #region Grid + /// + /// Draws a horizontal grid + /// + public static void Grid(Vector3 center, float edgeLength, int lines = 10) + { + Grid(center, edgeLength, lines, Vector3.forward, Vector3.up); + } + + /// + /// Draws a gird with custom orientantion + /// + public static void Grid(Vector3 center, float edgeLength, int lines, Vector3 forward, Vector3 normal) + { + if (lines <= 1) return; + + Prepare3D(); + + forward = forward.normalized; + normal = Vector3.ProjectOnPlane(normal, forward).normalized; + Vector3 right = Vector3.Cross(forward, normal); + + // forward lines + for (int i = 0; i < lines; i++) + { + Vector3 fDir = forward * edgeLength * 0.5f; + Vector3 rDir = right * (-(edgeLength * 0.5f) + (i * edgeLength / (lines - 1))); + + GL.Vertex(center - fDir + rDir); + GL.Vertex(center + fDir + rDir); + } + + // sideways lines + for (int i = 0; i < lines; i++) + { + Vector3 rDir = right * edgeLength * 0.5f; + Vector3 fDir = forward * (-(edgeLength * 0.5f) + (i * edgeLength / (lines - 1))); + + GL.Vertex(center - rDir + fDir); + GL.Vertex(center + rDir + fDir); + } + + Postpare(); + } + + #endregion + + + #region 3D Primitives + + /// + /// Draws a sphere in world space. Similar to Gizmos.DrawWireSphere, but with the ability to add radial and vertical segments + /// + public static void Sphere(Vector3 center, float radius, int verticalSegments = 1, int radialSegments = 2) + { + if (radialSegments > 2) + { + for (int i = 0; i < radialSegments; i++) + { + Vector3 normal = new Vector3(Mathf.Sin((i * Mathf.PI) / radialSegments), 0, Mathf.Cos((i * Mathf.PI) / radialSegments)); + + Circle3D(center, radius, normal); + } + } + else + { + Circle3D(center, radius, Vector3.forward); + Circle3D(center, radius, Vector3.right); + } + + if (verticalSegments > 1) + { + for (int i = 1; i < verticalSegments; i++) + { + Vector3 c = center + Vector3.up * (-radius + (i * 2 * (radius / (verticalSegments)))); + + // Radius of base circle is a=sqrt(h(2R-h)), + float height = ((float)i / verticalSegments) * radius * 2; + float ra = Mathf.Sqrt(height * (2 * radius - height)); + + Circle3D(c, ra, Vector3.up); + } + } + else + Circle3D(center, radius, Vector3.up); + } + + public static void Cube(Vector3 center, Vector3 size) + { + Cube(center, size, Vector3.forward, Vector3.up); + } + + public static void Cube(Vector3 center, Vector3 size, Vector3 forward, Vector3 up) + { + Prepare3D(); + + forward = forward.normalized; + up = Vector3.ProjectOnPlane(up, forward).normalized; + Vector3 right = Vector3.Cross(forward, up); + + Vector3 frw = forward * size.z * 0.5f; + Vector3 rgt = right * size.x * 0.5f; + Vector3 upw = up * size.y * 0.5f; + + // vertical lines + GL.Vertex(center - frw - rgt - upw); + GL.Vertex(center - frw - rgt + upw); + + GL.Vertex(center - frw + rgt - upw); + GL.Vertex(center - frw + rgt + upw); + + GL.Vertex(center + frw - rgt - upw); + GL.Vertex(center + frw - rgt + upw); + + GL.Vertex(center + frw + rgt - upw); + GL.Vertex(center + frw + rgt + upw); + + // horizontal lines + GL.Vertex(center - frw - rgt - upw); + GL.Vertex(center - frw + rgt - upw); + + GL.Vertex(center - frw - rgt + upw); + GL.Vertex(center - frw + rgt + upw); + + GL.Vertex(center + frw - rgt - upw); + GL.Vertex(center + frw + rgt - upw); + + GL.Vertex(center + frw - rgt + upw); + GL.Vertex(center + frw + rgt + upw); + + // forward lines + GL.Vertex(center - frw - rgt - upw); + GL.Vertex(center + frw - rgt - upw); + + GL.Vertex(center - frw + rgt - upw); + GL.Vertex(center + frw + rgt - upw); + + GL.Vertex(center - frw - rgt + upw); + GL.Vertex(center + frw - rgt + upw); + + GL.Vertex(center - frw + rgt + upw); + GL.Vertex(center + frw + rgt + upw); + + Postpare(); + } + + #endregion + + + #region Wireframe + + public struct Edge + { + public int i1; + public int i2; + + public Edge(int i1, int i2) + { + this.i1 = i1; + this.i2 = i2; + } + + public bool Match(Edge e) + { + return (e.i1 == i1 && e.i2 == i2) || (e.i1 == i2 && e.i2 == i1); + } + } + + /// + /// Draws mesh wireframe. Use Draw.GetEdgePointsFromMesh() to get edgePoints, preferably only once + /// + public static void Wireframe(Transform t, Vector3[] edgePoints) + { + if (edgePoints == null) return; + if (edgePoints.Length < 2) return; + + Prepare3D(); + + for (int i = 0; i < edgePoints.Length; i++) + GL.Vertex(t.TransformPoint(edgePoints[i])); + + Postpare(); + } + + /// + /// Gets edge points from a mesh. + /// Call this once, and then use Wireframe() to draw the lines + /// + public static Vector3[] GetEdgePointsFromMesh(Mesh mesh) + { + Edge[] edges = GetEdges(mesh); + return EdgesToVertices(edges, mesh); + } + + /// + /// Gets edge points from a mesh. + /// In case you want both the shaded model and wireframe to show, you can use normalPush to offset edges from the mesh so it doesn't intersect with it. + /// Call this once, and then use Wireframe() to draw the lines + /// + public static Vector3[] GetEdgePointsFromMesh(Mesh mesh, float normalPush) + { + Edge[] edges = GetEdges(mesh); + return EdgesToVertices(edges, mesh, normalPush); + } + + + static Edge[] GetEdges(Mesh mesh) + { + int[] tris = mesh.triangles; + + List edges = new List(); + + for (int i = 0; i < tris.Length; i += 3) + { + + Edge e1 = new Edge(tris[i], tris[i + 1]); + Edge e2 = new Edge(tris[i + 1], tris[i + 2]); + Edge e3 = new Edge(tris[i + 2], tris[i]); + + // if line already exists, skip + + foreach (var edge in edges) + if (edge.Match(e1)) goto NoE1; + + edges.Add(e1); + + NoE1: + + foreach (var edge in edges) + if (edge.Match(e2)) goto NoE2; + + edges.Add(e2); + + NoE2: + + foreach (var edge in edges) + if (edge.Match(e3)) goto NoE3; + + edges.Add(e3); + + NoE3:; + + } + + return edges.ToArray(); + } + + static Vector3[] EdgesToVertices(Edge[] edges, Mesh mesh, float normalPush = 0) + { + Vector3[] vertices = mesh.vertices; + + Vector3[] edgesV3 = new Vector3[edges.Length * 2]; + + Vector3[] normals = null; + + if (normalPush != 0) + normals = mesh.normals; + + for (int i = 0; i < edges.Length; i++) + { + edgesV3[i * 2] = vertices[edges[i].i1]; + edgesV3[i * 2 + 1] = vertices[edges[i].i2]; + + if (normalPush != 0) + { + edgesV3[i * 2] += normals[edges[i].i1] * normalPush; + edgesV3[i * 2 + 1] += normals[edges[i].i2] * normalPush; + } + } + + return edgesV3; + } + + #endregion Wireframe + + + #region Utils + + /// + /// Converts a coordinate in pixels to screen 0-1 fraction point. + /// Example: 400, 300, on a 800x600 screen will output 0.5, 0.5 (middle of the screen) + /// + public static Vector2 PixelToScreen(Vector2 pos) + { + return new Vector2(pos.x / Screen.width, pos.y / Screen.height); + } + + /// + /// Converts a coordinate in pixels to screen 0-1 fraction point. + /// Example: 400, 300, on a 800x600 screen will output 0.5, 0.5 (middle of the screen) + /// + public static Vector2 PixelToScreen(float x, float y) + { + return new Vector2(x / Screen.width, y / Screen.height); + } + + static void Prepare() + { + GL.PushMatrix(); + GL.LoadOrtho(); + + GL.Begin(GL.LINES); + + GL.Color(color); + } + + static void PreparePixel() + { + GL.PushMatrix(); + GL.LoadPixelMatrix(); + + GL.Begin(GL.LINES); + + GL.Color(color); + } + + static void Prepare3D() + { + GL.PushMatrix(); + + GL.Begin(GL.LINES); + + GL.Color(color); + } + + static void Postpare() + { + GL.End(); + GL.PopMatrix(); + } + + #endregion +} \ No newline at end of file diff --git a/_Debug/REPL.cs b/_Debug/REPL.cs index 29209ef..8195a56 100644 --- a/_Debug/REPL.cs +++ b/_Debug/REPL.cs @@ -4,11 +4,15 @@ using UnityEngine; class REPL { + public static Transform footCenter; + public static void StartREPL() { + AddHeightBox(); AddCamera(); AddRigidbodyDebug(); AddCubes(); + AddFootCenter(); } public static void AddCamera() @@ -39,18 +43,18 @@ class REPL public static void AddRigidbodyDebug() { string[] targets = { - "Head", - "Neck", - "Torso", - "Arm_Right", - "Arm_Left", - "Hand_Right", - "Hand_Left", - "Hip", - "Leg_Left", - "Leg_Right", - "Foot_Left", - "Foot_Right", + //"Head", + //"Neck", + //"Torso", + //"Arm_Right", + //"Arm_Left", + //"Hand_Right", + //"Hand_Left", + //"Hip", + //"Leg_Left", + //"Leg_Right", + //"Foot_Left", + //"Foot_Right", "Knee_Left", "Knee_Right", }; @@ -121,4 +125,31 @@ class REPL } } } + + public static void AddHeightBox() + { + float height = 2; + GameObject cube = new GameObject(); + cube.transform.position = new Vector3(4.39f, 31.397f + height / 2.0f, -9.96f); + var drb = cube.AddComponent(); + drb.mode = DebugRigidBody.EMode.Cube; + drb.color = UnityEngine.Color.red; + drb.cubeLen = height; + } + + public static void AddFootCenter() + { + float height = 2; + GameObject cube = new GameObject(); + footCenter = cube.transform; + cube.transform.position = new Vector3(4.39f, 31.397f + height / 2.0f, -9.96f); + var drb = cube.AddComponent(); + drb.mode = DebugRigidBody.EMode.Custom; + drb.color = UnityEngine.Color.white; + drb.cubeLen = 0.1f; + drb.customDraw = () => { + //Draw.Sphere(drb.transform.position, 0.05f); + }; + } + } -- cgit v1.1-26-g67d0