示例#1
0
        public static Mesh BakePart(BasePart part, Material material = null)
        {
            Mesh result = null;

            Asset meshAsset    = null;
            Asset textureAsset = null;

            Vector3 scale  = null;
            CFrame  offset = null;

            if (material != null)
            {
                material.LinkedTo     = part;
                material.Reflectance  = part.Reflectance;
                material.Transparency = part.Transparency;
            }

            if (part.Transparency < 1)
            {
                if (part.IsA("MeshPart"))
                {
                    MeshPart meshPart = part as MeshPart;

                    if (meshPart.MeshId != null && meshPart.MeshId.Length > 0)
                    {
                        string meshId = meshPart.MeshId;
                        meshAsset = Asset.GetByAssetId(meshId);
                    }
                    else
                    {
                        string partName = meshPart.Name;
                        StandardLimbs.TryGetValue(partName, out meshAsset);
                    }

                    if (meshPart.TextureId != null)
                    {
                        textureAsset = Asset.GetByAssetId(meshPart.TextureId);
                    }

                    scale  = meshPart.Size / meshPart.InitialSize;
                    offset = part.CFrame;
                }
                else
                {
                    SpecialMesh specialMesh = part.FindFirstChildOfClass <SpecialMesh>();
                    offset = part.CFrame;

                    if (specialMesh != null && specialMesh.MeshType == MeshType.FileMesh)
                    {
                        meshAsset = Asset.GetByAssetId(specialMesh.MeshId);
                        offset   *= new CFrame(specialMesh.Offset);
                        scale     = specialMesh.Scale;

                        if (material != null)
                        {
                            textureAsset         = Asset.GetByAssetId(specialMesh.TextureId);
                            material.VertexColor = specialMesh.VertexColor;
                        }
                    }
                    else
                    {
                        DataModelMesh legacy = part.FindFirstChildOfClass <DataModelMesh>();

                        if (legacy != null)
                        {
                            meshAsset = Head.ResolveHeadMeshAsset(legacy);
                            offset   *= new CFrame(legacy.Offset);
                            scale     = legacy.Scale;
                        }
                    }
                }
            }
            else
            {
                // Just give it a blank mesh to eat for now.
                result = new Mesh();
            }

            if (meshAsset != null)
            {
                if (material != null)
                {
                    material.TextureAsset = textureAsset;
                }

                result = FromAsset(meshAsset);
                result.BakeGeometry(scale, offset);
            }

            return(result);
        }