internal IList <Actor> CreatePrimitive(PrimitiveDefinition definition, Guid?parentId, bool addCollider)
        {
            var        factory = MREAPI.AppsAPI.PrimitiveFactory;
            GameObject newGO   = factory.CreatePrimitive(
                definition, GetGameObjectFromParentId(parentId), addCollider);

            List <Actor> actors = new List <Actor>()
            {
                newGO.AddComponent <Actor>()
            };

            newGO.layer = UnityConstants.ActorLayerIndex;
            return(actors);
        }
Beispiel #2
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);
            }
        }
Beispiel #3
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;
            }
        }
Beispiel #4
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());
        }
Beispiel #5
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);
        }
Beispiel #6
0
 /// <summary>
 /// Generates an array type
 /// </summary>
 /// <param name="emitter">The emitter to write to</param>
 /// <param name="typeName">The name of the type</param>
 /// <param name="def">The primitive definition</param>
 /// <param name="root">True if this is the root type, false otherwise</param>
 private void _generatePrimitiveType(CSharpEmitter emitter, string typeName, string fieldName, PrimitiveDefinition def, bool root)
 {
     if (root)
     {
         typeName = _getDefinitionName(typeName, fieldName, def);
         var elementTypeName = _getDefinitionName(null, null, def);
         using (var wrapper = _generateWrapperType(emitter, typeName, elementTypeName))
         {
         }
     }
 }