Exemple #1
0
        public ModelInstanceHandle CreateInstance(ModelHandle modelHandle, Material material, Transform initialTransform)
        {
            uint modelIndex = modelHandle.ModelIndex;

            Assure.NotNull(material);
            Assure.False(material.IsDisposed, "Given material was disposed!");
            return(GeometryCache.GetCacheByID(modelHandle.GeoCacheID).AllocInstance(modelIndex, OwningLayer.Index, material.Index, initialTransform));
        }
Exemple #2
0
 public FontCharacter(char unicodeValue, Rectangle boundary, ModelHandle modelHandle, FragmentShader fragmentShader, ShaderResourceView fontMapResourceView, int yOffset)
 {
     UnicodeValue        = unicodeValue;
     Boundary            = boundary;
     ModelHandle         = modelHandle;
     FragmentShader      = fragmentShader;
     FontMapResourceView = fontMapResourceView;
     YOffset             = yOffset;
 }
        public void TestGettingModelsByName()
        {
            GeometryCacheBuilder <DefaultVertex> gcbA = new GeometryCacheBuilder <DefaultVertex>();
            ModelHandle modelA = gcbA.AddModel("TGMBN_A", new[] { new DefaultVertex(Vector3.ONE) }, new[] { 1U });
            ModelHandle modelB = gcbA.AddModel("TGMBN_B", new[] { new DefaultVertex(Vector3.ONE) }, new[] { 1U });

            GeometryCache gcA = gcbA.Build();

            GeometryCacheBuilder <DefaultVertex> gcbB = new GeometryCacheBuilder <DefaultVertex>();
            ModelHandle   model1 = gcbB.AddModel("TGMBN_1", new[] { new DefaultVertex(Vector3.ONE) }, new[] { 1U });
            ModelHandle   model2 = gcbB.AddModel("TGMBN_2", new[] { new DefaultVertex(Vector3.ONE) }, new[] { 1U });
            ModelHandle   model3 = gcbB.AddModel("TGMBN_3", new[] { new DefaultVertex(Vector3.ONE) }, new[] { 1U });
            GeometryCache gcB    = gcbB.Build();

            Assert.AreEqual(modelA, gcA.GetModelByName("TGMBN_A"));
            Assert.AreEqual(model3, gcB.GetModelByName("TGMBN_3"));

            Assert.AreEqual(model1, GeometryCache.GloballyGetModelByName("TGMBN_1"));
            Assert.AreEqual(model2, GeometryCache.GloballyGetModelByName("TGMBN_2"));
            Assert.AreEqual(modelB, GeometryCache.GloballyGetModelByName("TGMBN_B"));

            try {
                gcA.GetModelByName("TGMBN_1");
                Assert.Fail();
            }
            catch (KeyNotFoundException) { }

            try {
                gcB.GetModelByName("TGMBN_B");
                Assert.Fail();
            }
            catch (KeyNotFoundException) { }

            try {
                GeometryCache.GloballyGetModelByName("TGMBN_C");
                Assert.Fail();
            }
            catch (KeyNotFoundException) { }
        }
