1

我有一个动态光照着色器,它的着色精灵在我自己的测试程序中很好,但是一旦我将它导入到我朋友的基于物理的游戏中,它就开始类似于日食。我通过将渐变简化为纯粹基于形状内的 X 值来缩小范围,并使精灵中的圆圈外部变为红色,但是如您所见,旋转继续导致问题(无法发布图像,所以这里是专辑的链接)。

不同旋转的圆圈(不是按顺序,而是用弧度值标记):http: //imgur.com/a/Preth

我对矩阵数学的所有研究都表明我使用了正确的旋转公式,但我想也许我做错了什么。这是我的 .fx 着色器代码:

float rotationrads; /*assumed rotation is in radians*/
sampler TextureSampler: register(s0);

float4 staticlight(float2 Tex: TEXCOORD0) : COLOR0
{
float4 Color = tex2D(TextureSampler, Tex);





    float2 NewTex;

    /*Get the new X and Y values by applying the UV formula with the rotation*/
    NewTex.x = (Tex.x * cos(rotationrads)) - (Tex.y * sin(rotationrads));
    NewTex.y = (Tex.y * sin(rotationrads)) + (Tex.y * cos(rotationrads));

if(Color.a > 0.0)
{
    Color.r = (Color.r * NewTex.x);
    Color.g = (Color.g * NewTex.x);
    Color.b = (Color.b * NewTex.x);
}
else
{
    Color.r = 100;
    Color.g = 0;
    Color.b = 0;
    Color.a = 100;
}


return Color;  
}

technique StaticLightOnly
{
pass Pass1
{
    PixelShader = compile ps_2_0 staticlight();
}
}

如果有人对 2d 着色器中基于精灵的旋转有经验,我将不胜感激!先谢谢了!

4

1 回答 1

1

因为旋转是围绕原点执行的,所以您必须将旋转中心 (0.5, 0.5) 移动到原点,执行旋转然后撤消平移。

于 2013-03-21T09:16:45.423 回答