Example #1
0
 public static MWVector3 FromGodotVector3(this MWVector3 _this, Vector3 other)
 {
     _this.X = other.x;
     _this.Y = other.y;
     _this.Z = other.z;
     return(_this);
 }
Example #2
0
 public static MWVector3 SetValue(this MWVector3 _this, Vector3 value)
 {
     _this.X = value.x;
     _this.Y = value.y;
     _this.Z = value.z;
     return(_this);
 }
        public static Vector3Patch GeneratePatch(MWVector3 _old, Vector3 _new)
        {
            if (_old == null && _new != null)
            {
                return(new Vector3Patch(_new));
            }
            else if (_new == null)
            {
                return(null);
            }

            var patch = new Vector3Patch()
            {
                X = _old.X != _new.x ? (float?)_new.x : null,
                Y = _old.Y != _new.y ? (float?)_new.y : null,
                Z = _old.Z != _new.z ? (float?)_new.z : null
            };

            if (patch.IsPatched())
            {
                return(patch);
            }
            else
            {
                return(null);
            }
        }
Example #4
0
 /// <inheritdoc />
 public void RigidBodyAddForceAtPosition(MWVector3 force, MWVector3 position)
 {
     _updateActions.Enqueue(
         (rigidBody) =>
     {
         rigidBody.AddForceAtPosition(_sceneRoot.TransformDirection(force.ToVector3()), _sceneRoot.TransformPoint(position.ToVector3()));
     });
 }
Example #5
0
 /// <inheritdoc />
 public void RigidBodyMovePosition(MWVector3 position)
 {
     _updateActions.Enqueue(
         (rigidBody) =>
     {
         rigidBody.MovePosition(_sceneRoot.TransformPoint(position.ToVector3()));
     });
 }
Example #6
0
 /// <inheritdoc />
 public void RigidBodyAddForce(MWVector3 force)
 {
     _updateActions.Enqueue(
         (rigidBody) =>
     {
         rigidBody.AddForce(_sceneRoot.TransformDirection(force.ToVector3()));
     });
 }
/*FIXME
 *              /// <inheritdoc />
 *              public void RigidBodyMovePosition(MWVector3 position)
 *              {
 *                      _updateActions.Enqueue(
 *                              (rigidBody) =>
 *                              {
 *                                      rigidBody.MovePosition(_sceneRoot.ToGlobal(position.ToVector3()));
 *                              });
 *              }
 *
 *              /// <inheritdoc />
 *              public void RigidBodyMoveRotation(MWQuaternion rotation)
 *              {
 *                      _updateActions.Enqueue(
 *                              (rigidBody) =>
 *                              {
 *                                      rigidBody.MoveRotation(_sceneRoot.rotation * rotation.ToQuaternion());
 *                              });
 *              }
 */
        /// <inheritdoc />
        public void RigidBodyAddForce(MWVector3 force)
        {
            _updateActions.Enqueue(
                (rigidBody) =>
            {
                rigidBody.AddCentralForce(_sceneRoot.ToGlobal(force.ToVector3()));
            });
        }
 /// <inheritdoc />
 public void RigidBodyAddTorque(MWVector3 torque)
 {
     _updateActions.Enqueue(
         (rigidBody) =>
     {
         rigidBody.AddTorque(_sceneRoot.ToGlobal(torque.ToVector3()));
     });
 }
 /// <inheritdoc />
 public void RigidBodyAddForceAtPosition(MWVector3 force, MWVector3 position)
 {
     _updateActions.Enqueue(
         (rigidBody) =>
     {
         rigidBody.AddForce(_sceneRoot.ToGlobal(position.ToVector3()), _sceneRoot.ToGlobal(force.ToVector3()));
     });
 }
Example #10
0
        public static Vector3 GetPatchApplied(this Vector3 _this, MWVector3 vector)
        {
            _this.x = _this.x.GetPatchApplied(vector.X);
            _this.y = _this.y.GetPatchApplied(vector.Y);
            _this.z = _this.z.GetPatchApplied(vector.Z);

            return(_this);
        }
