bool GenerateTiles(MapDataStore data, out CPUMesh tiles, out CPUMesh stencilTiles, out CPUMesh transparentTiles, out CPUMesh topTiles, out CPUMesh topStencilTiles, out CPUMesh topTransparentTiles, out CPUMesh collisionTiles, TempBuffers temp)
    {
        int block_x                = data.SliceOrigin.x / GameMap.blockSize;
        int block_y                = data.SliceOrigin.y / GameMap.blockSize;
        int block_z                = data.SliceOrigin.z;
        int bufferIndex            = 0;
        int stencilBufferIndex     = 0;
        int transparentBufferIndex = 0;
        int collisionIndex         = 0;

        for (int xx = (block_x * GameMap.blockSize); xx < (block_x + 1) * GameMap.blockSize; xx++)
        {
            for (int yy = (block_y * GameMap.blockSize); yy < (block_y + 1) * GameMap.blockSize; yy++)
            {
                if (!data.InSliceBounds(new DFCoord(xx, yy, block_z)))
                {
                    throw new UnityException("OOB");
                }
                if (data[xx, yy, block_z] == null)
                {
                    continue;
                }

                for (int i = 0; i < (int)MeshLayer.Count; i++)
                {
                    if (i < (int)MeshLayer.StaticCutout)
                    {
                        FillMeshBuffer(out temp.meshBuffer[bufferIndex], (MeshLayer)i, data[xx, yy, block_z], GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize), yy - (block_y * GameMap.blockSize), -GameMap.MapZOffset));
                        bufferIndex++;
                    }
                    else if (i < (int)MeshLayer.StaticTransparent)
                    {
                        FillMeshBuffer(out temp.stencilMeshBuffer[stencilBufferIndex], (MeshLayer)i, data[xx, yy, block_z], GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize), yy - (block_y * GameMap.blockSize), -GameMap.MapZOffset));
                        stencilBufferIndex++;
                    }
                    else if (i < (int)MeshLayer.Collision)
                    {
                        FillMeshBuffer(out temp.transparentMeshBuffer[transparentBufferIndex], (MeshLayer)i, data[xx, yy, block_z], GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize), yy - (block_y * GameMap.blockSize), -GameMap.MapZOffset));
                        transparentBufferIndex++;
                    }
                    else
                    {
                        FillMeshBuffer(out temp.collisionMeshBuffer[collisionIndex], (MeshLayer)i, data[xx, yy, block_z], GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize), yy - (block_y * GameMap.blockSize), -GameMap.MapZOffset));
                        collisionIndex++;
                    }
                }
            }
        }
        bool dontCare, success;

        stencilTiles        = MeshCombineUtility.ColorCombine(temp.stencilMeshBuffer, out dontCare, false);
        topStencilTiles     = MeshCombineUtility.ColorCombine(temp.stencilMeshBuffer, out dontCare, true);
        transparentTiles    = MeshCombineUtility.ColorCombine(temp.transparentMeshBuffer, out dontCare, false);
        topTransparentTiles = MeshCombineUtility.ColorCombine(temp.transparentMeshBuffer, out dontCare, true);
        topTiles            = MeshCombineUtility.ColorCombine(temp.meshBuffer, out dontCare, true);
        tiles          = MeshCombineUtility.ColorCombine(temp.meshBuffer, out success, false);
        collisionTiles = MeshCombineUtility.ColorCombine(temp.collisionMeshBuffer, out dontCare, false);

        return(success);
    }
示例#2
0
    private bool CopyMesh(MeshType type, CPUMesh mesh, string suffix)
    {
        bool madeNew = false;

        if (!meshFilters.ContainsKey(type))
        {
            var newGameObject = new GameObject();
            newGameObject.transform.parent = transform;
            newGameObject.transform.LocalReset();
            newGameObject.name          = type.ToString();
            meshFilters[type]           = newGameObject.AddComponent <MeshFilter>();
            meshRenderers[type]         = newGameObject.AddComponent <MeshRenderer>();
            meshFilters[type].mesh      = new Mesh();
            meshFilters[type].mesh.name = type.ToString() + "_" + suffix;
            madeNew = true;
        }
        meshFilters[type].mesh.Clear();
        mesh.CopyToMesh(meshFilters[type].mesh);
        return(madeNew);
    }
