Beispiel #1
0
    public void SetRendererAndFilter(GameObject go, MeshPartInfo ms)
    {
        MeshFilter meshFilter = go.GetComponent <MeshFilter>();

        if (meshFilter == null)
        {
            meshFilter = go.AddComponent <MeshFilter>();
        }
        meshFilter.mesh = ms.mesh;
        MeshRenderer meshRenderer = go.GetComponent <MeshRenderer>();

        if (meshRenderer == null)
        {
            meshRenderer = go.AddComponent <MeshRenderer>();
        }

        if (mat != null)
        {
            meshRenderer.sharedMaterial = mat;
        }
        else
        {
            meshRenderer.sharedMaterials = ms.mats;
        }

        meshRenderer.enabled = true;
    }
Beispiel #2
0
        private List <MeshPartInfo> LoadHeadMeshes(ObjItem objModel, bool fromDragAndDrop, ManType manType, float scale, ref int lastTriangle)
        {
            var result                   = new List <MeshPartInfo>();
            var vertexPositions          = new List <float>();
            var vertexNormals            = new List <float>();
            var vertexTextureCoordinates = new List <float>();
            var vertexBoneIndices        = new List <float>();
            var vertexBoneWeights        = new List <float>();
            var indeces                  = new List <uint>();

            foreach (var modelGroup in objModel.Groups) // one group - one mesh
            {
                //    if (/*!ProgramCore.PluginMode &&*/ ( modelGroup.Key.Name == "Tear" || modelGroup.Key.Name == "Cornea" || modelGroup.Key.Name == "EyeReflection"))     // очень плохие материалы. ИЗ-за них ломаются глазки.
                //       continue;           // если это плагин - то пропуск материалов ВСЕ ломает для экспортера в строчке GetObjFace. Возможно потребуется химичить с индексами

                vertexPositions.Clear();
                vertexNormals.Clear();
                vertexTextureCoordinates.Clear();
                vertexBoneIndices.Clear();
                vertexBoneWeights.Clear();
                indeces.Clear();

                foreach (var face in modelGroup.Value.Faces)          //  combine all meshes in group - to one mesh.
                {
                    GetObjFace(face, objModel, ref vertexPositions, ref vertexNormals, ref vertexTextureCoordinates, ref vertexBoneWeights, ref vertexBoneIndices, ref indeces, ref lastTriangle);
                }

                var positions = new List <Vector3>();
                var texCoords = new List <Vector2>();
                var index     = 0;
                for (var i = 0; i < vertexPositions.Count / 3; ++i)
                {
                    index = i * 3;
                    positions.Add(new Vector3(vertexPositions[index], vertexPositions[index + 1], vertexPositions[index + 2]));
                    texCoords.Add(new Vector2(vertexTextureCoordinates[i * 2], 1.0f - vertexTextureCoordinates[i * 2 + 1]));
                }

                var meshPartInfo = new MeshPartInfo
                {
                    VertexPositions = GetScaledVertices(positions, scale),
                    TextureCoords   = texCoords,
                    PartName        = modelGroup.Key.Name == "default" ? string.Empty : modelGroup.Key.Name,
                };

                meshPartInfo.Texture                = modelGroup.Key.Texture;
                meshPartInfo.TransparentTexture     = modelGroup.Key.TransparentTexture;
                meshPartInfo.TextureName            = modelGroup.Key.DiffuseTextureMap;
                meshPartInfo.TransparentTextureName = modelGroup.Key.TransparentTextureMap;
                meshPartInfo.Color =
                    new Vector4(modelGroup.Key.DiffuseColor.X, modelGroup.Key.DiffuseColor.Y,
                                modelGroup.Key.DiffuseColor.Z, modelGroup.Key.Transparency);
                meshPartInfo.MaterialName = modelGroup.Key.Name;

                result.Add(meshPartInfo);
            }

            return(result);
        }
 public bool CreateMeshPart(MeshPartInfo info)
 {
     var part = new RenderMeshPart();
     if (part.Create(info))
     {
         RenderMesh.AddPart(part);
         return true;
     }
     return false;
 }
        public bool CreateMeshPart(GenesisType genesis, MeshPartInfo info)
        {
            var part = new RenderMeshPart();

            if (part.Create(genesis, info))
            {
                RenderMesh.AddPart(part);
                return(true);
            }
            return(false);
        }
        public bool CreateMeshPart(MeshPartInfo info)
        {
            var part = new RenderMeshPart();

            if (part.Create(info))
            {
                RenderMesh.AddPart(part);
                return(true);
            }
            return(false);
        }