Example #11
0
 /// <inheritdoc />
 public void RigidBodyAddRelativeTorque(MWVector3 relativeTorque)
 {
     _updateActions.Enqueue(
         (rigidBody) =>
     {
         rigidBody.AddRelativeTorque(_sceneRoot.TransformDirection(relativeTorque.ToVector3()));
     });
 }
Example #12
0
 public static Vector3 ToVector3(this MWVector3 _this)
 {
     return(new Vector3()
     {
         x = _this.X,
         y = _this.Y,
         z = _this.Z
     });
 }
Example #13
0
 public static int LargestComponentIndex(this MWVector3 _this)
 {
     if (_this.Z > _this.Y && _this.Z > _this.X)
     {
         return(2);
     }
     else if (_this.Y > _this.X)
     {
         return(1);
     }
     else
     {
         return(0);
     }
 }
Example #14
0
        public static int LargestComponentIndex(this MWVector3 _this)
        {
            var largest = _this.LargestComponentValue();

            if (largest == _this.X)
            {
                return(0);
            }
            else if (largest == _this.Y)
            {
                return(1);
            }
            else
            {
                return(2);
            }
        }
Example #15
0
        public static int SecondLargestComponentIndex(this MWVector3 _this)
        {
            var second = _this.SecondLargestComponentValue();

            if (second == _this.X)
            {
                return(0);
            }
            else if (second == _this.Y)
            {
                return(1);
            }
            else
            {
                return(2);
            }
        }
Example #16
0
        internal ColliderGeometry ConvertPrimToCollider(PrimitiveDefinition prim, Guid meshId)
        {
            MWVector3 dims = prim.Dimensions;

            switch (prim.Shape)
            {
            case PrimitiveShape.Sphere:
                return(new SphereColliderGeometry()
                {
                    Radius = dims.SmallestComponentValue() / 2
                });

            case PrimitiveShape.Box:
                return(new BoxColliderGeometry()
                {
                    Size = dims ?? new MWVector3(1, 1, 1)
                });

            case PrimitiveShape.Capsule:
                return(new CapsuleColliderGeometry()
                {
                    Size = dims
                });

            case PrimitiveShape.Cylinder:
                dims = dims ?? new MWVector3(0.2f, 1, 0.2f);
                return(new MeshColliderGeometry()
                {
                    MeshId = meshId
                });

            case PrimitiveShape.Plane:
                dims = dims ?? new MWVector3(1, 0, 1);
                return(new BoxColliderGeometry()
                {
                    Size = new MWVector3(Mathf.Max(dims.X, 0.01f), Mathf.Max(dims.Y, 0.01f), Mathf.Max(dims.Z, 0.01f))
                });

            default:
                return(null);
            }
        }
