/// <summary> /// 得到材质的Asset集合 /// </summary> /// <param name="node"></param> private void ReadBitMap(MaterialNode node) { if (node.MaterialId != ElementId.InvalidElementId) { Asset theAsset = node.GetAppearance(); if (node.HasOverriddenAppearance) { theAsset = node.GetAppearanceOverride(); } if (theAsset == null) { Material material = _doc.GetElement(node.MaterialId) as Material; ElementId appearanceId = material.AppearanceAssetId; AppearanceAssetElement appearanceElem = _doc.GetElement(appearanceId) as AppearanceAssetElement; theAsset = appearanceElem.GetRenderingAsset(); } if (theAsset.Size == 0) { //Asset大小为0,则为欧特克材质 foreach (Asset objCurrentAsset in _objlibraryAsset) { if (objCurrentAsset.Name == theAsset.Name && objCurrentAsset.LibraryName == theAsset.LibraryName) { ReadAsset(objCurrentAsset, ttrgb); } } } else { ReadAsset(theAsset, ttrgb); } } }
/// <summary> /// 设置材质 /// </summary> /// <remarks> //可以为每个单独的输出网格调用OnMaterial方法 ///即使材质尚未实际更改。 因此通常 ///有利于存储当前材料并仅获取其属性 ///当材质实际更改时。 /// </remarks> public void OnMaterial(MaterialNode node) { if (currentMaterialId != node.MaterialId) { currentMaterialId = node.MaterialId; currentColor = node.Color; currentTransparency = node.Transparency; swMtl.Write(strNewmtl, currentMaterialId.IntegerValue.ToString(), (currentColor.Red / 256.0).ToString(), (currentColor.Green / 256.0).ToString(), (currentColor.Blue / 256.0).ToString(), currentTransparency); if (node.HasOverriddenAppearance) { currentAsset = node.GetAppearanceOverride(); } else { currentAsset = node.GetAppearance(); } try { //取得Asset中贴图信息 string textureFile = (FindTextureAsset(currentAsset as AssetProperty)["unifiedbitmap_Bitmap"] as AssetPropertyString).Value.Split('|')[0]; //用Asset中贴图信息和注册表里的材质库地址得到贴图文件所在位置 string texturePath = Path.Combine(textureFolder, textureFile.Replace("/", "\\")); //写入贴图名称 swMtl.Write("map_Kd " + Path.GetFileName(texturePath) + "\n"); //如果贴图文件真实存在,就复制到相应位置 if (File.Exists(texturePath)) { File.Copy(texturePath, Path.Combine(Path.GetDirectoryName(filePath), textureName), true); } } catch (Exception e) { Debug.Print("{0} Second exception.", e.Message); } } Debug.Print($"Material {node.NodeName}"); }
/// <summary> /// 设置当前材质 /// </summary> /// <param name="uidMaterial"></param> void SetCurrentMaterial(string uidMaterial, MaterialNode node) { if (!_materials.ContainsKey(uidMaterial)) { RMaterial material = CurrentDocument.GetElement(uidMaterial) as RMaterial; Color c = material.Color; MaterialBuilder m = null; try { if (material.Transparency != 0) { m = new MaterialBuilder() .WithAlpha(SharpGLTF.Materials.AlphaMode.BLEND) .WithDoubleSide(true) .WithMetallicRoughnessShader() .WithChannelParam(KnownChannel.BaseColor, new Vector4(c.Red / 256f, c.Green / 256f, c.Blue / 256f, 1 - (material.Transparency / 128f))); } else { m = new MaterialBuilder() .WithDoubleSide(true) .WithMetallicRoughnessShader() .WithChannelParam(KnownChannel.BaseColor, new Vector4(c.Red / 256f, c.Green / 256f, c.Blue / 256f, 1)); } Autodesk.Revit.DB.Visual.Asset currentAsset; if (node.HasOverriddenAppearance) { currentAsset = node.GetAppearanceOverride(); } else { currentAsset = node.GetAppearance(); } try { //取得Asset中贴图信息 Autodesk.Revit.DB.Visual.Asset findAssert = FindTextureAsset(currentAsset as AssetProperty); if (findAssert != null) { string textureFile = (findAssert["unifiedbitmap_Bitmap"] as AssetPropertyString).Value.Split('|')[0]; //用Asset中贴图信息和注册表里的材质库地址得到贴图文件所在位置 string textureName = textureFile.Replace("/", "\\"); string texturePath = Path.Combine(_textureFolder, textureName); m.WithChannelImage(KnownChannel.BaseColor, texturePath); } } catch (Exception e) { } } catch (Exception e) { } _materials.Add(uidMaterial, m); } _currentMaterial = _materials[uidMaterial]; }
// public void OnDaylightPortal(DaylightPortalNode node) //{ // Like RPCs, Daylight Portals too have their assets available only. // THere is no other public API for them currently available. // return; //} public void OnMaterial(MaterialNode node) { materialNode = node; MessageBuffer mb = new MessageBuffer(); mb.add(node.MaterialId.IntegerValue); mb.add(node.NodeName); mb.add(node.Color); mb.add((byte)(((100 - (node.Transparency)) / 100.0) * 255)); Asset asset; if (node.HasOverriddenAppearance) { asset = node.GetAppearanceOverride(); } else { asset = node.GetAppearance(); } String textureName = ""; AssetProperties properties = asset as AssetProperties; for (int index = 0; index < asset.Size; index++) { if (properties[index].Type == AssetPropertyType.Reference) { AssetPropertyReference e = properties[index] as AssetPropertyReference; if (e != null) { AssetProperty p = e.GetConnectedProperty(0); if (p.Type == AssetPropertyType.Asset) { Asset a = p as Asset; if (a != null) { Boolean foundValidTexture = false; AssetProperties prop = a as AssetProperties; for (int ind = 0; ind < a.Size; ind++) { if (prop[ind].Name == "unifiedbitmap_Bitmap") { AssetPropertyString ps = prop[ind] as AssetPropertyString; if (ps.Value != "") { textureName = ps.Value; foundValidTexture = true; } } if (prop[ind].Name == "texture_URepeat") { AssetPropertyBoolean ps = prop[ind] as AssetPropertyBoolean; if (foundValidTexture) { } //textureName = ps.Value; } if (prop[ind].Name == "texture_VRepeat") { AssetPropertyBoolean ps = prop[ind] as AssetPropertyBoolean; if (foundValidTexture) { } } } } } } } } mb.add(textureName); OpenCOVERPlugin.COVER.Instance.sendMessage(mb.buf, OpenCOVERPlugin.COVER.MessageTypes.NewMaterial); return; }
public void OnMaterial(MaterialNode node) { materialNode = node; MessageBuffer mb = new MessageBuffer(); mb.add(node.MaterialId.IntegerValue); mb.add(node.NodeName); mb.add(node.Color); mb.add((byte)(((100 - (node.Transparency)) / 100.0) * 255)); Asset asset; if (node.HasOverriddenAppearance) { asset = node.GetAppearanceOverride(); } else { asset = node.GetAppearance(); } String textureName = ""; AssetProperties properties = asset as AssetProperties; for (int index = 0; index < asset.Size; index++) { if (properties[index].Type == AssetPropertyType.APT_Reference) { AssetPropertyReference e = properties[index] as AssetPropertyReference; if (e != null) { AssetProperty p = e.GetConnectedProperty(0); if (p.Type == AssetPropertyType.APT_Asset) { Asset a = p as Asset; if (a != null) { Boolean foundValidTexture = false; AssetProperties prop = a as AssetProperties; for (int ind = 0; ind < a.Size; ind++) { if (prop[ind].Name == "unifiedbitmap_Bitmap") { AssetPropertyString ps = prop[ind] as AssetPropertyString; if (ps.Value != "") { textureName = ps.Value; foundValidTexture = true; } } if (prop[ind].Name == "texture_URepeat") { AssetPropertyBoolean ps = prop[ind] as AssetPropertyBoolean; if (foundValidTexture) { } //textureName = ps.Value; } if (prop[ind].Name == "texture_VRepeat") { AssetPropertyBoolean ps = prop[ind] as AssetPropertyBoolean; if (foundValidTexture) { } } } } } } } } mb.add(textureName); OpenCOVERPlugin.COVER.Instance.sendMessage(mb.buf, OpenCOVERPlugin.COVER.MessageTypes.NewMaterial); return; }
public void OnMaterial(MaterialNode node) { currentTransparencyDouble = node.Transparency; currentColor = node.Color; currentShiniess = node.Glossiness; currentTransparencyint = Convert.ToInt32(node.Transparency); if (node.MaterialId != ElementId.InvalidElementId) { Asset theAsset = node.GetAppearance(); if (node.HasOverriddenAppearance) { theAsset = node.GetAppearanceOverride(); } if (theAsset == null) { //Element m = _doc.GetElement(node.MaterialId); Material material = _doc.GetElement(node.MaterialId) as Material; ElementId appearanceId = material.AppearanceAssetId; AppearanceAssetElement appearanceElem = _doc.GetElement(appearanceId) as AppearanceAssetElement; theAsset = appearanceElem.GetRenderingAsset(); } if (theAsset.Size == 0) { //TaskDialog.Show("revit","欧特克材质"); foreach (Asset objCurrentAsset in _objlibraryAsset) { if (objCurrentAsset.Name == theAsset.Name && objCurrentAsset.LibraryName == theAsset.LibraryName) { //var theValue = objCurrentAsset.Type.ToString(); ReadAsset(objCurrentAsset); //TaskDialog.Show("欧特克材质", theValue); } } } else { ReadAsset(theAsset); } //AssetProperty aProperty = theAsset[assetIdx]; //if (aProperty.NumberOfConnectedProperties < 1) // continue; //// Find first connected property. //// Should work for all current (2018) schemas. //// Safer code would loop through all connected //// properties based on the number provided. //Asset connectedAsset = aProperty.GetConnectedProperty(0) as Asset; //// We are only checking for bitmap connected assets. //if (connectedAsset.Name == "UnifiedBitmapSchema") //{ // // This line is 2018.1 & up because of the // // property reference to UnifiedBitmap // // .UnifiedbitmapBitmap. In earlier versions, // // you can still reference the string name // // instead: "unifiedbitmap_Bitmap" // AssetPropertyString path = connectedAsset["unifiedbitmap_Bitmap"] as AssetPropertyString; // // This will be a relative path to the // // built -in materials folder, addiitonal // // render appearance folder, or an // // absolute path. // TaskDialog.Show("Connected bitmap", String.Format("{0} from {2}: {1}", aProperty.Name, path.Value, connectedAsset.LibraryName)); } if (currentMterialId != node.MaterialId) { var trgb = Util.ColorTransparencyToInt(currentColor, currentTransparencyint); face.Add(-1); face.Add(trgb); face.Add(currentTransparencyint); face.Add(-2); face.Add(-2); face.Add(-2); face.Add(-2); face.Add(-2); face.Add(-2); currentMterialId = node.MaterialId; var ttrgb = Util.ColorTransparencyString(currentColor, currentTransparencyint); if (!transparencys.ContainsKey(ttrgb)) { transparencys.Add(ttrgb, 1.0 - currentTransparencyDouble); } if (!colors.ContainsKey(ttrgb)) { colors.Add(ttrgb, currentColor); } } else { if (materialIndex == 0) { var trgb = Util.ColorTransparencyToInt(currentColor, currentTransparencyint); face.Add(-1); face.Add(trgb); face.Add(currentTransparencyint); face.Add(-2); face.Add(-2); face.Add(-2); face.Add(-2); face.Add(-2); face.Add(-2); currentMterialId = node.MaterialId; var ttrgb = Util.ColorTransparencyString(currentColor, currentTransparencyint); colors.Add(ttrgb, currentColor); transparencys.Add(ttrgb, currentTransparencyint); } } materialIndex++; }