示例#3
0
    public bool AddTypeElement(System.Xml.Linq.XElement elemtype)
    {
        XAttribute fileAtt = elemtype.Attribute("file");

        if (fileAtt == null)
        {
            //Add error message here
            return(false);
        }


        if (storeContainer.shapeStore != null &&
            (elemtype.Attribute("normal") != null ||
             elemtype.Attribute("occlusion") != null ||
             elemtype.Attribute("alpha") != null
            ))
        {
            _normalTexture = new NormalContent();
            _normalTexture.ExternalStorage = storeContainer.shapeStore;
            if (!_normalTexture.AddTypeElement(elemtype))
            {
                _normalTexture = null;
            }
        }

        if (storeContainer.specialStore != null &&
            (elemtype.Attribute("metallic") != null ||
             elemtype.Attribute("illumination") != null
            ))
        {
            _specialTexture = new SpecialMapContent();
            _specialTexture.ExternalStorage = storeContainer.specialStore;
            if (!_specialTexture.AddTypeElement(elemtype))
            {
                _specialTexture = null;
            }
        }

        if (storeContainer.materialStore != null &&
            (elemtype.Attribute("pattern") != null ||
             elemtype.Attribute("specular") != null
            ))
        {
            _matTexture = new TextureContent();
            _matTexture.ExternalStorage = storeContainer.materialStore;
            if (!_matTexture.AddTypeElement(elemtype))
            {
                _matTexture = null;
            }
        }


        if (fileAtt.Value == "NONE")
        {
            //This means we don't want to actually store a mesh,
            //but still want to use the category.
            MeshData = new Dictionary <MeshLayer, CPUMesh>();
        }
        else
        {
            string filePath = Path.Combine(Path.GetDirectoryName(new Uri(elemtype.BaseUri).LocalPath), fileAtt.Value);
            filePath = Path.GetFullPath(filePath);

            //	Load the OBJ in
            var lStream  = new FileStream(filePath, FileMode.Open);
            var lOBJData = OBJLoader.LoadOBJ(lStream);
            lStream.Close();
            MeshData = new Dictionary <MeshLayer, CPUMesh>();
            Mesh tempMesh = new Mesh();
            foreach (MeshLayer layer in Enum.GetValues(typeof(MeshLayer)))
            {
                MeshLayer translatedLayer;
                if (layer == MeshLayer.GrowthCutout1 ||
                    layer == MeshLayer.GrowthCutout2 ||
                    layer == MeshLayer.GrowthCutout3)
                {
                    translatedLayer = MeshLayer.GrowthCutout;
                }
                else if (layer == MeshLayer.GrowthMaterial1 ||
                         layer == MeshLayer.GrowthMaterial2 ||
                         layer == MeshLayer.GrowthMaterial3)
                {
                    translatedLayer = MeshLayer.GrowthMaterial;
                }
                else if (layer == MeshLayer.GrowthTransparent1 ||
                         layer == MeshLayer.GrowthTransparent2 ||
                         layer == MeshLayer.GrowthTransparent3)
                {
                    translatedLayer = MeshLayer.GrowthTransparent;
                }
                else
                {
                    translatedLayer = layer;
                }
                tempMesh.LoadOBJ(lOBJData, (layer.ToString()));
                if (tempMesh == null || tempMesh.vertexCount == 0)
                {
                    continue;
                }
                tempMesh.name = filePath + "." + layer.ToString();
                if (translatedLayer == MeshLayer.GrowthCutout ||
                    translatedLayer == MeshLayer.GrowthMaterial ||
                    translatedLayer == MeshLayer.GrowthTransparent)
                {
                    for (int i = (int)translatedLayer; i < (int)translatedLayer + 4; i++)
                    {
                        //This is because the tree growths can be in any order
                        //So we just copy the un-numbered one onto the rest.
                        MeshData[(MeshLayer)i] = new CPUMesh(tempMesh);
                    }
                }
                else
                {
                    MeshData[layer] = new CPUMesh(tempMesh);
                }
                tempMesh.Clear();
            }
            lStream  = null;
            lOBJData = null;
        }

        XAttribute rotAtt = elemtype.Attribute("rotation");

        if (rotAtt == null)
        {
            rotationType = RotationType.None;
        }
        else
        {
            try
            {
                rotationType = (RotationType)Enum.Parse(typeof(RotationType), rotAtt.Value);
            }
            catch
            {
                rotationType = RotationType.None;
                Debug.Log("Unknown rotation value: " + rotAtt.Value);
            }
        }
        return(true);
    }
    public static CPUMesh ColorCombine(MeshInstance[] combines, out bool success, bool topLayer = false, CPUMesh sourceMesh = null)
    {
        int length = combines.Length;
        int vertexCount = 0;
        int triangleCount = 0;
        int maxVertices = 0;
        for (int combIndex = 0; combIndex < length; combIndex++)
        {
            if (combines[combIndex].meshData != null)
            {
                int count = combines[combIndex].meshData.vertexCount;
                vertexCount += count;
                if (maxVertices < count)
                    maxVertices = count;
            }
        }
        for (int combIndex = 0; combIndex < length; combIndex++)
        {
            if (combines[combIndex].meshData != null)
            {
                triangleCount += combines[combIndex].meshData.triangles.Length;
            }
        }

        List<Vector3> vertices = new List<Vector3>(vertexCount);
        List<Vector3> normals = new List<Vector3>(vertexCount);
        List<Vector4> tangents = new List<Vector4>(vertexCount);
        List<Vector2> uvs = new List<Vector2>(vertexCount);
        List<Vector2> uv2s = new List<Vector2>(vertexCount);
        List<Vector2> uv3s = new List<Vector2>(vertexCount);
        List<Color> colors = new List<Color>(vertexCount);
        List<int> triangles = new List<int>(triangleCount);

        if (
            sourceMesh != null
            && sourceMesh.vertices != null
            && sourceMesh.vertices.Length != 0
            && sourceMesh.triangles != null
            && sourceMesh.triangles.Length != 0)
        {
            vertices.AddRange(sourceMesh.vertices);
            if (sourceMesh.normals != null)
                normals.AddRange(sourceMesh.normals);
            else
                normals.AddRange(new Vector3[sourceMesh.vertices.Length]);

            if (sourceMesh.tangents != null)
                tangents.AddRange(sourceMesh.tangents);
            else
                tangents.AddRange(new Vector4[sourceMesh.vertices.Length]);

            if (sourceMesh.uv != null)
                uvs.AddRange(sourceMesh.uv);
            else
                uvs.AddRange(new Vector2[sourceMesh.vertices.Length]);

            if (sourceMesh.uv2 != null)
                uv2s.AddRange(sourceMesh.uv2);
            else
                uv2s.AddRange(new Vector2[sourceMesh.vertices.Length]);

            if (sourceMesh.uv3 != null)
                uv3s.AddRange(sourceMesh.uv3);
            else
                uv3s.AddRange(new Vector2[sourceMesh.vertices.Length]);

            if (sourceMesh.colors != null)
                colors.AddRange(sourceMesh.colors);
            else
                colors.AddRange(new Color[sourceMesh.vertices.Length]);

            triangles.AddRange(sourceMesh.triangles);
        }

        Dictionary<int, int> indexTranslation = new Dictionary<int, int>(maxVertices);

        for (int combIndex = 0; combIndex < length; combIndex++)
        {
            if (combines[combIndex].meshData != null)
            {
                int[] inputtriangles = combines[combIndex].meshData.triangles;
                var inputVertices = combines[combIndex].meshData.vertices;
                var inputNormals = combines[combIndex].meshData.normals;
                var inputUVs = combines[combIndex].meshData.uv;
                var inputColors = combines[combIndex].meshData.colors;
                var inputTangents = combines[combIndex].meshData.tangents;
                for (int i = 0; i < inputtriangles.Length; i += 3)
                {
                    int vert0 = inputtriangles[i + 0];
                    int vert1 = inputtriangles[i + 1];
                    int vert2 = inputtriangles[i + 2];
                    if (topLayer)
                    {
                        if (!(((combines[combIndex].hiddenFaces & HiddenFaces.Up) == HiddenFaces.Up)
                            && (inputVertices[vert0].y > topThreshold)
                            && (inputVertices[vert1].y > topThreshold)
                            && (inputVertices[vert2].y > topThreshold)))
                            continue;
                    }
                    else
                    {
                        if (((combines[combIndex].hiddenFaces & HiddenFaces.Up) == HiddenFaces.Up)
                            && (inputVertices[vert0].y > topThreshold)
                            && (inputVertices[vert1].y > topThreshold)
                            && (inputVertices[vert2].y > topThreshold))
                            continue;
                    }
                    if (((combines[combIndex].hiddenFaces & HiddenFaces.Down) == HiddenFaces.Down)
                        && (inputVertices[vert0].y < -topThreshold)
                        && (inputVertices[vert1].y < -topThreshold)
                        && (inputVertices[vert2].y < -topThreshold))
                        continue;
                    if (((combines[combIndex].hiddenFaces & HiddenFaces.North) == HiddenFaces.North)
                        && (inputVertices[vert0].z > sideThreshold)
                        && (inputVertices[vert1].z > sideThreshold)
                        && (inputVertices[vert2].z > sideThreshold))
                        continue;
                    if (((combines[combIndex].hiddenFaces & HiddenFaces.South) == HiddenFaces.South)
                        && (inputVertices[vert0].z < -sideThreshold)
                        && (inputVertices[vert1].z < -sideThreshold)
                        && (inputVertices[vert2].z < -sideThreshold))
                        continue;
                    if (((combines[combIndex].hiddenFaces & HiddenFaces.East) == HiddenFaces.East)
                        && (inputVertices[vert0].x > sideThreshold)
                        && (inputVertices[vert1].x > sideThreshold)
                        && (inputVertices[vert2].x > sideThreshold))
                        continue;
                    if (((combines[combIndex].hiddenFaces & HiddenFaces.West) == HiddenFaces.West)
                        && (inputVertices[vert0].x < -sideThreshold)
                        && (inputVertices[vert1].x < -sideThreshold)
                        && (inputVertices[vert2].x < -sideThreshold))
                        continue;

                    int newVert0 = getIndex(indexTranslation, combines[combIndex], vert0, vertices, normals, tangents, uvs, uv2s, uv3s, colors, inputVertices, inputNormals, inputTangents, inputUVs, inputColors, combines[combIndex].uv2Force, combines[combIndex].uv3Force);
                    int newVert1 = getIndex(indexTranslation, combines[combIndex], vert1, vertices, normals, tangents, uvs, uv2s, uv3s, colors, inputVertices, inputNormals, inputTangents, inputUVs, inputColors, combines[combIndex].uv2Force, combines[combIndex].uv3Force);
                    int newVert2 = getIndex(indexTranslation, combines[combIndex], vert2, vertices, normals, tangents, uvs, uv2s, uv3s, colors, inputVertices, inputNormals, inputTangents, inputUVs, inputColors, combines[combIndex].uv2Force, combines[combIndex].uv3Force);

                    if (newVert0 > 65531 || newVert1 > 65531 || newVert2 > 65531)
                        goto failure;

                    triangles.Add(newVert0);
                    triangles.Add(newVert1);
                    triangles.Add(newVert2);
                }
            }
            indexTranslation.Clear();
        }
        success = true;
        return new CPUMesh(
            vertices: vertices.ToArray(),
            normals: normals.ToArray(),
            tangents: tangents.ToArray(),
            uv: uvs.ToArray(),
            uv2: uv2s.ToArray(),
            uv3: uv3s.ToArray(),
            colors: colors.ToArray(),
            triangles: triangles.ToArray()
            );
        failure:
        success = false;
        return new CPUMesh(
            vertices: vertices.ToArray(),
            normals: normals.ToArray(),
            tangents: tangents.ToArray(),
            uv: uvs.ToArray(),
            uv2: uv2s.ToArray(),
            uv3: uv3s.ToArray(),
            colors: colors.ToArray(),
            triangles: triangles.ToArray()
            );
    }