Beispiel #6
0
    public void SetCollider(GameObject go, MeshPartInfo ms)
    {
        Collider collider = go.GetComponent <Collider>();

        if (collider != null)
        {
            GameObject.DestroyImmediate(collider);
        }
        MeshCollider meshCollider = go.AddComponent <MeshCollider>();

        meshCollider.sharedMesh = ms.mesh;
    }
Beispiel #7
0
        public List <DynamicRenderMesh> AddMehes(string path, MeshType type, bool fromDragAndDrop, ManType manType, string animationPath, bool needExporter)
        {
            var result = new List <DynamicRenderMesh>();

            var objModel = ObjLoader.LoadObjFile(path, needExporter);

            if (objModel == null)
            {
                ProgramCore.EchoToLog(string.Format("Can't load obj model '{0}'", path), EchoMessageType.Error);
                return(result);
            }

            switch (type)
            {
            case MeshType.Hair:
            {
                var fi           = new FileInfo(path);
                var objModelNull = ObjLoader.LoadObjFile(Path.Combine(fi.DirectoryName, fi.Name.Replace(fi.Extension, string.Format("_null{0}", fi.Extension))));
                if (objModelNull != null &&
                    (objModelNull.Groups.Count != objModel.Groups.Count ||
                     objModel.Vertices.Count != objModelNull.Vertices.Count))
                {
                    //  objModel.TextureCoords.Count != objModelNull.TextureCoords.Count))
                    objModelNull = null;
                }

                result = LoadHairMeshes(objModel, objModelNull, fromDragAndDrop, manType, MeshType.Hair);
                foreach (var renderMesh in result)
                {
                    HairMeshes.Add(renderMesh);
                }
                break;
            }

            case MeshType.Accessory:
                return(objModel.accessoryByHeadShop ? LoadSpecialAccessoryMesh(objModel) : new List <DynamicRenderMesh> {
                    LoadAccessoryMesh(objModel)
                });

            case MeshType.Head:
            {
                var tempPluginTexture = string.Empty;
                if (ProgramCore.PluginMode)
                {
                    var folderPath = Path.Combine(Application.StartupPath, "Models\\Model", manType.GetObjDirPath());
                    switch (ProgramCore.Project.ManType)
                    {
                    case ManType.Male:
                        tempPluginTexture = Path.Combine(folderPath, "Maps", "RyNevio_faceB.jpg");
                        break;

                    case ManType.Female:
                        tempPluginTexture = Path.Combine(folderPath, "Maps", "RyBelle_face.jpg");
                        break;

                    case ManType.Child:
                        tempPluginTexture = Path.Combine(folderPath, "Maps", "AC_KidsRRHBy.jpg");
                        break;

                    default:
                        tempPluginTexture = Path.Combine(Application.StartupPath, "Plugin", "fsRndColor.png");
                        break;
                    }
                }

                foreach (var mesh in HeadMeshes)
                {
                    mesh.Destroy();
                }
                HeadMeshes.Clear();

                var objModelFull = animationPath == string.Empty ? null : ObjLoader.LoadObjFile(animationPath);
                if (objModelFull != null &&
                    (objModelFull.Groups.Count != objModel.Groups.Count ||
                     objModel.Vertices.Count != objModelFull.Vertices.Count ||
                     objModel.TextureCoords.Count != objModelFull.TextureCoords.Count))
                {
                    objModelFull = null;
                }
                LastTriangleIndex = 0;
                result            = LoadHairMeshes(objModel, objModelFull, fromDragAndDrop, manType, MeshType.Head);
                var meshPartInfos = new List <MeshPartInfo>();
                var a             = new Vector3(99999.0f, 99999.0f, 99999.0f);
                var b             = new Vector3(-99999.0f, -99999.0f, -99999.0f);
                foreach (var renderMesh in result)
                {
                    HeadMeshes.Add(renderMesh);

                    if (ProgramCore.PluginMode && ProgramCore.MainForm.PluginUvGroups.Contains(renderMesh.Material.Name))
                    {
                        if (string.IsNullOrEmpty(renderMesh.Material.DiffuseTextureMap))
                        {
                            renderMesh.Material.DiffuseTextureMap = tempPluginTexture;
                        }
                        else if (!File.Exists(renderMesh.Material.DiffuseTextureMap))
                        {
                            renderMesh.Material.DiffuseTextureMap = tempPluginTexture;
                        }
                    }

                    var meshPartInfo = new MeshPartInfo
                    {
                        VertexPositions        = renderMesh.GetVertices(),
                        TextureCoords          = renderMesh.GetTexCoords(),
                        PartName               = renderMesh.Title,
                        Color                  = renderMesh.Material.DiffuseColor,
                        Texture                = renderMesh.Material.Texture,
                        TransparentTexture     = renderMesh.Material.TransparentTexture,
                        TextureName            = renderMesh.Material.DiffuseTextureMap,
                        TransparentTextureName = renderMesh.Material.TransparentTextureMap
                    };                          // создаем инфу о голове. для работы с headshop
                    GetAABB(ref a, ref b, meshPartInfo.VertexPositions);
                    meshPartInfos.Add(meshPartInfo);
                }
                Vector3 dv = Vector3.Zero;
                foreach (var meshPartInfo in meshPartInfos)
                {
                    dv = MoveToPosition(ref meshPartInfo.VertexPositions, a, b, Vector3.Zero);
                    ProgramCore.MainForm.ctrlRenderControl.headMeshesController.CreateMeshPart(meshPartInfo);
                }

                ObjExport = objModel.ObjExport;
                if (ObjExport != null)
                {
                    ObjExport.Delta = -dv;
                }
                ProgramCore.MainForm.ctrlRenderControl.headMeshesController.FinishCreating();

                // ProgramCore.MainForm.ctrlRenderControl.headMeshesController.InitializeTexturing(HeadController.GetDots(ProgramCore.Project.ManType), HeadController.GetIndices(ProgramCore.Project.ManType));
                break;
            }

            default:
                return(result);
            }
            return(result);
        }
