Exemplo n.º 1
0
        private static void AddQuad(ref WorldSpaceMask rectMask, ref DynamicBuffer <MeshVertex> vertices,
                                    ref DynamicBuffer <MeshVertexIndex> triangles, float2 posMin, float2 posMax, float4 color, float2 uvMin,
                                    float2 uvMax)
        {
            RectMaskCut cut      = SpriteUtility.GetRectangleMaskCut(posMin, posMax, ref rectMask);
            var         cutSize  = cut.Max + cut.Min;
            var         rectSize = posMax - posMin;

            if (cutSize.x > rectSize.x || cutSize.y > rectSize.y)
            {
                return;
            }

            int startIndex = vertices.Length;

            vertices.Add(new MeshVertex()
            {
                Position  = new float3(new float2(posMin) + cut.Min, 0.0f),
                TexCoord0 = new float2(uvMin) + cut.UvMin,
                Color     = color
            });
            vertices.Add(new MeshVertex()
            {
                Position  = new float3(new float2(posMin.x + cut.Min.x, posMax.y - cut.Max.y), 0.0f),
                TexCoord0 = new float2(uvMin.x + cut.UvMin.x, uvMax.y - cut.UvMax.y),
                Color     = color
            });
            vertices.Add(new MeshVertex()
            {
                Position  = new float3(new float2(posMax.x, posMax.y) - cut.Max, 0.0f),
                TexCoord0 = new float2(uvMax.x, uvMax.y) - cut.UvMax,
                Color     = color
            });
            vertices.Add(new MeshVertex()
            {
                Position  = new float3(new float2(posMax.x - cut.Max.x, posMin.y + cut.Min.y), 0.0f),
                TexCoord0 = new float2(uvMax.x - cut.UvMax.x, uvMin.y + cut.UvMin.y),
                Color     = color
            });

            triangles.Add(new MeshVertexIndex {
                Value = startIndex
            });
            triangles.Add(new MeshVertexIndex {
                Value = startIndex + 1
            });
            triangles.Add(new MeshVertexIndex {
                Value = startIndex + 2
            });
            triangles.Add(new MeshVertexIndex {
                Value = startIndex + 2
            });
            triangles.Add(new MeshVertexIndex {
                Value = startIndex + 3
            });
            triangles.Add(new MeshVertexIndex {
                Value = startIndex
            });
        }