示例#5
0
    bool GenerateTiles(
        MapDataStore data,
        out CPUMesh tiles,
        out CPUMesh stencilTiles,
        out CPUMesh transparentTiles,
        out CPUMesh topTiles,
        out CPUMesh topStencilTiles,
        out CPUMesh topTransparentTiles,
        out CPUMesh collisionTiles,
        out CPUMesh terrainTiles,
        out CPUMesh topTerrainTiles)
    {
        int block_x                = data.SliceOrigin.x / GameMap.blockSize;
        int block_y                = data.SliceOrigin.y / GameMap.blockSize;
        int block_z                = data.SliceOrigin.z;
        int bufferIndex            = 0;
        int stencilBufferIndex     = 0;
        int transparentBufferIndex = 0;
        int collisionIndex         = 0;
        int terrainIndex           = 0;

        GameMap.BeginSample("Fill Mesh Buffer");
        for (int i = 0; i < (int)MeshLayer.Count; i++)
        {
            for (int xx = (block_x * GameMap.blockSize); xx < (block_x + 1) * GameMap.blockSize; xx++)
            {
                for (int yy = (block_y * GameMap.blockSize); yy < (block_y + 1) * GameMap.blockSize; yy++)
                {
                    if (!data.InSliceBounds(new DFCoord(xx, yy, block_z)))
                    {
                        throw new UnityException("OOB");
                    }
                    if (data[xx, yy, block_z] == null)
                    {
                        continue;
                    }

                    if (i < (int)MeshLayer.StaticCutout)
                    {
                        FillMeshBuffer(
                            out meshBuffer[bufferIndex],
                            (MeshLayer)i,
                            data[xx, yy, block_z],
                            GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                                                   yy - (block_y * GameMap.blockSize),
                                                   -GameMap.MapZOffset));
                        bufferIndex++;
                    }
                    else if (i < (int)MeshLayer.StaticTransparent)
                    {
                        FillMeshBuffer(
                            out stencilMeshBuffer[stencilBufferIndex],
                            (MeshLayer)i,
                            data[xx, yy, block_z],
                            GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                                                   yy - (block_y * GameMap.blockSize),
                                                   -GameMap.MapZOffset));
                        stencilBufferIndex++;
                    }
                    else if (i < (int)MeshLayer.Collision)
                    {
                        FillMeshBuffer(
                            out transparentMeshBuffer[transparentBufferIndex],
                            (MeshLayer)i,
                            data[xx, yy, block_z],
                            GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                                                   yy - (block_y * GameMap.blockSize),
                                                   -GameMap.MapZOffset));
                        transparentBufferIndex++;
                    }
                    else if (i < (int)MeshLayer.NaturalTerrain)
                    {
                        FillMeshBuffer(
                            out collisionMeshBuffer[collisionIndex],
                            (MeshLayer)i,
                            data[xx, yy, block_z],
                            GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                                                   yy - (block_y * GameMap.blockSize),
                                                   -GameMap.MapZOffset));
                        collisionIndex++;
                    }
                    else if (i == (int)MeshLayer.NaturalTerrain)
                    {
                        FillMeshBuffer(out terrainMeshBuffer[terrainIndex],
                                       (MeshLayer)i,
                                       data[xx, yy, block_z],
                                       GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                                                              yy - (block_y * GameMap.blockSize),
                                                              -GameMap.MapZOffset));
                        terrainIndex++;
                    }
                }
            }
        }
        GameMap.EndSample();
        bool dontCare, success;

        GameMap.BeginSample("Generate Voxels");
        VoxelGenerator voxelGen = new VoxelGenerator();

        if (block_z == 0)
        {
            voxelGen.bottomless = true;
        }
        var naturalTerrain = voxelGen.Triangulate(data);

        GameMap.EndSample();
        GameMap.BeginSample("Combine Meshes");
        terrainTiles        = MeshCombineUtility.ColorCombine(terrainMeshBuffer, out dontCare, false, naturalTerrain);
        topTerrainTiles     = MeshCombineUtility.ColorCombine(terrainMeshBuffer, out dontCare, true);
        stencilTiles        = MeshCombineUtility.ColorCombine(stencilMeshBuffer, out dontCare, false);
        topStencilTiles     = MeshCombineUtility.ColorCombine(stencilMeshBuffer, out dontCare, true);
        transparentTiles    = MeshCombineUtility.ColorCombine(transparentMeshBuffer, out dontCare, false);
        topTransparentTiles = MeshCombineUtility.ColorCombine(transparentMeshBuffer, out dontCare, true);
        topTiles            = MeshCombineUtility.ColorCombine(meshBuffer, out dontCare, true);
        tiles          = MeshCombineUtility.ColorCombine(meshBuffer, out success, false);
        collisionTiles = MeshCombineUtility.ColorCombine(collisionMeshBuffer, out dontCare, false, naturalTerrain);
        GameMap.EndSample();

        return(success);
    }
