Esempio n. 1
0
    void GenerateMeshes()
    {
        // var cube = MeshDraft.Cube(2f, true);
        var cube = CubeOpenTop(Vector3.right * 2f, Vector3.forward * 2f, Vector3.up * 2f, true);

        // var vs = cube.vertices;
        // cube.vertices = vs.Take(2).Concat(vs.Skip(4)).ToList();
        cube.FlipFaces();
        cubeMeshFilter.mesh = cube.ToMesh();

        var water = MeshDraft.Plane(2f, 2f, 200, 200, true);

        water.vertices = water.vertices
                         .Select(v => v - new Vector3(1f, 0, 1f))
                         // Put them into the same orientation as webgl.
                         .Select(v => new Vector3(v.x, v.z, 0f))
                         .ToList();
        waterMeshFilter.mesh = water.ToMesh();

        // var sphere = MeshDraft.Sphere(0.25f, 10, 10, true);
        var sphere = MeshDraft.Sphere(1f, 10, 10, true);

        sphereMeshFilter.mesh = sphere.ToMesh();
    }
Esempio n. 2
0
        public static MeshDraft TerrainDraft(int xSize, int zSize, int xSegments, int zSegments, int noiseScale)
        {
            var draft = MeshDraft.Plane(xSize, zSize, xSegments, zSegments);

            draft.Move(Vector3.left * xSize / 2 + Vector3.back * zSize / 2);

            var gradient = RandomE.gradientHSV;

            var noiseOffset = new Vector2(Random.Range(0f, 100f), Random.Range(0f, 100f));

            draft.colors.Clear();
            for (int i = 0; i < draft.vertices.Count; i++)
            {
                // Calculate noise value
                Vector3 vertex = draft.vertices[i];
                float   x      = noiseScale * vertex.x / xSize + noiseOffset.x;
                float   y      = noiseScale * vertex.z / zSize + noiseOffset.y;
                float   noise  = Mathf.PerlinNoise(x, y);
                draft.vertices[i] = new Vector3(vertex.x, noise, vertex.z);

                draft.colors.Add(gradient.Evaluate(noise));
            }
            return(draft);
        }
Esempio n. 3
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());
        }
Esempio n. 4
0
 public void Start()
 {
     template = MeshDraft.Plane();
 }
Esempio n. 5
0
 public static void Plane()
 {
     PrimitiveTemplate(plane, () => MeshDraft.Plane(10, 10, 10, 10).ToMesh());
 }
Esempio n. 6
0
 private void Start()
 {
     GetComponent <MeshFilter>().mesh = MeshDraft.Plane(xSize, zSize, xSegments, zSegments).ToMesh();
 }
Esempio n. 7
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);
        }