private bool OpenOsgModel(string osgPath, out IModel fmodel, out IModel smodel, out IPropertySet images, out IMatrix mat) { fmodel = null; smodel = null; images = null; mat = null; IResourceFactory resFactory = new ResourceFactoryClass(); if ((resFactory == null) || !File.Exists(osgPath)) { return(false); } Dictionary <string, string> dictionary = null; IDrawGroup group = null; IDrawPrimitive primitive = null; IPropertySet set = null; string str = ""; IImage property = null; try { resFactory.CreateModelAndImageFromFileEx(osgPath, out images, out smodel, out fmodel, out mat); if ((images != null) && (images.Count > 0)) { set = new PropertySetClass(); dictionary = new Dictionary <string, string>(); foreach (string str2 in images.GetAllKeys()) { property = images.GetProperty(str2) as IImage; IImage temp = null; string filePath = string.Format(string.Format(@"{0}\..\temp\{1}.png", Application.StartupPath, Guid.NewGuid().ToString()), new object[0]); if (property.WriteFile(filePath)) { temp = resFactory.CreateImageFromFile(filePath); } str = BitConverter.ToString(ObjectIdGenerator.Generate()).Replace("-", string.Empty).ToLowerInvariant(); dictionary.Add(str2, str); set.SetProperty(str, temp); if (File.Exists(filePath)) { File.Delete(filePath); } } images = set; } if ((fmodel != null) && (fmodel.GroupCount > 0)) { for (int i = 0; i < fmodel.GroupCount; i++) { group = fmodel.GetGroup(i); if (group != null) { if (!string.IsNullOrEmpty(group.CompleteMapTextureName) && dictionary.ContainsKey(group.CompleteMapTextureName)) { group.CompleteMapTextureName = dictionary[group.CompleteMapTextureName]; } if (!string.IsNullOrEmpty(group.LightMapTextureName) && dictionary.ContainsKey(group.LightMapTextureName)) { group.LightMapTextureName = dictionary[group.LightMapTextureName]; } if (group.PrimitiveCount > 0) { for (int j = 0; j < group.PrimitiveCount; j++) { primitive = group.GetPrimitive(j); if (((primitive != null) && (primitive.Material != null)) && (!string.IsNullOrEmpty(primitive.Material.TextureName) && dictionary.ContainsKey(primitive.Material.TextureName))) { primitive.Material.TextureName = dictionary[primitive.Material.TextureName]; } } } } } } if ((smodel != null) && (smodel.GroupCount > 0)) { for (int k = 0; k < smodel.GroupCount; k++) { group = smodel.GetGroup(k); if (group != null) { if (!string.IsNullOrEmpty(group.CompleteMapTextureName) && dictionary.ContainsKey(group.CompleteMapTextureName)) { group.CompleteMapTextureName = dictionary[group.CompleteMapTextureName]; } if (!string.IsNullOrEmpty(group.LightMapTextureName) && dictionary.ContainsKey(group.LightMapTextureName)) { group.LightMapTextureName = dictionary[group.LightMapTextureName]; } if (group.PrimitiveCount > 0) { for (int m = 0; m < group.PrimitiveCount; m++) { primitive = group.GetPrimitive(m); if (((primitive != null) && (primitive.Material != null)) && (!string.IsNullOrEmpty(primitive.Material.TextureName) && dictionary.ContainsKey(primitive.Material.TextureName))) { primitive.Material.TextureName = dictionary[primitive.Material.TextureName]; } } } } } } return(true); } catch (Exception exception) { return(false); } }
// 新建空模型 public bool NewEmptyModel(int[] index, RenderType renderType, object renderInfo, out IModel model) { model = null; if (index.Length == 0) { return(false); } string[] strArray = null; uint[] numArray = null; if (renderType == RenderType.Texture) { strArray = renderInfo as string[]; if ((strArray == null) || (strArray.Length < index.Length)) { return(false); } } else { numArray = renderInfo as uint[]; if ((numArray == null) || (numArray.Length < index.Length)) { return(false); } } IDrawGroup drawGroup = null; IDrawPrimitive primitive = null; IDrawMaterial material = null; IResourceFactory factory = new ResourceFactoryClass(); model = factory.CreateModel(); model.SwitchSize = SwitchSize; drawGroup = new DrawGroupClass(); for (int i = 0; i < index.Length; i++) { material = new DrawMaterialClass { CullMode = this._cullModel, EnableBlend = false, EnableLight = true, SpecularColor = this._specularColor, WrapModeS = gviTextureWrapMode.gviTextureWrapRepeat, WrapModeT = gviTextureWrapMode.gviTextureWrapRepeat }; primitive = new DrawPrimitiveClass { PrimitiveMode = gviPrimitiveMode.gviPrimitiveModeTriangleList, PrimitiveType = gviPrimitiveType.gviPrimitiveNormal, VertexArray = new FloatArrayClass(), IndexArray = new UInt16ArrayClass(), NormalArray = new FloatArrayClass() }; if (renderType == RenderType.Texture) { material.TextureName = strArray[index[i]]; primitive.TexcoordArray = new FloatArrayClass(); material.DiffuseColor = uint.MaxValue; } else { material.TextureName = ""; primitive.TexcoordArray = null; material.DiffuseColor = numArray[index[i]]; } primitive.Material = material; drawGroup.AddPrimitive(primitive); } model.AddGroup(drawGroup); return(true); }