public void BuildMesh(PartType partType, AdjacencyMatrix adjMatrix, MeshBuilder meshBuilder, BlockBounds clipBounds)
 {
     var slopeBuilder = new SlopedMeshBuilder(meshBuilder);
     if (partType == PartType.OuterCorner) {
         BuildMeshCorner(slopeBuilder);
     }
 }
Exemple #2
0
        private Vertex[] BuildNextSection(MeshBuilder meshBuilder, Vector3 curvePt, Vector3 tangent, Quaternion quat, Vertex[] lastVerts)
        {
            Vector3 pt1 = quat * new Vector3(TRACK_RADIUS * X_SCALE_TOP, TRACK_RADIUS, 0) + curvePt;
            Vector3 pt2 = quat * new Vector3(TRACK_RADIUS * X_SCALE_BOTTOM, -TRACK_RADIUS, 0) + curvePt;
            Vector3 pt3 = quat * new Vector3(-TRACK_RADIUS * X_SCALE_BOTTOM, -TRACK_RADIUS, 0) + curvePt;
            Vector3 pt4 = quat * new Vector3(-TRACK_RADIUS * X_SCALE_TOP, TRACK_RADIUS, 0) + curvePt;

            Vertex[] verts = new Vertex[]{
                meshBuilder.VertAutoNormal(pt1, new Vector2(), false),
                meshBuilder.VertAutoNormal(pt1, new Vector2(), false),
                meshBuilder.VertAutoNormal(pt2, new Vector2(), false),
                meshBuilder.VertAutoNormal(pt2, new Vector2(), false),
                meshBuilder.VertAutoNormal(pt3, new Vector2(), false),
                meshBuilder.VertAutoNormal(pt3, new Vector2(), false),
                meshBuilder.VertAutoNormal(pt4, new Vector2(), false),
                meshBuilder.VertAutoNormal(pt4, new Vector2(), false),
            };

            if (lastVerts != null)
            {
                meshBuilder.AddTriangleStrip(false, new Vertex[]{
                    verts[2], lastVerts[2], verts[1], lastVerts[1], // right side
                    verts[0], lastVerts[0], verts[7], lastVerts[7], // top side
                    verts[6], lastVerts[6], verts[5], lastVerts[5], // left side
                    verts[4], lastVerts[4], verts[3], lastVerts[3], // bottom side
                });
            }
            else
            {
                AddCap(meshBuilder, new Vector3[]{pt1, pt2, pt3, pt4}, false);
            }

            return verts;
        }
Exemple #3
0
        public Mesh ExtrudeAlong(BezierSpline spline, Vector3 down)
        {
            MeshBuilder meshBuilder = new MeshBuilder();
            meshBuilder.BeforeNext(Matrix4x4.identity, new Vector3());

            Vector3 lastTangent = spline.GetFirstDerivative(0, spline.pts[0]);
            Quaternion quat = Quaternion.LookRotation(lastTangent, -down);
            Vector3 up = down;
            Vertex[] lastVerts = null;

            for(int i = 0; i < spline.pts.Length; i ++)
            {
                BezierSpline.BezierPoint pt = spline.pts[i];
                if (Vector3.Distance(pt.startPos, pt.endPos) > 0.001f)
                {

                    int subdivisions = pt.numSubdivisions;

                    for (int div = 0; div <= subdivisions; div++)
                    {
                        float t = div / (float)pt.numSubdivisions;
                        Vector3 ptT = spline.GetPoint(t, pt);
                        Vector3 tangent = spline.GetFirstDerivative(t, pt).normalized;
                        quat = Quaternion.FromToRotation(lastTangent, tangent) * quat;

                        lastVerts = BuildNextSection(meshBuilder, ptT, tangent, quat, lastVerts);
                        lastTangent = tangent;
                    }
                }
            }

            AddCap(meshBuilder, new Vector3[] { lastVerts[0].position, lastVerts[2].position, lastVerts[4].position, lastVerts[6].position }, true);
            return meshBuilder.DoneCreateMesh();
        }
