How Use array in fx file?

Just a note:

If you render all lights in one loop (forward lighting) you can simply have an array with let’s say size 40 but only loop through the amount of lights you actually use.
Not sure if I’m way too late here, but yeah.

#define MAXLIGHT 20

float3  PointLightPosition[MAXLIGHT];
float4  PointLightColor[MAXLIGHT];
float   PointLightIntensity[MAXLIGHT];
float3  PointLightDirection[MAXLIGHT];
float   PointLightRadius[MAXLIGHT];
int     MaxLightsRendered = 0;

then in the pixel or vertex shader

 [loop]
    for (int i = 0; i < MaxLightsRendered; i++)
    {
        float3 DirectionToLight = PointLightPosition[i] - worldPos;
                           
        float DistanceSq = lengthSquared(DirectionToLight);

        float radius = PointLightRadius[i];
             
        [branch]
        if (DistanceSq < abs(radius*radius))
        {
            //calculate lighting
etc...

in the c# code to pass arrays I use…

private static readonly Vector3[] PointLightPosition = new Vector3[MaxLightsGpu];
private static readonly Vector4[] PointLightColor = new Vector4[MaxLightsGpu];
private static readonly float[] PointLightIntensity = new float[MaxLightsGpu];
private static readonly float[] PointLightRadius = new float[MaxLightsGpu];
private static readonly Vector3[] PointLightDirection = new Vector3[MaxLightsGpu];

public static void Initialize(Effect lightingEffect)
        {
            //POINTLIGHTS
            _lightingEffectPointLightPosition = lightingEffect.Parameters["PointLightPosition"];
            _lightingEffectPointLightColor = lightingEffect.Parameters["PointLightColor"];
            _lightingEffectPointLightIntensity = lightingEffect.Parameters["PointLightIntensity"];
            _lightingEffectPointLightDirection = lightingEffect.Parameters["PointLightDirection"];
            _lightingEffectPointLightRadius = lightingEffect.Parameters["PointLightRadius"];
            _lightingEffectMaxLightsRendered = lightingEffect.Parameters["MaxLightsRendered"];

            //LightTiles = new int[LightTilesHeight*LightTilesWidth];
        }

blabla calculations - is our light actually in the scene etc.

if(currentIndex>0)
            {
                _lightingEffectPointLightPosition.SetValue(PointLightPosition);
                _lightingEffectPointLightColor.SetValue(PointLightColor);
                _lightingEffectPointLightIntensity.SetValue(PointLightIntensity);
                _lightingEffectPointLightRadius.SetValue(PointLightRadius);
                _lightingEffectPointLightDirection.SetValue(PointLightDirection);
            }
            _lightingEffectMaxLightsRendered.SetValue(currentIndex);

I post this just as a short comprehensive post of how to pass arrays, not saying this lighting set up is ideal.

Thx all.

Solution proposed by @Ravendarke works very well. But I need to pass light effect on my player and few layers.
With my player animation I had to modifie frametime, and it was became a bad solution in my case.
Firthermore cause of additive blendstate, I had problem with my background.

@kosmonautgames thx, its a better solution for me.

[quote=“kosmonautgames, post:21, topic:8037”]
post this just as a short comprehensive post of how to pass arrays, not saying this lighting set up is ideal.
[/quote]al.

lol, no problem, I want to know how use array in a fx file and its ok now, thx :wink:

Wait, why I was whole time operating with assumption that you are using deferred rendering? What confused me? Nevermind.

Most probably because of the name of the method:
DeferredNormalPS
I thought the same thing

1 Like