Example #17
0
        public static void AddColliderToPrimitive(this GameObject _this, PrimitiveDefinition prim)
        {
            MWVector3 dims = prim.Dimensions;

            switch (prim.Shape)
            {
            case PrimitiveShape.Sphere:
                var sphereCollider = _this.AddComponent <SphereCollider>();
                sphereCollider.radius = prim.Radius.GetValueOrDefault(0.5f);
                break;

            case PrimitiveShape.Box:
                dims = dims ?? new MWVector3(1, 1, 1);
                var boxCollider = _this.AddComponent <BoxCollider>();
                boxCollider.size = dims.ToVector3();
                break;

            case PrimitiveShape.Capsule:
                dims = dims ?? new MWVector3(0, 1, 0);
                var capsuleCollider = _this.AddComponent <CapsuleCollider>();
                capsuleCollider.radius    = prim.Radius.GetValueOrDefault(0.5f);
                capsuleCollider.height    = dims.LargestComponentValue() + 2 * capsuleCollider.radius;
                capsuleCollider.direction = dims.LargestComponentIndex();
                break;

            case PrimitiveShape.Cylinder:
                dims = dims ?? new MWVector3(0, 1, 0);
                var cylinderCollider = _this.AddComponent <CapsuleCollider>();
                cylinderCollider.radius    = prim.Radius.GetValueOrDefault(0.5f);
                cylinderCollider.height    = dims.LargestComponentValue() + cylinderCollider.radius;
                cylinderCollider.direction = dims.LargestComponentIndex();
                break;

            case PrimitiveShape.Plane:
                dims = dims ?? new MWVector3(1, 0, 1);
                var planeCollider = _this.AddComponent <BoxCollider>();
                planeCollider.size = new Vector3(dims.X, 0.01f, dims.Z);
                break;
            }
        }
        public static MWVector3 ApplyPatch(this MWVector3 _this, Vector3Patch vector)
        {
            if (vector == null)
            {
                return(_this);
            }

            if (vector.X != null)
            {
                _this.X = vector.X.Value;
            }

            if (vector.Y != null)
            {
                _this.Y = vector.Y.Value;
            }

            if (vector.Z != null)
            {
                _this.Z = vector.Z.Value;
            }

            return(_this);
        }
        internal ColliderPatch GenerateInitialPatch()
        {
            ColliderGeometry colliderGeo = null;

            // Note: SDK has no "mesh" collider type
            if (Shape == ColliderType.Auto || Shape == ColliderType.Mesh)
            {
                colliderGeo = new AutoColliderGeometry();
            }
            else if (_collider is SphereCollider sphereCollider)
            {
                colliderGeo = new SphereColliderGeometry()
                {
                    Radius = sphereCollider.radius,
                    Center = sphereCollider.center.CreateMWVector3()
                };
            }
            else if (_collider is BoxCollider boxCollider)
            {
                colliderGeo = new BoxColliderGeometry()
                {
                    Size   = boxCollider.size.CreateMWVector3(),
                    Center = boxCollider.center.CreateMWVector3()
                };
            }
            else if (_collider is CapsuleCollider capsuleCollider)
            {
                // The size vector describes the dimensions of the bounding box containing the collider
                MWVector3 size;
                if (capsuleCollider.direction == 0)
                {
                    size = new MWVector3(capsuleCollider.height, 2 * capsuleCollider.radius, 2 * capsuleCollider.radius);
                }
                else if (capsuleCollider.direction == 1)
                {
                    size = new MWVector3(2 * capsuleCollider.radius, capsuleCollider.height, 2 * capsuleCollider.radius);
                }
                else
                {
                    size = new MWVector3(2 * capsuleCollider.radius, 2 * capsuleCollider.radius, capsuleCollider.height);
                }

                colliderGeo = new CapsuleColliderGeometry()
                {
                    Center = capsuleCollider.center.CreateMWVector3(),
                    Size   = size
                };
            }
            else
            {
                _ownerActor.App.Logger.LogWarning($"MRE SDK does not support the following Unity collider and will not " +
                                                  $"be available in the MRE app.  Collider Type: {_collider.GetType()}");
            }

            return(colliderGeo == null ? null : new ColliderPatch()
            {
                IsEnabled = _collider.enabled,
                IsTrigger = _collider.isTrigger,
                Geometry = colliderGeo
            });
        }
Example #20
0
 public static float SecondLargestComponentValue(this MWVector3 _this)
 {
     return(Mathf.Clamp(_this.Z, Mathf.Min(_this.X, _this.Y), Mathf.Max(_this.X, _this.Y)));
 }
Example #21
0
 public static MWVector3 ToLocalMWVector3(this MWVector3 _this, Vector3 point, Spatial objectRoot)
 {
     _this.FromGodotVector3(objectRoot.ToLocal(point));
     _this.Z = -_this.Z;
     return(_this);
 }
Example #22
0
 public static float SmallestComponentValue(this MWVector3 _this)
 {
     return(Mathf.Min(_this.X, Mathf.Min(_this.Y, _this.Z)));
 }
