光渲染导致低FPS

时间:2012-05-27 13:03:09

标签: c# xna rendering lightning

我正在为我的XNA游戏开发一个类,它可以渲染图像上的灯光。当时,我已经制作了绘制我的光照贴图的来源,但是,我的源码中的FPS非常低。我知道在循环每个像素时它会被残酷地减少,但是,我不知道任何其他方式来获得&在XNA中设置我的纹理上的每个像素,但使用“For”语句?

当前来源:

 public struct Light
    {
        public int Range;
        public int Intensity;
        public Color LightColor;
        public Vector2 LightLocation;

        public Light(int _Range, int _Intensity, Color _LightColor, Vector2 _LightLocation)
        {
            Range = _Range;
            Intensity = _Intensity;
            LightLocation = _LightLocation;
            LightColor = _LightColor;
        }
    }
    public class RenderClass
    {
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        public static extern bool MessageBox(IntPtr h, string S, string C, int a);

        public static Texture2D RenderImage(Light[] LightLocations, Texture2D ScreenImage, Viewport v, bool ShadowBack = false)
        {
            Texture2D[] Images = new Texture2D[LightLocations.Count()];
            int curCount = 0;

            /*LOOP THROUGHT EACH LIGHT*/
            foreach (Light LightLocation in LightLocations)
            {
                /*VARIABLES*/
                Color LightColor = LightLocation.LightColor;
                int Range = LightLocation.Range;
                int Intensity = LightLocation.Intensity;

                /*GET COLORS*/
                int Width = v.Width;
                int Height = v.Height;
                Color[] Data = new Color[Width * Height];
                ScreenImage.GetData<Color>(Data);

                /*VARIABLES TO SET COLOR*/
                Color[] SetColorData = new Color[Width * Height];

                /*CIRCEL*/
                int Radius = 15 / 2; // Define range to middle [Radius]
                int Area = (int)Math.PI * (Radius * Radius);

                for (int X = 0; X < Width; X++)
                {
                    for (int Y = 0; Y < Height; Y++)
                    {
                        int Destination = X + Y * Width;

                        #region Light
                        /*GET COLOR*/
                        Color nColor = Data[Destination];

                        /*CREATE NEW COLOR*/
                        Vector2 MiddlePos = new Vector2(LightLocation.LightLocation.X + Radius, LightLocation.LightLocation.Y + Radius);
                        Vector2 CurrentLocation = new Vector2(X, Y);

                        float Distance;
                        Distance = Vector2.Distance(MiddlePos, CurrentLocation);
                        Distance *= 100;
                        Distance /= MathHelper.Clamp(Range, 0, 100);

                        Vector3 newColors = nColor.ToVector3();

                        nColor = new Color(
                            newColors.X,
                            newColors.Y,
                            newColors.Z, 
                            Distance / 100);


                        /*SET COLOR*/
                        SetColorData[Destination] = nColor; // Add to array
                        #endregion
                        #region Shadow
                        #endregion
                    }
                }


                ScreenImage.SetData<Color>(SetColorData);
                Images[curCount] = ScreenImage;
                curCount++;
            }

            return Images[0]; // Temporarily returning the first image of the array.
        }
    }

正如您所看到的,这是一种缓慢而糟糕的方法。所以我想知道,有没有更好的方法来获得&amp;设置每个像素?

提前致谢,dotTutorials! =)

1 个答案:

答案 0 :(得分:1)

我认为这项工作最好在像素着色器中完成。

您可以创建一个一次操作一个灯光的效果文件。
XNA使用DX9,因此您将限制为128个常量寄存器,我认为您可以使用它来挤压最多三个灯光。 / p>

因此,您将光照贴图设置为渲染目标,遍历所有光源,在效果上设置常量数据,渲染渲染目标大小的四边形,并在像素着色器中计算光照方程。

本质上是这样的:

    // In LoadContent
RenderTarget2D lightmapRT = new RenderTarget2D(graphics.GraphicsDevice,
                                                 128,
                                                 128,
                                                 false, //No mip-mapping
                                                 SurfaceFormat.Color,
                                                 DepthFormat.Depth24);

// We now render to the lightmap in Render method
graphics.GraphicsDevice.SetRenderTarget(lightmapRT);

// Lightmap is black by default
graphics.GraphicsDevice.Clear(Color.Black);

// Use the sprite batch to draw quads with custom shader
spriteBatch.Begin(0, BlendState.Opaque, null, null, null, lightmapFx);

foreach (var light in lights)
{
    // Pass the light parameters to the shader
    lightmapFx.Parameters["Viewport"].SetValue(new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height));
    lightmapFx.Parameters["Range"].SetValue(light.Range);
    lightmapFx.Parameters["Intensity"].SetValue(light.Intensity);
    lightmapFx.Parameters["LightColor"].SetValue(light.LightColor);
    lightmapFx.Parameters["LightLocation"].SetValue(light.LightLocation);

    // Render quad
    spriteBatch.Draw(...);
}
spriteBatch.End();

FX文件看起来像这样:

float Range;
float Intensity;
float3 LightColor;
float2 LightLocation;
float2 Viewport;

struct VStoPS
{
    float4 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
};

VStoPS VS(in float4 color    : COLOR0,
          in float2 texCoord : TEXCOORD0,
          in float4 position : POSITION0)
{
    VStoPS vsout = (VStoPS)0;

    // Half pixel offset for correct texel centering.
    vsout.Position.xy -= 0.5;

    // Viewport adjustment.
    vsout.Position.xy = position.xy / Viewport;
    vsout.Position.xy *= float2(2, -2);
    vsout.Position.xy -= float2(1, -1);

    // Pass texcoords as is
    vsout.TexCoord = texCoord;

    return vsout;
}

float4 PS(VStoPS psin)
{
    // Do calculations here
    // Here I just set it to white
    return float4(1.0f, 1.0f, 1.0f, 1.0f);
}

technique Main
{
    pass p0
    {
        VertexShader = compile vs_3_0 VS();
        PixelShader = compile ps_3_0 PS();
    }
}

请注意,此未经测试的代码可能充满了错误。我把它留给你来弄清楚像素着色器需要做什么。