public UnityEngine.Material[] LoadModelMaterials(MaterialLoader materialLoader, MMDUnityConfig config, RawMMDModel model) { var ret = new UnityEngine.Material[model.Parts.Length]; for (var i = 0; i < model.Parts.Length; i++) { ret[i] = materialLoader.LoadMaterial(model.Parts[i].Material, config); ret[i].name = model.Parts[i].Material.Name; } return(ret); }
private void DoLoadModel(string filePath) { Debug.LogFormat("start load model {0}", filePath); _model = ModelReader.LoadMmdModel(filePath, _modelReadConfig); Release(); var directoryInfo = new FileInfo(filePath).Directory; if (directoryInfo == null) { throw new MmdFileParseException(filePath + " does not belong to any directory."); } var relativePath = directoryInfo.FullName; _materialLoader = new MaterialLoader(new TextureLoader(relativePath, DefaultMaxTextureSize)); var vertCount = _model.Vertices.Length; if (vertCount <= 65535) { var mesh = new Mesh { vertices = new Vector3[vertCount], normals = new Vector3[vertCount] }; var triangleCount = _model.TriangleIndexes.Length / 3; var triangles = _model.TriangleIndexes; FillSubMesh(mesh, triangleCount, triangles); var uv = ExtratUv(_model.Vertices); var uvVec = new Vector2[vertCount]; Utils.MmdUvToUnityUv(uv, uvVec); mesh.uv = uvVec; mesh.boneWeights = _model.Vertices.Select(x => ConvertBoneWeight(x.SkinningOperator)).ToArray(); ReleasePreviousMeshes(); Mesh = mesh; mesh.RecalculateBounds(); _partIndexes = null; } else { var triangleCount = _model.TriangleIndexes.Length / 3; var triangles = _model.TriangleIndexes; var uv = ExtratUv(_model.Vertices); ; var uvVec = new Vector2[vertCount]; Utils.MmdUvToUnityUv(uv, uvVec); FillPartMeshes(triangleCount, triangles, uv); } _poser = new Poser(_model); _physicsReactor = new BulletPyhsicsReactor(); _physicsReactor.AddPoser(_poser); InitMesh(); Debug.LogFormat("load model finished {0}", filePath); }
private void Release() { if (_materialLoader != null) { _materialLoader.Dispose(); _materialLoader = null; } if (_bonePoseFileStorage != null) { _bonePoseFileStorage.Release(); _bonePoseFileStorage = null; } StopBonePoseCalculation(); }
private void Release() //OYM:发布 { if (_materialLoader != null) { _materialLoader.Dispose();//OYM:添加材质,写到这里我快饿死了,工作完成差不多三成 _materialLoader = null; } if (_bonePoseFileStorage != null) { _bonePoseFileStorage.Release(); _bonePoseFileStorage = null; } StopBonePoseCalculation(); }
async Task <bool> LoadModel(string filePath) { ModelPath = filePath; try { DirectoryInfo directoryInfo = new FileInfo(filePath).Directory; if (directoryInfo == null) { throw new MMDFileParseException(filePath + " does not belong to any directory."); } RawMMDModel = ModelReader.LoadMmdModel(filePath, modelReadConfig); transform.name = RawMMDModel.Name; string relativePath = directoryInfo.FullName; MaterialLoader materialLoader = new MaterialLoader(new TextureLoader(relativePath, MaxTextureSize)); int vertCount = RawMMDModel.Vertices.Length; int triangleCount = RawMMDModel.TriangleIndexes.Length / 3; int[] triangles = RawMMDModel.TriangleIndexes; string meshName = Path.GetFileName(filePath); Mesh mesh = new Mesh { name = meshName, vertices = new Vector3[vertCount], normals = new Vector3[vertCount] }; mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; FillSubMesh(mesh, triangleCount, triangles); float[] uv = ExtratUv(RawMMDModel.Vertices); mesh.uv = Utils.MmdUvToUnityUv(uv); mesh.vertices = RawMMDModel.Vertices.Select(x => x.Coordinate).ToArray(); mesh.normals = RawMMDModel.Vertices.Select(x => x.Normal).ToArray(); mesh.boneWeights = RawMMDModel.Vertices.Select(x => ConvertBoneWeight(x.SkinningOperator)).ToArray(); mesh.RecalculateBounds(); Mesh = mesh; await AttachVertexMorph(mesh); await SearchForMaterialMorph(); Utils.ClearAllTransformChild(transform); (GameObject rootObj, Transform[] bones) = CreateBones(model: RawMMDModel, parentOfAll: gameObject); SkinnedMeshRenderer = rootObj.AddComponent <SkinnedMeshRenderer>(); SkinnedMeshRenderer.enabled = false; SkinnedMeshRenderer.quality = SkinQuality.Bone4; SkinnedMeshRenderer.rootBone = rootObj.transform; InitializeBoneDictionaries(bones); Materials = LoadModelMaterials(materialLoader, new MMDUnityConfig(), RawMMDModel); ReorderRender(Materials); SkinnedMeshRenderer.sharedMesh = mesh; BuildBindpose(Mesh, bones, SkinnedMeshRenderer); SkinnedMeshRenderer.materials = Materials; MaterialMorphRemover.HideMaterialMorphs(this); AttachPhysicComponents(bones, this); } catch (Exception e) { UnityEngine.Debug.LogException(e); return(false); } return(true); }