【问题标题】:C# Monogame Minicraft get pixels Colors from pngC# Monogame Minicraft 从 png 中获取像素颜色
【发布时间】:2021-05-29 16:04:59
【问题描述】:

我很难理解 ColorHelper.get 在这个项目中的工作原理。 Here is C# 来自 github 的版本。

为了更好地理解你需要来自 github 的那个项目

关于项目: 该项目是对THIS(在 JAVA 中)的重制,但有人将其重制为 C#。它的 Monogame 中的一款名为 Minicraft 的游戏。

问题:因为它是开源的,我只想玩这个项目并了解更多信息。主要问题是,调色板受到限制,我只能使用 4 种颜色的精灵。这意味着,当我想要彩色精灵时,我不能。着色的工作方式(在我看来)是通过 ColorHelper.get 函数,其中函数包含 4 个整数,每个整数代表特定的颜色。 (例如HERE

鞋面(人)来自游戏,有 4 种颜色,由值转换。 底部精灵是我的海洋材料,有 8 种颜色。使用 ColorHelper.get,我只能转换 4 种颜色,而我的精灵必须使用这 4 种特定颜色,因为顶部的人类(不能是彩色的)。

代码:

ColorHelper.cs

 public static int get(int a, int b, int c, int d)
    {
       
        return (get(d) << 24) + (get(c) << 16) + (get(b) << 8) + (get(a));
    }

    public static int get(int d)
    {
        if (d < 0) return 255;
        int r = d / 100 % 10;
        int g = d / 10 % 10;
        int b = d % 10;
        return r * 36 + g * 6 + b;
    }

屏幕.cs

 public void render(int xp, int yp, int tile, int colors, int bits)
    {
        xp -= xOffset;
        yp -= yOffset;
        var mirrorX = (bits & BIT_MIRROR_X) > 0;
        var mirrorY = (bits & BIT_MIRROR_Y) > 0;

        var xTile = tile % 32;
        var yTile = tile / 32;
        var toffs = xTile * 8 + yTile * 8 * sheet.width;

        for (int y = 0; y < 8; y++)
        {
           
            int ys = y;
            if (mirrorY) ys = 7 - y;
            if (y + yp < 0 || y + yp >= h) continue;
            for (int x = 0; x < 8; x++)
            {
                if (x + xp < 0 || x + xp >= w) continue;

                int xs = x;
                if (mirrorX) xs = 7 - x;
                int col = (colors >> (sheet.pixels[xs + ys * sheet.width + toffs] * 8)) & 255;
                if (col < 255)
                    pixels[(x + xp) + (y + yp) * w] = col;
            }
        }
   
    }

public void render(int xp, int yp, int tile, int colors, int bits) = **"int colors"代表

ColorHelper.get(-1,50,250,455)**(这些数字仅为虚构的例子。)

渲染示例:

//Hight of pixels 8 pixels * 2 from sprite so 16 pixels in x axis in sprite is rendered
        int h = 2;

        //Width of pixels 16 pixels * 13 in y axis in sprite is rendered
        int w = 13;

        //4 Colors sprite which i dont want ! 
        int titleColor = ColorHelper.get(0, 010, 131, 551);

        //Padding Left
        int xo = (screen.w - w * 8) / 2;

        //Padding top
        int yo = 24;

        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                //public void render(int xp, int yp, int tile, int colors, int bits) in Screen.cs 8x8 pixels are rendered
                screen.render(xo + x * 8, yo + y * 8, x + (y + 6) * 32, titleColor, 0);
            }
        }

我想要实现的目标 当我从“public void render”调试“int colors”时,我得到了非常奇怪的数字,这对我来说是不可读的。 (您可以自己尝试。只需创建一个控制台应用程序并粘贴THIS

所以我是来问你的。有没有人可以帮我重新制作这个项目,我可以从精灵中获取所有像素的颜色,就像它们在 png 文件中一样,而无需从 ColorHelper.get 函数转换?我只想按原样渲染 png。

如果没有,没关系,我会继续努力:D。我实际上正在尝试连续大约 3 天解决这个问题。我认为它很难重新制作。

也许我想要很多,但我的大脑无法理解这一点。只有这件事让我无法继续。我希望你明白这一点。

不重要的信息: 如果有人说我正在进入大项目,我应该尝试更简单的东西。我从 2017 年开始在 Unity 工作,我想完全控制我的项目并获得一些更好的编码知识。通过游戏学习是我最好的选择,因为我喜欢游戏。 :) 对于那些不理解我的人,我很抱歉,但我不是来自美国,我来自捷克。

非常感谢。

【问题讨论】:

  • 所以我对游戏开发不是很熟悉,但是我发现ColorHelper.get函数有点熟悉所以我会尝试一下。

标签: c# colors sprite pixel monogame


【解决方案1】:

我不知道如何注释一长段代码,所以我将它放在这里。我认为 .get(4 params) 正在尝试这样做

//rgba ranges from 0 - 255
public static UInt32 get(int b, int g, int r, int a)
{
        UInt32 rgba = ((UInt32)a << 24) | ((UInt32)r << 16)| ((UInt32)g << 8) | 
        ((UInt32)b);
        return rgba; //this will return a 32 bit integer like 3869934080 for 
        //example
}

所以我认为它根本没有采用 4 种颜色,它试图通过它的 rgba 输入来采用 1 种颜色(我不明白为什么它会使用 555 作为范围和 get(1参数)~也许有人可以解释一下)

因此,如果您想显示所需的颜色,请替换参数 r,g,b,a 依次为您提供 32 位无符号整数(这可能会传递给渲染函数。

关于位移的资源here

【讨论】:

    【解决方案2】:

    杜布雷登,

    绕过处理步骤并直接加载文件。 要将 .png 文件加载到 Texture2D,请使用:

    Texture2D 图像 = Texture2D.FromFile(graphicsDevice, @"C:\path\file.png");

    显然,image 必须在包含范围内使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-22
      • 2011-04-01
      • 2013-07-21
      • 2014-08-05
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多