Exemple #4
0
        private void AttachGameObject(MeshBlock meshBlock, GroupBranch groupBranch, AdjacencyCalculator adjacencyCalculator)
        {
            SplitBoundsBranch splitBoundsBranch = groupBranch.SplitBoundsBranchContaining(meshBlock.SplittedRegion);

            var meshBuilder = new MeshBuilder();
            adjacencyCalculator.SetupNext(groupBranch.SplittedMeshBlocks, groupBranch.Splits, meshBlock);

            for (var x = (int)meshBlock.OriginalBounds.MinX; x < meshBlock.OriginalBounds.MaxX; x++)
            {
                for (var y = (int)meshBlock.OriginalBounds.MinY; y < meshBlock.OriginalBounds.MaxY; y++)
                {
                    for (var z = (int)meshBlock.OriginalBounds.MinZ; z < meshBlock.OriginalBounds.MaxZ; z++)
                    {
                        AdjacencyMatrix adjacencyMatrix = adjacencyCalculator.CalculateAdjacency(x, y, z);
                        if (!adjacencyMatrix.IsVisible()) continue;

                        var clipBounds = new BlockBounds(x, y, z, x + 1, y + 1, z + 1);
                        clipBounds.ClipToBounds(groupBranch.Splits);
                        meshBlock.MeshSource.Build(new Vector3(x, y, z) - meshBlock.OriginalBounds.Position, adjacencyMatrix, meshBlock, meshBuilder, clipBounds);
                    }
                }
            }

            GameObject blockGameObject = Object.Instantiate(groupBranch.Level.Prefabs.BlockPrefab);
            blockGameObject.name = meshBlock.Id;
            blockGameObject.transform.localPosition = meshBlock.OriginalBounds.Position;
            splitBoundsBranch.BlocksLeaf.Attach(blockGameObject);

            Mesh mesh = meshBuilder.DoneCreateMesh();
            blockGameObject.GetComponent<MeshFilter>().mesh = mesh;
        }
Exemple #5
0
 private static void AddCap(MeshBuilder meshBuilder, Vector3[] verts, bool reverse)
 {
     meshBuilder.AddTriangleStrip(reverse, new Vertex[]{
         meshBuilder.VertAutoNormal(verts[0], new Vector2(), false),
         meshBuilder.VertAutoNormal(verts[1], new Vector2(), false),
         meshBuilder.VertAutoNormal(verts[3], new Vector2(), false),
         meshBuilder.VertAutoNormal(verts[2], new Vector2(), false),
     });
 }
 public void BuildMesh(PartType partType, AdjacencyMatrix adjMatrix,  MeshBuilder meshBuilder, BlockBounds clipBounds)
 {
     if (PartType.BlockFront == partType) {
         meshBuilder.AddTriangleStrip(
             false,
             meshBuilder.VertAutoNormal(new Vector3(1, 1, 0), new Vector2(1, 1), true),
             meshBuilder.VertAutoNormal(new Vector3(1, 0, 0), new Vector2(1, 0), true),
             meshBuilder.VertAutoNormal(new Vector3(0, 1, 0), new Vector2(0, 1), true),
             meshBuilder.VertAutoNormal(new Vector3(0, 0, 0), new Vector2(0, 0), true));;
     }
     else if (PartType.BlockBack == partType)
     {
         meshBuilder.AddTriangleStrip(
             false,
             meshBuilder.VertAutoNormal(new Vector3(0, 1, 1), new Vector2(1, 1), true),
             meshBuilder.VertAutoNormal(new Vector3(0, 0, 1), new Vector2(1, 0), true),
             meshBuilder.VertAutoNormal(new Vector3(1, 1, 1), new Vector2(0, 1), true),
             meshBuilder.VertAutoNormal(new Vector3(1, 0, 1), new Vector2(0, 0), true));
     }
     else if (PartType.BlockRight == partType)
     {
         meshBuilder.AddTriangleStrip(
             false,
             meshBuilder.VertAutoNormal(new Vector3(1, 1, 1), new Vector2(1, 1), true),
             meshBuilder.VertAutoNormal(new Vector3(1, 0, 1), new Vector2(1, 0), true),
             meshBuilder.VertAutoNormal(new Vector3(1, 1, 0), new Vector2(0, 1), true),
             meshBuilder.VertAutoNormal(new Vector3(1, 0, 0), new Vector2(0, 0), true));
     }
     else if (PartType.BlockLeft == partType)
     {
         meshBuilder.AddTriangleStrip(
             false,
             meshBuilder.VertAutoNormal(new Vector3(0, 1, 0), new Vector2(1, 1), true),
             meshBuilder.VertAutoNormal(new Vector3(0, 0, 0), new Vector2(1, 0), true),
             meshBuilder.VertAutoNormal(new Vector3(0, 1, 1), new Vector2(0, 1), true),
             meshBuilder.VertAutoNormal(new Vector3(0, 0, 1), new Vector2(0, 0), true));
     }
     else if (PartType.BlockTop == partType)
     {
         meshBuilder.AddTriangleStrip(
             false,
             meshBuilder.VertAutoNormal(new Vector3(1, 1, 1), new Vector2(1, 1), true),
             meshBuilder.VertAutoNormal(new Vector3(1, 1, 0), new Vector2(1, 0), true),
             meshBuilder.VertAutoNormal(new Vector3(0, 1, 1), new Vector2(0, 1), true),
             meshBuilder.VertAutoNormal(new Vector3(0, 1, 0), new Vector2(0, 0), true));
     }
     else if (PartType.BlockBottom == partType)
     {
         meshBuilder.AddTriangleStrip(
             false,
             meshBuilder.VertAutoNormal(new Vector3(1, 0, 0), new Vector2(1, 1), true),
             meshBuilder.VertAutoNormal(new Vector3(1, 0, 1), new Vector2(1, 0), true),
             meshBuilder.VertAutoNormal(new Vector3(0, 0, 0), new Vector2(0, 1), true),
             meshBuilder.VertAutoNormal(new Vector3(0, 0, 1), new Vector2(0, 0), true));
     }
 }
        public void BuildMesh(PartType partType, AdjacencyMatrix adjMatrix, MeshBuilder meshBuilder, BlockBounds clipBounds)
        {
            SlopedMeshBuilder slopeBuilder = new SlopedMeshBuilder(meshBuilder);
	
			if (PartType.OuterEdgeFront == partType) {

                slopeBuilder.ConstructSlopedFront(slopeBuilder.GetOuterSlopePoints());
			}else if (PartType.OuterEdgeLeft == partType) 
            {
                Matrix4x4 mat = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(-1, 1, 1));
                slopeBuilder.ConstructSlopedSide(false, mat, slopeBuilder.GetOuterSlopePoints());
			}else if (PartType.OuterEdgeRight == partType) 
            {
                Matrix4x4 mat = Matrix4x4.TRS(new Vector3(1, 0, 0), Quaternion.identity, new Vector3(1, 1, 1));
                slopeBuilder.ConstructSlopedSide(true, mat, slopeBuilder.GetOuterSlopePoints());
			}
        }
