protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { var customMaterial = new EffectMaterialContent(); customMaterial.Effect = new ExternalReference<EffectContent>(effectPath); //var basicMaterial = (BasicMaterialContent) material; //if (basicMaterial.Texture != null) //{ // customMaterial.Textures.Add(skyMapKey, basicMaterial.Texture); //} foreach (var texture in material.Textures) { customMaterial.Textures.Add(texture.Key, texture.Value); } var parameters = new OpaqueDataDictionary(); parameters["ColorKeyColor"] = ColorKeyColor; parameters["ColorKeyEnabled"] = ColorKeyEnabled; parameters["TextureFormat"] = TextureFormat; parameters["GenerateMipmaps"] = GenerateMipmaps; parameters["ResizeTexturesToPowerOfTwo"] = ResizeTexturesToPowerOfTwo; return context.Convert<MaterialContent, MaterialContent>( customMaterial, typeof(MaterialProcessor).Name, parameters); }
/// <summary> /// Use our custom EnvironmentMappedMaterialProcessor /// to convert all the materials on this model. /// </summary> protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { OpaqueDataDictionary processorParameters = new OpaqueDataDictionary(); processorParameters["ColorKeyColor"] = ColorKeyColor; processorParameters["ColorKeyEnabled"] = ColorKeyEnabled; processorParameters["TextureFormat"] = TextureFormat; processorParameters["GenerateMipmaps"] = GenerateMipmaps; processorParameters["ResizeTexturesToPowerOfTwo"] = ResizeTexturesToPowerOfTwo; return context.Convert<MaterialContent, MaterialContent>(material, "TankModelMaterialProcessor", processorParameters); }
/// <summary> /// Use the CustomEffectMaterialProcessor for all of the materials in the model. /// We pass the processor parameter along to the material processor for the /// effect file name. /// </summary> protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { OpaqueDataDictionary processorParameters = new OpaqueDataDictionary(); processorParameters.Add("CustomEffect", customEffect); processorParameters["ColorKeyColor"] = this.ColorKeyColor; processorParameters["ColorKeyEnabled"] = this.ColorKeyEnabled; processorParameters["TextureFormat"] = this.TextureFormat; processorParameters["GenerateMipmaps"] = this.GenerateMipmaps; processorParameters["ResizeTexturesToPowerOfTwo"] = this.ResizeTexturesToPowerOfTwo; return context.Convert<MaterialContent, MaterialContent>(material, "CustomEffectMaterialProcessor", processorParameters); }
protected virtual DRMaterialContent OnConvertMaterial(MaterialContent material, ContentProcessorContext context) { //var processorParameters = new OpaqueDataDictionary //{ // { "ColorKeyColor", ColorKeyColor }, // { "ColorKeyEnabled", ColorKeyEnabled }, // { "GenerateMipmaps", GenerateMipmaps }, // { "PremultiplyTextureAlpha", PremultiplyTextureAlpha }, // { "ResizeTexturesToPowerOfTwo", ResizeTexturesToPowerOfTwo }, // { "TextureFormat", TextureFormat }, // { "DefaultEffectType", DefaultEffectType }, // { "DefaultEffectFile", DefaultEffectFile }, //}; return context.Convert<MaterialContent, DRMaterialContent>(material, typeof(DRMaterialProcessor).Name/*, processorParameters*/); }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { var customMaterial = new EffectMaterialContent(); var effectPath = Path.GetFullPath(effectFilename); customMaterial.Effect = new ExternalReference<EffectContent>(effectPath); var parameters = new OpaqueDataDictionary(); parameters["ColorKeyColor"] = ColorKeyColor; parameters["ColorKeyEnabled"] = ColorKeyEnabled; parameters["TextureFormat"] = TextureFormat; parameters["GenerateMipmaps"] = GenerateMipmaps; parameters["ResizeTexturesToPowerOfTwo"] = ResizeTexturesToPowerOfTwo; return context.Convert<MaterialContent, MaterialContent>( customMaterial, typeof(MaterialProcessor).Name, parameters); }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { var basicMaterial = new EffectMaterialContent { Effect = new ExternalReference<EffectContent>("../HMEngineContent/Shaders/BasicShader.fx") }; var processorParameters = new OpaqueDataDictionary(); processorParameters["ColorKeyColor"] = ColorKeyColor; processorParameters["ColorKeyEnabled"] = ColorKeyEnabled; processorParameters["TextureFormat"] = TextureFormat; processorParameters["GenerateMipmaps"] = GenerateMipmaps; processorParameters["ResizeTexturesToPowerOfTwo"] = ResizeTexturesToPowerOfTwo; // Copy any textures already added to the built in material to our custom basicMaterial foreach (KeyValuePair<String, ExternalReference<TextureContent>> texture in material.Textures) { basicMaterial.Textures.Add(texture.Key, texture.Value); } // Convert the material using the HMMaterialProcessor. This will perform any special texture processing we need return context.Convert<MaterialContent, MaterialContent>(basicMaterial, typeof (HMMaterialProcessor).Name, processorParameters); }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { EffectMaterialContent deferredShadingMaterial = new EffectMaterialContent(); deferredShadingMaterial.Effect = new ExternalReference <EffectContent>("RenderGBuffer.fx"); // copy the textures in the original material to the new normal mapping // material, if they are relevant to our renderer. The // LookUpTextures function has added the normal map and specular map // textures to the Textures collection, so that will be copied as well. foreach (KeyValuePair <String, ExternalReference <TextureContent> > texture in material.Textures) { if ((texture.Key == "Texture") || (texture.Key == "NormalMap") || (texture.Key == "SpecularMap")) { deferredShadingMaterial.Textures.Add(texture.Key, texture.Value); } } return(context.Convert <MaterialContent, MaterialContent>(deferredShadingMaterial, typeof(MaterialProcessor).Name)); }
/// <summary> /// Use our custom material processor /// to convert selected materials in this model. /// </summary> protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { if (CustomMaterialProcessor == "") return base.ConvertMaterial(material, context); OpaqueDataDictionary processorParameters = new OpaqueDataDictionary(); processorParameters.Add("Section", section); processorParameters.Add("Skinned", skinned); return context.Convert<MaterialContent, MaterialContent>(material, CustomMaterialProcessor, processorParameters); }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { EffectMaterialContent deferredShadingMaterial = new EffectMaterialContent(); deferredShadingMaterial.Effect = new ExternalReference<EffectContent>("System/Effects/GBuffer.fx"); foreach (KeyValuePair<string, ExternalReference<TextureContent>> texture in material.Textures) { // MonoGame has a bug where texture names must be named after the texture sampler in the shader // // Example: // texture Texture; // sampler AlbedoSampler = sampler_state // { // texture = <Texture>; // MINFILTER = LINEAR; // MAGFILTER = LINEAR; // MIPFILTER = LINEAR; // ADDRESSU = WRAP; // ADDRESSV = WRAP; // }; if (texture.Key == "Texture") { deferredShadingMaterial.Textures.Add("Texture", texture.Value); } if (texture.Key == "NormalMap") { deferredShadingMaterial.Textures.Add("NormalMap", texture.Value); } if (texture.Key == "SpecularMap") { deferredShadingMaterial.Textures.Add("SpecularMap", texture.Value); } } return context.Convert<MaterialContent, MaterialContent>(deferredShadingMaterial, "MGMaterialProcessor"); }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { EffectMaterialContent Material = new EffectMaterialContent(); Material.Effect = new ExternalReference<EffectContent>(effect); foreach (KeyValuePair<String, ExternalReference<TextureContent>> texture in material.Textures) { if ((texture.Key == "Texture")) { Material.Textures.Add(diffuseMapKey, texture.Value); Material.Textures.Add(normalMapKey, new ExternalReference<TextureContent>(texture.Value.Filename.Substring(0, texture.Value.Filename.Length - 4) + "_N.jpg")); Material.Textures.Add(heightMapKey, new ExternalReference<TextureContent>(texture.Value.Filename.Substring(0, texture.Value.Filename.Length - 4) + "_H.jpg")); } } return context.Convert<MaterialContent, MaterialContent>(Material, typeof(MaterialProcessor).Name); }
/// <summary> /// Generates skydome geometry for an input sky texture. /// </summary> public override SkyContent Process(Texture2DContent input, ContentProcessorContext context) { MeshBuilder builder = MeshBuilder.StartMesh("sky"); // Create two rings of vertices around the top and bottom of the cylinder. List <int> topVertices = new List <int>(); List <int> bottomVertices = new List <int>(); for (int i = 0; i < cylinderSegments; i++) { float angle = MathHelper.TwoPi * i / cylinderSegments; float x = (float)Math.Cos(angle) * cylinderSize; float z = (float)Math.Sin(angle) * cylinderSize; topVertices.Add(builder.CreatePosition(x, cylinderSize * 5 / 12, z)); bottomVertices.Add(builder.CreatePosition(x, -cylinderSize * 5 / 12, z)); } // Create two center vertices, used for closing the top and bottom. int topCenterVertex = builder.CreatePosition(0, cylinderSize, 0); int bottomCenterVertex = builder.CreatePosition(0, -cylinderSize, 0); // Create a vertex channel for holding texture coordinates. int texCoordId = builder.CreateVertexChannel <Vector2>( VertexChannelNames.TextureCoordinate(0)); // Create the individual triangles that make up our skydome. for (int i = 0; i < cylinderSegments; i++) { int j = (i + 1) % cylinderSegments; // Calculate texture coordinates for this segment of the cylinder. float u1 = (float)i / (float)cylinderSegments; float u2 = (float)(i + 1) / (float)cylinderSegments; // Two triangles form a quad, one side segment of the cylinder. AddVertex(builder, topVertices[i], texCoordId, u1, texCoordTop); AddVertex(builder, topVertices[j], texCoordId, u2, texCoordTop); AddVertex(builder, bottomVertices[i], texCoordId, u1, texCoordBottom); AddVertex(builder, topVertices[j], texCoordId, u2, texCoordTop); AddVertex(builder, bottomVertices[j], texCoordId, u2, texCoordBottom); AddVertex(builder, bottomVertices[i], texCoordId, u1, texCoordBottom); // Triangle fanning inward to fill the top above this segment. AddVertex(builder, topCenterVertex, texCoordId, u1, 0); AddVertex(builder, topVertices[j], texCoordId, u2, texCoordTop); AddVertex(builder, topVertices[i], texCoordId, u1, texCoordTop); // Triangle fanning inward to fill the bottom below this segment. AddVertex(builder, bottomCenterVertex, texCoordId, u1, 1); AddVertex(builder, bottomVertices[i], texCoordId, u1, texCoordBottom); AddVertex(builder, bottomVertices[j], texCoordId, u2, texCoordBottom); } // Create the output object. SkyContent sky = new SkyContent(); // Chain to the ModelProcessor so it can convert the mesh we just generated. MeshContent skyMesh = builder.FinishMesh(); sky.Model = context.Convert <MeshContent, ModelContent>(skyMesh, "ModelProcessor"); input.ConvertBitmapType(typeof(Dxt1BitmapContent)); sky.Texture = input; return(sky); }
/// <summary> /// Loads the specified effect into the material. /// </summary> protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { EffectMaterialContent newMaterial = new EffectMaterialContent(); if (string.IsNullOrWhiteSpace(mEffectName)) { throw new ArgumentNullException("Effect asset undefined."); } newMaterial.Effect = new ExternalReference<EffectContent>(mEffectName); foreach (KeyValuePair<string, ExternalReference<TextureContent>> pair in material.Textures) { newMaterial.Textures.Add(pair.Key, pair.Value); } return context.Convert<MaterialContent, MaterialContent>(newMaterial, typeof(MaterialProcessor).Name); }
public override ModelContent Process(TrackDefinition input, ContentProcessorContext context) { int lanes = 3; float laneThickness = 0.9f; float interLaneSpace = 3f; float trackThickness = 1f; float laneWidth = 0.5f; MeshBuilder builder = MeshBuilder.StartMesh("terrain"); // extend the key points to vertices List <Vector3> trackPoints = GenerateTrackPoints(input.verts); List <Vector3> trackVerts = GenerateTrackVertices(trackPoints, trackThickness, lanes, laneThickness, laneWidth, interLaneSpace); List <Matrix> startPositions = GenerateStartingPositions(trackPoints, lanes, laneWidth, interLaneSpace); // add vertices to mesh foreach (Vector3 v in trackVerts) { builder.CreatePosition(v); } // Create a material TODO:point it at our terrain texture. BasicMaterialContent material = new BasicMaterialContent(); String texturePath = Path.Combine(Path.GetDirectoryName(input.filename), "tex.jpg"); material.Texture = new ExternalReference <TextureContent>(texturePath); material.SpecularColor = Color.Black.ToVector3(); material.SpecularPower = 0; builder.SetMaterial(material); int texCoordId = builder.CreateVertexChannel <Vector2>(VertexChannelNames.TextureCoordinate(0)); int PART_OFFSET = 4 * (lanes + 1); // define some constants: int INT = 0; // near face, inner side, top point int INB = 1; // near face, inner side, bottom point int ONT = 2; // near face, outer side, top point int ONB = 3; // near face, outer side, bottom point int IFT = INT + PART_OFFSET; // far face, inner side, top point int IFB = INB + PART_OFFSET; // far face, inner side, bottom point int OFT = ONT + PART_OFFSET; // far face, outer side, top point int OFB = ONB + PART_OFFSET; // far face, outer side, bottom point // setup the triangles: for (int i = 0; i < trackVerts.Count - PART_OFFSET; i += PART_OFFSET) { // triangle mapping: /* * See picture for vertices order */ //float dx, dz; //// for outer: //dx = trackVerts[i + 1].X - trackVerts[i + 5].X; //dz = trackVerts[i + 1].Z - trackVerts[i + 5].Z; //float vertDistOuter = (float)Math.Sqrt((dx * dx) + (dz * dz)) / halfTrackWidth; //// for inner: //dx = trackVerts[i + 0].X - trackVerts[i + 4].X; //dz = trackVerts[i + 0].Z - trackVerts[i + 4].Z; //float vertDistInner = (float)Math.Sqrt((dx * dx) + (dz * dz)) / halfTrackWidth; // side triangles: for (int j = 0; j <= lanes; j++) // less than or equal, since there are n lanes plus 1 for the entire track { int offset = i + 4 * j; // i.e. each lane has 4 points, thus offset is 4 per lane. // inner side builder.AddTriangle(offset, INT, IFT, INB, texCoordId); builder.AddTriangle(offset, INB, IFT, IFB, texCoordId); // outer side builder.AddTriangle(offset, ONT, ONB, OFT, texCoordId); builder.AddTriangle(offset, ONB, OFB, OFT, texCoordId); } // bottom face: builder.AddTriangle(i, INB, OFB + PART_OFFSET - 4, ONB + PART_OFFSET - 4, texCoordId); builder.AddTriangle(i, IFB, OFB + PART_OFFSET - 4, INB, texCoordId); // Top faces bool laneGroove = false; for (int j = 0; j < PART_OFFSET - 2; j += 2) { int offset = i + j; if (laneGroove) { builder.AddTriangle(offset, INB, ONB, IFB, texCoordId); builder.AddTriangle(offset, ONB, OFB, IFB, texCoordId); } else { builder.AddTriangle(offset, INT, ONT, IFT, texCoordId, 0.01f, 0.01f, 0.01f, 0.99f, 0.99f, 0.99f); builder.AddTriangle(offset, ONT, OFT, IFT, texCoordId, 0.01f, 0.01f, 0.01f, 0.99f, 0.99f, 0.99f); } // alternate groove and non-groove laneGroove = !laneGroove; } } MeshContent terrainMesh = builder.FinishMesh(); ModelContent mc = context.Convert <MeshContent, ModelContent>(terrainMesh, "ModelProcessor"); mc.Tag = startPositions; return(mc); }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { EffectMaterialContent normalMappingMaterial = new EffectMaterialContent(); string effectDirectory = Path.Combine(directory, "../../Effects/"); normalMappingMaterial.Effect = new ExternalReference<EffectContent> (Path.Combine(effectDirectory, "NormalMapping.fx")); OpaqueDataDictionary processorParameters = new OpaqueDataDictionary(); processorParameters["ColorKeyColor"] = this.ColorKeyColor; processorParameters["ColorKeyEnabled"] = this.ColorKeyEnabled; processorParameters["TextureFormat"] = this.TextureFormat; processorParameters["GenerateMipmaps"] = this.GenerateMipmaps; processorParameters["ResizeTexturesToPowerOfTwo"] = this.ResizeTexturesToPowerOfTwo; // copy the textures in the original material to the new normal mapping // material. this way the diffuse texture is preserved. The // PreprocessSceneHierarchy function has already added the normal map // texture to the Textures collection, so that will be copied as well. foreach (KeyValuePair<String, ExternalReference<TextureContent>> texture in material.Textures) { normalMappingMaterial.Textures.Add(texture.Key, texture.Value); } // and convert the material using the NormalMappingMaterialProcessor, // who has something special in store for the normal map. return context.Convert<MaterialContent, MaterialContent> (normalMappingMaterial, typeof(NormalMappingMaterialProcessor).Name, processorParameters); }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { string effectFile = this.effectFileName; if (string.IsNullOrEmpty(effectFile)) { if (string.IsNullOrEmpty(this.normalMapTexture)) { if (string.IsNullOrEmpty(this.diffuseTexture)) effectFile = "Effects\\DefaultSolidColor.fx"; else effectFile = "Effects\\Default.fx"; } else effectFile = "Effects\\DefaultNormalMap.fx"; } EffectMaterialContent normalMappingMaterial = new EffectMaterialContent(); normalMappingMaterial.Effect = new ExternalReference<EffectContent>(Path.Combine(directory, effectFile)); OpaqueDataDictionary processorParameters = new OpaqueDataDictionary(); processorParameters["ColorKeyColor"] = this.ColorKeyColor; processorParameters["ColorKeyEnabled"] = this.ColorKeyEnabled; processorParameters["TextureFormat"] = this.TextureFormat; processorParameters["PremultiplyTextureAlpha"] = this.PremultiplyTextureAlpha; processorParameters["PremultiplyVertexColors"] = this.PremultiplyVertexColors; processorParameters["GenerateMipmaps"] = this.GenerateMipmaps; processorParameters["ResizeTexturesToPowerOfTwo"] = this.ResizeTexturesToPowerOfTwo; // copy the textures in the original material to the new normal mapping // material. this way the diffuse texture is preserved. The // PreprocessSceneHierarchy function has already added the normal map // texture to the Textures collection, so that will be copied as well. foreach (KeyValuePair<String, ExternalReference<TextureContent>> texture in material.Textures) normalMappingMaterial.Textures.Add(texture.Key, texture.Value); if (!string.IsNullOrEmpty(diffuseTexture)) normalMappingMaterial.Textures.Add("DiffuseTexture", new ExternalReference<TextureContent>(Path.Combine(directory, diffuseTexture))); // and convert the material using the NormalMappingMaterialProcessor, // who has something special in store for the normal map. #if MONOGAME return context.Convert<MaterialContent, MaterialContent>(normalMappingMaterial, typeof(MGMaterialProcessor).Name, processorParameters); #else return context.Convert<MaterialContent, MaterialContent>(normalMappingMaterial, typeof(MaterialProcessor).Name, processorParameters); #endif }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { EffectMaterialContent deferredShadingMaterial = new EffectMaterialContent(); deferredShadingMaterial.Effect = new ExternalReference<EffectContent>("System/Effects/GBuffer.fx"); foreach (KeyValuePair<string, ExternalReference<TextureContent>> texture in material.Textures) { if (texture.Key == "Texture" || texture.Key == "NormalMap" || texture.Key == "SpecularMap") { deferredShadingMaterial.Textures.Add(texture.Key, texture.Value); } } return context.Convert<MaterialContent, MaterialContent>(deferredShadingMaterial, typeof(MaterialProcessor).Name); }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { EffectMaterialContent normalMappingMaterial = new EffectMaterialContent(); normalMappingMaterial.Effect = new ExternalReference<EffectContent> ("shaders/NormalMapping.fx"); // copy the textures in the original material to the new normal mapping // material. this way the diffuse texture is preserved. The // PreprocessSceneHierarchy function has already added the normal map // texture to the Textures collection, so that will be copied as well. foreach (KeyValuePair<String, ExternalReference<TextureContent>> texture in material.Textures) { normalMappingMaterial.Textures.Add(texture.Key, texture.Value); } return context.Convert<MaterialContent, MaterialContent> (normalMappingMaterial, typeof(MaterialProcessor).Name); }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { EffectMaterialContent lppMaterial = new EffectMaterialContent(); OpaqueDataDictionary processorParameters = new OpaqueDataDictionary(); processorParameters["ColorKeyColor"] = this.ColorKeyColor; processorParameters["ColorKeyEnabled"] = false; processorParameters["TextureFormat"] = this.TextureFormat; processorParameters["GenerateMipmaps"] = this.GenerateMipmaps; processorParameters["ResizeTexturesToPowerOfTwo"] = this.ResizeTexturesToPowerOfTwo; processorParameters["PremultiplyTextureAlpha"] = false; processorParameters["ColorKeyEnabled"] = false; lppMaterial.Effect = new ExternalReference<EffectContent>(_customFx.Length == 0 ? "shaders/LPPMainEffect.fx" : _customFx); lppMaterial.CompiledEffect = context.BuildAsset<EffectContent, CompiledEffectContent>(lppMaterial.Effect, "EffectProcessor"); // copy the textures in the original material to the new lpp // material ExtractTextures(lppMaterial, material); //extract the extra parameters ExtractDefines(lppMaterial, material, context); // and convert the material using the NormalMappingMaterialProcessor, // who has something special in store for the normal map. return context.Convert<MaterialContent, MaterialContent> (lppMaterial, typeof(LightPrePassMaterialProcessor).Name, processorParameters); }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { EffectMaterialContent deferredShadingMaterial = new EffectMaterialContent(); deferredShadingMaterial.Effect = new ExternalReference<EffectContent>("Shaders\\RenderGBuffer.fx"); //Copy textures in the original material to the new normal mapping material if they are relevant to our renderer. //The LookUpTextures function has added the normal map and specular map textures to the Textures colletion so that will be copied as well foreach (var kvp in material.Textures) { if ((kvp.Key == "Texture") || (kvp.Key == "NormalMap") || (kvp.Key == "SpecularMap")) { deferredShadingMaterial.Textures.Add(kvp.Key, kvp.Value); } } return context.Convert<MaterialContent, MaterialContent>(deferredShadingMaterial, typeof(MaterialProcessor).Name); }
//Convert the Material Content protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { //Initialize Effect Material Content EffectMaterialContent deferredShadingMaterial = new EffectMaterialContent(); //Set Effect to the Effect Material Content deferredShadingMaterial.Effect = new ExternalReference<EffectContent>("Effects/GBuffer.fx"); //Set the Textures to the Effect foreach (KeyValuePair<string, ExternalReference<TextureContent>> texture in material.Textures) { if ((texture.Key == "Texture") || (texture.Key == "NormalMap") || (texture.Key == "SpecularMap")) deferredShadingMaterial.Textures.Add(texture.Key, texture.Value); } //Return Converted Material return context.Convert<MaterialContent, MaterialContent>(deferredShadingMaterial, typeof(MaterialProcessor).Name); }
public static ModelContent CreateCapsule(Vector3 startPositionMajor, Vector3 endPositionMajor, Vector3 minorAxisDirection, float radius, ContentProcessorContext context) { MeshBuilder builder = MeshBuilder.StartMesh("BoundingCapsule"); Vector3 majorAxisDirection = startPositionMajor - endPositionMajor; majorAxisDirection.Normalize(); minorAxisDirection.Normalize(); Vector3 forward = minorAxisDirection; Vector3 backward = -minorAxisDirection; Vector3 left = Vector3.Cross(majorAxisDirection, minorAxisDirection); Vector3 right = -left; Vector3 ringStart; #region Set Vertices builder.CreatePosition(startPositionMajor + radius * majorAxisDirection); #region Top Hemisphere Midpoints ringStart = Vector3.Transform(forward * radius, Matrix.CreateFromAxisAngle(right, MathHelper.ToRadians(45f))); for (int i = 0; i < 8; i++) { builder.CreatePosition(startPositionMajor + Vector3.Transform(ringStart, Matrix.CreateFromAxisAngle(majorAxisDirection, (MathHelper.TwoPi * i / 8f)))); } #endregion #region Cylinder Startpoints for (int i = 0; i < 8; i++) { builder.CreatePosition(startPositionMajor + Vector3.Transform(forward * radius, Matrix.CreateFromAxisAngle(majorAxisDirection, (MathHelper.TwoPi * i / 8f)))); } #endregion #region Cylinder End Points for (int i = 0; i < 8; i++) { builder.CreatePosition(endPositionMajor + Vector3.Transform(forward * radius, Matrix.CreateFromAxisAngle(majorAxisDirection, (MathHelper.TwoPi * i / 8f)))); } #endregion #region Bottom Hemisphere Midpoints ringStart = Vector3.Transform(forward * radius, Matrix.CreateFromAxisAngle(right, MathHelper.ToRadians(-45f))); for (int i = 0; i < 8; i++) { builder.CreatePosition(endPositionMajor + Vector3.Transform(ringStart, Matrix.CreateFromAxisAngle(majorAxisDirection, (MathHelper.TwoPi * i / 8f)))); } #endregion builder.CreatePosition(endPositionMajor - radius * majorAxisDirection); #endregion #region Set Triangles #region Top Hemisphere for (int i = 1; i < 8; i++) { AddVertex(builder, (0)); AddVertex(builder, (i)); AddVertex(builder, (i + 1)); } AddVertex(builder, (0)); AddVertex(builder, (8)); AddVertex(builder, (1)); for (int i = 1; i < 8; i++) { AddVertex(builder, (i)); AddVertex(builder, (i + 8)); AddVertex(builder, (i + 1)); AddVertex(builder, (i + 8)); AddVertex(builder, (i + 9)); AddVertex(builder, (i + 1)); } AddVertex(builder, (1)); AddVertex(builder, (8)); AddVertex(builder, (8 + 8)); AddVertex(builder, (1)); AddVertex(builder, (8 + 8)); AddVertex(builder, (8 + 1)); #endregion #region Cylinder for (int i = 9; i < 16; i++) { AddVertex(builder, (i)); AddVertex(builder, (i + 8)); AddVertex(builder, (i + 1)); AddVertex(builder, (i + 8)); AddVertex(builder, (i + 9)); AddVertex(builder, (i + 1)); } AddVertex(builder, (9)); AddVertex(builder, (16)); AddVertex(builder, (16 + 8)); AddVertex(builder, (9)); AddVertex(builder, (16 + 8)); AddVertex(builder, (16 + 1)); #endregion #region Bottom Hemisphere for (int i = 17; i < 24; i++) { AddVertex(builder, (i)); AddVertex(builder, (i + 8)); AddVertex(builder, (i + 1)); AddVertex(builder, (i + 8)); AddVertex(builder, (i + 9)); AddVertex(builder, (i + 1)); } AddVertex(builder, (17)); AddVertex(builder, (24)); AddVertex(builder, (24 + 8)); AddVertex(builder, (17)); AddVertex(builder, (24 + 8)); AddVertex(builder, (24 + 1)); for (int i = 25; i < 32; i++) { AddVertex(builder, (33)); AddVertex(builder, (i + 1)); AddVertex(builder, (i)); } AddVertex(builder, (25)); AddVertex(builder, (32)); AddVertex(builder, (33)); #endregion #endregion MeshContent mesh = builder.FinishMesh(); return(context.Convert <MeshContent, ModelContent>(mesh, "ModelProcessor"));; }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { EffectMaterialContent lppMaterial = new EffectMaterialContent(); OpaqueDataDictionary processorParameters = new OpaqueDataDictionary(); processorParameters["ColorKeyColor"] = this.ColorKeyColor; processorParameters["ColorKeyEnabled"] = this.ColorKeyEnabled; processorParameters["TextureFormat"] = this.TextureFormat; processorParameters["GenerateMipmaps"] = this.GenerateMipmaps; processorParameters["ResizeTexturesToPowerOfTwo"] = this.ResizeTexturesToPowerOfTwo; processorParameters["PremultiplyTextureAlpha"] = false; processorParameters["ColorKeyEnabled"] = false; lppMaterial.Effect = new ExternalReference<EffectContent>("shaders/LPPMainEffect.fx"); lppMaterial.CompiledEffect = context.BuildAsset<EffectContent, CompiledEffectContent>(lppMaterial.Effect, "EffectProcessor"); //extract the extra parameters ExtractDefines(lppMaterial, material, context); // copy the textures in the original material to the new normal mapping // material. this way the diffuse texture is preserved. The // PreprocessSceneHierarchy function has already added the normal map // texture to the Textures collection, so that will be copied as well. foreach (KeyValuePair<String, ExternalReference<TextureContent>> texture in material.Textures) { lppMaterial.Textures.Add(texture.Key, texture.Value); } try { lppMaterial.OpaqueData.Add("DiffuseColor", new Vector4((Vector3)material.OpaqueData["DiffuseColor"], (float)material.OpaqueData["Alpha"])); lppMaterial.OpaqueData.Add("SpecularColor", material.OpaqueData["SpecularColor"]); lppMaterial.OpaqueData.Add("SpecularPower", material.OpaqueData["SpecularPower"]); } catch (Exception ex) { Console.WriteLine(ex); } // and convert the material using the NormalMappingMaterialProcessor, // who has something special in store for the normal map. return context.Convert<MaterialContent, MaterialContent> (lppMaterial, typeof(LightPrePassMaterialProcessor).Name, processorParameters); }
/// <summary> /// Generates a terrain mesh from an input heightfield texture. /// </summary> public override ModelContent Process(Texture2DContent input, ContentProcessorContext context) { MeshBuilder builder = MeshBuilder.StartMesh("terrain"); // Convert the input texture to float format, for ease of processing. input.ConvertBitmapType(typeof(PixelBitmapContent <float>)); PixelBitmapContent <float> heightfield; heightfield = (PixelBitmapContent <float>)input.Mipmaps[0]; // Create the terrain vertices. for (int y = 0; y < heightfield.Height; y++) { for (int x = 0; x < heightfield.Width; x++) { Vector3 position; // position the vertices so that the heightfield is centered // around x=0,z=0 position.X = terrainScale * (x - ((heightfield.Width - 1) / 2.0f)); position.Z = terrainScale * (y - ((heightfield.Height - 1) / 2.0f)); position.Y = (heightfield.GetPixel(x, y) - 1) * terrainBumpiness; builder.CreatePosition(position); } } // Create a material, and point it at our terrain texture. BasicMaterialContent material = new BasicMaterialContent(); material.SpecularColor = new Vector3(.4f, .4f, .4f); string directory = Path.GetDirectoryName(input.Identity.SourceFilename); string texture = Path.Combine(directory, terrainTexture); material.Texture = new ExternalReference <TextureContent>(texture); builder.SetMaterial(material); // Create a vertex channel for holding texture coordinates. int texCoordId = builder.CreateVertexChannel <Vector2>( VertexChannelNames.TextureCoordinate(0)); // Create the individual triangles that make up our terrain. for (int y = 0; y < heightfield.Height - 1; y++) { for (int x = 0; x < heightfield.Width - 1; x++) { AddVertex(builder, texCoordId, heightfield.Width, x, y); AddVertex(builder, texCoordId, heightfield.Width, x + 1, y); AddVertex(builder, texCoordId, heightfield.Width, x + 1, y + 1); AddVertex(builder, texCoordId, heightfield.Width, x, y); AddVertex(builder, texCoordId, heightfield.Width, x + 1, y + 1); AddVertex(builder, texCoordId, heightfield.Width, x, y + 1); } } // Chain to the ModelProcessor so it can convert the mesh we just generated. MeshContent terrainMesh = builder.FinishMesh(); ModelContent model = context.Convert <MeshContent, ModelContent>(terrainMesh, "ModelProcessor"); // generate information about the height map, and attach it to the finished // model's tag. model.Tag = new HeightMapInfoContent(heightfield, terrainScale, terrainBumpiness); return(model); }
/// <summary> /// Generates skydome geometry for an input sky texture. /// </summary> public override SkyContent Process(Texture2DContent input, ContentProcessorContext context) { MeshBuilder builder = MeshBuilder.StartMesh("sky"); // Create two rings of vertices around the top and bottom of the cylinder. List <int> topVertices = new List <int>(); List <int> bottomVertices = new List <int>(); for (int i = 0; i < cylinderSegments; i++) { float angle = MathHelper.TwoPi * i / cylinderSegments; float x = (float)Math.Cos(angle) * cylinderSize; float z = (float)Math.Sin(angle) * cylinderSize; topVertices.Add(builder.CreatePosition(x, cylinderSize, z)); bottomVertices.Add(builder.CreatePosition(x, -cylinderSize, z)); } // Create two center vertices, used for closing the top and bottom. int topCenterVertex = builder.CreatePosition(0, cylinderSize * 2, 0); int bottomCenterVertex = builder.CreatePosition(0, -cylinderSize * 2, 0); // Create a custom effect material, and point it at our sky rendering // effect file. The default ModelProcessor will compile sky.fx for us. EffectMaterialContent material = new EffectMaterialContent(); string directory = Path.GetDirectoryName(input.Identity.SourceFilename); string effect = Path.Combine(directory, "Shaders\\Sky.fx"); material.Effect = new ExternalReference <EffectContent>(effect); builder.SetMaterial(material); // Create a vertex channel for holding texture coordinates. int texCoordId = builder.CreateVertexChannel <Vector2>( VertexChannelNames.TextureCoordinate(0)); // Create the individual triangles that make up our skydome. for (int i = 0; i < cylinderSegments; i++) { int j = (i + 1) % cylinderSegments; // Calculate texture coordinates for this segment of the cylinder. float u1 = (float)i / (float)cylinderSegments; float u2 = (float)(i + 1) / (float)cylinderSegments; // Two triangles form a quad, one side segment of the cylinder. AddVertex(builder, topVertices[i], texCoordId, u1, texCoordTop); AddVertex(builder, topVertices[j], texCoordId, u2, texCoordTop); AddVertex(builder, bottomVertices[i], texCoordId, u1, texCoordBottom); AddVertex(builder, topVertices[j], texCoordId, u2, texCoordTop); AddVertex(builder, bottomVertices[j], texCoordId, u2, texCoordBottom); AddVertex(builder, bottomVertices[i], texCoordId, u1, texCoordBottom); // Triangle fanning inward to fill the top above this segment. AddVertex(builder, topCenterVertex, texCoordId, u1, 0); AddVertex(builder, topVertices[j], texCoordId, u2, texCoordTop); AddVertex(builder, topVertices[i], texCoordId, u1, texCoordTop); // Triangle fanning inward to fill the bottom below this segment. AddVertex(builder, bottomCenterVertex, texCoordId, u1, 1); AddVertex(builder, bottomVertices[i], texCoordId, u1, texCoordBottom); AddVertex(builder, bottomVertices[j], texCoordId, u2, texCoordBottom); } // Create the output object. SkyContent sky = new SkyContent(); // Chain to the ModelProcessor so it can convert the mesh we just generated. MeshContent skyMesh = builder.FinishMesh(); sky.Model = context.Convert <MeshContent, ModelContent>(skyMesh, "ModelProcessor"); // Chain to the TextureProcessor so it can convert the sky // texture. We don't use the default ModelTextureProcessor // here, because that would apply DXT compression, which // doesn't usually look very good with sky images. // Note: This could also be accomplished by creating a custom ModelProcessor // that would process its textures with the default TextureProcessor, // and adding the sky texture to the materials Textures dictionary. // For simplicity, the approach below is used instead. sky.Texture = context.Convert <TextureContent, TextureContent>(input, "TextureProcessor"); return(sky); }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { EffectMaterialContent shaderMaterial = new EffectMaterialContent(); shaderMaterial.Effect = new ExternalReference<EffectContent> ("Effects/ShaderModelEffect.fx"); // copy the textures in the original material to the new normal mapping // material. this way the diffuse texture is preserved. The // PreprocessSceneHierarchy function has already added the normal map // texture to the Textures collection, so that will be copied as well. foreach (KeyValuePair<String, ExternalReference<TextureContent>> texture in material.Textures) { shaderMaterial.Textures.Add(texture.Key, texture.Value); } // and convert the material using the ShaderMaterialProcessor, // who has something special in store for the normal map. return context.Convert<MaterialContent, MaterialContent> (shaderMaterial, typeof(ShaderMaterialProcessor).Name); }
/// <summary> /// Use our custom EnvironmentMappedMaterialProcessor /// to convert all the materials on this model. /// </summary> protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { return(context.Convert <MaterialContent, MaterialContent>(material, "ShipMaterialProcessor")); }
protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context) { EffectMaterialContent deferredShadingMaterial = new EffectMaterialContent(); deferredShadingMaterial.Effect = new ExternalReference<EffectContent>("../../GraphicsLibrary/GraphicsContent/Shaders/Deferred Rendering/RenderGBuffer.fx"); deferredShadingMaterial.CompiledEffect = context.BuildAsset<EffectContent, CompiledEffectContent>(deferredShadingMaterial.Effect, "EffectProcessor"); // copy the textures in the original material to the new normal mapping // material, if they are relevant to our renderer. The // LookUpTextures function has added the normal map and specular map // textures to the Textures collection, so that will be copied as well. foreach (KeyValuePair<String, ExternalReference<TextureContent>> texture in material.Textures) { if (texture.Key.Contains("DiffuseMap") || texture.Key.Contains("Texture")) deferredShadingMaterial.Textures.Add(texture.Key, texture.Value); if (texture.Key.Contains("NormalMap")) deferredShadingMaterial.Textures.Add(texture.Key, texture.Value); if (texture.Key.Contains("SpecularMap")) deferredShadingMaterial.Textures.Add(texture.Key, texture.Value); if (texture.Key.Contains("EmissiveMap")) deferredShadingMaterial.Textures.Add(texture.Key, texture.Value); } //extract the extra parameters ExtractDefines(deferredShadingMaterial, material, context); // Return material return context.Convert<MaterialContent, MaterialContent>(deferredShadingMaterial, typeof(DeferredRendererMaterialProcessor).Name); }