public override void UpdateTransparency(bool transparencyEnabled) { foreach (var model in Models) { var isBody = ((CustomBoneSkinMeshGeometry3D)model).IsBody; if (isBody) { continue; } var material = ((CustomBoneSkinMeshGeometry3D)model).Material as PhongMaterial; if (transparencyEnabled) { ((CustomBoneSkinMeshGeometry3D)model).IsTransparent = true; material.DiffuseColor = PhongMaterials.ToColor(1, 1, 1, .4f); } else { ((CustomBoneSkinMeshGeometry3D)model).IsTransparent = false; material.DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1); } } }
private void SetupScene() { RenderTechnique = new RenderTechnique("RenderCustom"); WhiteMaterial = new PhongMaterial { Name = "White", AmbientColor = PhongMaterials.ToColor(0.1, 0.1, 0.1, 1.0), DiffuseColor = defaultMaterialColor, SpecularColor = PhongMaterials.ToColor(0.0225, 0.0225, 0.0225, 1.0), EmissiveColor = PhongMaterials.ToColor(0.0, 0.0, 0.0, 1.0), SpecularShininess = 12.8f, }; SelectedMaterial = new PhongMaterial { Name = "White", AmbientColor = PhongMaterials.ToColor(0.1, 0.1, 0.1, 1.0), DiffuseColor = defaultSelectionColor, SpecularColor = PhongMaterials.ToColor(0.0225, 0.0225, 0.0225, 1.0), EmissiveColor = PhongMaterials.ToColor(0.0, 0.0, 0.0, 1.0), SpecularShininess = 12.8f, }; Model1Transform = new TranslateTransform3D(0, -0, 0); // camera setup Camera = new PerspectiveCamera(); SetCameraData(new CameraData()); DrawGrid(); }
/// <summary> /// Sets the transparency for the model /// </summary> public void Transparency() { if (SecondModelMaterial.DiffuseColor.Alpha == 1) { SecondModelMaterial.DiffuseColor = PhongMaterials.ToColor(1, 1, 1, .4); SecondModel.UpdateVertices(); if (ThirdModel != null) { ThirdModelMaterial.DiffuseColor = PhongMaterials.ToColor(1, 1, 1, .4); ThirdModel.UpdateVertices(); } } else { SecondModelMaterial.DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1); SecondModel.UpdateVertices(); if (ThirdModel != null) { ThirdModelMaterial.DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1); ThirdModel.UpdateVertices(); } } }
public static void UpdateFrom(this InserterBaseViewModel vm, BaseInserter m) { vm.InserterId = m.Id; vm.Position = new System.Windows.Media.Media3D.Point3D(m.Position.X, m.Position.Y, m.Position.Z); vm.Direction = new System.Windows.Media.Media3D.Vector3D(m.Direction.X, m.Direction.Y, m.Direction.Z); var material = PhongMaterials.Glass; material.DiffuseColor = PhongMaterials.ToColor(m.Color.R / 255.0, m.Color.G / 255.0, m.Color.B / 255.0, 1.0); vm.Material = material; }
private void SetupScene() { var ptColor = (Color)SharedDictionaryManager.DynamoColorsAndBrushesDictionary["PointColor"]; defaultPointColor = new Color4(ptColor.R / 255.0f, ptColor.G / 255.0f, ptColor.B / 255.0f, ptColor.A / 255.0f); var lineColor = (Color)SharedDictionaryManager.DynamoColorsAndBrushesDictionary["EdgeColor"]; defaultLineColor = new Color4(lineColor.R / 255.0f, lineColor.G / 255.0f, lineColor.B / 255.0f, lineColor.A / 255.0f); directionalLightColor = new Color4(0.9f, 0.9f, 0.9f, 1.0f); directionalLightDirection = new Vector3(-0.5f, -1.0f, 0.0f); var matColor = (Color)SharedDictionaryManager.DynamoColorsAndBrushesDictionary["MaterialColor"]; materialColor = new Color4(matColor.R / 255.0f, matColor.G / 255.0f, matColor.B / 255.0f, matColor.A / 255.0f); RenderTechnique = Techniques.RenderDynamo; WhiteMaterial = new PhongMaterial { Name = "White", AmbientColor = PhongMaterials.ToColor(0.1, 0.1, 0.1, 1.0), DiffuseColor = materialColor, SpecularColor = PhongMaterials.ToColor(0.0225, 0.0225, 0.0225, 1.0), EmissiveColor = PhongMaterials.ToColor(0.0, 0.0, 0.0, 1.0), SpecularShininess = 12.8f, }; var selColor = (Color)SharedDictionaryManager.DynamoColorsAndBrushesDictionary["SelectionColor"]; selectionColor = new Color4(selColor.R / 255.0f, selColor.G / 255.0f, selColor.B / 255.0f, selColor.A / 255.0f); SelectedMaterial = new PhongMaterial { Name = "White", AmbientColor = PhongMaterials.ToColor(0.1, 0.1, 0.1, 1.0), DiffuseColor = selectionColor, SpecularColor = PhongMaterials.ToColor(0.0225, 0.0225, 0.0225, 1.0), EmissiveColor = PhongMaterials.ToColor(0.0, 0.0, 0.0, 1.0), SpecularShininess = 12.8f, }; Model1Transform = new TranslateTransform3D(0, -0, 0); // camera setup Camera = new PerspectiveCamera { UpDirection = new Vector3D(0, 1, 0), FarPlaneDistance = 10000000, }; ResetCamera(); DrawGrid(); }
/// <summary> /// Sets the transparency for the model /// </summary> public void Transparency() { foreach (var model in ModelCollection) { if (!((CustomGM3D)model).ModelBody) { var material = (CustomPhongMaterial)((CustomGM3D)model).Material; if (material.DiffuseColor.Alpha == 1) { material.DiffuseColor = PhongMaterials.ToColor(1, 1, 1, .4); ((CustomGM3D)model).Geometry.UpdateVertices(); } else { material.DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1); ((CustomGM3D)model).Geometry.UpdateVertices(); } } } }
private DynamoGeometryModel3D CreateDynamoGeometryModel3D(HelixRenderPackage rp) { var meshGeometry3D = new DynamoGeometryModel3D(renderTechnique) { Transform = Model1Transform, Material = WhiteMaterial, IsHitTestVisible = false, RequiresPerVertexColoration = rp.RequiresPerVertexColoration, IsSelected = rp.IsSelected }; if (rp.Colors != null) { var pf = PixelFormats.Bgra32; var stride = (rp.ColorsStride / 4 * pf.BitsPerPixel + 7) / 8; try { var diffMap = BitmapSource.Create(rp.ColorsStride / 4, rp.Colors.Count() / rp.ColorsStride, 96.0, 96.0, pf, null, rp.Colors.ToArray(), stride); var diffMat = new PhongMaterial { Name = "White", AmbientColor = PhongMaterials.ToColor(0.1, 0.1, 0.1, 1.0), DiffuseColor = defaultMaterialColor, SpecularColor = PhongMaterials.ToColor(0.0225, 0.0225, 0.0225, 1.0), EmissiveColor = PhongMaterials.ToColor(0.0, 0.0, 0.0, 1.0), SpecularShininess = 12.8f, DiffuseMap = diffMap }; meshGeometry3D.Material = diffMat; } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } } return(meshGeometry3D); }
private CustomGM3D setModel(int m) { MeshGeometry3D mg = new MeshGeometry3D(); mg.Positions = mData[m].Mesh.Vertices; mg.Indices = mData[m].Mesh.Indices; mg.Normals = mData[m].Mesh.Normals; mg.TextureCoordinates = mData[m].Mesh.TextureCoordinates; mg.Colors = mData[m].Mesh.VertexColors; mg.Tangents = new HelixToolkit.Wpf.SharpDX.Core.Vector3Collection(); MeshBuilder.ComputeTangents(mg); mg.BiTangents = mData[m].Mesh.BiTangents; mData[m].Mesh.Tangents = mg.Tangents; CustomGM3D gm3d = new CustomGM3D(); List <byte> DDS = new List <byte>(); diffuse = null; if (mData[m].Diffuse != null) { diffuse = new MemoryStream(); BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(mData[m].Diffuse)); enc.Save(diffuse); } normal = null; if (mData[m].Normal != null) { normal = new MemoryStream(); BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(mData[m].Normal)); enc.Save(normal); } specular = null; if (mData[m].Specular != null) { specular = new MemoryStream(); var enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(mData[m].Specular)); enc.Save(specular); } alpha = null; if (mData[m].Alpha != null) { alpha = new MemoryStream(); var enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(mData[m].Alpha)); enc.Save(alpha); } emissive = null; if (mData[m].Emissive != null) { emissive = new MemoryStream(); var enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(mData[m].Emissive)); enc.Save(emissive); } if (mData.Count > 1) { float specularShine = 30f; CurrentSS = 30; if (mData[m].IsBody || mData[m].IsFace) { gm3d.Geometry = mg; gm3d.ModelBody = true; gm3d.Material = new CustomPhongMaterial() { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = specularShine, NormalMap = normal, DiffuseMap = diffuse, DiffuseAlphaMap = alpha, SpecularMap = specular, EmissiveMap = emissive }; } else if (!mData[m].IsBody && !second) { gm3d.Geometry = mg; gm3d.Material = new CustomPhongMaterial() { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = specularShine, NormalMap = normal, DiffuseMap = diffuse, DiffuseAlphaMap = alpha, SpecularMap = specular, EmissiveMap = emissive }; second = true; } else if (!mData[m].IsBody) { gm3d.Geometry = mg; gm3d.Material = new CustomPhongMaterial() { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = specularShine, NormalMap = normal, DiffuseMap = diffuse, DiffuseAlphaMap = alpha, SpecularMap = specular, EmissiveMap = emissive }; } } else { float specularShine = 30f; CurrentSS = 30; gm3d.Geometry = mg; gm3d.Material = new CustomPhongMaterial() { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = specularShine, NormalMap = normal, SpecularMap = specular, DiffuseMap = diffuse, DiffuseAlphaMap = alpha, EmissiveMap = emissive }; } return(gm3d); }
/// <summary> /// Updates or Adds the Model to the viewport /// </summary> /// <param name="model">The TexTools Model</param> /// <param name="textureDataDictionary">The textures associated with the model</param> /// <param name="item">The item for the model</param> /// <param name="modelRace">The race of the model</param> /// <param name="targetRace">The target race the model should be</param> public void UpdateModel(TTModel model, Dictionary <int, ModelTextureData> textureDataDictionary, IItemModel item, XivRace modelRace, XivRace targetRace) { _targetRace = targetRace; var itemType = $"{item.PrimaryCategory}_{item.SecondaryCategory}"; // If target race is different than the model race Apply racial deforms if (modelRace != targetRace) { ApplyDeformers(model, itemType, modelRace, targetRace); } SharpDX.BoundingBox?boundingBox = null; ModelModifiers.CalculateTangents(model); // Remove any existing models of the same item type RemoveModel(itemType); var totalMeshCount = model.MeshGroups.Count; for (var i = 0; i < totalMeshCount; i++) { var meshGeometry3D = GetMeshGeometry(model, i); var textureData = textureDataDictionary[model.GetMaterialIndex(i)]; Stream diffuse = null, specular = null, normal = null, alpha = null, emissive = null; if (textureData.Diffuse != null && textureData.Diffuse.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Diffuse, textureData.Width, textureData.Height)) { diffuse = new MemoryStream(); img.Save(diffuse, new PngEncoder()); } streamList.Add(diffuse); } if (textureData.Specular != null && textureData.Specular.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Specular, textureData.Width, textureData.Height)) { specular = new MemoryStream(); img.Save(specular, new PngEncoder()); } streamList.Add(specular); } if (textureData.Normal != null && textureData.Normal.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Normal, textureData.Width, textureData.Height)) { normal = new MemoryStream(); img.Save(normal, new PngEncoder()); } streamList.Add(normal); } if (textureData.Alpha != null && textureData.Alpha.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Alpha, textureData.Width, textureData.Height)) { alpha = new MemoryStream(); img.Save(alpha, new PngEncoder()); } streamList.Add(alpha); } if (textureData.Emissive != null && textureData.Emissive.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Emissive, textureData.Width, textureData.Height)) { emissive = new MemoryStream(); img.Save(emissive, new PngEncoder()); } streamList.Add(emissive); } var material = new PhongMaterial { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = 1f, DiffuseMap = diffuse, DiffuseAlphaMap = alpha, SpecularColorMap = specular, NormalMap = normal, EmissiveMap = emissive }; // Geometry that contains skeleton data var smgm3d = new CustomBoneSkinMeshGeometry3D { Geometry = meshGeometry3D, Material = material, ItemType = itemType, BoneMatrices = GetMatrices(targetRace), BoneList = model.Bones }; // Keep track of what bones are showing in the view foreach (var modelBone in model.Bones) { if (!shownBonesList.Contains(modelBone)) { shownBonesList.Add(modelBone); } } boundingBox = meshGeometry3D.Bound; smgm3d.CullMode = Properties.Settings.Default.Cull_Mode.Equals("None") ? CullMode.None : CullMode.Back; Models.Add(smgm3d); } SpecularShine = 1; var center = boundingBox.GetValueOrDefault().Center; _lightX = center.X; _lightY = center.Y; _lightZ = center.Z; Light3Direction = new Vector3D(_lightX, _lightY, _lightZ); Camera.UpDirection = new Vector3D(0, 1, 0); Camera.CameraInternal.PropertyChanged += CameraInternal_PropertyChanged; // Add the skeleton node for the target race AddSkeletonNode(targetRace); // Keep track of the models displayed in the viewport shownModels.Add(itemType, new DisplayedModelData { TtModel = model, ItemModel = item, ModelTextureData = textureDataDictionary }); }
/// <summary> /// Updates the model in the 3D viewport /// </summary> /// <param name="mdlData">The model data</param> /// <param name="textureDataDictionary">The texture dictionary for the model</param> public void UpdateModel(XivMdl mdlData, Dictionary <int, ModelTextureData> textureDataDictionary) { SharpDX.BoundingBox?boundingBox = null; var totalMeshCount = mdlData.LoDList[0].MeshCount + mdlData.LoDList[0].ExtraMeshCount; for (var i = 0; i < totalMeshCount; i++) { var meshData = mdlData.LoDList[0].MeshDataList[i].VertexData; var meshGeometry3D = new MeshGeometry3D { Positions = meshData.Positions, Normals = meshData.Normals, Indices = meshData.Indices, TextureCoordinates = meshData.TextureCoordinates0, }; try { MeshBuilder.ComputeTangents(meshGeometry3D); } catch { Debug.WriteLine($"Unable to compute tangents for mesh {i}"); } if (meshData.BiNormals != null && meshData.BiNormals.Count > 0) { meshGeometry3D.BiTangents = meshData.BiNormals; } var textureData = textureDataDictionary[mdlData.LoDList[0].MeshDataList[i].MeshInfo.MaterialIndex]; Stream diffuse = null, specular = null, normal = null, alpha = null, emissive = null; var pixelSettings = new PixelReadSettings(textureData.Width, textureData.Height, StorageType.Char, PixelMapping.RGBA); if (textureData.Diffuse != null && textureData.Diffuse.Length > 0) { using (var image = new MagickImage(textureData.Diffuse, pixelSettings)) { diffuse = new MemoryStream(image.ToByteArray(MagickFormat.Bmp)); } } if (textureData.Specular != null && textureData.Specular.Length > 0) { using (var image = new MagickImage(textureData.Specular, pixelSettings)) { specular = new MemoryStream(image.ToByteArray(MagickFormat.Bmp)); } } if (textureData.Normal != null && textureData.Normal.Length > 0) { using (var image = new MagickImage(textureData.Normal, pixelSettings)) { normal = new MemoryStream(image.ToByteArray(MagickFormat.Bmp)); } } if (textureData.Alpha != null && textureData.Alpha.Length > 0) { using (var image = new MagickImage(textureData.Alpha, pixelSettings)) { alpha = new MemoryStream(image.ToByteArray(MagickFormat.Bmp)); } } if (textureData.Emissive != null && textureData.Emissive.Length > 0) { using (var image = new MagickImage(textureData.Emissive, pixelSettings)) { emissive = new MemoryStream(image.ToByteArray(MagickFormat.Bmp)); } } var material = new PhongMaterial { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = 1f, DiffuseMap = diffuse, DiffuseAlphaMap = alpha, SpecularColorMap = specular, NormalMap = normal, EmissiveMap = emissive }; var mgm3d = new CustomMeshGeometryModel3D { Geometry = meshGeometry3D, Material = material, IsBody = mdlData.LoDList[0].MeshDataList[i].IsBody }; boundingBox = meshGeometry3D.Bound; mgm3d.CullMode = Properties.Settings.Default.Cull_Mode.Equals("None") ? CullMode.None : CullMode.Back; Models.Add(mgm3d); } SpecularShine = 1; var center = boundingBox.GetValueOrDefault().Center; _lightX = center.X; _lightY = center.Y; _lightZ = center.Z; Light3Direction = new Vector3D(_lightX, _lightY, _lightZ); Camera.UpDirection = new Vector3D(0, 1, 0); Camera.CameraInternal.PropertyChanged += CameraInternal_PropertyChanged; }
/// <summary> /// Updates the model in the 3D viewport /// </summary> /// <param name="mdlData">The model data</param> /// <param name="textureDataDictionary">The texture dictionary for the model</param> public void UpdateModel(TTModel model, Dictionary <int, ModelTextureData> textureDataDictionary) { SharpDX.BoundingBox?boundingBox = null; ModelModifiers.CalculateTangents(model); var totalMeshCount = model.MeshGroups.Count; for (var i = 0; i < totalMeshCount; i++) { var meshGeometry3D = GetMeshGeometry(model, i); var textureData = textureDataDictionary[model.GetMaterialIndex(i)]; Stream diffuse = null, specular = null, normal = null, alpha = null, emissive = null; if (textureData.Diffuse != null && textureData.Diffuse.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Diffuse, textureData.Width, textureData.Height)) { diffuse = new MemoryStream(); img.Save(diffuse, new PngEncoder()); } streamList.Add(diffuse); } if (textureData.Specular != null && textureData.Specular.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Specular, textureData.Width, textureData.Height)) { specular = new MemoryStream(); img.Save(specular, new PngEncoder()); } streamList.Add(specular); } if (textureData.Normal != null && textureData.Normal.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Normal, textureData.Width, textureData.Height)) { normal = new MemoryStream(); img.Save(normal, new PngEncoder()); } streamList.Add(normal); } if (textureData.Alpha != null && textureData.Alpha.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Alpha, textureData.Width, textureData.Height)) { alpha = new MemoryStream(); img.Save(alpha, new PngEncoder()); } streamList.Add(alpha); } if (textureData.Emissive != null && textureData.Emissive.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Emissive, textureData.Width, textureData.Height)) { emissive = new MemoryStream(); img.Save(emissive, new PngEncoder()); } streamList.Add(emissive); } var material = new PhongMaterial { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = 1f, DiffuseMap = diffuse, DiffuseAlphaMap = alpha, SpecularColorMap = specular, NormalMap = normal, EmissiveMap = emissive }; var mgm3d = new CustomMeshGeometryModel3D { Geometry = meshGeometry3D, Material = material //, //IsBody = mdlData.LoDList[0].MeshDataList[i].IsBody }; boundingBox = meshGeometry3D.Bound; mgm3d.CullMode = Properties.Settings.Default.Cull_Mode.Equals("None") ? CullMode.None : CullMode.Back; Models.Add(mgm3d); } SpecularShine = 1; var center = boundingBox.GetValueOrDefault().Center; _lightX = center.X; _lightY = center.Y; _lightZ = center.Z; Light3Direction = new Vector3D(_lightX, _lightY, _lightZ); Camera.UpDirection = new Vector3D(0, 1, 0); Camera.CameraInternal.PropertyChanged += CameraInternal_PropertyChanged; }
private void AggregateRenderPackages(PackageAggregationParams parameters) { //Clear the geometry values before adding the package. VisualizationManager_WorkspaceOpenedClosedHandled(); foreach (var rp in parameters.Packages) { //Node ID gets updated with a ":" everytime this function is called. //For example, if the same point node is called multiple times (CBN), the ID has a ":" //and this makes the dictionary to have multiple entries for the same node. var baseId = rp.Description; if (baseId.IndexOf(":", StringComparison.Ordinal) > 0) { baseId = baseId.Split(':')[0]; } var id = baseId; var p = rp.Points; if (p.Positions.Any()) { id = baseId + ":points"; PointGeometryModel3D pointGeometry3D; if (model3DDictionary.ContainsKey(id)) { pointGeometry3D = model3DDictionary[id] as PointGeometryModel3D; } else { pointGeometry3D = new PointGeometryModel3D { Geometry = HelixRenderPackage.InitPointGeometry(), Transform = Model1Transform, Color = SharpDX.Color.White, Figure = PointGeometryModel3D.PointFigure.Ellipse, Size = DefaultPointSize, IsHitTestVisible = true, IsSelected = rp.IsSelected }; model3DDictionary.Add(id, pointGeometry3D); } var points = pointGeometry3D.Geometry as PointGeometry3D; var startIdx = points.Positions.Count; points.Positions.AddRange(p.Positions); points.Colors.AddRange(p.Colors.Any() ? p.Colors : Enumerable.Repeat(defaultPointColor, points.Positions.Count)); points.Indices.AddRange(p.Indices.Select(i => i + startIdx)); if (rp.DisplayLabels) { var pt = p.Positions[0]; parameters.Text.TextInfo.Add(new TextInfo(HelixRenderPackage.CleanTag(rp.Description), new Vector3(pt.X + 0.025f, pt.Y + 0.025f, pt.Z + 0.025f))); Text = parameters.Text; } pointGeometry3D.Geometry = points; pointGeometry3D.Name = baseId; pointGeometry3D.MouseDown3D += meshGeometry3D_MouseDown3D; } var l = rp.Lines; if (l.Positions.Any()) { id = baseId + ":lines"; LineGeometryModel3D lineGeometry3D; if (model3DDictionary.ContainsKey(id)) { lineGeometry3D = model3DDictionary[id] as LineGeometryModel3D; } else { lineGeometry3D = new LineGeometryModel3D() { Geometry = HelixRenderPackage.InitLineGeometry(), Transform = Model1Transform, Color = SharpDX.Color.White, Thickness = 0.5, IsHitTestVisible = true, IsSelected = rp.IsSelected }; model3DDictionary.Add(id, lineGeometry3D); } var lineSet = lineGeometry3D.Geometry as LineGeometry3D; var startIdx = lineSet.Positions.Count; lineSet.Positions.AddRange(l.Positions); lineSet.Colors.AddRange(l.Colors.Any() ? l.Colors : Enumerable.Repeat(defaultLineColor, l.Positions.Count)); lineSet.Indices.AddRange(l.Indices.Any() ? l.Indices.Select(i => i + startIdx) : Enumerable.Range(startIdx, startIdx + l.Positions.Count)); if (rp.DisplayLabels) { var pt = lineSet.Positions[startIdx]; parameters.Text.TextInfo.Add(new TextInfo(HelixRenderPackage.CleanTag(rp.Description), new Vector3(pt.X + 0.025f, pt.Y + 0.025f, pt.Z + 0.025f))); Text = parameters.Text; } lineGeometry3D.Geometry = lineSet; lineGeometry3D.Name = baseId; lineGeometry3D.MouseDown3D += meshGeometry3D_MouseDown3D; } var m = rp.Mesh; if (!m.Positions.Any()) { continue; } id = ((rp.RequiresPerVertexColoration || rp.Colors != null) ? rp.Description : baseId) + ":mesh"; DynamoGeometryModel3D meshGeometry3D; if (model3DDictionary.ContainsKey(id)) { meshGeometry3D = model3DDictionary[id] as DynamoGeometryModel3D; } else { meshGeometry3D = new DynamoGeometryModel3D() { Transform = Model1Transform, Material = WhiteMaterial, IsHitTestVisible = true, RequiresPerVertexColoration = rp.RequiresPerVertexColoration, IsSelected = rp.IsSelected, }; if (rp.Colors != null) { var pf = PixelFormats.Bgra32; var stride = (rp.ColorsStride / 4 * pf.BitsPerPixel + 7) / 8; try { var diffMap = BitmapSource.Create(rp.ColorsStride / 4, rp.Colors.Count() / rp.ColorsStride, 96.0, 96.0, pf, null, rp.Colors.ToArray(), stride); var diffMat = new PhongMaterial { Name = "White", AmbientColor = PhongMaterials.ToColor(0.1, 0.1, 0.1, 1.0), DiffuseColor = materialColor, SpecularColor = PhongMaterials.ToColor(0.0225, 0.0225, 0.0225, 1.0), EmissiveColor = PhongMaterials.ToColor(0.0, 0.0, 0.0, 1.0), SpecularShininess = 12.8f, DiffuseMap = diffMap }; meshGeometry3D.Material = diffMat; } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } } ((MaterialGeometryModel3D)meshGeometry3D).SelectionColor = selectionColor; model3DDictionary.Add(id, meshGeometry3D); } var mesh = meshGeometry3D.Geometry == null?HelixRenderPackage.InitMeshGeometry() : meshGeometry3D.Geometry as MeshGeometry3D; var idxCount = mesh.Positions.Count; mesh.Positions.AddRange(m.Positions); mesh.Colors.AddRange(m.Colors); mesh.Normals.AddRange(m.Normals); mesh.TextureCoordinates.AddRange(m.TextureCoordinates); mesh.Indices.AddRange(m.Indices.Select(i => i + idxCount)); if (mesh.Colors.Any(c => c.Alpha < 1.0)) { meshGeometry3D.HasTransparency = true; } if (rp.DisplayLabels) { var pt = mesh.Positions[idxCount]; parameters.Text.TextInfo.Add(new TextInfo(HelixRenderPackage.CleanTag(rp.Description), new Vector3(pt.X + 0.025f, pt.Y + 0.025f, pt.Z + 0.025f))); Text = parameters.Text; } meshGeometry3D.Geometry = mesh; meshGeometry3D.Name = baseId; meshGeometry3D.MouseDown3D += meshGeometry3D_MouseDown3D; } Attach(); }
/// <summary> /// Updates the model in the 3D viewport /// </summary> /// <param name="mdlData">The model data</param> /// <param name="textureDataDictionary">The texture dictionary for the model</param> public void UpdateModel(XivMdl mdlData, Dictionary <int, ModelTextureData> textureDataDictionary) { SharpDX.BoundingBox?boundingBox = null; var totalMeshCount = mdlData.LoDList[0].MeshCount + mdlData.LoDList[0].ExtraMeshCount; for (var i = 0; i < totalMeshCount; i++) { var meshData = mdlData.LoDList[0].MeshDataList[i].VertexData; var meshGeometry3D = new MeshGeometry3D { Positions = new Vector3Collection(meshData.Positions), Normals = new Vector3Collection(meshData.Normals), Indices = new IntCollection(meshData.Indices), Colors = new Color4Collection(meshData.Colors4), TextureCoordinates = new Vector2Collection(meshData.TextureCoordinates0), }; try { MeshBuilder.ComputeTangents(meshGeometry3D); } catch { Debug.WriteLine($"Unable to compute tangents for mesh {i}"); } if (meshData.BiNormals != null && meshData.BiNormals.Count > 0) { meshGeometry3D.BiTangents = new Vector3Collection(meshData.BiNormals); } var textureData = textureDataDictionary[mdlData.LoDList[0].MeshDataList[i].MeshInfo.MaterialIndex]; Stream diffuse = null, specular = null, normal = null, alpha = null, emissive = null; if (textureData.Diffuse != null && textureData.Diffuse.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Diffuse, textureData.Width, textureData.Height)) { diffuse = new MemoryStream(); img.Save(diffuse, new PngEncoder()); } streamList.Add(diffuse); } if (textureData.Specular != null && textureData.Specular.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Specular, textureData.Width, textureData.Height)) { specular = new MemoryStream(); img.Save(specular, new PngEncoder()); } streamList.Add(specular); } if (textureData.Normal != null && textureData.Normal.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Normal, textureData.Width, textureData.Height)) { normal = new MemoryStream(); img.Save(normal, new PngEncoder()); } streamList.Add(normal); } if (textureData.Alpha != null && textureData.Alpha.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Alpha, textureData.Width, textureData.Height)) { alpha = new MemoryStream(); img.Save(alpha, new PngEncoder()); } streamList.Add(alpha); } if (textureData.Emissive != null && textureData.Emissive.Length > 0) { using (var img = Image.LoadPixelData <Rgba32>(textureData.Emissive, textureData.Width, textureData.Height)) { emissive = new MemoryStream(); img.Save(emissive, new PngEncoder()); } streamList.Add(emissive); } var material = new PhongMaterial { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = 1f, DiffuseMap = diffuse, DiffuseAlphaMap = alpha, SpecularColorMap = specular, NormalMap = normal, EmissiveMap = emissive }; var mgm3d = new CustomMeshGeometryModel3D { Geometry = meshGeometry3D, Material = material, IsBody = mdlData.LoDList[0].MeshDataList[i].IsBody }; boundingBox = meshGeometry3D.Bound; mgm3d.CullMode = Properties.Settings.Default.Cull_Mode.Equals("None") ? CullMode.None : CullMode.Back; Models.Add(mgm3d); } SpecularShine = 1; var center = boundingBox.GetValueOrDefault().Center; _lightX = center.X; _lightY = center.Y; _lightZ = center.Z; Light3Direction = new Vector3D(_lightX, _lightY, _lightZ); Camera.UpDirection = new Vector3D(0, 1, 0); Camera.CameraInternal.PropertyChanged += CameraInternal_PropertyChanged; }
private void SetupScene() { var ptColor = (System.Windows.Media.Color)SharedDictionaryManager.DynamoColorsAndBrushesDictionary["PointColor"]; defaultPointColor = new Color4(ptColor.R / 255.0f, ptColor.G / 255.0f, ptColor.B / 255.0f, ptColor.A / 255.0f); var lineColor = (System.Windows.Media.Color)SharedDictionaryManager.DynamoColorsAndBrushesDictionary["EdgeColor"]; defaultLineColor = new Color4(lineColor.R / 255.0f, lineColor.G / 255.0f, lineColor.B / 255.0f, lineColor.A / 255.0f); ShadowMapResolution = new Vector2(2048, 2048); ShowShadows = false; // setup lighting AmbientLightColor = new Color4(0.0f, 0.0f, 0.0f, 1.0f); DirectionalLightColor = new Color4(0.9f, 0.9f, 0.9f, 1.0f); DirectionalLightDirection = new Vector3(-0.5f, -1.0f, 0.0f); FillLightColor = new Color4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f)); FillLightDirection = new Vector3(0.5f, 1.0f, 0f); var matColor = (System.Windows.Media.Color)SharedDictionaryManager.DynamoColorsAndBrushesDictionary["MaterialColor"]; materialColor = new Color4(matColor.R / 255.0f, matColor.G / 255.0f, matColor.B / 255.0f, matColor.A / 255.0f); RenderTechnique = Techniques.RenderPhong; WhiteMaterial = new PhongMaterial { Name = "White", AmbientColor = PhongMaterials.ToColor(0.1, 0.1, 0.1, 1.0), DiffuseColor = materialColor, SpecularColor = PhongMaterials.ToColor(0.0225, 0.0225, 0.0225, 1.0), EmissiveColor = PhongMaterials.ToColor(0.0, 0.0, 0.0, 1.0), SpecularShininess = 12.8f, }; var selColor = (System.Windows.Media.Color)SharedDictionaryManager.DynamoColorsAndBrushesDictionary["SelectionColor"]; selectionColor = new Color4(selColor.R / 255.0f, selColor.G / 255.0f, selColor.B / 255.0f, selColor.A / 255.0f); SelectedMaterial = new PhongMaterial { Name = "White", AmbientColor = PhongMaterials.ToColor(0.1, 0.1, 0.1, 1.0), DiffuseColor = selectionColor, SpecularColor = PhongMaterials.ToColor(0.0225, 0.0225, 0.0225, 1.0), EmissiveColor = PhongMaterials.ToColor(0.0, 0.0, 0.0, 1.0), SpecularShininess = 12.8f, }; Model1Transform = new TranslateTransform3D(0, -0, 0); // camera setup Camera = new PerspectiveCamera { Position = new Point3D(10, 15, 10), LookDirection = new Vector3D(-10, -10, -10), UpDirection = new Vector3D(0, 1, 0), NearPlaneDistance = 1, FarPlaneDistance = 2000000, }; DrawGrid(); }
private void setModel(int m) { MeshGeometryModel3D mgm3d = new MeshGeometryModel3D(); MeshGeometry3D mg = new MeshGeometry3D(); mg.Positions = mData[m].Mesh.Vertices; mg.Indices = mData[m].Mesh.Indices; mg.Normals = mData[m].Mesh.Normals; mg.TextureCoordinates = mData[m].Mesh.TextureCoordinates; mg.Colors = mData[m].Mesh.VertexColors; MeshBuilder.ComputeTangents(mg); mgm3d.Geometry = mg; List <byte> DDS = new List <byte>(); diffuse = null; if (mData[m].Diffuse != null) { diffuse = new MemoryStream(); BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(mData[m].Diffuse)); enc.Save(diffuse); } normal = null; if (mData[m].Normal != null) { normal = new MemoryStream(); BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(mData[m].Normal)); enc.Save(normal); } colorTable = null; if (mData[m].ColorTable != null) { colorTable = new MemoryStream(); var enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(mData[m].ColorTable)); enc.Save(colorTable); } mask = null; if (mData[m].Mask != null) { mask = new MemoryStream(); BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(mData[m].Mask)); enc.Save(mask); } specular = null; if (mData[m].Specular != null) { specular = new MemoryStream(); var enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(mData[m].Specular)); enc.Save(specular); } alpha = null; if (mData[m].Alpha != null) { alpha = new MemoryStream(); var enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(mData[m].Alpha)); enc.Save(alpha); } if (mData.Count > 1) { float specularShine = 30f; if (mData[m].IsBody || mData[m].IsFace) { this.Model = mg; ModelMaterial = new CustomPhongMaterial() { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = specularShine, NormalMap = normal, MaskMap = mask, DiffuseMap = diffuse, DiffuseAlphaMap = alpha, ColorTable = colorTable, SpecularMap = specular }; } else if (!mData[m].IsBody && !second) { this.SecondModel = mg; SecondModelMaterial = new CustomPhongMaterial() { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = specularShine, NormalMap = normal, MaskMap = mask, ColorTable = colorTable, DiffuseMap = diffuse, DiffuseAlphaMap = alpha, SpecularMap = specular }; second = true; } else if (!mData[m].IsBody) { this.ThirdModel = mg; ThirdModelMaterial = new CustomPhongMaterial() { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = specularShine, NormalMap = normal, MaskMap = mask, ColorTable = colorTable, DiffuseMap = diffuse, DiffuseAlphaMap = alpha, SpecularMap = specular }; } } else { this.SecondModel = mg; SecondModelMaterial = new CustomPhongMaterial() { DiffuseColor = PhongMaterials.ToColor(1, 1, 1, 1), SpecularShininess = 30f, NormalMap = normal, MaskMap = mask, ColorTable = colorTable, SpecularMap = specular, DiffuseMap = diffuse, DiffuseAlphaMap = alpha }; } }