示例#6
0
    public static CPUMesh ColorCombine(MeshInstance[] combines, out bool success, bool topLayer = false, CPUMesh sourceMesh = null)
    {
        int length        = combines.Length;
        int vertexCount   = 0;
        int triangleCount = 0;
        int maxVertices   = 0;

        for (int combIndex = 0; combIndex < length; combIndex++)
        {
            if (combines[combIndex].meshData != null)
            {
                int count = combines[combIndex].meshData.vertexCount;
                vertexCount += count;
                if (maxVertices < count)
                {
                    maxVertices = count;
                }
            }
        }
        for (int combIndex = 0; combIndex < length; combIndex++)
        {
            if (combines[combIndex].meshData != null)
            {
                triangleCount += combines[combIndex].meshData.triangles.Length;
            }
        }

        List <Vector3> vertices  = new List <Vector3>(vertexCount);
        List <Vector3> normals   = new List <Vector3>(vertexCount);
        List <Vector4> tangents  = new List <Vector4>(vertexCount);
        List <Vector2> uvs       = new List <Vector2>(vertexCount);
        List <Vector2> uv2s      = new List <Vector2>(vertexCount);
        List <Vector2> uv3s      = new List <Vector2>(vertexCount);
        List <Color>   colors    = new List <Color>(vertexCount);
        List <int>     triangles = new List <int>(triangleCount);

        if (
            sourceMesh != null &&
            sourceMesh.vertices != null &&
            sourceMesh.vertices.Length != 0 &&
            sourceMesh.triangles != null &&
            sourceMesh.triangles.Length != 0)
        {
            vertices.AddRange(sourceMesh.vertices);
            if (sourceMesh.normals != null)
            {
                normals.AddRange(sourceMesh.normals);
            }
            else
            {
                normals.AddRange(new Vector3[sourceMesh.vertices.Length]);
            }

            if (sourceMesh.tangents != null)
            {
                tangents.AddRange(sourceMesh.tangents);
            }
            else
            {
                tangents.AddRange(new Vector4[sourceMesh.vertices.Length]);
            }

            if (sourceMesh.uv != null)
            {
                uvs.AddRange(sourceMesh.uv);
            }
            else
            {
                uvs.AddRange(new Vector2[sourceMesh.vertices.Length]);
            }

            if (sourceMesh.uv2 != null)
            {
                uv2s.AddRange(sourceMesh.uv2);
            }
            else
            {
                uv2s.AddRange(new Vector2[sourceMesh.vertices.Length]);
            }

            if (sourceMesh.uv3 != null)
            {
                uv3s.AddRange(sourceMesh.uv3);
            }
            else
            {
                uv3s.AddRange(new Vector2[sourceMesh.vertices.Length]);
            }

            if (sourceMesh.colors != null)
            {
                colors.AddRange(sourceMesh.colors);
            }
            else
            {
                colors.AddRange(new Color[sourceMesh.vertices.Length]);
            }

            triangles.AddRange(sourceMesh.triangles);
        }

        Dictionary <int, int> indexTranslation = new Dictionary <int, int>(maxVertices);


        for (int combIndex = 0; combIndex < length; combIndex++)
        {
            if (combines[combIndex].meshData != null)
            {
                int[] inputtriangles = combines[combIndex].meshData.triangles;
                var   inputVertices  = combines[combIndex].meshData.vertices;
                var   inputNormals   = combines[combIndex].meshData.normals;
                var   inputUVs       = combines[combIndex].meshData.uv;
                var   inputColors    = combines[combIndex].meshData.colors;
                var   inputTangents  = combines[combIndex].meshData.tangents;
                for (int i = 0; i < inputtriangles.Length; i += 3)
                {
                    int vert0 = inputtriangles[i + 0];
                    int vert1 = inputtriangles[i + 1];
                    int vert2 = inputtriangles[i + 2];
                    if (topLayer)
                    {
                        if (!(((combines[combIndex].hiddenFaces & HiddenFaces.Up) == HiddenFaces.Up) &&
                              (inputVertices[vert0].y > topThreshold) &&
                              (inputVertices[vert1].y > topThreshold) &&
                              (inputVertices[vert2].y > topThreshold)))
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if (((combines[combIndex].hiddenFaces & HiddenFaces.Up) == HiddenFaces.Up) &&
                            (inputVertices[vert0].y > topThreshold) &&
                            (inputVertices[vert1].y > topThreshold) &&
                            (inputVertices[vert2].y > topThreshold))
                        {
                            continue;
                        }
                    }
                    if (((combines[combIndex].hiddenFaces & HiddenFaces.Down) == HiddenFaces.Down) &&
                        (inputVertices[vert0].y < -topThreshold) &&
                        (inputVertices[vert1].y < -topThreshold) &&
                        (inputVertices[vert2].y < -topThreshold))
                    {
                        continue;
                    }
                    if (((combines[combIndex].hiddenFaces & HiddenFaces.North) == HiddenFaces.North) &&
                        (inputVertices[vert0].z > sideThreshold) &&
                        (inputVertices[vert1].z > sideThreshold) &&
                        (inputVertices[vert2].z > sideThreshold))
                    {
                        continue;
                    }
                    if (((combines[combIndex].hiddenFaces & HiddenFaces.South) == HiddenFaces.South) &&
                        (inputVertices[vert0].z < -sideThreshold) &&
                        (inputVertices[vert1].z < -sideThreshold) &&
                        (inputVertices[vert2].z < -sideThreshold))
                    {
                        continue;
                    }
                    if (((combines[combIndex].hiddenFaces & HiddenFaces.East) == HiddenFaces.East) &&
                        (inputVertices[vert0].x > sideThreshold) &&
                        (inputVertices[vert1].x > sideThreshold) &&
                        (inputVertices[vert2].x > sideThreshold))
                    {
                        continue;
                    }
                    if (((combines[combIndex].hiddenFaces & HiddenFaces.West) == HiddenFaces.West) &&
                        (inputVertices[vert0].x < -sideThreshold) &&
                        (inputVertices[vert1].x < -sideThreshold) &&
                        (inputVertices[vert2].x < -sideThreshold))
                    {
                        continue;
                    }

                    int newVert0 = getIndex(indexTranslation, combines[combIndex], vert0, vertices, normals, tangents, uvs, uv2s, uv3s, colors, inputVertices, inputNormals, inputTangents, inputUVs, inputColors, combines[combIndex].uv2Force, combines[combIndex].uv3Force);
                    int newVert1 = getIndex(indexTranslation, combines[combIndex], vert1, vertices, normals, tangents, uvs, uv2s, uv3s, colors, inputVertices, inputNormals, inputTangents, inputUVs, inputColors, combines[combIndex].uv2Force, combines[combIndex].uv3Force);
                    int newVert2 = getIndex(indexTranslation, combines[combIndex], vert2, vertices, normals, tangents, uvs, uv2s, uv3s, colors, inputVertices, inputNormals, inputTangents, inputUVs, inputColors, combines[combIndex].uv2Force, combines[combIndex].uv3Force);

                    if (newVert0 > 65531 || newVert1 > 65531 || newVert2 > 65531)
                    {
                        goto failure;
                    }

                    triangles.Add(newVert0);
                    triangles.Add(newVert1);
                    triangles.Add(newVert2);
                }
            }
            indexTranslation.Clear();
        }
        success = true;
        return(new CPUMesh(
                   vertices: vertices.ToArray(),
                   normals: normals.ToArray(),
                   tangents: tangents.ToArray(),
                   uv: uvs.ToArray(),
                   uv2: uv2s.ToArray(),
                   uv3: uv3s.ToArray(),
                   colors: colors.ToArray(),
                   triangles: triangles.ToArray()
                   ));

failure:
        success = false;
        return(new CPUMesh(
                   vertices: vertices.ToArray(),
                   normals: normals.ToArray(),
                   tangents: tangents.ToArray(),
                   uv: uvs.ToArray(),
                   uv2: uv2s.ToArray(),
                   uv3: uv3s.ToArray(),
                   colors: colors.ToArray(),
                   triangles: triangles.ToArray()
                   ));
    }
