Exemplo n.º 1
0
 public bool BuildTriMesh(CRenderContext rc, Graphics.Mesh.CGfxMeshPrimitives mesh, ref CConvexDecompDesc desc, bool doConvex = true)
 {
     unsafe
     {
         mesh.PreUse(true);
         if (doConvex)
         {
             fixed(CConvexDecompDesc *p = &desc)
             {
                 return(SDK_SimGeom_BuildTriMesh(CoreObject, rc.CoreObject, mesh.CoreObject, p));
             }
         }
         else
         {
             return(SDK_SimGeom_BuildTriMesh(CoreObject, rc.CoreObject, mesh.CoreObject, (CConvexDecompDesc *)IntPtr.Zero));
         }
     }
 }
Exemplo n.º 2
0
        public bool InitMesh(CRenderContext rc, Graphics.Mesh.CGfxMeshPrimitives mesh)
        {
            mMesh = mesh;
            mesh.PreUse();
            EnablePosition        = false;
            EnableNormal          = false;
            EnableTangent         = false;
            EnableColor           = false;
            EnableUV              = false;
            EnableLightMapUV      = false;
            EnableSkinIndex       = false;
            EnableSkinWeight      = false;
            EnableTerrainIndex    = false;
            EnableTerrainGradient = false;

            AABB = mesh.AABB;

            int vertNum = 0;

            Vertices.Clear();

            #region Position
            var vb = mesh.GeometryMesh.GetVertexBuffer(EVertexSteamType.VST_Position);
            if (vb != null)
            {
                EnablePosition = true;
                var blob = new Support.CBlobObject();
                vb.GetBufferData(rc, blob);
                unsafe
                {
                    vertNum = (int)blob.Size / sizeof(Vector3);
                    var ptr = (Vector3 *)blob.Data.ToPointer();
                    for (int i = 0; i < vertNum; i++)
                    {
                        var vert = new Vertex();
                        vert.Position = ptr[i];
                        Vertices.Add(vert);
                    }
                }
            }
            else
            {
                return(false);
            }
            #endregion

            #region Normal
            vb = mesh.GeometryMesh.GetVertexBuffer(EVertexSteamType.VST_Normal);
            if (vb != null)
            {
                EnableNormal = true;
                var blob = new Support.CBlobObject();
                vb.GetBufferData(rc, blob);
                unsafe
                {
                    var ptr = (Vector3 *)blob.Data.ToPointer();
                    for (int i = 0; i < vertNum; i++)
                    {
                        Vertices[i].Normal = ptr[i];
                    }
                }
            }
            #endregion

            #region Tangent
            vb = mesh.GeometryMesh.GetVertexBuffer(EVertexSteamType.VST_Tangent);
            if (vb != null)
            {
                EnableTangent = true;
                var blob = new Support.CBlobObject();
                vb.GetBufferData(rc, blob);
                unsafe
                {
                    var ptr = (Vector4 *)blob.Data.ToPointer();
                    for (int i = 0; i < vertNum; i++)
                    {
                        Vertices[i].Tangent = ptr[i];
                    }
                }
            }
            #endregion

            #region Color
            vb = mesh.GeometryMesh.GetVertexBuffer(EVertexSteamType.VST_Color);
            if (vb != null)
            {
                EnableColor = true;
                var blob = new Support.CBlobObject();
                vb.GetBufferData(rc, blob);
                unsafe
                {
                    var ptr = (Byte4 *)blob.Data.ToPointer();
                    for (int i = 0; i < vertNum; i++)
                    {
                        Vertices[i].Color = ptr[i];
                    }
                }
            }
            #endregion

            #region UV
            vb = mesh.GeometryMesh.GetVertexBuffer(EVertexSteamType.VST_UV);
            if (vb != null)
            {
                EnableUV = true;
                var blob = new Support.CBlobObject();
                vb.GetBufferData(rc, blob);
                unsafe
                {
                    var ptr = (Vector2 *)blob.Data.ToPointer();
                    for (int i = 0; i < vertNum; i++)
                    {
                        Vertices[i].UV = ptr[i];
                    }
                }
            }
            #endregion

            #region LightMapUV
            vb = mesh.GeometryMesh.GetVertexBuffer(EVertexSteamType.VST_LightMap);
            if (vb != null)
            {
                EnableLightMapUV = true;
                var blob = new Support.CBlobObject();
                vb.GetBufferData(rc, blob);
                unsafe
                {
                    var ptr = (Vector2 *)blob.Data.ToPointer();
                    for (int i = 0; i < vertNum; i++)
                    {
                        Vertices[i].LightMapUV = ptr[i];
                    }
                }
            }
            #endregion

            #region SkinIndex
            vb = mesh.GeometryMesh.GetVertexBuffer(EVertexSteamType.VST_SkinIndex);
            if (vb != null)
            {
                EnableSkinIndex = true;
                var blob = new Support.CBlobObject();
                vb.GetBufferData(rc, blob);
                unsafe
                {
                    var ptr = (Byte4 *)blob.Data.ToPointer();
                    for (int i = 0; i < vertNum; i++)
                    {
                        Vertices[i].SkinIndex = ptr[i];
                    }
                }
            }
            #endregion

            #region SkinWeight
            vb = mesh.GeometryMesh.GetVertexBuffer(EVertexSteamType.VST_SkinWeight);
            if (vb != null)
            {
                EnableSkinWeight = true;
                var blob = new Support.CBlobObject();
                vb.GetBufferData(rc, blob);
                unsafe
                {
                    var ptr = (Vector4 *)blob.Data.ToPointer();
                    for (int i = 0; i < vertNum; i++)
                    {
                        Vertices[i].SkinWeight = ptr[i];
                    }
                }
            }
            #endregion

            #region TerrainIndex
            vb = mesh.GeometryMesh.GetVertexBuffer(EVertexSteamType.VST_TerrainIndex);
            if (vb != null)
            {
                EnableTerrainIndex = true;
                var blob = new Support.CBlobObject();
                vb.GetBufferData(rc, blob);
                unsafe
                {
                    var ptr = (Byte4 *)blob.Data.ToPointer();
                    for (int i = 0; i < vertNum; i++)
                    {
                        Vertices[i].TerrainIndex = ptr[i];
                    }
                }
            }
            #endregion

            #region TerrainGradient
            vb = mesh.GeometryMesh.GetVertexBuffer(EVertexSteamType.VST_TerrainGradient);
            if (vb != null)
            {
                EnableTerrainGradient = true;
                var blob = new Support.CBlobObject();
                vb.GetBufferData(rc, blob);
                unsafe
                {
                    var ptr = (Byte4 *)blob.Data.ToPointer();
                    for (int i = 0; i < vertNum; i++)
                    {
                        Vertices[i].TerrainGradient = ptr[i];
                    }
                }
            }
            #endregion

            List <int> OriIndices = new List <int>();
            #region Face
            var ib = mesh.GeometryMesh.GetIndexBuffer();
            if (ib != null)
            {
                var blob = new Support.CBlobObject();
                ib.GetBufferData(rc, blob);
                unsafe
                {
                    if (ib.Desc.Type == EIndexBufferType.IBT_Int16)
                    {
                        int indexNum = (int)blob.Size / (sizeof(UInt16));
                        var ptr      = (UInt16 *)blob.Data.ToPointer();
                        for (int i = 0; i < indexNum; i++)
                        {
                            OriIndices.Add(ptr[i]);
                        }
                    }
                    else
                    {
                        int indexNum = (int)blob.Size / (sizeof(int));
                        var ptr      = (int *)blob.Data.ToPointer();
                        for (int i = 0; i < indexNum; i++)
                        {
                            OriIndices.Add(ptr[i]);
                        }
                    }
                }
            }
            #endregion

            #region Mtl
            Faces.Clear();
            Atoms = new List <MeshAtom>();
            for (UInt32 i = 0; i < mesh.AtomNumber; i++)
            {
                var atom = new MeshAtom();
                atom.Mtl = (int)i;
                mesh.GetAtom(i, 0, ref atom.OriginDesc);
                Atoms.Add(atom);

                int index = (int)atom.OriginDesc.StartIndex / 3;
                for (int j = 0; j < atom.OriginDesc.NumPrimitives; j++)
                {
                    var f = new Face();
                    f.A   = OriIndices[(int)atom.OriginDesc.StartIndex + j * 3 + 0];
                    f.B   = OriIndices[(int)atom.OriginDesc.StartIndex + j * 3 + 1];
                    f.C   = OriIndices[(int)atom.OriginDesc.StartIndex + j * 3 + 2];
                    f.Mtl = (int)i;
                    atom.Faces.Add(Faces.Count);
                    Faces.Add(f);
                }
            }
            #endregion

            BuildFixedVertices(0.000001f);
            BuildFaces();

            BuildEdges();
            BuildLinkers();

            for (int i = 0; i < Atoms.Count; i++)
            {
                Atoms[i].FaceLods.Clear();
                Atoms[i].FaceLods.Add(Atoms[i].Faces);
            }

            return(true);
        }