public bool Process() { if (this.ContentState == Unity3DTileContentState.PROCESSING) { this.ContentState = Unity3DTileContentState.READY; this.Content.Initialize(this.Tileset.TilesetOptions.CreateColliders); var indexMode = this.Tileset.TilesetOptions.LoadIndices; if (indexMode != IndexMode.Default && indexMode != IndexMode.None) { Action <IndexMode, string, string> fail = (mode, url, msg) => { //we could log a warning here, but if indices are expected but not available //that might not actually be a true error condition //and this would spam the log //Debug.LogWarning("failed to load " + mode + " index for " + this.ContentUrl + ": " + msg); }; Action <Unity3DTileIndex> success = index => { this.Content.Index = index; }; this.Tileset.Behaviour .StartCoroutine(Unity3DTileIndex.Load(indexMode, this.ContentUrl, success, fail)); } return(true); } return(false); }
public bool Process() { if (ContentState == Unity3DTileContentState.PROCESSING) { ContentState = Unity3DTileContentState.READY; Content.SetShadowMode(Tileset.TilesetOptions.ShadowCastingMode, Tileset.TilesetOptions.RecieveShadows); Content.Initialize(Tileset.TilesetOptions.CreateColliders); var indexMode = Tileset.TilesetOptions.LoadIndices; if (indexMode != IndexMode.Default && indexMode != IndexMode.None) { Action <IndexMode, string, string> fail = (mode, url, msg) => { //we could log a warning here, but if indices are expected but not available //that might not actually be a true error condition //and this would spam the log if (Unity3DTileIndex.EnableLoadWarnings) { #pragma warning disable 0162 Debug.LogWarning("failed to load " + mode + " index for " + ContentUrl + ": " + msg); #pragma warning restore 0162 } }; Action <Unity3DTileIndex> success = index => { Content.Index = index; }; Tileset.Behaviour.StartCoroutine(Unity3DTileIndex.Load(indexMode, ContentUrl, success, fail)); } return(true); } return(false); }
public static Unity3DTileIndex LoadFromPNG(Stream stream) { #if IMAGE_SHARP_PNG //requires extra dependency DLLs which bloat the webgl build using (var png = SixLabors.ImageSharp.Image.Load <Rgba64>(stream)) var index = new Unity3DTileIndex(png.Width, png.Height); { for (int r = 0; r < png.Height; r++) { for (int c = 0; c < png.Width; c++) { var pixel = png[c, r]; index[0, r, c] = pixel.R; index[1, r, c] = pixel.G; index[2, r, c] = pixel.B; } } } return(index); #elif PNGCS_PNG var png = new PngReader(stream); png.SetUnpackedMode(true); var info = png.ImgInfo; if (info.Channels != 3) { throw new Exception("expected 3 channel PNG, got " + info.Channels); } var index = new Unity3DTileIndex(info.Cols, info.Rows); var buf = new int[3 * info.Cols]; for (int r = 0; r < info.Rows; r++) { png.ReadRow(buf, r); for (int c = 0; c < info.Cols; c++) { index[0, r, c] = (uint)buf[3 * c + 0]; index[1, r, c] = (uint)buf[3 * c + 1]; index[2, r, c] = (uint)buf[3 * c + 2]; } } png.End(); return(index); #else return(null); #endif }
//http://netpbm.sourceforge.net/doc/ppm.html public static Unity3DTileIndex LoadFromPPM(Stream stream, bool compressed) { if (compressed) { stream = new GZipStream(stream, CompressionMode.Decompress); } using (var br = new BinaryReader(stream)) { char readChar() { int b = br.Read(); if (b < 0) { throw new Exception("unexpected EOF parsing PPM header"); } return((char)b); } char ch = readChar(); string readToken(bool eatWhitespace = true) { var sb = new StringBuilder(); bool ignoring = false; string tok = null; for (int i = 0; i < 1000; i++) { if ((!ignoring && char.IsWhiteSpace(ch)) || (ignoring && (ch == '\r' || ch == '\n'))) { tok = sb.ToString(); break; } else if (ch == '#') { ignoring = true; } else { sb.Append(ch); } ch = readChar(); } for (int i = 0; eatWhitespace && char.IsWhiteSpace(ch) && i < 1000; i++) { ch = readChar(); } return(tok); } string f = readToken(); if (f != "P6") { throw new Exception("unexpected PPM magic: " + f); } int width = 0, height = 0, maxVal = 0; string w = readToken(); if (w != null && !int.TryParse(w, out width)) { throw new Exception("error parsing PPM width: " + w); } string h = readToken(); if (h != null && !int.TryParse(h, out height)) { throw new Exception("error parsing PPM height: " + h); } string m = readToken(eatWhitespace: false); if (m != null && !int.TryParse(m, out maxVal)) { throw new Exception("unexpected PPM max val: " + m); } if (maxVal <= 0 || maxVal > 65535) { throw new Exception("max value must be in range 1-65535, got: " + maxVal); } var index = new Unity3DTileIndex(width, height); int bytesPerVal = maxVal < 256 ? 1 : 2; for (int r = 0; r < height; r++) { for (int c = 0; c < width; c++) { for (int b = 0; b < 3; b++) { byte[] bytes = br.ReadBytes(bytesPerVal); //PPM data is in network byte order (big endian) if (BitConverter.IsLittleEndian) { Array.Reverse(bytes); } if (bytes.Length < bytesPerVal) { throw new Exception($"unexpected EOF at PPM row={r} of {height}, col={c} of {width}"); } index[b, r, c] = bytesPerVal == 2 ? BitConverter.ToUInt16(bytes, 0) : (ushort)bytes[0]; } } } return(index); } }