Example #23
0
        /// <inheritdoc />
        public Mesh CreatePrimitive(PrimitiveDefinition definition)
        {
            MWVector3 dims = definition.Dimensions;

            MeshDraft meshDraft;
            float     radius, height;

            switch (definition.Shape)
            {
            case PrimitiveShape.Sphere:
                dims      = dims ?? new MWVector3(1, 1, 1);
                meshDraft = MeshDraft.Sphere(
                    definition.Dimensions.SmallestComponentValue() / 2,
                    definition.USegments.GetValueOrDefault(36),
                    definition.VSegments.GetValueOrDefault(18),
                    true);
                break;

            case PrimitiveShape.Box:
                dims      = dims ?? new MWVector3(1, 1, 1);
                meshDraft = MeshDraft.Hexahedron(dims.X, dims.Z, dims.Y, true);
                break;

            case PrimitiveShape.Capsule:
                dims      = dims ?? new MWVector3(0.2f, 1, 0.2f);
                radius    = definition.Dimensions.SmallestComponentValue() / 2;
                height    = definition.Dimensions.LargestComponentValue() - 2 * radius;
                meshDraft = MeshDraft.Capsule(
                    height,
                    radius,
                    definition.USegments.GetValueOrDefault(36),
                    definition.VSegments.GetValueOrDefault(18));

                // default capsule is Y-aligned; rotate if necessary
                if (dims.LargestComponentIndex() == 0)
                {
                    meshDraft.Rotate(Quaternion.Euler(0, 0, 90));
                }
                else if (dims.LargestComponentIndex() == 2)
                {
                    meshDraft.Rotate(Quaternion.Euler(90, 0, 0));
                }
                break;

            case PrimitiveShape.Cylinder:
                dims   = dims ?? new MWVector3(0.2f, 1, 0.2f);
                radius = 0.2f;
                height = 1;
                if (Mathf.Approximately(dims.X, dims.Y))
                {
                    height = dims.Z;
                    radius = dims.X / 2;
                }
                else if (Mathf.Approximately(dims.X, dims.Z))
                {
                    height = dims.Y;
                    radius = dims.X / 2;
                }
                else
                {
                    height = dims.X;
                    radius = dims.Y / 2;
                }

                meshDraft = MeshDraft.Cylinder(
                    radius,
                    definition.USegments.GetValueOrDefault(36),
                    height,
                    true);

                // default cylinder is Y-aligned; rotate if necessary
                if (dims.X == height)
                {
                    meshDraft.Rotate(Quaternion.Euler(0, 0, 90));
                }
                else if (dims.Z == height)
                {
                    meshDraft.Rotate(Quaternion.Euler(90, 0, 0));
                }
                break;

            case PrimitiveShape.Plane:
                dims      = dims ?? new MWVector3(1, 0, 1);
                meshDraft = MeshDraft.Plane(
                    dims.X,
                    dims.Z,
                    definition.USegments.GetValueOrDefault(1),
                    definition.VSegments.GetValueOrDefault(1),
                    true);
                meshDraft.Move(new Vector3(-dims.X / 2, 0, -dims.Z / 2));
                break;

            default:
                throw new Exception($"{definition.Shape.ToString()} is not a known primitive type.");
            }

            return(meshDraft.ToMesh());
        }
Example #24
0
 public static float LargestComponentValue(this MWVector3 _this)
 {
     return(Math.Max(_this.X, Math.Max(_this.Y, _this.Z)));
 }
 public static MWVector3 ToLocalMWVector3(this MWVector3 _this, Vector3 point, Transform objectRoot)
 {
     _this.FromUnityVector3(objectRoot.InverseTransformPoint(point));
     return(_this);
 }
 public Vector3Patch(MWVector3 vector)
 {
     X = vector.X;
     Y = vector.Y;
     Z = vector.Z;
 }
 internal ScaledTransformPatch(MWVector3 position, MWQuaternion rotation, MWVector3 scale)
     : base(position, rotation)
 {
     this.Scale = new Vector3Patch(scale);
 }