示例#7
0
    bool GenerateTiles(MapDataStore data, out CPUMesh tiles, out CPUMesh stencilTiles, out CPUMesh transparentTiles, out CPUMesh topTiles, out CPUMesh topStencilTiles, out CPUMesh topTransparentTiles, TempBuffers temp)
    {
        int block_x = data.SliceOrigin.x / GameMap.blockSize;
        int block_y = data.SliceOrigin.y / GameMap.blockSize;
        int block_z = data.SliceOrigin.z;
        int bufferIndex = 0;
        int stencilBufferIndex = 0;
        int transparentBufferIndex = 0;
        for (int xx = (block_x * GameMap.blockSize); xx < (block_x + 1) * GameMap.blockSize; xx++)
            for (int yy = (block_y * GameMap.blockSize); yy < (block_y + 1) * GameMap.blockSize; yy++)
            {
                if (!data.InSliceBounds(new DFCoord(xx, yy, block_z))) throw new UnityException("OOB");
                if (data[xx, yy, block_z] == null) continue;

                for (int i = 0; i < (int)MeshLayer.Count; i++)
                {
                    if (i < (int)MeshLayer.StaticCutout)
                    {
                        FillMeshBuffer(out temp.meshBuffer[bufferIndex], (MeshLayer)i, data[xx, yy, block_z], GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize), yy - (block_y * GameMap.blockSize), -GameMap.MapZOffset));
                        bufferIndex++;
                    }
                    else if (i < (int)MeshLayer.StaticTransparent)
                    {
                        FillMeshBuffer(out temp.stencilMeshBuffer[stencilBufferIndex], (MeshLayer)i, data[xx, yy, block_z], GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize), yy - (block_y * GameMap.blockSize), -GameMap.MapZOffset));
                        stencilBufferIndex++;
                    }
                    else
                    {
                        FillMeshBuffer(out temp.transparentMeshBuffer[transparentBufferIndex], (MeshLayer)i, data[xx, yy, block_z], GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize), yy - (block_y * GameMap.blockSize), -GameMap.MapZOffset));
                        transparentBufferIndex++;
                    }
                }
            }
        bool dontCare, success;
        stencilTiles = MeshCombineUtility.ColorCombine(temp.stencilMeshBuffer, out dontCare, false);
        topStencilTiles = MeshCombineUtility.ColorCombine(temp.stencilMeshBuffer, out dontCare, true);
        transparentTiles = MeshCombineUtility.ColorCombine(temp.transparentMeshBuffer, out dontCare, false);
        topTransparentTiles = MeshCombineUtility.ColorCombine(temp.transparentMeshBuffer, out dontCare, true);
        topTiles = MeshCombineUtility.ColorCombine(temp.meshBuffer, out dontCare, true);
        tiles = MeshCombineUtility.ColorCombine(temp.meshBuffer, out success, false);

        return success;
    }
