Esempio n. 1
0
 public MeshInfo(ref MeshFilter meshFilter, GetMeshData getMeshDataFunction)
 {
     uvs         = new List <Vector2>();
     vertices    = new List <Vector3>();
     triangles   = new List <int>();
     filter      = meshFilter;
     getMeshData = getMeshDataFunction;
 }
Esempio n. 2
0
        public static unsafe bool GenerateTangentSpace(MikktspaceContext context, float angularThreshold = 180)
        {
            try
            {
                // explicitly store the delegates on the stack so they're not collected by the GC during the execution of this function
                GetNumFaces          _getNumFaces          = context.GetNumFaces;
                GetNumVerticesOfFace _getNumVerticesOfFace = context.GetNumVerticesOfFace;

                GetMeshData _getPosition = context.GetPosition;
                GetMeshData _getNormal   = context.GetNormal;
                GetMeshData _getTexCoord = context.GetTexCoord;

                SetTangentSpace      _setTangentSpace      = context.SetTangentSpace;
                SetTangentSpaceBasic _setTangentSpaceBasic = context.SetTangentSpaceBasic;

                var mikkInterface = new SMikkTSpaceInterface();

                mikkInterface.m_getNumFaces          = Marshal.GetFunctionPointerForDelegate(_getNumFaces);
                mikkInterface.m_getNumVerticesOfFace = Marshal.GetFunctionPointerForDelegate(_getNumVerticesOfFace);

                mikkInterface.m_getPosition = Marshal.GetFunctionPointerForDelegate(_getPosition);
                mikkInterface.m_getNormal   = Marshal.GetFunctionPointerForDelegate(_getNormal);
                mikkInterface.m_getTexCoord = Marshal.GetFunctionPointerForDelegate(_getTexCoord);

                if (context.UsesBasicTangentHandler)
                {
                    mikkInterface.m_setTSpaceBasic = Marshal.GetFunctionPointerForDelegate(_setTangentSpaceBasic);
                }
                else
                {
                    mikkInterface.m_setTSpace = Marshal.GetFunctionPointerForDelegate(_setTangentSpace);
                }

                var mikkContext = new SMikkTSpaceContext();
                mikkContext.m_pInterface = new IntPtr(&mikkInterface);

                var pContext = new IntPtr(&mikkContext);

                var result = Native.genMikktTangSpace(pContext, angularThreshold);

                GC.KeepAlive(context);

                GC.KeepAlive(_getNumFaces);
                GC.KeepAlive(_getNumVerticesOfFace);

                GC.KeepAlive(_getPosition);
                GC.KeepAlive(_getNormal);
                GC.KeepAlive(_getTexCoord);

                GC.KeepAlive(_setTangentSpace);
                GC.KeepAlive(_setTangentSpaceBasic);

                return(result);
            }
            finally
            {
            }
        }
    public void CreateMeshData(object token)
    {
        try
        {
            //Thread.CurrentThread.Name = "CRT" + chunkX.ToString() + "," + chunkZ.ToString();
            //Create an empty meshdata object
            chunkMeshData  = new MeshData();
            natureMeshData = new MeshData();

            //Start with determining the biome for this chunk
            //Biome biome = (Biome)Chance.RandomItemFromList(biomes, chunkX, chunkZ, 4);
            //Profiler.BeginThreadProfiling("My threads", "Chunk Builder");
            //CustomSampler sampler = CustomSampler.Create("Chunk builder");
            //sampler.Begin();

            cornerBiomes = WorldGenUtil.GetCornerBiomes(regions, chunkX, chunkZ, regionSize, chunkSize, out isSeamingChunk);



            //If the chunk is not on the disk and is not a seaming chunk just calculate it
            if (!isSeamingChunk)
            {
                for (int x = 0; x < chunkSize; x += LODLevel)
                {
                    if (quitThread)
                    {
                        break;
                    }
                    for (int z = 0; z < chunkSize; z += LODLevel)
                    {
                        if (quitThread)
                        {
                            break;
                        }
                        int  heighthere     = Chance.GetHeight(chunkX * 8 + x, chunkZ * 8 + z, cornerBiomes[0], null); chunkMeshData = GetMeshData.Blockdata(chunkX * 8 + x, heighthere, chunkZ * 8 + z, chunkMeshData, cornerBiomes[0], null, true, x, z, LODLevel, chunkSize);
                        Item vegetationItem = Chance.Vegetation(chunkX * 8 + x, chunkZ * 8 + z, heighthere, cornerBiomes[0]);
                        if (vegetationItem != null)
                        {
                            natureMeshData = GetItemMeshData.MeshForObject(chunkX * 8 + x, heighthere, chunkZ * 8 + z, natureMeshData, vegetationItem);
                        }
                    }
                }
            }


            //If the chunk is not on disk but is seaming collect the height of the corners and sent this with the data
            if (isSeamingChunk)
            {
                heightGrid = WorldGenUtil.GetHeightGrid(cornerBiomes, chunkSize, chunkX, chunkZ);
                for (int x = 0; x < chunkSize; x += LODLevel)
                {
                    if (quitThread)
                    {
                        break;
                    }
                    for (int z = 0; z < chunkSize; z += LODLevel)
                    {
                        if (quitThread)
                        {
                            break;
                        }
                        int heighthere = heightGrid[x, z];
                        chunkMeshData = GetMeshData.Blockdata(chunkX * 8 + x, heighthere, chunkZ * 8 + z, chunkMeshData, cornerBiomes[0], heightGrid, true, x, z, LODLevel, chunkSize);
                        Item vegetationItem = Chance.Vegetation(chunkX * 8 + x, chunkZ * 8 + z, heighthere, cornerBiomes[0]);
                        if (vegetationItem != null)
                        {
                            natureMeshData = GetItemMeshData.MeshForObject(chunkX * 8 + x, heighthere, chunkZ * 8 + z, natureMeshData, vegetationItem);
                        }
                    }
                }
            }

            finished = true;
            //sampler.End();
        }
        catch (System.Exception ex)
        {
            Debug.LogError(ex.Message);
        }
    }