Exemplo n.º 2
0
            private void PopulateMesh(WorldSpaceRect rect, ElementScale scale, WorldSpaceMask mask, TextRenderer settings, float4 color, ref DynamicBuffer <TextData> textBuffer, ref DynamicBuffer <ControlVertexData> vertices, ref DynamicBuffer <ControlVertexIndex> triangles)
            {
                _VerticalAlignmentOptions   verticalAlignment   = (_VerticalAlignmentOptions)settings.Alignment;
                _HorizontalAlignmentOptions horizontalAlignment = (_HorizontalAlignmentOptions)settings.Alignment;

                var font      = FontAssetFromEntity[settings.Font];
                var glyphData = FontGlyphDataFromEntity[settings.Font];

                float2 canvasScale = settings.Size * scale.Value / font.PointSize;


                float stylePadding         = 1.25f + (settings.Bold ? font.BoldStyle / 4.0f : font.NormalStyle / 4.0f);
                float styleSpaceMultiplier = 1.0f + (settings.Bold ? font.BoldSpace * 0.01f : font.NormalSpace * 0.01f);

                NativeList <TextUtils.TextLineInfo> lines = new NativeList <TextUtils.TextLineInfo>(Allocator.Temp);

                TextUtils.CalculateLines(ref rect, canvasScale, styleSpaceMultiplier, ref glyphData, ref textBuffer, lines);
                float textBlockHeight = lines.Length * font.LineHeight * canvasScale.y;

                float2 alignedStartPosition = TextUtils.GetAlignedStartPosition(ref rect, ref settings, ref font, textBlockHeight, canvasScale);
                float2 currentCharacter     = alignedStartPosition;

                int lineIdx = 0;

                for (int i = 0; i < textBuffer.Length; i++)
                {
                    if (lineIdx < lines.Length && i == lines[lineIdx].CharacterOffset)
                    {
                        currentCharacter = new float2(TextUtils.GetAlignedLinePosition(ref rect, lines[lineIdx].LineWidth, horizontalAlignment),
                                                      alignedStartPosition.y - font.LineHeight * canvasScale.y * lineIdx);
                        lineIdx++;
                    }

                    var character = textBuffer[i].Value;
                    if (character == (ushort)'\n')
                    {
                        // It's handled in GetLinesOffsets
                        continue;
                    }
                    if (TextUtils.GetGlyph(character, ref glyphData, out FontGlyphData ch))
                    {
                        int startIndex = vertices.Length;

                        float2 uv2 = new float2(ch.Scale, ch.Scale) * math.select(canvasScale, -canvasScale, settings.Bold);

                        float2 vMin = currentCharacter + new float2(ch.Metrics.horizontalBearingX - stylePadding, ch.Metrics.horizontalBearingY - ch.Metrics.height - stylePadding) * canvasScale;
                        float2 vMax = vMin + new float2(ch.Metrics.width + stylePadding * 2.0f, ch.Metrics.height + stylePadding * 2.0f) * canvasScale;

                        float4 uv = new float4(ch.Rect.x - stylePadding, ch.Rect.y - stylePadding, (ch.Rect.x + ch.Rect.width + stylePadding), (ch.Rect.y + ch.Rect.height + stylePadding)) / new float4(font.AtlasSize, font.AtlasSize);

                        RectMaskCut cut     = SpriteUtility.GetRectangleMaskCut(vMin, vMax, uv.xy, uv.zw, ref mask);
                        var         cutSize = cut.Max + cut.Min;
                        if (!(cutSize.x > vMax.x - vMin.x) && !(cutSize.y > vMax.y - vMin.y))
                        {
                            float4 vertexPos = new float4(vMin + cut.Min, vMax - cut.Max);
                            uv.xy = uv.xy + cut.UvMin;
                            uv.zw = uv.zw - cut.UvMax;

                            triangles.Add(new ControlVertexIndex()
                            {
                                Value = startIndex + 2
                            });
                            triangles.Add(new ControlVertexIndex()
                            {
                                Value = startIndex + 1
                            });
                            triangles.Add(new ControlVertexIndex()
                            {
                                Value = startIndex
                            });

                            triangles.Add(new ControlVertexIndex()
                            {
                                Value = startIndex + 3
                            });
                            triangles.Add(new ControlVertexIndex()
                            {
                                Value = startIndex + 2
                            });
                            triangles.Add(new ControlVertexIndex()
                            {
                                Value = startIndex
                            });

                            vertices.Add(new ControlVertexData()
                            {
                                Position  = new float3(vertexPos.xy, 0.0f),
                                Normal    = new float3(0.0f, 0.0f, -1.0f),
                                TexCoord0 = uv.xy,
                                TexCoord1 = uv2,
                                Color     = color
                            });
                            vertices.Add(new ControlVertexData()
                            {
                                Position  = new float3(vertexPos.zy, 0.0f),
                                Normal    = new float3(0.0f, 0.0f, -1.0f),
                                TexCoord0 = uv.zy,
                                TexCoord1 = uv2,
                                Color     = color
                            });
                            vertices.Add(new ControlVertexData()
                            {
                                Position  = new float3(vertexPos.zw, 0.0f),
                                Normal    = new float3(0.0f, 0.0f, -1.0f),
                                TexCoord0 = uv.zw,
                                TexCoord1 = uv2,
                                Color     = color
                            });
                            vertices.Add(new ControlVertexData()
                            {
                                Position  = new float3(vertexPos.xw, 0.0f),
                                Normal    = new float3(0.0f, 0.0f, -1.0f),
                                TexCoord0 = uv.xw,
                                TexCoord1 = uv2,
                                Color     = color
                            });
                        }

                        currentCharacter += (new float2(ch.Metrics.horizontalAdvance * styleSpaceMultiplier, 0.0f) *
                                             canvasScale);
                    }
                }
            }