示例#8
0
        public static geometry MeshToGeometry(CPUMesh inputMesh, string id)
        {
            geometry outputGeometry = new geometry();
            mesh     outputMesh     = new mesh();

            outputGeometry.id   = id + "-lib";
            outputGeometry.name = inputMesh.name + "-mesh";
            outputGeometry.Item = outputMesh;


            //vertex Positions
            List <source> sourceList    = new List <source>();
            var           inputVertices = inputMesh.vertices;

            if (inputVertices.Length == 0)
            {
                return(null);
            }
            sourceList.Add(ArrayToSource(inputMesh.vertices, id + "-POSITION"));

            vertices vertexList = new vertices();

            vertexList.id                = id + "-VERTEX";
            vertexList.input             = new InputLocal[1];
            vertexList.input[0]          = new InputLocal();
            vertexList.input[0].semantic = "POSITION";
            vertexList.input[0].source   = "#" + sourceList[0].id;
            outputMesh.vertices          = vertexList;

            List <InputLocalOffset> offsetList = new List <InputLocalOffset>();

            {
                InputLocalOffset offset = new InputLocalOffset();
                offset.semantic = "VERTEX";
                offset.offset   = 0;
                offset.source   = "#" + vertexList.id;
                offsetList.Add(offset);
            }

            var inputNormals = inputMesh.normals;

            if (inputNormals.Length > 0)
            {
                var array = ArrayToSource(inputNormals, id + "-Normal0");
                InputLocalOffset offset = new InputLocalOffset();
                offset.semantic = "NORMAL";
                offset.offset   = (ulong)sourceList.Count;
                offset.source   = "#" + array.id;
                sourceList.Add(array);
                offsetList.Add(offset);
            }
            var inputUV1s = inputMesh.uv;

            if (inputUV1s.Length > 0)
            {
                var array = ArrayToSource(inputUV1s, id + "-UV0");
                InputLocalOffset offset = new InputLocalOffset();
                offset.semantic     = "TEXCOORD";
                offset.offset       = (ulong)sourceList.Count;
                offset.source       = "#" + array.id;
                offset.set          = 0;
                offset.setSpecified = true;
                sourceList.Add(array);
                offsetList.Add(offset);
            }
            var inputUV2s = inputMesh.uv2;

            if (inputUV2s.Length > 0)
            {
                var array = ArrayToSource(inputUV2s, id + "-UV1");
                InputLocalOffset offset = new InputLocalOffset();
                offset.semantic     = "TEXCOORD";
                offset.offset       = (ulong)sourceList.Count;
                offset.source       = "#" + array.id;
                offset.set          = 1;
                offset.setSpecified = true;
                sourceList.Add(array);
                offsetList.Add(offset);
            }
            var inputColors = inputMesh.colors;

            if (inputColors.Length > 0)
            {
                var array = ArrayToSource(inputColors, id + "-VERTEX_COLOR0");
                InputLocalOffset offset = new InputLocalOffset();
                offset.semantic     = "COLOR";
                offset.offset       = (ulong)sourceList.Count;
                offset.source       = "#" + array.id;
                offset.set          = 0;
                offset.setSpecified = true;
                sourceList.Add(array);
                offsetList.Add(offset);
            }

            outputMesh.source = sourceList.ToArray();


            triangles triangleList = new triangles();

            triangleList.input = offsetList.ToArray();

            var inputTriangles = inputMesh.triangles;

            triangleList.count = (ulong)inputTriangles.Length / 3;

            if (triangleList.count == 0)
            {
                return(null);
            }

            StringBuilder pString = new StringBuilder();

            for (int i = 0; i < inputTriangles.Length; i++)
            {
                for (int j = 0; j < triangleList.input.Length; j++)
                {
                    pString.Append(inputTriangles[i]).Append(" ");
                }
                if (i % 3 == 2)
                {
                    pString.AppendLine();
                }
                else
                {
                    pString.Append("   ");
                }
            }

            triangleList.p = pString.ToString();

            outputMesh.Items    = new object[1];
            outputMesh.Items[0] = triangleList;

            return(outputGeometry);
        }