Beispiel #8
0
    private MeshPartInfo InnerDoCombine(MeshPartInfo info, int id)
    {
        List <MeshFilter> mfs = info.meshFilters;
        int count             = mfs.Count;

        if (count == 0)
        {
            Debug.LogError("meshFiltersToCombined.Count == 0");
            return(null);
        }
        CombineInstance[] combines = new CombineInstance[count];
        Material[]        mats     = null;
        if (mat == null)
        {
            mats = new Material[count];
        }
        Matrix4x4 matrix = source.worldToLocalMatrix;

        for (int i = 0; i < count; i++)
        {
            MeshFilter   mf = mfs[i];
            MeshRenderer mr = mf.GetComponent <MeshRenderer>();
            if (mr == null)
            {
                continue;
            }
            Mesh ms = mf.sharedMesh;
            if (ms.isReadable == false)
            {
                Debug.LogError("模型必须设置为 Read/Write Enabled = True !! :" + ms);
                continue;
            }
            combines[i].mesh      = ms;
            combines[i].transform = matrix * mf.transform.localToWorldMatrix;
            //combines[i].transform=mf.transform;
            mr.enabled = false;//不渲染

            if (mat == null)
            {
                mats[i] = mr.sharedMaterial;
            }

            MeshCombineHelper.AddGo(mf.gameObject, this);
        }
        //source.gameObject.SetActive(false);

        Mesh newMesh = new Mesh();

        newMesh.indexFormat = indexFormat;
        newMesh.name        = source.name + "_Combined" + id;
        if (mat != null)
        {
            newMesh.name = mat.name + "_Combined" + id;
        }
        bool mergeSubMeshes = mat != null;

        newMesh.CombineMeshes(combines, mergeSubMeshes);//核心 合并网格

        // MeshPartInfo info=new MeshPartInfo();
        info.mesh = newMesh;
        info.mats = mats;
        return(info);
    }
        public List<DynamicRenderMesh> AddMehes(string path, MeshType type, bool fromDragAndDrop, ManType manType, string animationPath, bool needExporter)
        {
            var result = new List<DynamicRenderMesh>();

            var objModel = ObjLoader.LoadObjFile(path, needExporter);
            if (objModel == null)
            {
                ProgramCore.EchoToLog(string.Format("Can't load obj model '{0}'", path), EchoMessageType.Error);
                return result;
            }

            switch (type)
            {
                case MeshType.Hair:
                    {
                        var fi = new FileInfo(path);
                        var objModelNull = ObjLoader.LoadObjFile(Path.Combine(fi.DirectoryName, fi.Name.Replace(fi.Extension, string.Format("_null{0}", fi.Extension))));
                        if (objModelNull != null &&
                            (objModelNull.Groups.Count != objModel.Groups.Count ||
                            objModel.Vertices.Count != objModelNull.Vertices.Count))
                            //  objModel.TextureCoords.Count != objModelNull.TextureCoords.Count))
                            objModelNull = null;

                        result = LoadHairMeshes(objModel, objModelNull, fromDragAndDrop, manType, MeshType.Hair);
                        foreach (var renderMesh in result)
                            HairMeshes.Add(renderMesh);
                        break;
                    }
                case MeshType.Accessory:
                    result.AddRange(objModel.accessoryByHeadShop ? LoadSpecialAccessoryMesh(objModel) : new List<DynamicRenderMesh> { LoadAccessoryMesh(objModel) });
                    break;
                case MeshType.Head:
                    {
                        var tempPluginTexture = string.Empty;
                        if (ProgramCore.PluginMode)
                        {
                            var folderPath = Path.Combine(Application.StartupPath, "Models\\Model", manType.GetObjDirPath());
                            switch (ProgramCore.Project.ManType)
                            {
                                case ManType.Male:
                                    tempPluginTexture = Path.Combine(folderPath, "Maps", "RyNevio_faceB.jpg");
                                    break;
                                case ManType.Female:
                                    tempPluginTexture = Path.Combine(folderPath, "Maps", "RyBelle_face.jpg");
                                    break;
                                case ManType.Child:
                                    tempPluginTexture = Path.Combine(folderPath, "Maps", "AC_KidsRRHBy.jpg");
                                    break;
                                default:
                                    tempPluginTexture = Path.Combine(Application.StartupPath, "Plugin", "fsRndColor.png");
                                    break;
                            }
                        }

                        foreach (var mesh in HeadMeshes)
                            mesh.Destroy();
                        HeadMeshes.Clear();

                        var objModelFull = animationPath == string.Empty ? null : ObjLoader.LoadObjFile(animationPath);
                        if (objModelFull != null &&
                            (objModelFull.Groups.Count != objModel.Groups.Count ||
                            objModel.Vertices.Count != objModelFull.Vertices.Count ||
                            objModel.TextureCoords.Count != objModelFull.TextureCoords.Count))
                            objModelFull = null;
                        LastTriangleIndex = 0;
                        result = LoadHairMeshes(objModel, objModelFull, fromDragAndDrop, manType, MeshType.Head);
                        var meshPartInfos = new List<MeshPartInfo>();
                        var a = new Vector3(99999.0f, 99999.0f, 99999.0f);
                        var b = new Vector3(-99999.0f, -99999.0f, -99999.0f);
                        foreach (var renderMesh in result)
                        {
                            HeadMeshes.Add(renderMesh);

                            if (ProgramCore.PluginMode && ProgramCore.MainForm.PluginUvGroups.Contains(renderMesh.Material.Name))
                            {
                                if (string.IsNullOrEmpty(renderMesh.Material.DiffuseTextureMap))
                                    renderMesh.Material.DiffuseTextureMap = tempPluginTexture;
                                else if (!File.Exists(renderMesh.Material.DiffuseTextureMap))
                                    renderMesh.Material.DiffuseTextureMap = tempPluginTexture;
                            }

                            var meshPartInfo = new MeshPartInfo
                            {
                                VertexPositions = renderMesh.GetVertices(),
                                TextureCoords = renderMesh.GetTexCoords(),
                                PartName = renderMesh.Title,
                                Color = renderMesh.Material.DiffuseColor,
                                Texture = renderMesh.Material.Texture,
                                TransparentTexture = renderMesh.Material.TransparentTexture,
                                TextureName = renderMesh.Material.DiffuseTextureMap,
                                TransparentTextureName = renderMesh.Material.TransparentTextureMap
                            };                  // создаем инфу о голове. для работы с headshop
                            GetAABB(ref a, ref b, meshPartInfo.VertexPositions);
                            meshPartInfos.Add(meshPartInfo);

                        }
                        var dv = Vector3.Zero;
                        foreach (var meshPartInfo in meshPartInfos)
                        {
                            dv = MoveToPosition(ref meshPartInfo.VertexPositions, a, b, Vector3.Zero);
                            ProgramCore.MainForm.ctrlRenderControl.headMeshesController.CreateMeshPart(meshPartInfo);
                        }

                        ObjExport = objModel.ObjExport;
                        if (ObjExport != null)
                            ObjExport.Delta = -dv;
                        ProgramCore.MainForm.ctrlRenderControl.headMeshesController.FinishCreating();

                        // ProgramCore.MainForm.ctrlRenderControl.headMeshesController.InitializeTexturing(HeadController.GetDots(ProgramCore.Project.ManType), HeadController.GetIndices(ProgramCore.Project.ManType));
                        break;
                    }
                default:
                    return result;
            }

            foreach (var item in result)
                item.Path = path;

            return result;
        }