Exemple #8
0
 public SlopedMeshBuilder(MeshBuilder meshBuilder)
 {
     this.builder = meshBuilder;
 }
Exemple #9
0
        public void Build(Vector3 pos, AdjacencyMatrix adjacencyMatrix, MeshBlock block, MeshBuilder meshBuilder, BlockBounds clipBounds)
        {

        }
        public void Build(Vector3 pos, AdjacencyMatrix adjacencyMatrix, MeshBlock block, MeshBuilder meshBuilder, BlockBounds clipBounds)
        {
            Quaternion rot = block.RotationQuat;
            Matrix4x4 transform = Matrix4x4.TRS(pos + new Vector3(0.5f, 0.5f, 0.5f), rot, new Vector3(1, 1, 1));
            BlockBounds localClipBounds = new BlockBounds (clipBounds.Position - block.OriginalBounds.Position - pos, clipBounds.Size);
            localClipBounds.SetToRotationFrom (Quaternion.Inverse(block.RotationQuat), new Vector3(0.5f, 0.5f, 0.5f));
            meshBuilder.BeforeNext (transform, new Vector3(-0.5f, -0.5f, -0.5f), localClipBounds);

            foreach (Part part in _blockParts)
            {
                if ((part.Direction == null) || (!adjacencyMatrix.RPt(part.Direction[0], part.Direction[1], part.Direction[2]).Occluded))
                {
                    IProcMeshGenerator meshGenerator;
                    if(!_meshGenerators.TryGetValue(part.MeshGenerator, out meshGenerator)){
                        _meshGenerators[part.MeshGenerator] = (meshGenerator = (IProcMeshGenerator)Activator.CreateInstance(part.MeshGenerator));
                    }

                    meshGenerator.BuildMesh(part.PartType, adjacencyMatrix, meshBuilder, localClipBounds);
                }
            }
        }