示例#9
0
        public static geometry MeshToGeometry(CPUMesh inputMesh, string id)
        {
            geometry outputGeometry = new geometry();
            mesh outputMesh = new mesh();

            outputGeometry.id = id + "-lib";
            outputGeometry.name = inputMesh.name + "-mesh";
            outputGeometry.Item = outputMesh;

            //vertex Positions
            List<source> sourceList = new List<source>();
            var inputVertices = inputMesh.vertices;
            if (inputVertices.Length == 0)
                return null;
            sourceList.Add(ArrayToSource(inputMesh.vertices, id + "-POSITION"));

            vertices vertexList = new vertices();
            vertexList.id = id + "-VERTEX";
            vertexList.input = new InputLocal[1];
            vertexList.input[0] = new InputLocal();
            vertexList.input[0].semantic = "POSITION";
            vertexList.input[0].source = "#" + sourceList[0].id;
            outputMesh.vertices = vertexList;

            List<InputLocalOffset> offsetList = new List<InputLocalOffset>();

            {
                InputLocalOffset offset = new InputLocalOffset();
                offset.semantic = "VERTEX";
                offset.offset = 0;
                offset.source = "#" + vertexList.id;
                offsetList.Add(offset);
            }

            var inputNormals = inputMesh.normals;
            if(inputNormals.Length > 0)
            {
                var array = ArrayToSource(inputNormals, id + "-Normal0");
                InputLocalOffset offset = new InputLocalOffset();
                offset.semantic = "NORMAL";
                offset.offset = (ulong)sourceList.Count;
                offset.source = "#" + array.id;
                sourceList.Add(array);
                offsetList.Add(offset);
            }
            var inputUV1s = inputMesh.uv;
            if (inputUV1s.Length > 0)
            {
                var array = ArrayToSource(inputUV1s, id + "-UV0");
                InputLocalOffset offset = new InputLocalOffset();
                offset.semantic = "TEXCOORD";
                offset.offset = (ulong)sourceList.Count;
                offset.source = "#" + array.id;
                offset.set = 0;
                offset.setSpecified = true;
                sourceList.Add(array);
                offsetList.Add(offset);
            }
            var inputUV2s = inputMesh.uv2;
            if (inputUV2s.Length > 0)
            {
                var array = ArrayToSource(inputUV2s, id + "-UV1");
                InputLocalOffset offset = new InputLocalOffset();
                offset.semantic = "TEXCOORD";
                offset.offset = (ulong)sourceList.Count;
                offset.source = "#" + array.id;
                offset.set = 1;
                offset.setSpecified = true;
                sourceList.Add(array);
                offsetList.Add(offset);
            }
            var inputColors = inputMesh.colors;
            if (inputColors.Length > 0)
            {
                var array = ArrayToSource(inputColors, id + "-VERTEX_COLOR0");
                InputLocalOffset offset = new InputLocalOffset();
                offset.semantic = "COLOR";
                offset.offset = (ulong)sourceList.Count;
                offset.source = "#" + array.id;
                offset.set = 0;
                offset.setSpecified = true;
                sourceList.Add(array);
                offsetList.Add(offset);
            }

            outputMesh.source = sourceList.ToArray();

            triangles triangleList = new triangles();
            triangleList.input = offsetList.ToArray();

            var inputTriangles = inputMesh.triangles;

            triangleList.count = (ulong)inputTriangles.Length / 3;

            if (triangleList.count == 0)
                return null;

            StringBuilder pString = new StringBuilder();

            for(int i = 0; i < inputTriangles.Length; i++)
            {
                for(int j = 0; j < triangleList.input.Length; j++)
                {
                    pString.Append(inputTriangles[i]).Append(" ");
                }
                if (i % 3 == 2)
                    pString.AppendLine();
                else
                    pString.Append("   ");
            }

            triangleList.p = pString.ToString();

            outputMesh.Items = new object[1];
            outputMesh.Items[0] = triangleList;

            return outputGeometry;
        }
示例#10
0
    public bool AddTypeElement(System.Xml.Linq.XElement elemtype)
    {
        XAttribute fileAtt = elemtype.Attribute("file");
        if (fileAtt == null)
        {
            //Add error message here
            return false;
        }

        if ((elemtype.Attribute("normal") != null
            || elemtype.Attribute("occlusion") != null
            || elemtype.Attribute("alpha") != null
            || elemtype.Attribute("pattern") != null
            ))
        {
            _normalTexture = new NormalContent();
            _normalTexture.ExternalStorage = storeContainer.shapeStore;
            if (!_normalTexture.AddTypeElement(elemtype))
                _normalTexture = null;
        }

        if ((elemtype.Attribute("metallic") != null
            || elemtype.Attribute("illumination") != null
            ))
        {
            _specialTexture = new SpecialMapContent();
            _specialTexture.ExternalStorage = storeContainer.specialStore;
            if (!_specialTexture.AddTypeElement(elemtype))
                _specialTexture = null;
        }

        if ((elemtype.Attribute("pattern") != null
            || elemtype.Attribute("specular") != null
            ))
        {
            _matTexture = new TextureContent();
            _matTexture.ExternalStorage = storeContainer.materialStore;
            if (!_matTexture.AddTypeElement(elemtype))
                _matTexture = null;
        }

        if (fileAtt.Value == "NONE")
        {
            //This means we don't want to actually store a mesh,
            //but still want to use the category.
            MeshData = new Dictionary<MeshLayer, CPUMesh>();
        }
        else
        {
            string filePath = Path.Combine(Path.GetDirectoryName(new Uri(elemtype.BaseUri).LocalPath), fileAtt.Value);
            filePath = Path.GetFullPath(filePath);

            //	Load the OBJ in
            if (!File.Exists(filePath))
                return false;
            var lStream = new FileStream(filePath, FileMode.Open);
            var lOBJData = OBJLoader.LoadOBJ(lStream);
            lStream.Close();
            MeshData = new Dictionary<MeshLayer, CPUMesh>();
            Mesh tempMesh = new Mesh();
            foreach(MeshLayer layer in Enum.GetValues(typeof(MeshLayer)))
            {
                MeshLayer translatedLayer;
                if (layer == MeshLayer.GrowthCutout1
                    || layer == MeshLayer.GrowthCutout2
                    || layer == MeshLayer.GrowthCutout3)
                    translatedLayer = MeshLayer.GrowthCutout;
                else if (layer == MeshLayer.GrowthMaterial1
                    || layer == MeshLayer.GrowthMaterial2
                    || layer == MeshLayer.GrowthMaterial3)
                    translatedLayer = MeshLayer.GrowthMaterial;
                else if (layer == MeshLayer.GrowthTransparent1
                    || layer == MeshLayer.GrowthTransparent2
                    || layer == MeshLayer.GrowthTransparent3)
                    translatedLayer = MeshLayer.GrowthTransparent;
                else translatedLayer = layer;
                tempMesh.LoadOBJ(lOBJData, (layer.ToString()));
                if (tempMesh == null || tempMesh.vertexCount == 0)
                    continue;
                tempMesh.name = filePath + "." + layer.ToString();
                if(translatedLayer == MeshLayer.GrowthCutout
                    || translatedLayer == MeshLayer.GrowthMaterial
                    || translatedLayer == MeshLayer.GrowthTransparent)
                {
                    for(int i = (int)translatedLayer; i < (int)translatedLayer + 4; i++)
                    {
                        //This is because the tree growths can be in any order
                        //So we just copy the un-numbered one onto the rest.
                        MeshData[(MeshLayer)i] = new CPUMesh(tempMesh);
                    }
                }
                else MeshData[layer] = new CPUMesh(tempMesh);
                tempMesh.Clear();
            }
            lStream = null;
            lOBJData = null;
        }

        XAttribute rotAtt = elemtype.Attribute("rotation");
        if (rotAtt == null)
            rotationType = RotationType.None;
        else
        {
            try
            {
                rotationType = (RotationType)Enum.Parse(typeof(RotationType), rotAtt.Value);
            }
            catch
            {
                rotationType = RotationType.None;
                Debug.Log("Unknown rotation value: " + rotAtt.Value);
            }
        }
        UniqueIndex = num_created;
        num_created++;
        return true;
    }