Example #28
0
 internal TransformPatch(MWVector3 position, MWQuaternion rotation, MWVector3 scale)
 {
     this.Position = new Vector3Patch(position);
     this.Rotation = new QuaternionPatch(rotation);
     this.Scale    = new Vector3Patch(scale);
 }
Example #29
0
        /// <inheritdoc />
        public GameObject CreatePrimitive(PrimitiveDefinition definition, GameObject parent, bool addCollider)
        {
            var spawnedPrimitive = new GameObject(definition.Shape.ToString());

            spawnedPrimitive.transform.SetParent(parent.transform, false);

            MWVector3 dims = definition.Dimensions;

            MeshDraft meshDraft;

            switch (definition.Shape)
            {
            case PrimitiveShape.Sphere:
                meshDraft = MeshDraft.Sphere(
                    definition.Radius.GetValueOrDefault(0.5f),
                    definition.USegments.GetValueOrDefault(36),
                    definition.VSegments.GetValueOrDefault(36),
                    true);
                break;

            case PrimitiveShape.Box:
                dims      = dims ?? new MWVector3(1, 1, 1);
                meshDraft = MeshDraft.Hexahedron(dims.X, dims.Z, dims.Y, true);
                break;

            case PrimitiveShape.Capsule:
                dims      = dims ?? new MWVector3(0, 1, 0);
                meshDraft = MeshDraft.Capsule(
                    dims.LargestComponentValue(),
                    definition.Radius.GetValueOrDefault(0.5f),
                    definition.USegments.GetValueOrDefault(36),
                    definition.VSegments.GetValueOrDefault(36));

                // default capsule is Y-aligned; rotate if necessary
                if (dims.LargestComponentIndex() == 0)
                {
                    meshDraft.Rotate(Quaternion.Euler(0, 0, 90));
                }
                else if (dims.LargestComponentIndex() == 2)
                {
                    meshDraft.Rotate(Quaternion.Euler(90, 0, 0));
                }
                break;

            case PrimitiveShape.Cylinder:
                dims      = dims ?? new MWVector3(0, 1, 0);
                meshDraft = MeshDraft.Cylinder(
                    definition.Radius.GetValueOrDefault(0.5f),
                    definition.USegments.GetValueOrDefault(36),
                    dims.LargestComponentValue(),
                    true);

                // default cylinder is Y-aligned; rotate if necessary
                if (dims.LargestComponentIndex() == 0)
                {
                    meshDraft.Rotate(Quaternion.Euler(0, 0, 90));
                }
                else if (dims.LargestComponentIndex() == 2)
                {
                    meshDraft.Rotate(Quaternion.Euler(90, 0, 0));
                }
                break;

            case PrimitiveShape.Plane:
                dims      = dims ?? new MWVector3(1, 0, 1);
                meshDraft = MeshDraft.Plane(
                    dims.X,
                    dims.Z,
                    definition.USegments.GetValueOrDefault(1),
                    definition.VSegments.GetValueOrDefault(1),
                    true);
                meshDraft.Move(new Vector3(-dims.X / 2, 0, -dims.Z / 2));
                break;

            case PrimitiveShape.InnerSphere:
                meshDraft = MeshDraft.Sphere(
                    definition.Radius.GetValueOrDefault(0.5f),
                    definition.USegments.GetValueOrDefault(36),
                    definition.VSegments.GetValueOrDefault(36),
                    true);
                meshDraft.FlipFaces();
                break;

            default:
                throw new Exception($"{definition.Shape.ToString()} is not a known primitive type.");
            }

            spawnedPrimitive.AddComponent <MeshFilter>().mesh = meshDraft.ToMesh();
            var renderer = spawnedPrimitive.AddComponent <MeshRenderer>();

            renderer.sharedMaterial = MREAPI.AppsAPI.DefaultMaterial;

            if (addCollider)
            {
                spawnedPrimitive.AddColliderToPrimitive(definition);
            }

            return(spawnedPrimitive);
        }