Exemplo 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();
    }
        static MeshObjectData CreateFoliage(TreeData data, TreeBranch Branch, int index)
        {
            MeshDraft m;

            switch (data.FoliageBasePrimitiveShape)
            {
            case TreeData.BasePrimitiveShapes.Dodecahedron:
                m = MeshDraft.Dodecahedron(0.5f);
                break;

            case TreeData.BasePrimitiveShapes.Icosahedron:
                m = MeshDraft.Icosahedron(0.5f, false);
                break;

            case TreeData.BasePrimitiveShapes.Prism:
                m = MeshDraft.Prism(0.5f, data.foliageSegments, 0.1f, false);
                break;

            case TreeData.BasePrimitiveShapes.Pyramid:
                m = MeshDraft.Pyramid(0.5f, data.foliageSegments, 0.5f, false);
                break;

            default:
                m = MeshDraft.Sphere(0.5f, data.foliageSegments, data.foliageSegments, false);
                break;
            }

            MeshObjectData plant = new MeshObjectData();

            plant.vertices  = m.vertices.ToArray();
            plant.triangles = m.triangles.ToArray();
            plant.tangents  = m.tangents.ToArray();
            plant.AutoWeldMesh(0.0001f, 0.4f);

            Vector3[] verts        = plant.vertices;
            float     currentNoise = data.noise;

            currentNoise *= 0.4f;
            Vector3 Pos = Branch.To;

            plant.position = Pos;

            int  s = data.TreeSeed + index + Mathf.RoundToInt(Pos.x) + Mathf.RoundToInt(Pos.y) + Mathf.RoundToInt(Pos.z) + Mathf.RoundToInt(Branch.Length);
            Rand r = new Rand(s);

            for (int i = 0; i < verts.Length; i++)
            {
                verts[i].x += r.Range(-currentNoise, currentNoise);
                verts[i].y += r.Range(-currentNoise, currentNoise);
                verts[i].z += r.Range(-currentNoise, currentNoise);
            }

            plant.vertices = verts;

            plant.flatShade();

            Color[] vertexColor = new Color[plant.vertices.Length];
            Color   CC          = data.foliageColors[r.Range(0, data.foliageColors.Length)];

            for (int c = 0; c < plant.vertices.Length; c++)
            {
                vertexColor[c] = CC;
            }
            plant.colors = vertexColor;
            return(plant);
        }
Exemplo 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());
        }
Exemplo n.º 4
0
 private void Start()
 {
     GetComponent <MeshFilter>().mesh = MeshDraft.Sphere(radius, horizontalSegments, verticalSegments).ToMesh();
 }
Exemplo n.º 5
0
 public static void Sphere()
 {
     PrimitiveTemplate(sphere, () => MeshDraft.Sphere(1, 16, 16).ToMesh());
 }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
        public static void Build(RockThreadReturnData returnData)
        {
            RockData  data = returnData.RockData;
            MeshDraft MD;

            switch (data.RockBasePrimitiveShape)
            {
            case RockData.BasePrimitiveShapes.Dodecahedron:
                MD = MeshDraft.Dodecahedron(0.5f);
                break;

            case RockData.BasePrimitiveShapes.Icosahedron:
                MD = MeshDraft.Icosahedron(0.5f, false);
                break;

            case RockData.BasePrimitiveShapes.Prism:
                MD = MeshDraft.Prism(0.5f, data.Segments, 1f, false);
                break;

            case RockData.BasePrimitiveShapes.Pyramid:
                MD = MeshDraft.Pyramid(0.5f, data.Segments, 1f, false);
                break;

            default:
                MD = MeshDraft.Sphere(0.5f, data.Segments, data.Segments, false);
                break;
            }
            ;
            MeshObjectData rock = new MeshObjectData();

            rock.vertices  = MD.vertices.ToArray();
            rock.triangles = MD.triangles.ToArray();
            rock.tangents  = MD.tangents.ToArray();
            rock.AutoWeldMesh(0.0001f, 0.4f);
            Vector3[] verts = rock.vertices;
            INoise    noise = new SimplexNoise(data.RockSeed, 0.3f, 0.2f);
            Rand      r     = new Rand(data.RockSeed);

            for (int i = 0; i < verts.Length; i++)
            {
                float currentNoise = NoiseGen(noise, 3, verts[i].x / 0.5f, verts[i].y / 0.5f, verts[i].z / 0.5f);
                //currentNoise*=2;
                Vector3 norm = verts[i].normalized;
                verts[i].x += currentNoise * norm.x;
                verts[i].y += currentNoise * norm.y;
                verts[i].z += currentNoise * norm.z;

                verts[i].x *= 3;
                verts[i].y *= 1.2f;
                verts[i].z *= 1.5f;
            }
            rock.vertices = verts;
            rock.flatShade();

            Color[] vertexColor = new Color[rock.vertices.Length];

            for (int i = 0; i < rock.vertices.Length; i++)
            {
                vertexColor[i] = data.RockGradientColor.Color.Evaluate(1 - rock.vertices[i].y);
            }

            rock.colors = vertexColor;
            returnData.RockBuildData = rock;
            returnData.ManagerCallBack(returnData);
        }