/// <summary> /// Initializes a new instance of the <see cref="PhongMaterial"/> class. /// </summary> /// <param name="core">The core.</param> public PhongMaterial(PhongMaterialCore core) : base(core) { AmbientColor = core.AmbientColor; DiffuseColor = core.DiffuseColor; DisplacementMap = core.DisplacementMap; EmissiveColor = core.EmissiveColor; Name = core.Name; NormalMap = core.NormalMap; ReflectiveColor = core.ReflectiveColor; SpecularColor = core.SpecularColor; SpecularShininess = core.SpecularShininess; DiffuseMap = core.DiffuseMap; DiffuseAlphaMap = core.DiffuseAlphaMap; SpecularColorMap = core.SpecularColorMap; EmissiveMap = core.EmissiveMap; DisplacementMapScaleMask = core.DisplacementMapScaleMask; DiffuseMapSampler = core.DiffuseMapSampler; DisplacementMapSampler = core.DisplacementMapSampler; MaxTessellationDistance = core.MaxTessellationDistance; MinTessellationDistance = core.MinTessellationDistance; MaxDistanceTessellationFactor = core.MaxDistanceTessellationFactor; MinDistanceTessellationFactor = core.MinDistanceTessellationFactor; EnableTessellation = core.EnableTessellation; RenderDiffuseAlphaMap = core.RenderDiffuseAlphaMap; RenderDiffuseMap = core.RenderDiffuseMap; RenderDisplacementMap = core.RenderDisplacementMap; RenderNormalMap = core.RenderNormalMap; RenderEnvironmentMap = core.RenderEnvironmentMap; RenderShadowMap = core.RenderShadowMap; RenderSpecularColorMap = core.RenderSpecularColorMap; RenderEmissiveMap = core.RenderEmissiveMap; EnableAutoTangent = core.EnableAutoTangent; UVTransform = core.UVTransform; EnableFlatShading = core.EnableFlatShading; }
/// <summary> /// Initializes a new instance of the <see cref="PhongMaterialVariables"/> class. /// </summary> /// <param name="manager">The manager.</param> /// <param name="technique">The technique.</param> /// <param name="materialCore">The material core.</param> /// <param name="defaultPassName">Default pass name</param> public PhongMaterialVariables(IEffectsManager manager, IRenderTechnique technique, PhongMaterialCore materialCore, string defaultPassName = DefaultPassNames.Default) : base(manager, technique, DefaultMeshConstantBufferDesc, materialCore) { this.material = materialCore; texDiffuseSlot = texAlphaSlot = texDisplaceSlot = texNormalSlot = -1; samplerDiffuseSlot = samplerDisplaceSlot = samplerShadowSlot = -1; textureManager = manager.MaterialTextureManager; statePoolManager = manager.StateManager; MaterialPass = technique[defaultPassName]; OITPass = technique[DefaultPassNames.OITPass]; OITDepthPeelingInit = technique[DefaultPassNames.OITDepthPeelingInit]; OITDepthPeeling = technique[DefaultPassNames.OITDepthPeeling]; ShadowPass = technique[DefaultPassNames.ShadowPass]; WireframePass = technique[DefaultPassNames.Wireframe]; WireframeOITPass = technique[DefaultPassNames.WireframeOITPass]; WireframeOITDPPass = technique[DefaultPassNames.WireframeOITDPPass]; TessellationPass = technique[DefaultPassNames.MeshTriTessellation]; TessellationOITPass = technique[DefaultPassNames.MeshTriTessellationOIT]; TessellationOITDPPass = technique[DefaultPassNames.MeshPBRTriTessellationOITDP]; DepthPass = technique[DefaultPassNames.DepthPrepass]; UpdateMappings(MaterialPass); EnableTessellation = materialCore.EnableTessellation; }
/// <summary> /// Initializes a new instance of the <see cref="PhongMaterialVariables"/> class. /// </summary> /// <param name="manager">The manager.</param> /// <param name="technique">The technique.</param> /// <param name="materialCore">The material core.</param> /// <param name="materialPassName">Name of the material pass.</param> /// <param name="wireframePassName">Name of the wireframe pass.</param> /// <param name="materialOITPassName">Name of the material oit pass.</param> /// <param name="wireframeOITPassName">Name of the wireframe oit pass.</param> /// <param name="shadowPassName">Name of the shadow pass.</param> /// <param name="tessellationPassName">Name of the tessellation pass.</param> /// <param name="tessellationOITPassName">Name of the tessellation oit pass.</param> /// <param name="depthPassName">Name of the depth pass</param> public PhongMaterialVariables(IEffectsManager manager, IRenderTechnique technique, PhongMaterialCore materialCore, string materialPassName = DefaultPassNames.Default, string wireframePassName = DefaultPassNames.Wireframe, string materialOITPassName = DefaultPassNames.OITPass, string wireframeOITPassName = DefaultPassNames.WireframeOITPass, string shadowPassName = DefaultPassNames.ShadowPass, string tessellationPassName = DefaultPassNames.MeshTriTessellation, string tessellationOITPassName = DefaultPassNames.MeshTriTessellationOIT, string depthPassName = DefaultPassNames.DepthPrepass) : base(manager, technique, DefaultMeshConstantBufferDesc, materialCore) { this.material = materialCore; texDiffuseSlot = texAlphaSlot = texDisplaceSlot = texNormalSlot = -1; samplerDiffuseSlot = samplerDisplaceSlot = samplerShadowSlot = -1; textureManager = manager.MaterialTextureManager; statePoolManager = manager.StateManager; MaterialPass = technique[materialPassName]; MaterialOITPass = technique[materialOITPassName]; ShadowPass = technique[shadowPassName]; WireframePass = technique[wireframePassName]; WireframeOITPass = technique[wireframeOITPassName]; TessellationPass = technique[tessellationPassName]; TessellationOITPass = technique[tessellationOITPassName]; DepthPass = technique[depthPassName]; UpdateMappings(MaterialPass); EnableTessellation = materialCore.EnableTessellation; currentMaterialPass = EnableTessellation ? TessellationPass : MaterialPass; currentOITPass = EnableTessellation ? TessellationOITPass : MaterialOITPass; }
public static PhongMaterial ConvertToPhongMaterial(this PhongMaterialCore core) { return(core == null ? null : new PhongMaterial() { DiffuseColor = core.DiffuseColor, AmbientColor = core.AmbientColor, EmissiveColor = core.EmissiveColor, SpecularColor = core.SpecularColor, ReflectiveColor = core.ReflectiveColor, SpecularShininess = core.SpecularShininess, DiffuseAlphaMap = core.DiffuseAlphaMap, DiffuseAlphaMapSampler = core.DiffuseAlphaMapSampler, DiffuseMap = core.DiffuseMap, DiffuseMapSampler = core.DiffuseMapSampler, DisplacementMap = core.DisplacementMap, DisplacementMapSampler = core.DisplacementMapSampler, NormalMap = core.NormalMap, NormalMapSampler = core.NormalMapSampler, DisplacementMapScaleMask = core.DisplacementMapScaleMask, Name = core.Name, UVTransform = core.UVTransform, EnableTessellation = core.EnableTessellation, MaxDistanceTessellationFactor = core.MaxDistanceTessellationFactor, MaxTessellationDistance = core.MaxTessellationDistance, MinDistanceTessellationFactor = core.MinDistanceTessellationFactor, MinTessellationDistance = core.MinTessellationDistance, }); }
/// <summary> /// Gets the material from the specified path. /// </summary> /// <param name="texturePath"> /// The texture path. /// </param> /// <returns> /// The material. /// </returns> public PhongMaterialCore GetMaterial(string texturePath) { if (this.Material == null) { this.Material = this.CreateMaterial(texturePath); //this.Material.Freeze(); } return(this.Material); }
/// <summary> /// Creates the material. /// </summary> /// <param name="texturePath">The texture path.</param> /// <returns>A WPF material.</returns> private PhongMaterialCore CreateMaterial(string texturePath) { MemoryStream diffuseMapMS = null; if (DiffuseMap != null) { using (var fs = new FileStream(Path.GetFullPath(Path.Combine(texturePath, "./" + this.DiffuseMap)), FileMode.Open)) { diffuseMapMS = new MemoryStream(); fs.CopyTo(diffuseMapMS); } } MemoryStream bumpMapMS = null; if (BumpMap != null) { using (var fs = new FileStream(Path.GetFullPath(Path.Combine(texturePath, "./" + this.BumpMap)), FileMode.Open)) { bumpMapMS = new MemoryStream(); fs.CopyTo(bumpMapMS); } } MemoryStream alphaMapMS = null; if (AlphaMap != null) { using (var fs = new FileStream(Path.GetFullPath(Path.Combine(texturePath, "./" + this.AlphaMap)), FileMode.Open)) { alphaMapMS = new MemoryStream(); fs.CopyTo(alphaMapMS); } } var mat = new PhongMaterialCore() { AmbientColor = this.Ambient, //AmbientMap = this.AmbientMap, DiffuseColor = this.Diffuse, DiffuseMap = diffuseMapMS, SpecularColor = this.Specular, SpecularShininess = (float)this.SpecularCoefficient, //SpecularMap = this.SpecularMap, NormalMap = bumpMapMS, DiffuseAlphaMap = alphaMapMS, //Dissolved = this.Dissolved, //Illumination = this.Illumination, }; //return mg.Children.Count != 1 ? mg : mg.Children[0]; return(mat); }
/// <summary> /// /// </summary> /// <param name="manager"></param> /// <param name="material"></param> private DiffuseMaterialVariables(IEffectsManager manager, PhongMaterialCore material) { this.material = material; needUpdate = true; material.PropertyChanged += Material_OnMaterialPropertyChanged; texDiffuseSlot = -1; samplerDiffuseSlot = samplerShadowSlot = -1; textureManager = manager.MaterialTextureManager; statePoolManager = manager.StateManager; CreateTextureViews(); CreateSamplers(); this.PropertyChanged += (s, e) => { OnInvalidateRenderer?.Invoke(this, EventArgs.Empty); }; }
/// <summary> /// /// </summary> /// <param name="manager"></param> /// <param name="material"></param> public TextureSharedPhongMaterialVariables(IEffectsManager manager, PhongMaterialCore material) { this.material = material; needUpdate = true; material.PropertyChanged += Material_OnMaterialPropertyChanged; texDiffuseSlot = texAlphaSlot = texDisplaceSlot = texNormalSlot = -1; samplerDiffuseSlot = samplerAlphaSlot = samplerDisplaceSlot = samplerNormalSlot = samplerShadowSlot = -1; textureManager = manager.MaterialTextureManager; statePoolManager = manager.StateManager; CreateTextureViews(); CreateSamplers(); EnableTessellation = material.EnableTessellation; this.PropertyChanged += (s, e) => { OnInvalidateRenderer?.Invoke(this, EventArgs.Empty); }; }
/// <summary> /// Adds the properties. /// </summary> /// <param name="phong">The phong.</param> /// <param name="assimpMaterial">The assimp material.</param> protected virtual void AddProperties(PhongMaterialCore phong, global::Assimp.Material assimpMaterial) { assimpMaterial.ShadingMode = ShadingMode.Blinn; assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.COLOR_DIFFUSE_BASE, phong.DiffuseColor.ToAssimpColor4D())); assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.COLOR_SPECULAR_BASE, phong.SpecularColor.ToAssimpColor4D())); assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.COLOR_REFLECTIVE_BASE, phong.ReflectiveColor.ToAssimpColor4D())); assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.COLOR_EMISSIVE_BASE, phong.EmissiveColor.ToAssimpColor4D())); assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.COLOR_AMBIENT_BASE, phong.AmbientColor.ToAssimpColor4D())); assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.SHININESS_BASE, phong.SpecularShininess)); if (phong.DiffuseColor.Alpha < 1f) { assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.OPACITY_BASE, phong.DiffuseColor.Alpha)); } if (phong.DiffuseMap != null && !string.IsNullOrEmpty(phong.DiffuseMapFilePath)) { assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.TEXTURE_BASE, phong.DiffuseMapFilePath, TextureType.Diffuse, 0)); assimpMaterial.AddProperty(new MaterialProperty( AiMatKeys.GetFullTextureName(AiMatKeys.MAPPINGMODE_U_BASE, TextureType.Diffuse, 0), (int)ToAssimpAddressMode(phong.DiffuseMapSampler.AddressU))); assimpMaterial.AddProperty(new MaterialProperty( AiMatKeys.GetFullTextureName(AiMatKeys.MAPPINGMODE_V_BASE, TextureType.Diffuse, 0), (int)ToAssimpAddressMode(phong.DiffuseMapSampler.AddressV))); } if (phong.EmissiveMap != null && !string.IsNullOrEmpty(phong.EmissiveMapFilePath)) { assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.TEXTURE_BASE, phong.EmissiveMapFilePath, TextureType.Emissive, 0)); } if (phong.NormalMap != null && !string.IsNullOrEmpty(phong.NormalMapFilePath)) { assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.TEXTURE_BASE, phong.NormalMapFilePath, TextureType.Normals, 0)); } if (phong.DisplacementMap != null && !string.IsNullOrEmpty(phong.DisplacementMapFilePath)) { assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.TEXTURE_BASE, phong.DisplacementMapFilePath, TextureType.Displacement, 0)); } if (phong.SpecularColorMap != null && !string.IsNullOrEmpty(phong.SpecularColorMapFilePath)) { assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.TEXTURE_BASE, phong.SpecularColorMapFilePath, TextureType.Specular, 0)); } if (phong.DiffuseAlphaMap != null && !string.IsNullOrEmpty(phong.DiffuseAlphaMapFilePath)) { assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.TEXTURE_BASE, phong.DiffuseAlphaMapFilePath, TextureType.Opacity, 0)); } if (phong.UVTransform.HasUVTransform) { assimpMaterial.AddProperty(new MaterialProperty(AiMatKeys.UVTRANSFORM_BASE, phong.UVTransform.ToArray())); } }
/// <summary> /// /// </summary> /// <param name="manager"></param> /// <param name="technique"></param> /// <param name="material"></param> private DiffuseMaterialVariables(IEffectsManager manager, IRenderTechnique technique, PhongMaterialCore material) : base(manager, technique, DefaultMeshConstantBufferDesc) { this.material = material; material.PropertyChanged += Material_OnMaterialPropertyChanged; texDiffuseSlot = -1; samplerDiffuseSlot = samplerShadowSlot = -1; textureManager = manager.MaterialTextureManager; statePoolManager = manager.StateManager; MaterialPass = technique[DefaultShaderPassName]; TransparentPass = technique[TransparentPassName]; UpdateMappings(MaterialPass); CreateTextureViews(); CreateSamplers(); }
private static Material GetMaterialFromMaterialCore(PhongMaterialCore material) { if (material == null) { return(null); } var mat = new PhongMaterial { Name = material.Name, SpecularColor = material.SpecularColor, SpecularShininess = material.SpecularShininess, AmbientColor = material.AmbientColor, DiffuseColor = material.DiffuseColor, DiffuseMap = material.DiffuseMap, DiffuseMapSampler = material.DiffuseMapSampler, DiffuseAlphaMap = material.DiffuseAlphaMap, DiffuseAlphaMapSampler = material.DiffuseAlphaMapSampler, DisplacementMap = material.DisplacementMap, DisplacementMapSampler = material.DisplacementMapSampler, DisplacementMapScaleMask = material.DisplacementMapScaleMask, EmissiveColor = material.EmissiveColor, EnableTessellation = material.EnableTessellation, MaxDistanceTessellationFactor = material.MaxDistanceTessellationFactor, MaxTessellationDistance = material.MaxTessellationDistance, MinDistanceTessellationFactor = material.MinDistanceTessellationFactor, MinTessellationDistance = material.MinTessellationDistance, NormalMap = material.NormalMap, NormalMapSampler = material.NormalMapSampler, ReflectiveColor = material.ReflectiveColor, RenderDiffuseAlphaMap = material.RenderDiffuseAlphaMap, RenderDiffuseMap = material.RenderDiffuseMap, RenderDisplacementMap = material.RenderDisplacementMap, RenderNormalMap = material.RenderNormalMap, }; return(mat); }
public static PhongMaterial ConvertToPhongMaterial(this PhongMaterialCore core) { return(core == null ? null : new PhongMaterial() { DiffuseColor = core.DiffuseColor, AmbientColor = core.AmbientColor, EmissiveColor = core.EmissiveColor, SpecularColor = core.SpecularColor, ReflectiveColor = core.ReflectiveColor, SpecularShininess = core.SpecularShininess, DiffuseAlphaMap = core.DiffuseAlphaMap, DiffuseAlphaMapSampler = core.DiffuseAlphaMapSampler, DiffuseMap = core.DiffuseMap, DiffuseMapSampler = core.DiffuseMapSampler, DisplacementMap = core.DisplacementMap, DisplacementMapSampler = core.DisplacementMapSampler, NormalMap = core.NormalMap, NormalMapSampler = core.NormalMapSampler, DisplacementMapScaleMask = core.DisplacementMapScaleMask, Name = core.Name, }); }
/// <summary> /// reads the Material of a chunck /// </summary> /// <param name="reader"></param> /// <param name="chunkSize"></param> private void ReadMaterial(BinaryReader reader, int chunkSize) { int total = 6; string name = null; var luminance = Color.Transparent; //SharpDX.Color not System.Windows.Media.Color var diffuse = Color.Transparent; var specular = Color.Transparent; var shininess = Color.Transparent; double opacity = 0; string texture = null; float specularPower = 100;//check if we can find this somewhere instead of just setting it to 100 while (total < chunkSize) { ChunkID id = this.ReadChunkId(reader); int size = this.ReadChunkSize(reader); total += size; switch (id) { case ChunkID.MAT_NAME01: name = this.ReadString(reader); break; case ChunkID.MAT_TRANSPARENCY: // skip the first 6 bytes this.ReadData(reader, 6); // read the percent value as 16Bit Uint byte[] data = this.ReadData(reader, 2); opacity = (100 - BitConverter.ToUInt16(data, 0)) / 100.0; break; case ChunkID.MAT_LUMINANCE: luminance = this.ReadColor(reader); break; case ChunkID.MAT_DIFFUSE: diffuse = this.ReadColor(reader); break; case ChunkID.MAT_SPECULAR: specular = this.ReadColor(reader); break; case ChunkID.MAT_SHININESS: //byte[] bytes = this.ReadData(reader, size - 6); specularPower = this.ReadPercent(reader, size - 6); break; case ChunkID.MAT_MAP: texture = this.ReadMatMap(reader, size - 6); break; case ChunkID.MAT_MAPFILE: this.ReadData(reader, size - 6); break; default: this.ReadData(reader, size - 6); break; } } var image = ReadBitmapSoure(texture, diffuse); if (Math.Abs(opacity) > 0.001) { diffuse.A = (byte)(opacity * 255); luminance.A = (byte)(opacity * 255); } var material = new PhongMaterialCore() { DiffuseColor = diffuse, AmbientColor = luminance, //not really sure about this, lib3ds uses 0xA010 as AmbientColor SpecularColor = specular, SpecularShininess = specularPower, }; if (image != null) { material.NormalMap = image; } if (name != null) { materials[name] = material; } }
/// <summary> /// To the phong material. /// </summary> /// <param name="material">The material.</param> /// <returns></returns> protected virtual PhongMaterialCore OnCreatePhongMaterial(global::Assimp.Material material) { var phong = new PhongMaterialCore { AmbientColor = (material.HasColorAmbient && !configuration.IgnoreAmbientColor) ? material.ColorAmbient.ToSharpDXColor4() : Color.Black, DiffuseColor = material.HasColorDiffuse ? material.ColorDiffuse.ToSharpDXColor4() : Color.White, SpecularColor = material.HasColorSpecular ? material.ColorSpecular.ToSharpDXColor4() : Color.Black, EmissiveColor = (material.HasColorEmissive && !configuration.IgnoreEmissiveColor) ? material.ColorEmissive.ToSharpDXColor4() : Color.Black, ReflectiveColor = material.HasColorReflective ? material.ColorReflective.ToSharpDXColor4() : Color.Black, SpecularShininess = material.Shininess }; if (material.HasOpacity) { var c = phong.DiffuseColor; c.Alpha = material.Opacity; phong.DiffuseColor = c; } if (material.HasTextureDiffuse) { phong.DiffuseMap = LoadTexture(material.TextureDiffuse.FilePath); phong.DiffuseMapFilePath = material.TextureDiffuse.FilePath; var desc = Shaders.DefaultSamplers.LinearSamplerClampAni1; desc.AddressU = ToDXAddressMode(material.TextureDiffuse.WrapModeU); desc.AddressV = ToDXAddressMode(material.TextureDiffuse.WrapModeV); phong.DiffuseMapSampler = desc; } if (material.HasTextureNormal) { phong.NormalMap = LoadTexture(material.TextureNormal.FilePath); phong.NormalMapFilePath = material.TextureNormal.FilePath; } else if (material.HasTextureHeight) { phong.NormalMap = LoadTexture(material.TextureHeight.FilePath); phong.NormalMapFilePath = material.TextureHeight.FilePath; } if (material.HasTextureSpecular) { phong.SpecularColorMap = LoadTexture(material.TextureSpecular.FilePath); phong.SpecularColorMapFilePath = material.TextureSpecular.FilePath; } if (material.HasTextureDisplacement) { phong.DisplacementMap = LoadTexture(material.TextureDisplacement.FilePath); phong.DisplacementMapFilePath = material.TextureDisplacement.FilePath; } if (material.HasTextureOpacity) { phong.DiffuseAlphaMap = LoadTexture(material.TextureOpacity.FilePath); phong.DiffuseAlphaMapFilePath = material.TextureOpacity.FilePath; } if (material.HasTextureEmissive) { phong.EmissiveMap = LoadTexture(material.TextureEmissive.FilePath); phong.EmissiveMapFilePath = material.TextureEmissive.FilePath; } if (material.HasNonTextureProperty(AiMatKeys.UVTRANSFORM_BASE)) { var values = material.GetNonTextureProperty(AiMatKeys.UVTRANSFORM_BASE).GetFloatArrayValue(); if (values != null && values.Length == 5) { phong.UVTransform = new UVTransform(values[0], new Vector2(values[1], values[2]), new Vector2(values[3], values[4])); } } return(phong); }
/// <summary> /// Initializes a new instance of the <see cref="PhongMaterialVariables"/> class. This construct will be using the PassName pass into constructor only. /// </summary> /// <param name="passName">Name of the pass.</param> /// <param name="manager">The manager.</param> /// <param name="technique"></param> /// <param name="material">The material.</param> public PhongMaterialVariables(string passName, IEffectsManager manager, IRenderTechnique technique, PhongMaterialCore material) : this(manager, technique, material, passName) { }
/// <summary> /// Initializes a new instance of the <see cref="TextureSharedPhongMaterialVariables"/> class. This construct will be using the PassName pass into constructor only. /// </summary> /// <param name="passName">Name of the pass.</param> /// <param name="manager">The manager.</param> /// <param name="material">The material.</param> public DiffuseMaterialVariables(string passName, IEffectsManager manager, PhongMaterialCore material) : this(manager, material) { DefaultShaderPassName = passName; fixedPassName = true; }
/// <summary> /// Initializes a new instance of the <see cref="TextureSharedPhongMaterialVariables"/> class. This construct will be using the PassName pass into constructor only. /// </summary> /// <param name="passName">Name of the pass.</param> /// <param name="manager">The manager.</param> /// <param name="technique"></param> /// <param name="material">The material.</param> public TextureSharedPhongMaterialVariables(string passName, IEffectsManager manager, IRenderTechnique technique, PhongMaterialCore material) : this(manager, technique, material) { DefaultShaderPassName = passName; fixedPassName = true; }