public void GetPlanetMaps(string folder, MyModContext context, MyPlanetMaps mapsToUse, out MyCubemap[] maps) { if (m_planetMaps.ContainsKey(folder)) { maps = m_planetMaps[folder]; return; } maps = new MyCubemap[4]; MyCubemapData<byte>[] tmpMaps = new MyCubemapData<byte>[4 * 6]; byte[][] streams = new byte[4][]; string fullPath; ProfilerShort.Begin("MyHeightmapLoadingSystem::GetPlanetMaps()"); ProfilerShort.Begin("Load _mat"); // Round one: material, ore, biome if (mapsToUse.Material || mapsToUse.Biome || mapsToUse.Ores) for (int i = 0; i < 6; ++i) { string name = Path.Combine(folder, MyCubemapHelpers.GetNameForFace(i)); using (var texture = TryGetPlanetTexture(name, context, "_mat", out fullPath)) { if (texture == null) { ClearMatValues(tmpMaps); break; } PixelBuffer buffer = texture.GetPixelBuffer(0, 0, 0); if (buffer.Format != Format.B8G8R8A8_UNorm && buffer.Format != Format.R8G8B8A8_UNorm) { MyDebug.FailRelease("While loading maps from {1}: Unsupported planet map format: {0}.", buffer.Format, fullPath); break; } if (buffer.Width != buffer.Height) { MyDebug.FailRelease("While loading maps from {0}: Width and height must be the same.", fullPath); break; } if (mapsToUse.Material) { tmpMaps[i * 4] = new MyCubemapData<byte>(buffer.Width); streams[0] = tmpMaps[i * 4].Data; } if (mapsToUse.Biome) { tmpMaps[i * 4 + 1] = new MyCubemapData<byte>(buffer.Width); streams[1] = tmpMaps[i * 4 + 1].Data; } if (mapsToUse.Ores) { tmpMaps[i * 4 + 2] = new MyCubemapData<byte>(buffer.Width); streams[2] = tmpMaps[i * 4 + 2].Data; } // Invert channels for BGRA if (buffer.Format == Format.B8G8R8A8_UNorm) { var tmp = streams[2]; streams[2] = streams[0]; streams[0] = tmp; } ReadChannelsFromImage(streams, buffer); texture.Dispose(); } } ProfilerShort.BeginNextBlock("Load _add"); // round two: add map if (mapsToUse.Occlusion) for (int i = 0; i < 6; ++i) { string name = Path.Combine(folder, MyCubemapHelpers.GetNameForFace(i)); using (var texture = TryGetPlanetTexture(name, context,"_add", out fullPath)) { if (texture == null) { ClearAddValues(tmpMaps); break; } PixelBuffer buffer = texture.GetPixelBuffer(0, 0, 0); if (buffer.Format != Format.B8G8R8A8_UNorm && buffer.Format != Format.R8G8B8A8_UNorm) { MyDebug.FailRelease("While loading maps from {1}: Unsupported planet map format: {0}.", buffer.Format, fullPath); break; } if (buffer.Width != buffer.Height) { MyDebug.FailRelease("While loading maps from {0}: Width and height must be the same.", fullPath); break; } if (mapsToUse.Occlusion) { tmpMaps[i * 4 + 3] = new MyCubemapData<byte>(buffer.Width); streams[0] = tmpMaps[i * 4 + 3].Data; } streams[1] = streams[2] = null; // Invert channels for BGRA if (buffer.Format == Format.B8G8R8A8_UNorm) { var tmp = streams[2]; streams[2] = streams[0]; streams[0] = tmp; } ReadChannelsFromImage(streams, buffer); texture.Dispose(); } } ProfilerShort.BeginNextBlock("Finish"); for (int i = 0; i < 4; ++i) { if (tmpMaps[i] != null) { var cmaps = new MyCubemapData<byte>[6]; for (int j = 0; j < 6; j++) { cmaps[j] = tmpMaps[i + j * 4]; } maps[i] = new MyCubemap(cmaps); } } m_planetMaps[folder] = maps; ProfilerShort.End(); ProfilerShort.End(); }
private void ClearAddValues(MyCubemapData<byte>[] maps) { for (int i = 0; i < 6; ++i) { maps[i * 4 + 3] = null; } }
private void ClearMatValues(MyCubemapData<byte>[] maps) { for (int i = 0; i < 6; ++i) { maps[i * 4] = null; maps[i * 4 + 1] = null; maps[i * 4 + 2] = null; } }
private unsafe byte ComputeMapBlend(Vector2 coords, int face, ref MapBlendCache cache, MyCubemapData<byte> map) { coords = coords * map.Resolution - .5f; Vector2I isample = new Vector2I(coords); if (cache.HashCode != m_hashCode || cache.Face != face || cache.Cell != isample) { byte tl, tr, bl, br; cache.HashCode = m_hashCode; cache.Cell = isample; cache.Face = (byte)face; // Biome if (m_materialMap != null) { map.GetValue(isample.X, isample.Y, out tl); map.GetValue(isample.X + 1, isample.Y, out tr); map.GetValue(isample.X, isample.Y + 1, out bl); map.GetValue(isample.X + 1, isample.Y + 1, out br); byte* ss = stackalloc byte[4]; ss[0] = tl; ss[1] = tr; ss[2] = bl; ss[3] = br; if (tl == tr && bl == br && bl == tl) { fixed (ushort* smpls = cache.Data) { smpls[0] = (ushort)((tl << 8) | 0xF); smpls[1] = 0; smpls[2] = 0; smpls[3] = 0; } } else { fixed (ushort* smpls = cache.Data) { Sort4(ss); ComputeTilePattern(tl, tr, bl, br, ss, smpls); } } } } byte value; fixed (ushort* smpls = cache.Data) { coords -= Vector2.Floor(coords); if (coords.X == 1) coords.X = .99999f; if (coords.Y == 1) coords.Y = .99999f; SampleTile(smpls, ref coords, out value); } return value; }
public void GetPlanetMaps(string folder, MyModContext context, MyPlanetMaps mapsToUse, out MyCubemap[] maps) { if (m_planetMaps.ContainsKey(folder)) { maps = m_planetMaps[folder]; return; } maps = new MyCubemap[4]; MyCubemapData <byte>[] tmpMaps = new MyCubemapData <byte> [4 * 6]; byte[][] streams = new byte[4][]; string fullPath; ProfilerShort.Begin("MyHeightmapLoadingSystem::GetPlanetMaps()"); ProfilerShort.Begin("Load _mat"); // Round one: material, ore, biome if (mapsToUse.Material || mapsToUse.Biome || mapsToUse.Ores) { for (int i = 0; i < 6; ++i) { string name = Path.Combine(folder, MyCubemapHelpers.GetNameForFace(i)); using (var texture = TryGetPlanetTexture(name, context, "_mat", out fullPath)) { if (texture == null) { ClearMatValues(tmpMaps); break; } PixelBuffer buffer = texture.GetPixelBuffer(0, 0, 0); if (buffer.Format != Format.B8G8R8A8_UNorm && buffer.Format != Format.R8G8B8A8_UNorm) { MyDebug.FailRelease("While loading maps from {1}: Unsupported planet map format: {0}.", buffer.Format, fullPath); break; } if (buffer.Width != buffer.Height) { MyDebug.FailRelease("While loading maps from {0}: Width and height must be the same.", fullPath); break; } if (mapsToUse.Material) { tmpMaps[i * 4] = new MyCubemapData <byte>(buffer.Width); streams[0] = tmpMaps[i * 4].Data; } if (mapsToUse.Biome) { tmpMaps[i * 4 + 1] = new MyCubemapData <byte>(buffer.Width); streams[1] = tmpMaps[i * 4 + 1].Data; } if (mapsToUse.Ores) { tmpMaps[i * 4 + 2] = new MyCubemapData <byte>(buffer.Width); streams[2] = tmpMaps[i * 4 + 2].Data; } // Invert channels for BGRA if (buffer.Format == Format.B8G8R8A8_UNorm) { var tmp = streams[2]; streams[2] = streams[0]; streams[0] = tmp; } ReadChannelsFromImage(streams, buffer); } } } ProfilerShort.BeginNextBlock("Load _add"); // round two: add map if (mapsToUse.Occlusion) { for (int i = 0; i < 6; ++i) { string name = Path.Combine(folder, MyCubemapHelpers.GetNameForFace(i)); using (var texture = TryGetPlanetTexture(name, context, "_add", out fullPath)) { if (texture == null) { ClearAddValues(tmpMaps); break; } PixelBuffer buffer = texture.GetPixelBuffer(0, 0, 0); if (buffer.Format != Format.B8G8R8A8_UNorm && buffer.Format != Format.R8G8B8A8_UNorm) { MyDebug.FailRelease("While loading maps from {1}: Unsupported planet map format: {0}.", buffer.Format, fullPath); break; } if (buffer.Width != buffer.Height) { MyDebug.FailRelease("While loading maps from {0}: Width and height must be the same.", fullPath); break; } if (mapsToUse.Occlusion) { tmpMaps[i * 4 + 3] = new MyCubemapData <byte>(buffer.Width); streams[0] = tmpMaps[i * 4 + 3].Data; } streams[1] = streams[2] = null; // Invert channels for BGRA if (buffer.Format == Format.B8G8R8A8_UNorm) { var tmp = streams[2]; streams[2] = streams[0]; streams[0] = tmp; } ReadChannelsFromImage(streams, buffer); } } } ProfilerShort.BeginNextBlock("Finish"); for (int i = 0; i < 4; ++i) { if (tmpMaps[i] != null) { var cmaps = new MyCubemapData <byte> [6]; for (int j = 0; j < 6; j++) { cmaps[j] = tmpMaps[i + j * 4]; } maps[i] = new MyCubemap(cmaps); } } m_planetMaps[folder] = maps; ProfilerShort.End(); ProfilerShort.End(); }
public void GetPlanetMaps(string folder, MyModContext context, MyPlanetMapTypeSet mapsToUse, out MyCubemap[] maps) { int num; int num2; maps = new MyCubemap[4]; MyCubemapData <byte>[] dataArray = new MyCubemapData <byte> [0x18]; byte[][] streams = new byte[4][]; if (mapsToUse == 0) { goto TR_000A; } else { num = 0; } goto TR_0026; TR_000A: num2 = 0; while (num2 < 4) { if (dataArray[num2] != null) { MyCubemapData <byte>[] faces = new MyCubemapData <byte> [6]; int index = 0; while (true) { if (index >= 6) { maps[num2] = new MyCubemap(faces); break; } faces[index] = dataArray[num2 + (index * 4)]; index++; } } num2++; } return; TR_000D: num++; TR_0026: while (true) { if (num >= 6) { break; } string name = Path.Combine(folder, MyCubemapHelpers.GetNameForFace(num)); try { string str; Image image = this.TryGetPlanetTexture(name, context, "_mat", out str); if (image == null) { this.ClearMatValues(dataArray); break; } using (image) { PixelBuffer buffer = image.GetPixelBuffer(0, 0, 0); if ((buffer.Format != Format.B8G8R8A8_UNorm) && (buffer.Format != Format.R8G8B8A8_UNorm)) { object[] args = new object[] { buffer.Format, str }; MyLog.Default.Error("While loading maps from {1}: Unsupported planet map format: {0}.", args); break; } if (buffer.Width == buffer.Height) { if (mapsToUse.HasFlag(MyPlanetMapTypeSet.Material)) { dataArray[num * 4] = new MyCubemapData <byte>(buffer.Width, null); streams[0] = dataArray[num * 4].Data; } if (mapsToUse.HasFlag(MyPlanetMapTypeSet.Biome)) { dataArray[(num * 4) + 1] = new MyCubemapData <byte>(buffer.Width, null); streams[1] = dataArray[(num * 4) + 1].Data; } if (mapsToUse.HasFlag(MyPlanetMapTypeSet.Ore)) { dataArray[(num * 4) + 2] = new MyCubemapData <byte>(buffer.Width, null); streams[2] = dataArray[(num * 4) + 2].Data; } if (buffer.Format == Format.B8G8R8A8_UNorm) { streams[2] = streams[0]; streams[0] = streams[2]; } this.ReadChannelsFromImage(streams, buffer); image.Dispose(); goto TR_000D; } else { object[] args = new object[] { str }; MyLog.Default.Error("While loading maps from {0}: Width and height must be the same.", args); } break; } } catch (Exception exception) { MyLog.Default.Error(exception.ToString(), Array.Empty <object>()); break; } goto TR_000D; } goto TR_000A; }