Beispiel #10
0
        public bool Create(MeshPartInfo info)
        {
            if (info.VertexPositions.Count == 0)
            {
                return(false);
            }
            Guid                   = Guid.NewGuid();
            Color                  = info.Color;
            Texture                = info.Texture;
            TransparentTexture     = info.TransparentTexture;
            TextureName            = info.TextureName;
            TransparentTextureName = info.TransparentTextureName;

            //Name.Contains("SkinHead") || Name.Contains("SkinNeck")
            Name = info.PartName;
            if (Name.Contains("Pupil"))
            {
                Type = HeadMeshType.Eyes;
            }
            if (Name.Contains("SkinFace"))
            {
                Type = HeadMeshType.Face;
            }
            else
            if (Name.Contains("Lip"))
            {
                Type = HeadMeshType.Lip;
            }

            Indices.Clear();
            var positions = new List <Vector3>();
            var texCoords = new List <Vector2>();

            var positionsDict     = new Dictionary <VertexInfo, uint>(new VectorEqualityComparer());
            var pointnsDict       = new Dictionary <Vector3, int>(new VectorEqualityComparer());
            var pointsIndicesDict = new Dictionary <int, int>();

            Points.Clear();
            for (var i = 0; i < info.VertexPositions.Count; i++)
            {
                var vertexInfo = new VertexInfo
                {
                    Position  = info.VertexPositions[i],
                    TexCoords = info.TextureCoords[i]
                };
                if (!positionsDict.ContainsKey(vertexInfo))
                {
                    var index = (uint)positions.Count;
                    positionsDict.Add(vertexInfo, index);
                    Indices.Add(index);
                    positions.Add(vertexInfo.Position);
                    texCoords.Add(vertexInfo.TexCoords);

                    if (!pointnsDict.ContainsKey(vertexInfo.Position))
                    {
                        pointnsDict.Add(vertexInfo.Position, Points.Count);
                        pointsIndicesDict.Add((int)index, Points.Count);
                        Points.Add(new Point3d
                        {
                            Indices = new List <uint> {
                                index
                            },
                            Position = vertexInfo.Position
                        });
                    }
                    else
                    {
                        var id = pointnsDict[vertexInfo.Position];
                        Points[id].Indices.Add(index);
                        pointsIndicesDict.Add((int)index, id);
                    }
                }
                else
                {
                    Indices.Add(positionsDict[vertexInfo]);
                }
            }

            CountIndices = Indices.Count;
            Vertices     = new Vertex3d[positions.Count];

            var normals = Normal.CalculateNormals(positions, Indices);

            for (var i = 0; i < Vertices.Length; i++)
            {
                Vertices[i].Position         = positions[i];
                Vertices[i].OriginalPosition = positions[i];
                Vertices[i].Normal           = normals[i];
                Vertices[i].TexCoord         = texCoords[i];
                Vertices[i].AutodotsTexCoord = new Vector3(texCoords[i].X, texCoords[i].Y, 1.0f);
                Vertices[i].Color            = Vector4.One;
            }
            FillPoints(pointsIndicesDict);
            Destroy();
            GL.GenBuffers(1, out VertexBuffer);
            GL.GenBuffers(1, out IndexBuffer);
            return(true);
        }