Exemple #4
0
        public static Font Load(string fontFile, FragmentShader textFS, uint?lineHeightPixels, int?kerningPixels)
        {
            Assure.NotNull(fontFile);
            Assure.NotNull(textFS);
            Assure.False(textFS.IsDisposed);
            if (!IOUtils.IsValidFilePath(fontFile) || !File.Exists(fontFile))
            {
                throw new FileNotFoundException("File '" + fontFile + "' not found: Could not load font.");
            }

            XDocument fontDocument  = XDocument.Load(fontFile, LoadOptions.None);
            XElement  root          = fontDocument.Root;
            XElement  commonElement = root.Element("common");

            if (commonElement == null)
            {
                throw new InvalidOperationException("Could not find common element in given font file.");
            }

            string name = Path.GetFileNameWithoutExtension(fontFile).CapitalizeFirst();
            uint   texWidth;
            uint   texHeight;

            try {
                texWidth  = uint.Parse(commonElement.Attribute("scaleW").Value);
                texHeight = uint.Parse(commonElement.Attribute("scaleH").Value);
                if (lineHeightPixels == null)
                {
                    lineHeightPixels = uint.Parse(commonElement.Attribute("lineHeight").Value) / 2U;
                }
            }
            catch (Exception e) {
                throw new InvalidOperationException("Could not read scaleW, scaleH, or lineHeight value!", e);
            }

            XElement pagesElement = root.Element("pages");
            IEnumerable <XElement> pageElements = pagesElement.Elements("page");

            ITexture2D[]         pageArray          = new ITexture2D[pageElements.Count()];
            ShaderResourceView[] characterPageViews = new ShaderResourceView[pageArray.Length];

            foreach (XElement pageElement in pageElements)
            {
                int    id;
                string filename;
                try {
                    id       = int.Parse(pageElement.Attribute("id").Value);
                    filename = pageElement.Attribute("file").Value;
                }
                catch (Exception e) {
                    throw new InvalidOperationException("Could not read page ID or filename for page " + pageElement + ".", e);
                }

                string fullFilename = Path.Combine(Path.GetDirectoryName(fontFile), filename);
                if (!IOUtils.IsValidFilePath(fullFilename) || !File.Exists(fullFilename))
                {
                    throw new InvalidOperationException("Page file '" + fullFilename + "' does not exist!");
                }
                if (id < 0 || id >= pageArray.Length || pageArray[id] != null)
                {
                    throw new InvalidOperationException("Invalid or duplicate page ID '" + id + "'.");
                }

                pageArray[id] = TextureFactory.LoadTexture2D()
                                .WithFilePath(fullFilename)
                                .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                                .WithUsage(ResourceUsage.Immutable)
                                .Create();

                characterPageViews[id] = pageArray[id].CreateView();
            }

            GeometryCacheBuilder <DefaultVertex> characterCacheBuilder = new GeometryCacheBuilder <DefaultVertex>();
            Dictionary <char, FontCharacter>     charMap = new Dictionary <char, FontCharacter>();
            XElement charsElement = root.Element("chars");

            foreach (XElement charElement in charsElement.Elements("char"))
            {
                char unicodeValue;
                uint x, y, width, height;
                int  pageID, yOffset;

                try {
                    unicodeValue = (char)short.Parse(charElement.Attribute("id").Value);
                    x            = uint.Parse(charElement.Attribute("x").Value);
                    y            = uint.Parse(charElement.Attribute("y").Value);
                    width        = uint.Parse(charElement.Attribute("width").Value);
                    height       = uint.Parse(charElement.Attribute("height").Value);
                    pageID       = int.Parse(charElement.Attribute("page").Value);
                    yOffset      = int.Parse(charElement.Attribute("yoffset").Value);
                }
                catch (Exception e) {
                    throw new InvalidOperationException("Could not acquire character ID, page ID, or dimensions for char " + charElement + ".", e);
                }

                Rectangle charMapBoundary = new Rectangle(x, y, width, height);

                ModelHandle modelHandle = characterCacheBuilder.AddModel(
                    "Font_" + name + "_Character_" + unicodeValue,
                    new[] {
                    new DefaultVertex(
                        new Vector3(0f, 0f, 1f),
                        Vector3.BACKWARD, new Vector2(
                            charMapBoundary.GetCornerX(Rectangle.RectangleCorner.BottomLeft) / texWidth,
                            charMapBoundary.GetCornerY(Rectangle.RectangleCorner.BottomLeft) / texHeight
                            )),
                    new DefaultVertex(
                        new Vector3(charMapBoundary.Width, 0f, 1f),
                        Vector3.BACKWARD, new Vector2(
                            charMapBoundary.GetCornerX(Rectangle.RectangleCorner.BottomRight) / texWidth,
                            charMapBoundary.GetCornerY(Rectangle.RectangleCorner.BottomRight) / texHeight
                            )),
                    new DefaultVertex(
                        new Vector3(charMapBoundary.Width, -charMapBoundary.Height, 1f),
                        Vector3.BACKWARD, new Vector2(
                            charMapBoundary.GetCornerX(Rectangle.RectangleCorner.TopRight) / texWidth,
                            charMapBoundary.GetCornerY(Rectangle.RectangleCorner.TopRight) / texHeight
                            )),
                    new DefaultVertex(
                        new Vector3(0f, -charMapBoundary.Height, 1f),
                        Vector3.BACKWARD, new Vector2(
                            charMapBoundary.GetCornerX(Rectangle.RectangleCorner.TopLeft) / texWidth,
                            charMapBoundary.GetCornerY(Rectangle.RectangleCorner.TopLeft) / texHeight
                            )),
                },
                    new[] { 0U, 1U, 3U, 1U, 2U, 3U }
                    );

                //yOffset = 0;
                //if (unicodeValue == '.') yOffset = (int) (lineHeightPixels.Value * 0.9f);

                charMap.Add(
                    unicodeValue,
                    new FontCharacter(
                        unicodeValue,
                        charMapBoundary,
                        modelHandle,
                        textFS,
                        characterPageViews[pageID],
                        yOffset
                        )
                    );
            }

            if (kerningPixels == null)
            {
                kerningPixels = (int)(charMap.Values.Max(value => value.Boundary.Width) * 0.15f);
            }

            uint maxCharHeight = (uint)charMap.Values.Max(fc => fc.Boundary.Height);

            return(new Font(
                       name,
                       lineHeightPixels.Value,
                       kerningPixels.Value,
                       characterCacheBuilder.Build(),
                       pageArray,
                       characterPageViews,
                       charMap,
                       (ConstantBufferBinding)textFS.GetBindingByIdentifier(TEXT_COLOR_SHADER_CB_NAME),
                       maxCharHeight
                       ));
        }
 public static void SetHUDTextureModel(ModelHandle modelHandle)
 {
     lock (staticMutationLock) {
         hudTextureModel = modelHandle;
     }
 }
Exemple #6
0
 public static ModelInstanceHandle CreateModelInstance(this SceneLayer @this, ModelHandle modelHandle, Material material,
                                                       Transform initialTransform)
 {
     Assure.NotNull(@this);
     return(@this.GetResource <LayerModelInstanceManager>().CreateInstance(modelHandle, material, initialTransform));
 }