示例#11
0
    bool GenerateTiles(MapDataStore data, out CPUMesh tiles, out CPUMesh stencilTiles, out CPUMesh transparentTiles, out CPUMesh topTiles, out CPUMesh topStencilTiles, out CPUMesh topTransparentTiles, out CPUMesh collisionTiles, out CPUMesh terrainTiles, out CPUMesh topTerrainTiles)
    {
        int block_x = data.SliceOrigin.x / GameMap.blockSize;
        int block_y = data.SliceOrigin.y / GameMap.blockSize;
        int block_z = data.SliceOrigin.z;
        int bufferIndex = 0;
        int stencilBufferIndex = 0;
        int transparentBufferIndex = 0;
        int collisionIndex = 0;
        int terrainIndex = 0;
        for (int xx = (block_x * GameMap.blockSize); xx < (block_x + 1) * GameMap.blockSize; xx++)
            for (int yy = (block_y * GameMap.blockSize); yy < (block_y + 1) * GameMap.blockSize; yy++)
            {
                if (!data.InSliceBounds(new DFCoord(xx, yy, block_z))) throw new UnityException("OOB");
                if (data[xx, yy, block_z] == null) continue;

                for (int i = 0; i < (int)MeshLayer.Count; i++)
                {
                    if (i < (int)MeshLayer.StaticCutout)
                    {
                        FillMeshBuffer(
                            out meshBuffer[bufferIndex],
                            (MeshLayer)i,
                            data[xx, yy, block_z],
                            GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                            yy - (block_y * GameMap.blockSize),
                            -GameMap.MapZOffset));
                        bufferIndex++;
                    }
                    else if (i < (int)MeshLayer.StaticTransparent)
                    {
                        FillMeshBuffer(
                            out stencilMeshBuffer[stencilBufferIndex],
                            (MeshLayer)i,
                            data[xx, yy, block_z],
                            GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                            yy - (block_y * GameMap.blockSize),
                            -GameMap.MapZOffset));
                        stencilBufferIndex++;
                    }
                    else if (i < (int)MeshLayer.Collision)
                    {
                        FillMeshBuffer(
                            out transparentMeshBuffer[transparentBufferIndex],
                            (MeshLayer)i,
                            data[xx, yy, block_z],
                            GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                            yy - (block_y * GameMap.blockSize),
                            -GameMap.MapZOffset));
                        transparentBufferIndex++;
                    }
                    else if (i < (int)MeshLayer.NaturalTerrain)
                    {
                        FillMeshBuffer(
                            out collisionMeshBuffer[collisionIndex],
                            (MeshLayer)i,
                            data[xx, yy, block_z],
                            GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                            yy - (block_y * GameMap.blockSize),
                            -GameMap.MapZOffset));
                        collisionIndex++;
                    }
                    else if(i == (int)MeshLayer.NaturalTerrain)
                    {
                        FillMeshBuffer(out terrainMeshBuffer[terrainIndex],
                            (MeshLayer)i,
                            data[xx, yy, block_z],
                            GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                            yy - (block_y * GameMap.blockSize),
                            -GameMap.MapZOffset));
                        terrainIndex++;
                    }
                }
            }
        bool dontCare, success;
        VoxelGenerator voxelGen = new VoxelGenerator();
        if (block_z == 0)
            voxelGen.bottomless = true;
        terrainTiles = voxelGen.Triangulate(data);
        terrainTiles = MeshCombineUtility.ColorCombine(terrainMeshBuffer, out dontCare, false, terrainTiles);
        topTerrainTiles = MeshCombineUtility.ColorCombine(terrainMeshBuffer, out dontCare, true);
        stencilTiles = MeshCombineUtility.ColorCombine(stencilMeshBuffer, out dontCare, false);
        topStencilTiles = MeshCombineUtility.ColorCombine(stencilMeshBuffer, out dontCare, true);
        transparentTiles = MeshCombineUtility.ColorCombine(transparentMeshBuffer, out dontCare, false);
        topTransparentTiles = MeshCombineUtility.ColorCombine(transparentMeshBuffer, out dontCare, true);
        topTiles = MeshCombineUtility.ColorCombine(meshBuffer, out dontCare, true);
        tiles = MeshCombineUtility.ColorCombine(meshBuffer, out success, false);
        collisionTiles = MeshCombineUtility.ColorCombine(collisionMeshBuffer, out dontCare, false);

        return success;
    }