void CompressDefault <T>(TargetPlatform platform, Color color, int width = 16, int height = 16) { var context = new TestProcessorContext(platform, "dummy.xnb"); var processor = new TextureProcessor { ColorKeyEnabled = false, GenerateMipmaps = true, PremultiplyAlpha = false, ResizeToPowerOfTwo = false, TextureFormat = TextureProcessorOutputFormat.Compressed }; var face = new PixelBitmapContent <Color>(width, height); Fill(face, color); var input = new Texture2DContent(); input.Faces[0] = face; var output = processor.Process(input, context); Assert.NotNull(output); Assert.AreEqual(1, output.Faces.Count, "Expected number of faces"); Assert.AreEqual(5, output.Faces[0].Count, "Expected number of mipmaps"); Assert.IsAssignableFrom <T>(output.Faces[0][0], "Incorrect pixel format"); }
void CompressDefault <T>(TargetPlatform platform, Color color) { var context = new TestProcessorContext(platform, "dummy.xnb"); var processor = new TextureProcessor { ColorKeyEnabled = false, GenerateMipmaps = false, PremultiplyAlpha = false, ResizeToPowerOfTwo = false, TextureFormat = TextureProcessorOutputFormat.Compressed }; var face = new PixelBitmapContent <Color>(16, 16); Fill(face, color); var input = new Texture2DContent(); input.Faces[0] = face; var output = processor.Process(input, context); Assert.NotNull(output); Assert.AreEqual(1, output.Faces.Count); Assert.AreEqual(1, output.Faces[0].Count); Assert.IsAssignableFrom <T>(output.Faces[0][0]); }
public void MipmapNonSquareNonPowerOfTwo() { var context = new TestProcessorContext(TargetPlatform.Windows, "dummy.xnb"); var processor = new TextureProcessor { ColorKeyEnabled = false, GenerateMipmaps = true, PremultiplyAlpha = false, ResizeToPowerOfTwo = false, TextureFormat = TextureProcessorOutputFormat.Color }; var width = 23; var height = 5; var face = new PixelBitmapContent <Color>(width, height); Fill(face, Color.Red); var input = new Texture2DContent(); input.Faces[0] = face; var output = processor.Process(input, context); Assert.NotNull(output); Assert.AreEqual(1, output.Faces.Count); var outChain = output.Faces[0]; Assert.AreEqual(5, outChain.Count); foreach (var outFace in outChain) { Assert.AreEqual(width, outFace.Width); Assert.AreEqual(height, outFace.Height); var bitmap = (PixelBitmapContent <Color>)outFace; for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { Assert.AreEqual(Color.Red, bitmap.GetPixel(x, y)); } } if (width > 1) { width /= 2; } if (height > 1) { height /= 2; } } }
public static Effect CompileEffect(GraphicsDevice graphicsDevice, params string[] pathParts) { var effectProcessor = new EffectProcessor(); var context = new TestProcessorContext(TargetPlatform.Windows, "notused.xnb"); var effectPath = Paths.Effect(pathParts); var compiledEffect = effectProcessor.Process(new EffectContent { EffectCode = File.ReadAllText(effectPath), Identity = new ContentIdentity(effectPath) }, context); return(new Effect(graphicsDevice, compiledEffect.GetEffectCode())); }
public static Microsoft.Xna.Framework.Graphics.Effect CompileEffect(GraphicsDevice graphicsDevice, string effectPath) { #if !WINDOWS || DIRECTX || XNA var effectProcessor = new EffectProcessor(); var context = new TestProcessorContext(TargetPlatform.Windows, "notused.xnb"); var compiledEffect = effectProcessor.Process(new EffectContent { EffectCode = File.ReadAllText(effectPath), Identity = new ContentIdentity(effectPath) }, context); return(new Microsoft.Xna.Framework.Graphics.Effect(graphicsDevice, compiledEffect.GetEffectCode())); #else // OpenGL throw new NotImplementedException(); #endif }
private void BuildEffect(string effectFile, TargetPlatform targetPlatform, string defines = null) { var importerContext = new ImporterContext(); var importer = new EffectImporter(); var input = importer.Import(effectFile, importerContext); Assert.NotNull(input); var processorContext = new TestProcessorContext(targetPlatform, Path.ChangeExtension(effectFile, ".xnb")); var processor = new EffectProcessor { Defines = defines }; var output = processor.Process(input, processorContext); Assert.NotNull(output); // TODO: Should we test the writer? }
public void ColorKey() { var context = new TestProcessorContext(TargetPlatform.Windows, "dummy.xnb"); var processor = new TextureProcessor { ColorKeyColor = Color.Red, ColorKeyEnabled = true, GenerateMipmaps = false, PremultiplyAlpha = false, ResizeToPowerOfTwo = false, TextureFormat = TextureProcessorOutputFormat.Color }; var face = new PixelBitmapContent <Color>(8, 8); Fill(face, Color.Red); var input = new Texture2DContent(); input.Faces[0] = face; var output = processor.Process(input, context); Assert.NotNull(output); Assert.AreEqual(1, output.Faces.Count); Assert.AreEqual(1, output.Faces[0].Count); Assert.IsAssignableFrom <PixelBitmapContent <Color> >(output.Faces[0][0]); var outFace = (PixelBitmapContent <Color>)output.Faces[0][0]; Assert.AreEqual(8, outFace.Width); Assert.AreEqual(8, outFace.Height); for (var y = 0; y < outFace.Height; y++) { for (var x = 0; x < outFace.Width; x++) { Assert.AreEqual(Color.Transparent, outFace.GetPixel(x, y)); } } }
public void ColorConversion() { var context = new TestProcessorContext(TargetPlatform.Windows, "dummy.xnb"); var processor = new FontTextureProcessor { FirstCharacter = '0', PremultiplyAlpha = false }; var face = new PixelBitmapContent <Color>(32, 9); var O = Color.Transparent.PackedValue; var M = Color.Fuchsia.PackedValue; var R = Color.Red.PackedValue; var G = Color.Green.PackedValue; var B = Color.Blue.PackedValue; var W = Color.White.PackedValue; var pixelData = new[] { M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, O, O, O, O, O, O, M, M, O, O, O, O, O, O, M, M, O, O, O, O, O, O, M, M, O, O, O, O, O, O, M, M, O, O, W, W, O, O, M, M, O, O, O, R, O, O, M, M, O, G, G, G, O, O, M, M, O, B, B, B, B, O, M, M, O, W, O, O, W, O, M, M, O, O, R, R, O, O, M, M, O, O, O, O, G, O, M, M, O, O, O, B, O, O, M, M, O, W, O, O, W, O, M, M, O, O, O, R, O, O, M, M, O, O, G, G, O, O, M, M, O, O, B, B, O, O, M, M, O, W, O, O, W, O, M, M, O, O, O, R, O, O, M, M, O, G, O, O, O, O, M, M, O, O, O, O, B, O, M, M, O, O, W, W, O, O, M, M, O, R, R, R, R, O, M, M, O, G, G, G, G, O, M, M, O, B, B, B, O, O, M, M, O, O, O, O, O, O, M, M, O, O, O, O, O, O, M, M, O, O, O, O, O, O, M, M, O, O, O, O, O, O, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, }; var pixelBytes = new byte[face.Width * 4 * face.Height]; Buffer.BlockCopy(pixelData, 0, pixelBytes, 0, pixelData.Length * 4); face.SetPixelData(pixelBytes); var input = new Texture2DContent(); input.Faces[0] = face; var output = processor.Process(input, context); Assert.NotNull(output); Assert.AreEqual(output.CharacterMap[0], '0'); Assert.AreEqual(output.CharacterMap[1], '1'); Assert.AreEqual(output.CharacterMap[2], '2'); Assert.AreEqual(output.CharacterMap[3], '3'); Assert.AreEqual(1, output.Texture.Faces.Count); Assert.AreEqual(1, output.Texture.Faces[0].Count); Assert.IsAssignableFrom <PixelBitmapContent <Color> >(output.Texture.Faces[0][0]); var outFace = (PixelBitmapContent <Color>)output.Texture.Faces[0][0]; for (var i = 0; i < 4; ++i) { // (2, 2, 4, 5) is the top,left and width,height of the first glyph. All test glyphs are 4x5 var inRect = new Rectangle(i * 8 + 2, 2, 4, 5); var outRect = output.Glyphs[i]; Assert.AreEqual(inRect.Width, outRect.Width); Assert.AreEqual(inRect.Height, outRect.Height); for (var y = 0; y < inRect.Height; ++y) { for (var x = 0; x < inRect.Width; ++x) { Assert.AreEqual(pixelData[(x + inRect.Left) + (y + inRect.Top) * face.Width], outFace.GetPixel(x + outRect.Left, y + outRect.Top).PackedValue); } } } Assert.AreEqual(new Rectangle(1, 1, 6, 7), output.Cropping[0]); Assert.AreEqual(new Rectangle(1, 1, 6, 7), output.Cropping[1]); Assert.AreEqual(new Rectangle(1, 1, 6, 7), output.Cropping[2]); Assert.AreEqual(new Rectangle(1, 1, 6, 7), output.Cropping[3]); }
public void BasicMeshTests() { var input = CreateBasicMesh(); var processorContext = new TestProcessorContext(TargetPlatform.Windows, "dummy.xnb"); var processor = new ModelProcessor { RotationX = 10, RotationY = 20, RotationZ = 30 }; var output = processor.Process(input, processorContext); // The transform the processor above is applying to the model. var processorXform = Matrix.CreateRotationZ(MathHelper.ToRadians(30)) * Matrix.CreateRotationX(MathHelper.ToRadians(10)) * Matrix.CreateRotationY(MathHelper.ToRadians(20)); // Test some basics. Assert.NotNull(output); Assert.NotNull(output.Meshes); Assert.AreEqual(2, output.Meshes.Count); Assert.NotNull(output.Bones); Assert.AreEqual(3, output.Bones.Count); Assert.NotNull(output.Root); Assert.AreEqual(output.Root, output.Bones[0]); // Stuff to make the tests below cleaner. var inputMesh1 = input.Children[0] as MeshContent; Assert.NotNull(inputMesh1); var inputMesh2 = input.Children[1] as MeshContent; Assert.NotNull(inputMesh2); // Test the bones. Assert.AreEqual("Root", output.Bones[0].Name); Assert.That(input.Transform, Is.EqualTo(output.Bones[0].Transform).Using(MatrixComparer.Epsilon)); Assert.AreEqual("Mesh1", output.Bones[1].Name); Assert.That(Matrix.Identity, Is.EqualTo(output.Bones[1].Transform).Using(MatrixComparer.Epsilon)); Assert.AreEqual("Mesh2", output.Bones[2].Name); Assert.That(Matrix.Identity, Is.EqualTo(output.Bones[2].Transform).Using(MatrixComparer.Epsilon)); // Test the first mesh. { var mesh = output.Meshes[0]; Assert.AreEqual("Mesh1", mesh.Name); Assert.AreEqual(output.Bones[1], mesh.ParentBone); Assert.AreEqual(inputMesh1, mesh.SourceMesh); Assert.AreEqual(new BoundingSphere(Vector3.Zero, 0), mesh.BoundingSphere); Assert.NotNull(mesh.MeshParts); Assert.AreEqual(1, mesh.MeshParts.Count); var part = mesh.MeshParts[0]; Assert.NotNull(part); Assert.IsNull(part.IndexBuffer); Assert.IsNull(part.VertexBuffer); Assert.AreEqual(0, part.NumVertices); Assert.AreEqual(0, part.PrimitiveCount); Assert.AreEqual(0, part.StartIndex); Assert.AreEqual(0, part.VertexOffset); Assert.IsAssignableFrom <BasicMaterialContent>(part.Material); var material = part.Material as BasicMaterialContent; Assert.NotNull(material); Assert.IsNotEmpty(material.OpaqueData); Assert.IsNull(material.Name); Assert.IsNull(material.Identity); Assert.IsNull(material.Alpha); Assert.IsNull(material.DiffuseColor); Assert.IsNull(material.EmissiveColor); Assert.IsNull(material.SpecularColor); Assert.IsNull(material.SpecularPower); Assert.IsNull(material.Texture); Assert.IsEmpty(material.Textures); Assert.IsTrue(material.OpaqueData.ContainsKey("VertexColorEnabled")); Assert.IsNotNull(material.VertexColorEnabled); Assert.IsFalse(material.VertexColorEnabled.Value); } // Test the second mesh. { var mesh = output.Meshes[1]; Assert.AreEqual("Mesh2", mesh.Name); Assert.AreEqual(output.Bones[2], mesh.ParentBone); Assert.AreEqual(inputMesh2, mesh.SourceMesh); Assert.That(new BoundingSphere(new Vector3(0.3809527f, 0.5858122f, 0.5115654f), 0.8660253f), Is.EqualTo(mesh.BoundingSphere).Using(BoundingSphereComparer.Epsilon)); Assert.NotNull(mesh.MeshParts); Assert.AreEqual(1, mesh.MeshParts.Count); var part = mesh.MeshParts[0]; Assert.NotNull(part); Assert.AreEqual(1, part.PrimitiveCount); Assert.AreEqual(0, part.StartIndex); Assert.AreEqual(0, part.VertexOffset); Assert.AreEqual(3, part.NumVertices); Assert.NotNull(part.IndexBuffer); Assert.AreEqual(3, part.IndexBuffer.Count); Assert.AreEqual(0, part.IndexBuffer[0]); Assert.AreEqual(1, part.IndexBuffer[1]); Assert.AreEqual(2, part.IndexBuffer[2]); Assert.NotNull(part.VertexBuffer); Assert.NotNull(part.VertexBuffer.VertexData); var vertexData = part.VertexBuffer.VertexData; Assert.AreEqual(36, vertexData.Length); var positionArray = ArrayUtil.ConvertTo <Vector3>(vertexData); Assert.AreEqual(3, positionArray.Length); Assert.AreEqual(Vector3.Transform(new Vector3(0, 0, 0), processorXform), positionArray[0]); Assert.AreEqual(Vector3.Transform(new Vector3(1, 0, 0), processorXform), positionArray[1]); Assert.AreEqual(Vector3.Transform(new Vector3(1, 1, 1), processorXform), positionArray[2]); Assert.IsAssignableFrom <BasicMaterialContent>(part.Material); var material = part.Material as BasicMaterialContent; Assert.NotNull(material); Assert.IsNotEmpty(material.OpaqueData); Assert.AreEqual("Material1", material.Name); Assert.IsNull(material.Identity); Assert.IsTrue(material.OpaqueData.ContainsKey("Alpha")); Assert.NotNull(material.Alpha); Assert.AreEqual(0.5f, material.Alpha.Value); Assert.IsTrue(material.OpaqueData.ContainsKey("DiffuseColor")); Assert.NotNull(material.DiffuseColor); Assert.AreEqual(Color.Red.ToVector3(), material.DiffuseColor.Value); Assert.IsNull(material.EmissiveColor); Assert.IsNull(material.SpecularColor); Assert.IsNull(material.SpecularPower); Assert.IsNull(material.Texture); Assert.IsEmpty(material.Textures); Assert.IsTrue(material.OpaqueData.ContainsKey("VertexColorEnabled")); Assert.IsNotNull(material.VertexColorEnabled); Assert.IsFalse(material.VertexColorEnabled.Value); } }
public void DefaultEffectTest() { NodeContent input; { input = new NodeContent(); var mesh = new MeshContent() { Name = "Mesh1" }; mesh.Positions.Add(new Vector3(0, 0, 0)); mesh.Positions.Add(new Vector3(1, 0, 0)); mesh.Positions.Add(new Vector3(1, 1, 1)); var geom = new GeometryContent(); geom.Vertices.Add(0); geom.Vertices.Add(1); geom.Vertices.Add(2); geom.Indices.Add(0); geom.Indices.Add(1); geom.Indices.Add(2); geom.Vertices.Channels.Add(VertexChannelNames.TextureCoordinate(0), new[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), }); var wieghts = new BoneWeightCollection(); wieghts.Add(new BoneWeight("bone1", 0.5f)); geom.Vertices.Channels.Add(VertexChannelNames.Weights(0), new[] { wieghts, wieghts, wieghts }); mesh.Geometry.Add(geom); input.Children.Add(mesh); var bone1 = new BoneContent { Name = "bone1", Transform = Matrix.CreateTranslation(0, 1, 0) }; input.Children.Add(bone1); var anim = new AnimationContent() { Name = "anim1", Duration = TimeSpan.Zero }; input.Animations.Add(anim.Name, anim); } var processorContext = new TestProcessorContext(TargetPlatform.Windows, "dummy.xnb"); var processor = new ModelProcessor { DefaultEffect = MaterialProcessorDefaultEffect.SkinnedEffect, }; var output = processor.Process(input, processorContext); // TODO: Not sure why, but XNA always returns a BasicMaterialContent // even when we specify SkinnedEffect as the default. We need to fix // the test first before we can enable the assert here. //Assert.IsInstanceOf(typeof(SkinnedMaterialContent), output.Meshes[0].MeshParts[0].Material); }
public void BuildLocalizedFont(TargetPlatform platform, TextureProcessorOutputFormat format) { var context = new TestProcessorContext(platform, "Localized.xnb"); var processor = new LocalizedFontProcessor() { TextureFormat = format, PremultiplyAlpha = true, }; LocalizedFontDescription fontDescription = null; using (var fs = File.OpenRead(Path.Combine("Assets", "Fonts", "Localized.spritefont"))) using (var input = XmlReader.Create(new StreamReader(fs))) fontDescription = IntermediateSerializer.Deserialize <LocalizedFontDescription>(input, ""); fontDescription.Identity = new ContentIdentity("Localized.spritefont"); var output = processor.Process(fontDescription, context); Assert.IsNotNull(output, "output should not be null"); Assert.IsNotNull(output.Texture, "output.Texture should not be null"); var textureType = output.Texture.Faces[0][0].GetType(); switch (format) { case TextureProcessorOutputFormat.Color: Assert.IsTrue(textureType == typeof(PixelBitmapContent <Color>)); break; case TextureProcessorOutputFormat.Color16Bit: Assert.IsTrue(textureType == typeof(PixelBitmapContent <Microsoft.Xna.Framework.Graphics.PackedVector.Bgr565>)); break; case TextureProcessorOutputFormat.Compressed: switch (platform) { case TargetPlatform.Windows: case TargetPlatform.DesktopGL: Assert.IsTrue(textureType == typeof(Dxt3BitmapContent)); break; case TargetPlatform.iOS: Assert.IsTrue(textureType == typeof(PixelBitmapContent <Microsoft.Xna.Framework.Graphics.PackedVector.Bgra4444>)); break; case TargetPlatform.Android: Assert.IsTrue(textureType == typeof(PixelBitmapContent <Microsoft.Xna.Framework.Graphics.PackedVector.Bgra4444>)); break; } break; case TextureProcessorOutputFormat.PvrCompressed: // because the font is not power of 2 we should use Brga4444 Assert.IsTrue(textureType == typeof(PixelBitmapContent <Microsoft.Xna.Framework.Graphics.PackedVector.Bgra4444>)); break; case TextureProcessorOutputFormat.Etc1Compressed: // because the font has Alpha we should use Brga4444 Assert.IsTrue(textureType == typeof(PixelBitmapContent <Microsoft.Xna.Framework.Graphics.PackedVector.Bgra4444>)); break; default: Assert.Fail("Test not written for " + format); break; } }