private static void ReadMeshFromATO(Reader reader, GeometricObject m) { // Revolution only: Before creating the gameobject, read the actual model data from the ATO if (Settings.s.game == Settings.Game.R2Revolution) { MapLoader l = MapLoader.Loader; List <GeometricObject> meshObjects = new List <GeometricObject>(); for (uint i = 0; i < m.num_elements; i++) { if (m.element_types[i] == 1) { R2PS2Loader ps2l = (R2PS2Loader)l; meshObjects.Add(ps2l.ato.meshes[ps2l.ato.meshes.Length - 1 - ps2l.meshesRead - meshObjects.Count]); } } if (meshObjects.Count > 0) { int currentSubblock = 0; int curNumVertices = 0; bool tryMapping = true; if (m.vertices == null) { m.num_vertices = (ushort)meshObjects.Sum(mesh => mesh.num_vertices); m.vertices = new Vector3[m.num_vertices]; m.normals = new Vector3[m.num_vertices]; tryMapping = false; } for (int i = 0; i < meshObjects.Count; i++) { GeometricObject mo = meshObjects[i]; while (currentSubblock < m.num_elements && m.element_types[currentSubblock] != 1) { currentSubblock++; } GeometricObjectElementTriangles me = (GeometricObjectElementTriangles)m.elements[currentSubblock]; GeometricObjectElementTriangles moe = ((GeometricObjectElementTriangles)mo.elements[0]); if (!tryMapping) { Array.Copy(mo.vertices, 0, m.vertices, curNumVertices, mo.num_vertices); if (mo.normals != null) { Array.Copy(mo.normals, 0, m.normals, curNumVertices, mo.num_vertices); } me.OPT_mapping_vertices = Enumerable.Range(curNumVertices, mo.num_vertices).ToArray(); curNumVertices += mo.num_vertices; } else { me.OPT_mapping_vertices = new int[moe.OPT_num_mapping_entries]; for (int j = 0; j < mo.vertices.Length; j++) { me.OPT_mapping_vertices[j] = Array.IndexOf(m.vertices, mo.vertices[j]); if (me.OPT_mapping_vertices[j] == -1 || me.OPT_mapping_vertices[j] != Array.IndexOf(m.vertices, mo.vertices[j])) { Debug.LogError("Failed matching vertices between Renderware and OpenSpace"); } } } me.OPT_disconnectedTriangles = moe.OPT_disconnectedTriangles; me.OPT_num_disconnectedTriangles = moe.OPT_num_disconnectedTriangles; me.triangles = null; me.num_triangles = 0; me.num_uvMaps = moe.num_uvMaps; me.num_uvs = moe.num_uvs; me.uvs = moe.uvs; me.OPT_mapping_uvs = moe.OPT_mapping_uvs; me.OPT_num_mapping_entries = moe.OPT_num_mapping_entries; me.vertexColors = moe.vertexColors; currentSubblock++; if (me.lightmap_index != -1) { R2PS2Loader ps2l = ((R2PS2Loader)l); string id_r = me.lightmap_index.ToString("D3") + "." + 0; //l.print(id_r + " - " + me.num_disconnected_triangles + " - " + m.num_vertices + " - " + mo.num_vertices); Texture2D lm = ps2l.GetLightmap(id_r); if (me.visualMaterial != null) { if (lm != null) { string id_g = me.lightmap_index.ToString("D3") + "." + 1; string id_b = me.lightmap_index.ToString("D3") + "." + 2; Texture2D lm_g = ps2l.GetLightmap(id_g); Texture2D lm_b = ps2l.GetLightmap(id_b); if (lm_g != null && lm_b != null) { for (int j = 0; j < lm.width; j++) { for (int k = 0; k < lm.height; k++) { Color r = lm.GetPixel(j, k); Color g = lm_g.GetPixel(j, k); Color b = lm_b.GetPixel(j, k); lm.SetPixel(j, k, new Color(r.a, g.a, b.a, 1f)); } } lm.Apply(); } } else { lm = new Texture2D(1, 1); lm.SetPixel(0, 0, Color.white); lm.wrapMode = TextureWrapMode.Clamp; lm.filterMode = FilterMode.Bilinear; lm.Apply(); } Vector2[] lightmapUVs = new Vector2[mo.num_vertices]; Pointer.DoAt(ref reader, ps2l.off_lightmapUV[me.lightmap_index], () => { for (int j = 0; j < mo.num_vertices; j++) { lightmapUVs[j] = new Vector2(reader.ReadSingle(), reader.ReadSingle()); } }); me.AddLightmap(lm, lightmapUVs); } } } ((R2PS2Loader)l).meshesRead += (uint)meshObjects.Count; } } }
public static GeometricObjectElementTriangles Read(Reader reader, Pointer offset, GeometricObject geo) { MapLoader l = MapLoader.Loader; GeometricObjectElementTriangles sm = new GeometricObjectElementTriangles(offset, geo); sm.name = "Submesh @ pos " + offset; //l.print(sm.name); sm.backfaceCulling = !l.forceDisplayBackfaces; sm.off_material = Pointer.Read(reader); if (Settings.s.game == Settings.Game.LargoWinch) { //sm.visualMaterial = VisualMaterial.FromOffset(sm.off_material); sm.visualMaterial = VisualMaterial.FromOffsetOrRead(sm.off_material, reader); } else if (Settings.s.engineVersion == Settings.EngineVersion.R3 || Settings.s.game == Settings.Game.R2Revolution) { sm.visualMaterial = VisualMaterial.FromOffset(sm.off_material); } else { sm.gameMaterial = GameMaterial.FromOffsetOrRead(sm.off_material, reader); sm.visualMaterial = sm.gameMaterial.visualMaterial; } sm.visualMaterialOG = sm.visualMaterial; /*if (sm.visualMaterial != null && sm.visualMaterial.textures.Count > 0 && sm.visualMaterial.textures[0].off_texture != null) { * sm.name += " - VisMatTex:" + sm.visualMaterial.textures[0].offset + " - TexInfo:" + sm.visualMaterial.textures[0].off_texture; * }*/ if (sm.visualMaterial != null) { sm.backfaceCulling = ((sm.visualMaterial.flags & VisualMaterial.flags_backfaceCulling) != 0) && !l.forceDisplayBackfaces; } sm.num_triangles = reader.ReadUInt16(); if (Settings.s.game == Settings.Game.R2Revolution) { sm.lightmap_index = reader.ReadInt16(); sm.off_triangles = Pointer.Read(reader); } else { sm.num_uvs = reader.ReadUInt16(); if (Settings.s.engineVersion == Settings.EngineVersion.R3) { sm.num_uvMaps = reader.ReadUInt16(); sm.lightmap_index = reader.ReadInt16(); } sm.off_triangles = Pointer.Read(reader); // 1 entry = 3 shorts. Max: num_vertices if (Settings.s.mode == Settings.Mode.Rayman3GC) { reader.ReadUInt32(); } sm.off_mapping_uvs = Pointer.Read(reader); // 1 entry = 3 shorts. Max: num_weights sm.off_normals = Pointer.Read(reader); // 1 entry = 3 floats sm.off_uvs = Pointer.Read(reader); // 1 entry = 2 floats if (Settings.s.game == Settings.Game.LargoWinch) { sm.off_mapping_lightmap = Pointer.Read(reader); sm.num_mapping_lightmap = reader.ReadUInt16(); reader.ReadUInt16(); } else if (Settings.s.engineVersion == Settings.EngineVersion.R3) { reader.ReadUInt32(); reader.ReadUInt32(); } else if (Settings.s.engineVersion == Settings.EngineVersion.Montreal) { reader.ReadUInt32(); } if (Settings.s.game != Settings.Game.TTSE) { sm.off_vertex_indices = Pointer.Read(reader); sm.num_vertex_indices = reader.ReadUInt16(); sm.parallelBox = reader.ReadUInt16(); reader.ReadUInt32(); } } if (Settings.s.engineVersion == Settings.EngineVersion.R3) { if (Settings.s.game != Settings.Game.Dinosaur && Settings.s.game != Settings.Game.LargoWinch && Settings.s.mode != Settings.Mode.RaymanArenaGCDemo) { sm.isVisibleInPortal = reader.ReadByte(); reader.ReadByte(); sm.OPT_num_mapping_entries = reader.ReadUInt16(); // num_shorts sm.OPT_off_mapping_vertices = Pointer.Read(reader); // shorts_offset1 (1st array of size num_shorts, max_num_vertices) sm.OPT_off_mapping_uvs = Pointer.Read(reader); // shorts_offset2 (2nd array of size num_shorts, max: num_weights) sm.OPT_num_triangleStrip = reader.ReadUInt16(); // num_shorts2 sm.OPT_num_disconnectedTriangles = reader.ReadUInt16(); sm.OPT_off_triangleStrip = Pointer.Read(reader); // shorts2_offset (array of size num_shorts2) sm.OPT_off_disconnectedTriangles = Pointer.Read(reader); if (Settings.s.hasNames) { sm.name += reader.ReadString(0x34); } } else { sm.OPT_num_mapping_entries = 0; sm.OPT_off_mapping_vertices = null; sm.OPT_off_mapping_uvs = null; sm.OPT_num_triangleStrip = 0; sm.OPT_num_disconnectedTriangles = 0; sm.OPT_off_triangleStrip = null; sm.OPT_off_disconnectedTriangles = null; sm.isVisibleInPortal = 1; if (Settings.s.mode == Settings.Mode.RaymanArenaGCDemo) { sm.isVisibleInPortal = reader.ReadByte(); reader.ReadByte(); sm.OPT_num_mapping_entries = reader.ReadUInt16(); // num_shorts } } } else { // Defaults for Rayman 2, no optimized mesh feature sm.num_uvMaps = 1; sm.OPT_num_mapping_entries = 0; sm.OPT_off_mapping_vertices = null; sm.OPT_off_mapping_uvs = null; sm.OPT_num_triangleStrip = 0; sm.OPT_num_disconnectedTriangles = 0; sm.OPT_off_triangleStrip = null; sm.OPT_off_disconnectedTriangles = null; sm.isVisibleInPortal = 1; } // Read mapping tables sm.OPT_mapping_uvs = new int[sm.num_uvMaps][]; if (sm.OPT_num_mapping_entries > 0) { Pointer.Goto(ref reader, sm.OPT_off_mapping_vertices); //print("Mapping offset: " + String.Format("0x{0:X}", fs.Position)); sm.OPT_mapping_vertices = new int[sm.OPT_num_mapping_entries]; for (int j = 0; j < sm.OPT_num_mapping_entries; j++) { sm.OPT_mapping_vertices[j] = reader.ReadInt16(); } Pointer.Goto(ref reader, sm.OPT_off_mapping_uvs); for (int j = 0; j < sm.num_uvMaps; j++) { sm.OPT_mapping_uvs[j] = new int[sm.OPT_num_mapping_entries]; } for (int j = 0; j < sm.OPT_num_mapping_entries; j++) { for (int um = 0; um < sm.num_uvMaps; um++) { sm.OPT_mapping_uvs[um][j] = reader.ReadInt16(); } } } if (sm.num_triangles > 0) { Pointer.Goto(ref reader, sm.off_mapping_uvs); sm.mapping_uvs = new int[sm.num_uvMaps][]; for (int j = 0; j < sm.num_uvMaps; j++) { sm.mapping_uvs[j] = new int[sm.num_triangles * 3]; } // Why is uv maps here the outer loop instead of inner like the other thing? for (int um = 0; um < sm.num_uvMaps; um++) { for (int j = 0; j < sm.num_triangles * 3; j++) { sm.mapping_uvs[um][j] = reader.ReadInt16(); } } } // Read UVs Pointer.DoAt(ref reader, sm.off_uvs, () => { sm.uvs = new Vector2[sm.num_uvs]; for (int j = 0; j < sm.num_uvs; j++) { sm.uvs[j] = new Vector2(reader.ReadSingle(), reader.ReadSingle()); } }); // Read triangle data Pointer.DoAt(ref reader, sm.OPT_off_triangleStrip, () => { sm.OPT_triangleStrip = new int[sm.OPT_num_triangleStrip]; for (int j = 0; j < sm.OPT_num_triangleStrip; j++) { sm.OPT_triangleStrip[j] = reader.ReadInt16(); } }); Pointer.DoAt(ref reader, sm.OPT_off_disconnectedTriangles, () => { sm.OPT_disconnectedTriangles = new int[sm.OPT_num_disconnectedTriangles * 3]; //print("Loading disconnected triangles at " + String.Format("0x{0:X}", fs.Position)); for (int j = 0; j < sm.OPT_num_disconnectedTriangles; j++) { sm.OPT_disconnectedTriangles[(j * 3) + 0] = reader.ReadInt16(); sm.OPT_disconnectedTriangles[(j * 3) + 1] = reader.ReadInt16(); sm.OPT_disconnectedTriangles[(j * 3) + 2] = reader.ReadInt16(); } }); if (sm.num_triangles > 0) { Pointer.Goto(ref reader, sm.off_triangles); sm.triangles = new int[sm.num_triangles * 3]; //print("Loading disconnected triangles at " + String.Format("0x{0:X}", fs.Position)); for (int j = 0; j < sm.num_triangles; j++) { sm.triangles[(j * 3) + 0] = reader.ReadInt16(); sm.triangles[(j * 3) + 1] = reader.ReadInt16(); sm.triangles[(j * 3) + 2] = reader.ReadInt16(); } if (sm.off_normals != null) { Pointer.Goto(ref reader, sm.off_normals); sm.normals = new Vector3[sm.num_triangles]; for (int j = 0; j < sm.num_triangles; j++) { float x = reader.ReadSingle(); float z = reader.ReadSingle(); float y = reader.ReadSingle(); sm.normals[j] = new Vector3(x, y, z); } } } if (Settings.s.game == Settings.Game.LargoWinch && sm.lightmap_index != -1) { LWLoader lwl = MapLoader.Loader as LWLoader; if (lwl.lms != null && sm.lightmap_index >= 0 && sm.lightmap_index < lwl.lms.Count) { /*if (sm.lightmap_index < l.off_lightmapUV.Length - 1) { * int amount = ((int)l.off_lightmapUV[sm.lightmap_index + 1].offset - (int)l.off_lightmapUV[sm.lightmap_index].offset); * amount = amount / 8; * l.print(offset + " - UVs: " + amount + " - " + sm.mesh.num_vertices + " - " + sm.num_mapping_entries + " - " + sm.num_uvs + " - " + sm.num_disconnected_triangles_spe + " - " + sm.num_mapping_lightmap); * }*/ Vector2[] lightmapUVs = new Vector2[sm.num_mapping_lightmap]; Pointer.DoAt(ref reader, sm.off_mapping_lightmap, () => { sm.mapping_lightmap = new int[sm.num_mapping_lightmap]; for (int i = 0; i < sm.num_mapping_lightmap; i++) { sm.mapping_lightmap[i] = reader.ReadInt16(); } }); Pointer.DoAt(ref reader, l.off_lightmapUV[sm.lightmap_index], () => { for (int j = 0; j < lightmapUVs.Length; j++) { lightmapUVs[j] = new Vector2(reader.ReadSingle(), reader.ReadSingle()); } }); sm.AddLightmap(lwl.GetLightmap(sm.lightmap_index), lightmapUVs); } } return(sm); }