Custom VertexBuffer and lighting?

Trying to get basic lighting to work when using a custom vertex and index buffer with layout of position/normal/color. Normals are set up correctly.

I tried using “mixin ParadoxDefaultForwardShader” to no avail.

Adding “mixin VertexColorShading” gets the colors to appear - but absolutely nothing I tried can get the lighting of any kind to work. I’m not sure how to tell what’s going on with the shader system, it’s a bit of a black box.

I tried following the examples but from what I can tell, it seems like things fall apart when you’re not using models loaded from a file.

I didn’t have any issues using my own vertex element. Can you show how you setup the code? Also are you using the entity system with a material cause that makes things quite a bit different. If you are drawing your mesh “manually” it’s actually not too hard to setup a simple shader.

I am using a Model and Mesh and setting the vertex buffer index buffer on the mesh. The models are in an Entity. I am using a custom vertex format of Position (Vector3), Normal (Half4), Color (Color). I feel like the problem is it’s not using the normals since I just get a flat color.

Anyway, I create a pipeline with:

RenderPipelineLightingFactory.CreateDefaultForward(this, "VoxEffectMain", Color.SkyBlue, true, true);

And my directional light with (in an entity):

var directLight = CreateDirectLight(new Vector3(-1, 1, -1), new Color3(1, 1, 1), 0.2f, ShadowMapFilterType.Variance);
Entities.Add(directLight);

CreateDirectLight:

private Entity CreateDirectLight(Vector3 direction, Color3 color, float intensity, ShadowMapFilterType filterType)
{
    return new Entity()
    {
        new LightComponent
        {
            Type = LightType.Directional,
            Color = color,
            Deferred = false,
            Enabled = true,
            Intensity = intensity,
            LightDirection = direction,
            Layers = RenderLayers.RenderLayerAll,
            ShadowMap = false,
            ShadowFarDistance = 3000,
            ShadowNearDistance = 10,
            ShadowMapMaxSize = 1024,
            ShadowMapMinSize = 512,
            ShadowMapCascadeCount = 4,
            ShadowMapFilterType = filterType,
            BleedingFactor = 0,
            MinVariance = 0
        }
    };
}

And my shader looks like:

using SiliconStudio.Paradox.Effects.Data;

namespace Vox.Effects
{
    shader VoxEffectMain
    {
        mixin ParadoxDefaultForwardShader;
        mixin VertexColorShading; // adding this line changes from all black to flat color
    };
}

I have no idea what I should put in the shader language. Do we have to use custom coded shaders?

VertexColorShading will overwrite the pixel shader to just output the vertex color, so any lighting won’t take effect.

ParadoxDefaultForwardShader should work fine, but it doesn’t automatically use your vertex color, so it will fall back to black. You will need to tell it where to look for a color value by setting the shaders albedoDiffuse variable. Try changing your shader like this:

...
mixin ParadoxDefaultForwardShader
mixin compose albedoDiffuse = ComputeColorStream;
...

However, you don’t need to modify the shader yourself. This can also be achieved by adding a Material to your mesh and set it’s MaterialParameters.AlbedoDiffuse property.

There was an earlier discussion on how to do this in code.
More specifically, to use the vertex color, you will need to change the example like so:

...
mixinSource.Mixins.Add(new ShaderClassSource("ComputeColorStream"));
...

That should do it!

I can verify that this works the way jwollen is saying, I would recommend the material as it’s very easy to change in the future if you need to, or you can set it per entity.

Thanks, this is what I needed! I am seeing the lighting now.

I’m not really sure how I would have figured this out on my own. I guess I need to really get into the guts of the shader system and understand what’s going on.

Would be cool if Paradox output an .fx file with the generated shader code to better understand what it is generating (though I’m not sure if that is how it does it internally - is it just creating bytecode on the fly?)

Thanks again!

It does output the .fx file! You can check the bin/platform/Debug/log folder. :slight_smile:

Well I’ll be damned, lol - thanks Toaster!