private static void LoadStandartMesh(OBJECTINFO objMainInfo, string uniqueMeshName) { if (BinaryReader.ReadUInt16() == 0) { numLODs = BinaryReader.ReadByte(); for (int i = 0; i < numLODs; i++) { float relativeDistance = BinaryReader.ReadSingle(); int numVertices = BinaryReader.ReadUInt16(); List <Vector3> positionOfPoint = new List <Vector3>(); List <Vector3> normalVector = new List <Vector3>(); List <Vector2> textureCoordinate = new List <Vector2>(); List <int[]> meshTriangles = new List <int[]>(); List <int> materialsId = new List <int>(); for (int l = 0; l < numVertices; l++) { positionOfPoint.Add(CustomReader.ReadType <Vector3>()); normalVector.Add(CustomReader.ReadType <Vector3>()); textureCoordinate.Add(CustomReader.ReadType <Vector2>()); } int numFaceGroups = BinaryReader.ReadByte(); for (int l = 0; l < numFaceGroups; l++) { List <int> faceTriangles = new List <int>(); int numTriangles = BinaryReader.ReadUInt16(); for (int n = 0; n < numTriangles * 3; n++) { faceTriangles.Add(BinaryReader.ReadUInt16()); } int materialId = BinaryReader.ReadUInt16(); meshTriangles.Add(faceTriangles.ToArray()); materialsId.Add(materialId); } if (i == rootLod) { SMeshInfo sMeshInfo = new SMeshInfo(); sMeshInfo.name = uniqueMeshName; sMeshInfo.texs = materialsId.ToArray(); sMeshInfo.triangles = meshTriangles.ToArray(); sMeshInfo.vertices = positionOfPoint.ToArray(); sMeshInfo.normals = normalVector.ToArray(); sMeshInfo.uv = textureCoordinate.ToArray(); CreateMesh(sMeshInfo, objMainInfo); } } } }
private static void CreateMesh(SMeshInfo sMeshInfo, OBJECTINFO objMainInfo) { UniqueMesh.transform.position = objMainInfo.positionMesh * WorldController.WorldScale; UniqueMesh.transform.localScale = objMainInfo.scaleMesh; Quaternion fixedRotation = new Quaternion(objMainInfo.rotationMesh.y, objMainInfo.rotationMesh.z, objMainInfo.rotationMesh.w, -objMainInfo.rotationMesh.x); UniqueMesh.transform.rotation = fixedRotation; MeshRenderer meshRenderer = UniqueMesh.AddComponent <MeshRenderer>(); meshRenderer.materials = new Material[sMeshInfo.triangles.Length]; MeshFilter meshFilter = UniqueMesh.AddComponent <MeshFilter>(); meshFilter.sharedMesh = new Mesh(); for (int i = 0; i < sMeshInfo.vertices.Length; i++) { sMeshInfo.vertices[i] = sMeshInfo.vertices[i] * WorldController.WorldScale; } meshFilter.sharedMesh.subMeshCount = sMeshInfo.triangles.Length; meshFilter.sharedMesh.vertices = sMeshInfo.vertices; meshFilter.sharedMesh.normals = sMeshInfo.normals; meshFilter.sharedMesh.uv = sMeshInfo.uv; for (int i = 0; i < sMeshInfo.triangles.Length; i++) { meshRenderer.materials[i].shader = Shader.Find("Mobile/Diffuse"); meshRenderer.materials[i].mainTexture = TextureLoader.LoadTexture(_4DSTextures[sMeshInfo.texs[i] - 1]); meshFilter.sharedMesh.SetTriangles(sMeshInfo.triangles[i], i); } meshFilter.sharedMesh.Optimize(); }
private static void LoadObjectsInfo() { int CountObjects = BinaryReader.ReadUInt16(); for (int i = 0; i < CountObjects; i++) { byte frameType = BinaryReader.ReadByte(), visualType = 0; if (frameType == (int)FrameType.FRAME_VISUAL) { visualType = BinaryReader.ReadByte(); byte[] visualFlags = BinaryReader.ReadBytes(2); } OBJECTINFO objMainInfo = CustomReader.ReadType <OBJECTINFO>(); byte cullingFlags = BinaryReader.ReadByte(); byte uniqueMeshNameLength = BinaryReader.ReadByte(); string uniqueMeshName = new string(BinaryReader.ReadChars(uniqueMeshNameLength)); byte meshParametersLength = BinaryReader.ReadByte(); string meshParameters = new string(BinaryReader.ReadChars(meshParametersLength)); GameObject uniqueMesh = new GameObject(uniqueMeshName); uniqueMesh.transform.parent = ModelObject.transform; UniqueMeshes.Add(uniqueMesh); _4DSObjects.Add(objMainInfo); UniqueMesh = uniqueMesh; if (frameType == (int)FrameType.FRAME_VISUAL) { switch (visualType) { case (int)VisualType.VISUAL_OBJECT: LoadStandartMesh(objMainInfo, uniqueMeshName); break; case (int)VisualType.VISUAL_SINGLEMESH: LoadStandartMesh(objMainInfo, uniqueMeshName); LoadSingleMesh(); break; case (int)VisualType.VISUAL_SINGLEMORPH: LoadStandartMesh(objMainInfo, uniqueMeshName); LoadSingleMesh(); LoadMorph(); break; case (int)VisualType.VISUAL_BILLBOARD: LoadStandartMesh(objMainInfo, uniqueMeshName); BinaryReader.BaseStream.Position += 5; break; case (int)VisualType.VISUAL_MORPH: LoadStandartMesh(objMainInfo, uniqueMeshName); LoadMorph(); break; case (int)VisualType.VISUAL_MIRROR: LoadMirror(); break; default: Debug.Log("Unknown Visual Type. Abort..."); return; } } switch (frameType) { case (int)FrameType.FRAME_VISUAL: continue; case (int)FrameType.FRAME_SECTOR: LoadSector(); break; case (int)FrameType.FRAME_TARGET: LoadTarget(); break; case (int)FrameType.FRAME_DUMMY: LoadDummy(); break; case (int)FrameType.FRAME_JOINT: JointInfo jointInfo = new JointInfo(); jointInfo.boneObj = uniqueMesh; LoadJoint(out jointInfo.transformMatrix, out jointInfo.boneID); _4DSJoints.Add(jointInfo); break; default: Debug.Log("Unknown Frame Type. Abort..."); return; } } ParentObjects(); }