椭圆纹理未按预期做

问题描述 投票:0回答:1

我正在单游戏中为简单形状生成动态纹理。是的,我知道该系统的缺点,但是我只是在尝试构建自己的物理引擎。我正在尝试为here所述的椭圆生成纹理。

我有一个PaintDescriptor函数,该函数接受x和y像素坐标,并返回它应该是什么颜色。红色只是我在调试时,通常它是Color.Transparent。

public override Color PaintDescriptor(int x, int y)
{
      float c = (float)Width / 2;
      float d = (float)Height / 2;
      return pow((x - c) / c, 2) + pow((y - d) / d, 2) <= 1 ? BackgroundColor : Color.Red;
}

现在,如果Width == Height,那么这将起作用,因此是一个圆。但是,如果它们不相等,则会生成带有一些椭圆形形状的纹理,但也会出现带状/条纹状。]

normal circlebandingstriping

我曾尝试查看自己的宽度和高度是否已更改,而我还是尝试了其他几件事。要注意的一件事是,在desmos的法线坐标系中,我有(y + d)/ d,但是由于屏幕的y轴是翻转的,因此我必须翻转代码中的y偏移量:(y-d)/ d。用于纹理生成和绘制的其余相关代码在这里:

public Texture2D GenerateTexture(GraphicsDevice device, Func<int, int, Color> paint)
{
    Texture2D texture = new Texture2D(device, Width, Height);

    Color[] data = new Color[Width * Height];

    for (int pixel = 0; pixel < data.Count(); pixel++)
        data[pixel] = paint(pixel / Width, pixel % Height);

    texture.SetData(data);

    return texture;
}

public void Draw(float scale = 1, float layerdepth = 0, SpriteEffects se = SpriteEffects.None)
{
    if (SBRef == null)
        throw new Exception("No reference to spritebatch object");

    SBRef.Draw(Texture, new Vector2(X, Y), null, null, null, 0, new Vector2(scale, scale), Color.White, se, layerdepth);
}

public float pow(float num, float power) //this is a redirect of math.pow to make code shorter and more readable
{
    return (float)Math.Pow(num, power);
}

为什么这个不匹配desmos?为什么它不成椭圆形?

编辑:我忘了提及,但是我遇到的一种可能的解决方案是始终绘制一个圆,然后将其缩放到所需的宽度和高度。对于我来说,这是不可接受的,因为可能会导致图形模糊或其他瑕疵,但更主要是因为我想了解使用此解决方案目前无法获得的任何内容。

c# math textures monogame ellipse
1个回答
0
投票

[入睡后,第十次有了新的想法之后,我找到了答案。在GenerateTexture函数中:

data[pixel] = paint(pixel / Width, pixel % Height);

应该是

data[pixel] = paint(pixel % Width, pixel / Height);
© www.soinside.com 2019 - 2024. All rights reserved.