Skip to content

Commit 1a04a8b

Browse files
committed
several changes to meshing, etc
1 parent d82a402 commit 1a04a8b

14 files changed

Lines changed: 1524 additions & 1592 deletions

File tree

CSJ2K/J2kImage.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939
namespace CSJ2K
4040
{
41-
4241
public class J2kImage
4342
{
4443

OpenMetaverse.Rendering.Meshmerizer/MeshmerizerR.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,15 @@ public OMVR.SimpleMesh GenerateSimpleMesh(OMV.Primitive prim, OMVR.DetailLevel l
5656
if (newPrim == null)
5757
return null;
5858

59-
SimpleMesh mesh = new SimpleMesh();
60-
mesh.Path = new Path();
61-
mesh.Prim = prim;
62-
mesh.Profile = new Profile();
63-
mesh.Vertices = new List<Vertex>(newPrim.coords.Count);
64-
for (int i = 0; i < newPrim.coords.Count; i++)
59+
SimpleMesh mesh = new()
6560
{
66-
PrimMesher.Coord c = newPrim.coords[i];
67-
mesh.Vertices.Add(new Vertex { Position = new Vector3(c.X, c.Y, c.Z) });
68-
}
61+
Path = new Path(),
62+
Prim = prim,
63+
Profile = new Profile(),
64+
Vertices = new List<Vertex>(newPrim.coords.Count)
65+
};
66+
for (int i = 0; i < newPrim.coords.Count; i++)
67+
mesh.Vertices.Add(new Vertex { Position = newPrim.coords[i] });
6968

7069
mesh.Indices = new List<ushort>(newPrim.faces.Count * 3);
7170
for (int i = 0; i < newPrim.faces.Count; i++)
@@ -117,35 +116,36 @@ public OMVR.SimpleMesh GenerateSimpleSculptMesh(OMV.Primitive prim, System.Drawi
117116
/// <returns>The generated mesh</returns >
118117
public OMVR.FacetedMesh GenerateFacetedMesh(OMV.Primitive prim, OMVR.DetailLevel lod)
119118
{
120-
bool isSphere = ((OMV.ProfileCurve)(prim.PrimData.profileCurve & 0x07) == OMV.ProfileCurve.HalfCircle);
121119
PrimMesher.PrimMesh newPrim = GeneratePrimMesh(prim, lod, true);
122120
if (newPrim == null)
123121
return null;
124122

125123
// copy the vertex information into OMVR.IRendering structures
126-
OMVR.FacetedMesh omvrmesh = new OMVR.FacetedMesh();
127-
omvrmesh.Faces = new List<OMVR.Face>();
128-
omvrmesh.Prim = prim;
129-
omvrmesh.Profile = new OMVR.Profile();
130-
omvrmesh.Profile.Faces = new List<OMVR.ProfileFace>();
131-
omvrmesh.Profile.Positions = new List<OMV.Vector3>();
132-
omvrmesh.Path = new OMVR.Path();
133-
omvrmesh.Path.Points = new List<OMVR.PathPoint>();
124+
OMVR.FacetedMesh omvrmesh = new()
125+
{
126+
Faces = [],
127+
Prim = prim,
128+
Profile = new() { Faces = [], Positions = [] },
129+
Path = new() { Points = [] }
130+
};
131+
134132
var indexer = newPrim.GetVertexIndexer();
135133

136134
for (int i = 0; i < indexer.numPrimFaces; i++)
137135
{
138-
OMVR.Face oface = new OMVR.Face();
139-
oface.Vertices = new List<OMVR.Vertex>();
140-
oface.Indices = new List<ushort>();
141-
oface.TextureFace = prim.Textures.GetFace((uint)i);
136+
OMVR.Face oface = new()
137+
{
138+
Vertices = [],
139+
Indices = [],
140+
TextureFace = prim.Textures.GetFace((uint)i)
141+
};
142142

143143
for (int j = 0; j < indexer.viewerVertices[i].Count; j++)
144144
{
145145
var vert = new OMVR.Vertex();
146146
var m = indexer.viewerVertices[i][j];
147-
vert.Position = new Vector3(m.v.X, m.v.Y, m.v.Z);
148-
vert.Normal = new Vector3(m.n.X, m.n.Y, m.n.Z);
147+
vert.Position = m.v;
148+
vert.Normal = m.n;
149149
vert.TexCoord = new OMV.Vector2(m.uv.U, 1.0f - m.uv.V);
150150
oface.Vertices.Add(vert);
151151
}

OpenMetaverse/Assets/AssetTypes/AssetMesh.cs

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
*/
2626

2727
using System;
28+
using System.Buffers;
2829
using System.IO;
2930
using System.IO.Compression;
30-
using OpenMetaverse;
3131
using OpenMetaverse.StructuredData;
3232

3333
namespace OpenMetaverse.Assets
@@ -79,22 +79,38 @@ public override bool Decode()
7979

8080
foreach(string partName in header.Keys)
8181
{
82-
if (header[partName].Type != OSDType.Map)
82+
OSD partOSD = header[partName];
83+
if(partOSD is not OSDMap partInfo)
8384
{
84-
MeshData[partName] = header[partName];
85+
MeshData[partName] = partOSD;
8586
continue;
8687
}
8788

88-
OSDMap partInfo = (OSDMap)header[partName];
89-
if (partInfo["offset"] < 0 || partInfo["size"] == 0)
89+
if(!partInfo.TryGetInt("offset", out int partDataOffset))
9090
{
9191
MeshData[partName] = partInfo;
9292
continue;
9393
}
9494

95-
byte[] part = new byte[partInfo["size"]];
96-
Buffer.BlockCopy(AssetData, partInfo["offset"] + (int)start, part, 0, part.Length);
97-
MeshData[partName] = DecompressMeshOSD(part);
95+
partDataOffset += (int)start;
96+
if(partDataOffset > AssetData.Length)
97+
{
98+
MeshData[partName] = partInfo;
99+
continue;
100+
}
101+
102+
if(!partInfo.TryGetInt("size", out int partDataSize))
103+
{
104+
MeshData[partName] = partInfo;
105+
continue;
106+
}
107+
if(partDataOffset + partDataSize > AssetData.Length)
108+
{
109+
MeshData[partName] = partInfo;
110+
continue;
111+
}
112+
113+
MeshData[partName] = DecompressMeshOSD(AssetData, partDataOffset, partDataSize);
98114
}
99115
}
100116
return true;
@@ -106,34 +122,50 @@ public override bool Decode()
106122
}
107123
}
108124

109-
public static OSD DecompressMeshOSD(byte[] data)
125+
public static OSD DecodeBlock(byte[] MeshBytes, string BlockName)
110126
{
111-
OSD decodedOsd = null;
112-
113-
using (MemoryStream inMs = new MemoryStream(data))
127+
try
114128
{
115-
using (MemoryStream outMs = new MemoryStream())
116-
{
117-
using (DeflateStream decompressionStream = new DeflateStream(inMs, CompressionMode.Decompress))
118-
{
119-
byte[] readBuffer = new byte[2048];
120-
inMs.Read(readBuffer, 0, 2); // skip first 2 bytes in header
121-
int readLen = 0;
129+
using MemoryStream data = new(MeshBytes);
122130

123-
while ((readLen = decompressionStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
124-
outMs.Write(readBuffer, 0, readLen);
131+
OSDMap header = (OSDMap)OSDParser.DeserializeLLSDBinary(data);
132+
long start = data.Position;
125133

126-
outMs.Flush();
134+
if (!header.TryGetOSDMap(BlockName, out OSDMap LayerInfo))
135+
return null;
127136

128-
outMs.Seek(0, SeekOrigin.Begin);
137+
if (!LayerInfo.TryGetInt("offset", out int BlockDataOffset))
138+
return null;
129139

130-
byte[] decompressedBuf = outMs.GetBuffer();
140+
BlockDataOffset += (int)start;
141+
if (BlockDataOffset > MeshBytes.Length)
142+
return null;
131143

132-
decodedOsd = OSDParser.DeserializeLLSDBinary(decompressedBuf);
133-
}
134-
}
144+
if (!LayerInfo.TryGetInt("size", out int BlockDataSize))
145+
return null;
146+
147+
if (BlockDataOffset + BlockDataSize > MeshBytes.Length)
148+
return null;
149+
150+
return DecompressMeshOSD(MeshBytes, BlockDataOffset, BlockDataSize);
151+
}
152+
catch (Exception ex)
153+
{
154+
Logger.Log("Failed to decode mesh asset", Helpers.LogLevel.Error, ex);
155+
return null;
156+
}
157+
}
158+
159+
public static OSD DecompressMeshOSD(byte[] data, int start, int len)
160+
{
161+
using MemoryStream outMs = new(4 * len);
162+
using (MemoryStream inMs = new(data, start + 2, len - 2)) // skip first 2 bytes
163+
{
164+
using DeflateStream decompressionStream = new(inMs, CompressionMode.Decompress);
165+
decompressionStream.CopyTo(outMs);
135166
}
136-
return decodedOsd;
167+
outMs.Seek(0, SeekOrigin.Begin);
168+
return OSDParser.DeserializeLLSDBinary(outMs);
137169
}
138170
}
139171
}

OpenMetaverse/Imaging/ManagedImage.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public ManagedImage(int width, int height, ImageChannels channels)
116116
Bump = new byte[n];
117117
}
118118

119-
#if !NO_UNSAFE
120119
/// <summary>
121120
///
122121
/// </summary>
@@ -218,7 +217,6 @@ public ManagedImage(System.Drawing.Bitmap bitmap)
218217
throw new NotSupportedException("Unrecognized pixel format: " + bitmap.PixelFormat.ToString());
219218
}
220219
}
221-
#endif
222220

223221
/// <summary>
224222
/// Convert the channels in the image. Channels are created or destroyed as required.

OpenMetaverse/Imaging/OpenJPEG.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
using System;
2828
using System.Drawing;
2929
using System.Drawing.Imaging;
30-
using System.Reflection;
3130
using System.Runtime.InteropServices;
3231

3332
namespace OpenMetaverse.Imaging

0 commit comments

Comments
 (0)