コード例 #1
0
        private void CreateTextMesh()
        {
            if (_fontMap == null)
            {
                throw new ArgumentException("Can not create Text Mesh - FontMap not found!");
            }


            var verts   = new List <float3>();
            var uvs     = new List <float2>();
            var tris    = new List <ushort>();
            var normals = new List <float3>();

            // build complete structure

            // var charInfo = Font.CharInfo;
            var atlasWidth  = _fontMap.Image.Width;
            var atlasHeight = _fontMap.Image.Height;

            ushort vertex   = 0;
            var    advanceX = 0f;
            var    advanceY = 0f;

            var textMeshHeight = 0f;

            // now build the mesh
            foreach (var letter in _text)
            {
                GlyphOnMap glyphOnMap = _fontMap.GetGlyphOnMap(letter);
                GlyphInfo  glyphInfo  = _fontMap.Font.GetGlyphInfo(letter);

                var x = advanceX + glyphOnMap.BitmapL;
                var y = advanceY - glyphOnMap.BitmapT;
                var w = glyphOnMap.BitmapW;
                var h = glyphOnMap.BitmapH;

                if (-y > textMeshHeight)
                {
                    textMeshHeight = -y;
                }

                advanceX += glyphInfo.AdvanceX;
                advanceY += glyphInfo.AdvanceY;

                // skip glyphs that have no pixels
                if ((w <= M.EpsilonFloat) || (h <= M.EpsilonFloat))
                {
                    continue;
                }

                var bitmapW    = glyphOnMap.BitmapW;
                var bitmapH    = glyphOnMap.BitmapH;
                var texOffsetX = glyphOnMap.TexOffX;
                var texOffsetY = glyphOnMap.TexOffY;

                // vertices
                verts.Add(new float3(x, -y - h, 0));
                verts.Add(new float3(x, -y, 0));
                verts.Add(new float3(x + w, -y - h, 0));
                verts.Add(new float3(x + w, -y, 0));

                normals.Add(-float3.UnitZ);
                normals.Add(-float3.UnitZ);
                normals.Add(-float3.UnitZ);
                normals.Add(-float3.UnitZ);

                // uvs
                uvs.Add(new float2(texOffsetX, texOffsetY + bitmapH / atlasHeight));
                uvs.Add(new float2(texOffsetX, texOffsetY));
                uvs.Add(new float2(texOffsetX + bitmapW / atlasWidth, texOffsetY + bitmapH / atlasHeight));
                uvs.Add(new float2(texOffsetX + bitmapW / atlasWidth, texOffsetY));

                // indices
                tris.Add((ushort)(vertex + 1));
                tris.Add(vertex);
                tris.Add((ushort)(vertex + 2));

                tris.Add((ushort)(vertex + 1));
                tris.Add((ushort)(vertex + 2));
                tris.Add((ushort)(vertex + 3));

                vertex += 4;
            }

            Vertices  = verts.ToArray();
            Triangles = tris.ToArray();
            UVs       = uvs.ToArray();
            Normals   = normals.ToArray();

            Vertices = _fontMap.FixTextKerning(Vertices, _text, 1);

            var meshWidth       = Vertices[Vertices.Length - 1].x - Vertices[0].x;
            var translateToZero = Vertices[0].x;

            for (var i = 0; i < Vertices.Length; i++)
            {
                var translateVert = new float3(Vertices[i].x - translateToZero - meshWidth / 2, Vertices[i].y - textMeshHeight / 2, Vertices[i].z);
                var scaledVert    = translateVert / meshWidth;
                Vertices[i] = scaledVert;
            }
        }
コード例 #2
0
ファイル: GUIElement.cs プロジェクト: reliefpfeiler42/Fusee
        protected void SetTextMesh(int posX, int posY)
        {
            if (FontMap == null)
            {
                return;
            }

            // relative coordinates from -1 to +1
            var scaleX = (float)2 / RContext.ViewportWidth;
            var scaleY = (float)2 / RContext.ViewportHeight;

            var x = -1 + posX * scaleX;
            var y = +1 - posY * scaleY;

            // build complete structure
            var vertices = new float3[4 * Text.Length];
            var uvs      = new float2[4 * Text.Length];
            var indices  = new ushort[6 * Text.Length];

            // var charInfo = Font.CharInfo;
            var atlasWidth  = FontMap.Image.Width;
            var atlasHeight = FontMap.Image.Height;

            var    index  = 0;
            ushort vertex = 0;

            // now build the mesh
            foreach (var letter in Text)
            {
                GlyphOnMap glyphOnMap = FontMap.GetGlyphOnMap(letter);
                GlyphInfo  glyphInfo  = FontMap.Font.GetGlyphInfo(letter);

                var x2 = x + glyphOnMap.BitmapL * scaleX;
                var y2 = -y - glyphOnMap.BitmapT * scaleY;
                var w  = glyphOnMap.BitmapW * scaleX;
                var h  = glyphOnMap.BitmapH * scaleY;

                x += glyphInfo.AdvanceX * scaleX;
                y += glyphInfo.AdvanceY * scaleY;

                // skip glyphs that have no pixels
                if ((w <= M.EpsilonFloat) || (h <= M.EpsilonFloat))
                {
                    continue;
                }

                var bitmapW    = glyphOnMap.BitmapW;
                var bitmapH    = glyphOnMap.BitmapH;
                var texOffsetX = glyphOnMap.TexOffX;
                var texOffsetY = glyphOnMap.TexOffY;

                // vertices
                vertices[vertex]     = new float3(x2, -y2 - h, 0);
                vertices[vertex + 1] = new float3(x2, -y2, 0);
                vertices[vertex + 2] = new float3(x2 + w, -y2 - h, 0);
                vertices[vertex + 3] = new float3(x2 + w, -y2, 0);

                // uvs
                uvs[vertex]     = new float2(texOffsetX, texOffsetY + bitmapH / atlasHeight);
                uvs[vertex + 1] = new float2(texOffsetX, texOffsetY);
                uvs[vertex + 2] = new float2(texOffsetX + bitmapW / atlasWidth, texOffsetY + bitmapH / atlasHeight);
                uvs[vertex + 3] = new float2(texOffsetX + bitmapW / atlasWidth, texOffsetY);

                // indices
                indices[index++] = (ushort)(vertex + 1);
                indices[index++] = vertex;
                indices[index++] = (ushort)(vertex + 2);

                indices[index++] = (ushort)(vertex + 1);
                indices[index++] = (ushort)(vertex + 2);
                indices[index++] = (ushort)(vertex + 3);

                vertex += 4;
            }

            vertices = FontMap.FixTextKerning(vertices, Text, scaleX);

            // create final mesh
            CreateTextMesh(vertices, uvs, indices);
        }