创建圆角矩形纹理2D

2023-12-20

我正在构建一个带有对话框的游戏,我希望能够以与最终幻想系列非常相似的风格以程序方式生成盒子的纹理(想像最终幻想VII http://i79.photobucket.com/albums/j148/Xngears/Final%20Fantasy/FFVII%20Retranslated/ff72009-03-1900-26-46-30.jpg)。这是我到目前为止所拥有的代码片段:

public class DialogBox
{
    public Rectangle BoxArea { get; set; }
    public List<Color> BoxColors { get; set; }
    public List<Color> BorderColors { get; set; }
    public int BorderThickness { get; set; }
    public int BorderRadius { get; set; }
    private Texture2D texture;

    public void CreateBackdrop(ref GraphicsDevice graphics)
    {
        texture = new Texture2D(graphics,
                                BoxArea.Width,
                                BoxArea.Height,
                                true,
                                SurfaceFormat.Color);
        Color[] color = new Color[texture.Width * texture.Height];

        for(int x = 0; x < texture.Width; x++)
        {
            for(int y = 0; y < texture.Height; y++)
            {
                switch(BoxColors.Count)
                {
                    case 4:
                        Color leftColor = Color.Lerp(BoxColor[0], BoxColor[1], (y / (texture.Height - 1)));
                        Color rightColor = Color.Lerp(BoxColor[2], BoxColor[3], (y / (texture.Height - 1)));
                        color[x + y * texture.Width] = Color.Lerp(leftColor,
                                                                  RightColor,
                                                                  (x / (texture.Width - 1)));
                        break;
                    case 2:
                        color[x + y * texture.Width] = Color.Lerp(BoxColors[0],
                                                                  BoxColors[1],
                                                                  (x / (texture.Width - 1)));
                        break;
                    default:
                        color[x + y * texture.Width];
                        break;
                 }
            }
        }
        texture.SetData<Color>(color);
    }
}

我正在寻找的是以下内容:

  • 4路渐变色(已实现)
  • 圆角矩形
  • 渐变边框

任何帮助将不胜感激。

我通过以下方式找出了矩形这个问题 https://stackoverflow.com/questions/2792694/draw-rectangle-with-xna.


我弄清楚了我遇到的问题是什么;计算拐角圆弧。我发现人们可以使用距一点的距离来理解绘制边界的方法,而不是使用数学来计算圆内的点。下面我发布了生成圆角矩形的代码。

public Texture2D CreateRoundedRectangleTexture(GraphicsDevice graphics, int width, int height, int borderThickness, int borderRadius, int borderShadow, List<Color> backgroundColors, List<Color> borderColors, float initialShadowIntensity, float finalShadowIntensity)
{
    if (backgroundColors == null || backgroundColors.Count == 0) throw new ArgumentException("Must define at least one background color (up to four).");
    if (borderColors == null || borderColors.Count == 0) throw new ArgumentException("Must define at least one border color (up to three).");
    if (borderRadius < 1) throw new ArgumentException("Must define a border radius (rounds off edges).");
    if (borderThickness < 1) throw new ArgumentException("Must define border thikness.");
    if (borderThickness + borderRadius > height / 2 || borderThickness + borderRadius > width / 2) throw new ArgumentException("Border will be too thick and/or rounded to fit on the texture.");
    if (borderShadow > borderRadius) throw new ArgumentException("Border shadow must be lesser in magnitude than the border radius (suggeted: shadow <= 0.25 * radius).");

    Texture2D texture = new Texture2D(graphics, width, height, false, SurfaceFormat.Color);
    Color[] color = new Color[width * height];

    for (int x = 0; x < texture.Width; x++)
    {
        for (int y = 0; y < texture.Height; y++)
        {
            switch (backgroundColors.Count)
            {
                case 4:
                    Color leftColor0 = Color.Lerp(backgroundColors[0], backgroundColors[1], ((float)y / (width - 1)));
                    Color rightColor0 = Color.Lerp(backgroundColors[2], backgroundColors[3], ((float)y / (height - 1)));
                    color[x + width * y] = Color.Lerp(leftColor0, rightColor0, ((float)x / (width - 1)));
                    break;
                case 3:
                    Color leftColor1 = Color.Lerp(backgroundColors[0], backgroundColors[1], ((float)y / (width - 1)));
                    Color rightColor1 = Color.Lerp(backgroundColors[1], backgroundColors[2], ((float)y / (height - 1)));
                    color[x + width * y] = Color.Lerp(leftColor1, rightColor1, ((float)x / (width - 1)));
                    break;
                case 2:
                    color[x + width * y] = Color.Lerp(backgroundColors[0], backgroundColors[1], ((float)x / (width - 1)));
                    break;
                default:
                    color[x + width * y] = backgroundColors[0];
                    break;
            }

            color[x + width * y] = ColorBorder(x, y, width, height, borderThickness, borderRadius, borderShadow, color[x + width * y], borderColors, initialShadowIntensity, finalShadowIntensity);
        }
    }

    texture.SetData<Color>(color);
    return texture;
}

private Color ColorBorder(int x, int y, int width, int height, int borderThickness, int borderRadius, int borderShadow, Color initialColor, List<Color> borderColors, float initialShadowIntensity, float finalShadowIntensity)
{
    Rectangle internalRectangle = new Rectangle((borderThickness + borderRadius), (borderThickness + borderRadius), width - 2 * (borderThickness + borderRadius), height - 2 * (borderThickness + borderRadius));

    if (internalRectangle.Contains(x, y)) return initialColor;

    Vector2 origin = Vector2.Zero;
    Vector2 point = new Vector2(x, y);

    if (x < borderThickness + borderRadius)
    {
        if (y < borderRadius + borderThickness)
            origin = new Vector2(borderRadius + borderThickness, borderRadius + borderThickness);
        else if (y > height - (borderRadius + borderThickness))
            origin = new Vector2(borderRadius + borderThickness, height - (borderRadius + borderThickness));
        else
            origin = new Vector2(borderRadius + borderThickness, y);
    }
    else if (x > width - (borderRadius + borderThickness))
    {
        if (y < borderRadius + borderThickness)
            origin = new Vector2(width - (borderRadius + borderThickness), borderRadius + borderThickness);
        else if (y > height - (borderRadius + borderThickness))
            origin = new Vector2(width - (borderRadius + borderThickness), height - (borderRadius + borderThickness));
        else
            origin = new Vector2(width - (borderRadius + borderThickness), y);
    }
    else
    {
        if (y < borderRadius + borderThickness)
            origin = new Vector2(x, borderRadius + borderThickness);
        else if (y > height - (borderRadius + borderThickness))
            origin = new Vector2(x, height - (borderRadius + borderThickness));
    }

    if (!origin.Equals(Vector2.Zero))
    {
        float distance = Vector2.Distance(point, origin);

        if (distance > borderRadius + borderThickness + 1)
        {
            return Color.Transparent;
        }
        else if (distance > borderRadius + 1)
        {
            if (borderColors.Count > 2)
            {
                float modNum = distance - borderRadius;

                if (modNum < borderThickness / 2)
                {
                    return Color.Lerp(borderColors[2], borderColors[1], (float)((modNum) / (borderThickness / 2.0)));
                }
                else
                {
                    return Color.Lerp(borderColors[1], borderColors[0], (float)((modNum - (borderThickness / 2.0)) / (borderThickness / 2.0)));
                }
            }


            if (borderColors.Count > 0)
                return borderColors[0];
        }
        else if (distance > borderRadius - borderShadow + 1)
        {
            float mod = (distance - (borderRadius - borderShadow)) / borderShadow;
            float shadowDiff = initialShadowIntensity - finalShadowIntensity;
            return DarkenColor(initialColor, ((shadowDiff * mod) + finalShadowIntensity));
        }
    }

    return initialColor;
}

private Color DarkenColor(Color color, float shadowIntensity)
{
    return Color.Lerp(color, Color.Black, shadowIntensity);
}

With all of the code above, this is the result: Rounded Texture in Action

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

创建圆角矩形纹理2D 的相关文章

  • 在实体框架拦截器中向 DbScanExpression 添加内部联接

    我正在尝试使用实体框架 CommandTree 拦截器通过 DbContext 向每个查询添加过滤器 为了简单起见 我有两个表 一个称为 User 有两列 UserId 和 EmailAddress 另一个称为 TenantUser 有两列
  • FileStream 构造函数和默认缓冲区大小

    我们有一个使用 NET 4 用 C 编写的日志记录类 我想添加一个构造函数参数 该参数可以选择设置文件选项 WriteThrough http msdn microsoft com en us library system io fileo
  • 读取 C# 中的默认应用程序设置

    我的自定义网格控件有许多应用程序设置 在用户范围内 其中大部分是颜色设置 我有一个表单 用户可以在其中自定义这些颜色 并且我想添加一个用于恢复默认颜色设置的按钮 如何读取默认设置 例如 我有一个名为的用户设置CellBackgroundCo
  • 信号处理程序有单独的堆栈吗?

    信号处理程序是否有单独的堆栈 就像每个线程都有单独的堆栈一样 这是在 Linux C 环境中 来自 Linux 手册页signal 7 http kernel org doc man pages online pages man7 sign
  • 与 Qt 项目的静态链接

    我有一个在 Visual Studio 2010 Professional 中构建的 Qt 项目 但是 当我运行它 在调试或发布模式下 时 它会要求一些 Qt dll 如果我提供 dll 并将它们放入 System32 中 它就可以工作 但
  • 找不到 assimp-vc140-mt.dll ASSIMP

    我已经从以下位置下载了 Assimp 项目http assimp sourceforge net main downloads html http assimp sourceforge net main downloads html Ass
  • ASP.Net Core 内容配置附件/内联

    我正在从 WebAPI 控制器返回一个文件 Content Disposition 标头值自动设置为 附件 例如 处置 附件 文件名 30956 pdf 文件名 UTF 8 30956 pdf 当它设置为附件时 浏览器将要求保存文件而不是打
  • fprintf() 线程安全吗?

    我正在为野人就餐问题的某些变量编写一个 C 解决方案 现在 我创建线程 每个线程都将 FILE 获取到同一个调试文件 在线程内我正在使用 fprintf 进行一些打印 打印的语句不受任何类型的互斥锁等保护 我没有在调试文件中观察到任何交错行
  • 在 JSQMessagesViewController 中显示 LocationMediaItem

    我刚刚尝试实施LocationMediaItem in my Xamarin iOS应用程序使用JSQMessagesViewController 一切都很顺利 唯一的问题是UICollectionView应该显示位置的单元格永远停留在加载
  • 如何在服务器端按钮点击时关闭当前标签页?

    我尝试在确认后关闭当前选项卡 因此我将以下代码放在确认按钮的末尾 但选项卡没有关闭 string jScript ClientScript RegisterClientScriptBlock this GetType keyClientBl
  • AES 输出是否小于输入?

    我想加密一个字符串并将其嵌入到 URL 中 因此我想确保加密的输出不大于输入 AES 是可行的方法吗 不可能创建任何始终会创建比输入更小的输出的算法 但可以将任何输出反转回输入 如果您允许 不大于输入 那么基本上您只是在谈论同构算法alwa
  • 如何在标准 WPF ListView 中启用 UI 虚拟化

    我正在使用 NET 4 5 VS2012 并且我有一个 ListView 看起来像这样
  • C# 中的 strstr() 等效项

    我有两个byte 我想找到第二个的第一次出现byte 在第一个byte 或其中的一个范围 我不想使用字符串来提高效率 翻译第一个byte to a string会效率低下 基本上我相信就是这样strstr 在 C 中做 最好的方法是什么 这
  • IEnumerable.Except 不起作用,那么我该怎么办?

    我有一个 linq to sql 数据库 非常简单 我们有 3 个表 项目和用户 有一个名为 User Projects 的连接表将它们连接在一起 我已经有了一个获得的工作方法IEnumberable
  • 新任务中使用的依赖注入服务

    我在需要时使用依赖项注入来访问我的服务 但我现在想要创建一个并发任务 但这会由于依赖项注入对象及其生命周期而导致问题 我读过这篇文章 标题 防止多线程 Link http mehdi me ambient dbcontext in ef6
  • 每个数据库多个/单个 *.edmx 文件

    我有一个通过 ADO net 数据服务与数据库交互的项目 数据库很大 近 150 个具有依赖关系的表 该项目几年前开始 当时使用的是数据集 现在我们正在转向实体模型关系 由于我们添加了更多需要使用的表 该模型正在不断增长 这是管理这一切的正
  • C++ Streambuf 方法可以抛出异常吗?

    我正在尝试找到一种方法来获取读取或写入流的字符数 即使存在错误并且读 写结束时间较短 该方法也是可靠的 我正在做这样的事情 return stream rdbuf gt sputn buffer buffer size 但如果streamb
  • 矩阵到数组 C#

    这将是转换方阵的最有效方法 例如 1 2 3 4 5 6 7 8 9 into 1 2 3 4 5 6 7 8 9 in c 我在做 int array2D new int 1 2 3 4 5 6 7 8 9 int array1D new
  • C++0x中disable_if在哪里?

    Boost 两者都有enable if and disable if 但 C 0x 似乎缺少后者 为什么它被排除在外 C 0x 中是否有元编程工具允许我构建disable if按照enable if 哦 我刚刚注意到std enable i
  • xsi:type 属性搞乱了 C# XML 反序列化

    我使用 XSD exe 根据 XML 架构 xsd 文件 自动生成 C 对象 我正在反序列化 OpenCover 输出 但其中一个部分类未正确生成 这是导致异常的行

随机推荐

  • 谷歌图表中悬停时的垂直线

    我正在我的项目中使用谷歌折线图和 angularjs 指令 我正在搜索如何在悬停时获得垂直线 例如谷歌趋势 https trends google com trends explore q angular相反 放置固定线路 但我找不到如何做
  • 检测到 Android 设备已连接到 PC

    我有一个 Android 设备 我想知道它何时通过 Java NET 连接到 PC 我知道这是可能的 Samsung New PC Studio 正在这样做 每当我连接 Android 设备时 它都会通知我 我怎样才能达到同样的效果 您必须
  • 在 Woocommerce 中每 3 件商品增加统一费率运输费用

    我正在经营一家 woocommerce 商店并使用统一费率送货 15 我写了一个公式来添加 1 25对于每个附加项目 13 50 1 25 数量 啜饮 统一费率设置 1 25对于附加的每项 但我想加上这个费用 1 25每 3 件商品 我的意
  • 打印斐波那契数列的结果

    我知道斐波那契数列的编码是 int fib int n if n 0 n 1 return n else return fib n 1 fib n 2 我想知道是否有一种方法 使用上述代码来打印该系列的先前结果 但既不使用 void 函数
  • C++ 改变控制台输出

    在控制台中显示变化的数字的最简单方法是什么 我有一个普通的 C 命令行程序 它使用cout 但我想显示一个表示进度的百分比数字 最多可计数到 100 而不打印新行 这是怎么做到的 如果重要的话 我使用的是 Windows 7 当我需要时 我
  • 从具有多个数据集的散点图中获取 x,y?

    我有一个由不同调用组成的散点图scatter import matplotlib pyplot as plt import numpy as np def onpick3 event index event ind print print
  • IBatis + Java:检索 HashMap

    如何使用 IBatis 检索 HashMap 我有两个值的选择 我想将其映射到键值映射中 有人遇到过同样的问题吗 提前致谢 看这个问题 如何在iBatis中获得排序结果 https stackoverflow com questions 3
  • 通过 JavaScript 动态创建 JSON 对象(无需连接字符串)

    我有这个 JSON 数据 employees firstName John lastName Doe firstName Anna lastName Smith firstName Peter lastName Jones 假设我不知道我有
  • 没有 @import 的全局 CSS 变量

    我希望为我的 SASS CSS 文件中的媒体查询定义一个变量 给定页面的 sass 文件的一般结构是 文件 scss 变量 scss 颜色 scss web scss tablet scss mobile scss file scss 只有
  • mcrypt安装问题

    我已要求系统管理员安装mcrypt在服务器上 他们说一切正常 但是当我运行我的简单脚本时 我得到了这个 警告 mcrypt get iv size function mcrypt get iv size 模块初始化失败 它来自这一行 iv
  • 如何将显示的sql数据加载到wpf可滚动列表视图中?大型集合(20k+)而不会遇到内存问题?

    我在 sql 数据库中有一个很大的集合 当我使用时DataSet 我假设我一次提取所有 当我说全部时 我的意思是 20000 数据库条目来填充可滚动的 WPF 列表视图 当我提取这么多数据并添加更多数据时 我的内存不足 我在这里问了类似的问
  • 多个目标框架项目:不同框架上同一 NuGet 包的不同版本?

    我使用 EntityFramework 5 0 13 组合了一个 Net 5 for Windows 应用程序 现在我们尝试在尚未安装 Net 5 框架的特定服务器上运行它 管理服务器的好人告诉我 他们尝试安装新东西 但似乎无法使应用程序运
  • 通过滑动手势关闭键盘

    在 Messages app 中 您可以通过滚动列表视图来关闭键盘 需要明确的是 它不仅仅是响应scrollViewDidScroll事件 当您向下滑动时 键盘会随着您的手指进行跟踪 知道这是如何做到的吗 从 iOS 7 开始 您可以使用
  • 绑定适配器实时数据值始终为空

    由于某种原因 两个绑定适配器的第二个参数值总是返回 null 我无法弄清楚为什么 我正在选择一个plantIndividual从概述片段中的 RecyclerView 并使用它导航到详细信息页面 单个片段 两个 Fragment 共享一个
  • 如何动态创建 PWA manifest.json?

    我在 manifest json 中使用静态变量创建了一个功能 例如 name short name start url 它对于 添加到主屏幕 运行良好 当我动态设置manifest json变量时 添加到屏幕不起作用 基本上我有一个电子商
  • 读取位置 0x0000000c 访问冲突

    在我的应用程序中 我使用 LowLevelKeyboardProc 禁用 开始 菜单 但是当我连续按 tab 键时出现以下错误 错误 0x00352d58 处未处理的异常 Timer soosai exe 0xC0000005 访问 违规读
  • 创建之前执行的函数[重复]

    这个问题在这里已经有答案了 我不是 JavaScript 专家 但我发现了一些这样的代码 a function a alert a 我很惊讶地发现它可以工作 我认为类似的东西在 Python 中是行不通的 我期望该功能a创建之前不能执行 解
  • 语法突出显示不适用于 IVsInvisibleEditor

    我创建了 IVsInvisibleEditor 的实例 我使用以下代码来实现此目的 public IWpfTextViewHost CreateEditor string targetFile var componentModel ICom
  • 如何使用 JAVA 高级 REST 客户端发出 Elasticsearch 的 Point InTime API 请求

    如果有人可以向我指出一种使用 REST 高级客户端从 JAVA 或 JAVA 中的任何其他方式执行 Elasticsearch 7 10 1 中新引入的 Point In Time API 请求的方法 那将会很有帮助 传输客户端除外 因为它
  • 创建圆角矩形纹理2D

    我正在构建一个带有对话框的游戏 我希望能够以与最终幻想系列非常相似的风格以程序方式生成盒子的纹理 想像最终幻想VII http i79 photobucket com albums j148 Xngears Final 20Fantasy