public UUID AddNewMaterial(FaceMaterial fm) { if (fm.DiffuseAlphaMode == 1 && fm.NormalMapID == UUID.Zero && fm.SpecularMapID == UUID.Zero) { fm.ID = UUID.Zero; return(UUID.Zero); } fm.genID(); UUID id = fm.ID; lock (materialslock) { if (m_Materials.ContainsKey(id)) { m_MaterialsRefCount[id]++; } else { m_Materials[id] = fm; m_MaterialsRefCount[id] = 1; m_changed[fm] = Util.GetTimeStamp(); } } return(id); }
private void RemoveMaterialInFace(Primitive.TextureEntryFace face) { UUID id = face.MaterialID; if (id == UUID.Zero) { return; } lock (materialslock) { if (!m_Materials.ContainsKey(id)) { return; } else { m_MaterialsRefCount[id]--; if (m_MaterialsRefCount[id] <= 0) { FaceMaterial oldFaceMat = m_Materials[id]; m_changed.Remove(oldFaceMat); m_Materials.Remove(id); m_MaterialsRefCount.Remove(id); m_cache.Expire(id.ToString()); } } } }
public FaceMaterial GetMaterialCopy(UUID ID) { FaceMaterial fm = null; if (m_Materials.TryGetValue(ID, out fm)) { return(new FaceMaterial(fm)); } return(null); }
private AssetBase MakeAsset(FaceMaterial fm, bool local) { // this are not true assets, should had never been... AssetBase asset = null; byte[] data = fm.toLLSDxml(); asset = new AssetBase(fm.ID, "llmaterial", (sbyte)OpenSimAssetType.Material, "00000000-0000-0000-0000-000000000000"); asset.Data = data; asset.Local = local; return(asset); }
private AssetBase MakeAsset(FaceMaterial fm, bool local) { // this are not true assets, should had never been... AssetBase asset = null; string txt = fm.toLLSDxml(); byte[] data = System.Text.Encoding.ASCII.GetBytes(txt); asset = new AssetBase(fm.ID, "llmaterial", (sbyte)OpenSimAssetType.Material, "00000000-0000-0000-0000-000000000000"); asset.Data = data; asset.Local = local; return(asset); }
/// <summary> /// Read face materials. /// </summary> /// <param name="reader"> /// The reader. /// </param> /// <param name="msize"> /// The msize. /// </param> /// <returns> /// The materials. /// </returns> private List <FaceMaterial> ReadFaceMaterials(BinaryReader reader, int msize) { int total = 6; var list = new List <FaceMaterial>(); while (total < msize) { var id = this.ReadChunkId(reader); int size = this.ReadChunkSize(reader); total += size; switch (id) { case ChunkID.TRI_FACEMAT: { string name = this.ReadString(reader); int n = reader.ReadUInt16(); var c = new Int32Collection(); for (int i = 0; i < n; i++) { c.Add(reader.ReadUInt16()); } var fm = new FaceMaterial { Name = name, Faces = c }; list.Add(fm); break; } case ChunkID.TRI_SMOOTH: { this.ReadData(reader, size - 6); break; } default: { this.ReadData(reader, size - 6); break; } } } return(list); }
public void RemoveMaterial(UUID id) { if (id == UUID.Zero) { return; } lock (materialslock) { if (m_Materials.ContainsKey(id)) { m_MaterialsRefCount[id]--; if (m_MaterialsRefCount[id] == 0) { FaceMaterial fm = m_Materials[id]; m_changed.Remove(fm); delayedDelete.Enqueue(id); } } } }
public void RemoveMaterial(UUID id) { if (id == UUID.Zero) { return; } lock (materialslock) { if (m_Materials.ContainsKey(id)) { m_MaterialsRefCount[id]--; if (m_MaterialsRefCount[id] <= 0) { FaceMaterial fm = m_Materials[id]; m_changed.Remove(fm); m_Materials.Remove(id); m_MaterialsRefCount.Remove(id); m_cache.Expire(id.ToString()); } } } }
public void RenderMaterialsPutCap(IOSHttpRequest request, IOSHttpResponse response, UUID agentID) { OSDMap req; try { req = (OSDMap)OSDParser.DeserializeLLSDXml(request.InputStream); } catch { response.StatusCode = (int)HttpStatusCode.BadRequest; return; } OSDMap materialsFromViewer = null; OSDArray respArr = new OSDArray(); OSD tmpOSD; HashSet <SceneObjectPart> parts = new HashSet <SceneObjectPart>(); if (req.TryGetValue("Zipped", out tmpOSD)) { OSD osd = null; byte[] inBytes = tmpOSD.AsBinary(); try { osd = ZDecompressBytesToOsd(inBytes); if (osd != null && osd is OSDMap) { materialsFromViewer = osd as OSDMap; if (materialsFromViewer.TryGetValue("FullMaterialsPerFace", out tmpOSD) && (tmpOSD is OSDArray)) { OSDArray matsArr = tmpOSD as OSDArray; try { foreach (OSDMap matsMap in matsArr) { uint primLocalID = 0; try { primLocalID = matsMap["ID"].AsUInteger(); } catch (Exception e) { m_log.Warn("[Materials]: cannot decode \"ID\" from matsMap: " + e.Message); continue; } SceneObjectPart sop = m_scene.GetSceneObjectPart(primLocalID); if (sop == null) { m_log.WarnFormat("[Materials]: SOP not found for localId: {0}", primLocalID.ToString()); continue; } if (!m_scene.Permissions.CanEditObject(sop.UUID, agentID)) { m_log.WarnFormat("User {0} can't edit object {1} {2}", agentID, sop.Name, sop.UUID); continue; } OSDMap mat = null; try { mat = matsMap["Material"] as OSDMap; } catch (Exception e) { m_log.Warn("[Materials]: cannot decode \"Material\" from matsMap: " + e.Message); continue; } Primitive.TextureEntry te = new Primitive.TextureEntry(sop.Shape.TextureEntry, 0, sop.Shape.TextureEntry.Length); if (te == null) { m_log.WarnFormat("[Materials]: Error in TextureEntry for SOP {0} {1}", sop.Name, sop.UUID); continue; } int face = -1; UUID oldid = UUID.Zero; Primitive.TextureEntryFace faceEntry = null; if (matsMap.TryGetValue("Face", out tmpOSD)) { face = tmpOSD.AsInteger(); faceEntry = te.CreateFace((uint)face); } else { faceEntry = te.DefaultTexture; } if (faceEntry == null) { continue; } UUID id; FaceMaterial newFaceMat = null; if (mat == null) { // This happens then the user removes a material from a prim id = UUID.Zero; } else { newFaceMat = new FaceMaterial(mat); if (newFaceMat.DiffuseAlphaMode == 1 && newFaceMat.NormalMapID == UUID.Zero && newFaceMat.SpecularMapID == UUID.Zero ) { id = UUID.Zero; } else { newFaceMat.genID(); id = newFaceMat.ID; } } oldid = faceEntry.MaterialID; if (oldid == id) { continue; } if (faceEntry != null) { faceEntry.MaterialID = id; //m_log.DebugFormat("[Materials]: in \"{0}\" {1}, setting material ID for face {2} to {3}", sop.Name, sop.UUID, face, id); // We can't use sop.UpdateTextureEntry(te) because it filters, so do it manually sop.Shape.TextureEntry = te.GetBytes(9); } if (oldid != UUID.Zero) { RemoveMaterial(oldid); } lock (materialslock) { if (id != UUID.Zero) { if (m_Materials.ContainsKey(id)) { m_MaterialsRefCount[id]++; } else { m_Materials[id] = newFaceMat; m_MaterialsRefCount[id] = 1; m_changed[newFaceMat] = Util.GetTimeStamp(); } } } if (!parts.Contains(sop)) { parts.Add(sop); } } foreach (SceneObjectPart sop in parts) { if (sop.ParentGroup != null && !sop.ParentGroup.IsDeleted) { sop.TriggerScriptChangedEvent(Changed.TEXTURE); sop.ScheduleFullUpdate(); sop.ParentGroup.HasGroupChanged = true; } } } catch (Exception e) { m_log.Warn("[Materials]: exception processing received material ", e); response.StatusCode = (int)HttpStatusCode.BadRequest; return; } } } } catch (Exception e) { m_log.Warn("[Materials]: exception decoding zipped CAP payload ", e); response.StatusCode = (int)HttpStatusCode.BadRequest; return; } } OSDMap resp = new OSDMap(); resp["Zipped"] = ZCompressOSD(respArr, false); response.RawBuffer = Encoding.UTF8.GetBytes(OSDParser.SerializeLLSDXmlString(resp)); //m_log.Debug("[Materials]: cap request: " + request); //m_log.Debug("[Materials]: cap request (zipped portion): " + ZippedOsdBytesToString(req["Zipped"].AsBinary())); //m_log.Debug("[Materials]: cap response: " + response); }
/// <summary> /// Find the materials used in one Face, and add them to 'm_regionMaterials'. /// </summary> private bool GetStoredMaterialInFace(SceneObjectPart part, Primitive.TextureEntryFace face) { UUID id = face.MaterialID; if (id == UUID.Zero) { return(false); } OSDMap mat; lock (materialslock) { if (m_Materials.ContainsKey(id)) { m_MaterialsRefCount[id]++; return(false); } AssetBase matAsset = m_scene.AssetService.Get(id.ToString()); if (matAsset == null || matAsset.Data == null || matAsset.Data.Length == 0) { // grid may just be down... return(false); } byte[] data = matAsset.Data; // string txt = System.Text.Encoding.ASCII.GetString(data); try { mat = (OSDMap)OSDParser.DeserializeLLSDXml(data); } catch (Exception e) { m_log.WarnFormat("[Materials]: cannot decode material asset {0}: {1}", id, e.Message); return(false); } FaceMaterial fmat = new FaceMaterial(mat); if (fmat == null || (fmat.DiffuseAlphaMode == 1 && fmat.NormalMapID == UUID.Zero && fmat.SpecularMapID == UUID.Zero)) { face.MaterialID = UUID.Zero; return(true); } fmat.ID = id; if (m_Materials.ContainsKey(id)) { m_MaterialsRefCount[id]++; } else { m_Materials[id] = fmat; m_MaterialsRefCount[id] = 1; } return(false); } }
/// <summary> /// Finds any legacy materials stored in DynAttrs that may exist for this part and add them to 'm_regionMaterials'. /// </summary> /// <param name="part"></param> private bool GetLegacyStoredMaterialsInPart(SceneObjectPart part) { if (part.DynAttrs == null) { return(false); } OSD OSMaterials = null; OSDArray matsArr = null; bool partchanged = false; lock (part.DynAttrs) { if (part.DynAttrs.ContainsStore("OpenSim", "Materials")) { OSDMap materialsStore = part.DynAttrs.GetStore("OpenSim", "Materials"); if (materialsStore == null) { return(false); } materialsStore.TryGetValue("Materials", out OSMaterials); part.DynAttrs.RemoveStore("OpenSim", "Materials"); partchanged = true; } if (OSMaterials != null && OSMaterials is OSDArray) { matsArr = OSMaterials as OSDArray; } else { return(partchanged); } } if (matsArr == null) { return(partchanged); } foreach (OSD elemOsd in matsArr) { if (elemOsd != null && elemOsd is OSDMap) { OSDMap matMap = elemOsd as OSDMap; OSD OSDID; OSD OSDMaterial; if (matMap.TryGetValue("ID", out OSDID) && matMap.TryGetValue("Material", out OSDMaterial) && OSDMaterial is OSDMap) { try { lock (materialslock) { UUID id = OSDID.AsUUID(); if (m_Materials.ContainsKey(id)) { continue; } OSDMap theMatMap = (OSDMap)OSDMaterial; FaceMaterial fmat = new FaceMaterial(theMatMap); if (fmat == null || (fmat.DiffuseAlphaMode == 1 && fmat.NormalMapID == UUID.Zero && fmat.SpecularMapID == UUID.Zero)) { continue; } fmat.ID = id; m_Materials[id] = fmat; m_MaterialsRefCount[id] = 0; } } catch (Exception e) { m_log.Warn("[Materials]: exception decoding persisted legacy material: " + e.ToString()); } } } } return(partchanged); }
/// <summary> /// Read face materials. /// </summary> /// <param name="reader"> /// The reader. /// </param> /// <param name="msize"> /// The msize. /// </param> /// <returns> /// The materials. /// </returns> private List<FaceMaterial> ReadFaceMaterials(BinaryReader reader, int msize) { int total = 6; var list = new List<FaceMaterial>(); while (total < msize) { var id = this.ReadChunkId(reader); int size = this.ReadChunkSize(reader); total += size; switch (id) { case ChunkID.TRI_FACEMAT: { string name = this.ReadString(reader); int n = reader.ReadUInt16(); var c = new Int32Collection(); for (int i = 0; i < n; i++) { c.Add(reader.ReadUInt16()); } var fm = new FaceMaterial { Name = name, Faces = c }; list.Add(fm); break; } case ChunkID.TRI_SMOOTH: { this.ReadData(reader, size - 6); break; } default: { this.ReadData(reader, size - 6); break; } } } return list; }
public bool ProcessChunk(FileContext ctx) { while (ctx.ReadChunk()) { switch (ctx.chunk.id) { case kMeshVerts: { nVerts = ctx.PopWord(); verts = new Vertex[nVerts]; for (ushort i = 0; i < nVerts; i++) { // flip y and z verts[i] = new Vertex(); verts[i].x = ctx.PopFloat(); verts[i].z = ctx.PopFloat(); verts[i].y = ctx.PopFloat(); } } break; case kMeshFaces: { nFaces = ctx.PopWord(); faces = new Face3S[nFaces]; // Read all the faces for (ushort i = 0; i < nFaces; i++) { // wind faces CCW faces[i].c = ctx.PopWord(); faces[i].b = ctx.PopWord(); faces[i].a = ctx.PopWord(); faces[i].flags = ctx.PopWord(); faces[i].material = 0; } } break; case kMeshMaterial: { // Material mapping Info. FaceMaterial fmat = new FaceMaterial(); fmat.name = ctx.PopString(); fmat.ne = ctx.PopWord(); fmat.faces = new short[fmat.ne]; // read all faces (actually indices to the face list) for (ushort i = 0; i < fmat.ne; ++i) { fmat.faces[i] = (short)ctx.PopWord(); } faceMaterials.Add(fmat); } break; case kMeshTexVert: { // Material mapping coords int i; ushort imnv, off; imnv = mnv = ctx.PopWord(); // No. of mapping coords mc = new float[mnv * 2]; // Read mapping coords // These are actually texture coords for vertices. for (i = 0; i < imnv; i++) { off = (ushort)(i * 2); mc[off + 0] = ctx.PopFloat(); // u mc[off + 1] = ctx.PopFloat(); // v } } break; case kMeshXFMatrix: { // Local transformation matrix int i, j; float[,] mat = new float[4, 4]; for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) { mat[i, j] = ctx.PopFloat(); } } //lmat = new Matrix(mat[0, 0], mat[0, 1], mat[0, 2], mat[0, 3], // mat[1, 0], mat[1, 1], mat[1, 2], mat[1, 3], // mat[2, 0], mat[2, 1], mat[2, 2], mat[2, 3], // mat[3, 0], mat[3, 1], mat[3, 2], mat[3, 3]); lmat = new Matrix(mat[0, 0], mat[0, 1], mat[0, 2], 0, mat[1, 0], mat[1, 1], mat[1, 2], 0, mat[2, 0], mat[2, 1], mat[2, 2], 0, mat[3, 0], mat[3, 1], mat[3, 2], 1); } break; case kMeshSmoothGroup: { for (ushort i = 0; i < nFaces; ++i) { faces[i].smooth = (ushort)ctx.PopDword(); } } break; default: ctx.SkipChunk(); break; } } return(true); }