I’m trying to apply an Effect (pixel shader) to a TiledMap of the monogame.extended framework.
This shader is very simple since it only forces the color of the pixels to black and white. It works perfectly on a simple 2D texture displayed with the spriteBatch.Draw method.
My TiledMap is displayed correctly without using any effects:
_mapRenderer.Draw (_camera.GetViewMatrix (), null, null);
on the other hand, when I pass the Effect as a parameter, the map simply no longer appears on the screen:
_mapRenderer.Draw (_camera.GetViewMatrix (), null, effect);
Does anyone use any effects that work on a TiledMap? Or would have an idea on the problem, if it is a bug or a misunderstanding on my part …?
Also, I don’t see a vertex shader in your shader program. The vertex shader is important as it transforms the positions of the tiles from model space all the way to clip space.
I tried affect or not a WorldViewProjection matrix ( mapEffect.Parameters[“WorldViewProjection”].SetValue(_level.getCameraViewMatrix());) with the same result : NullReferenceException, in the MonoGame.Extended.Graphics.Effects.DefaultEffect.UpdateMaterialColor() method.
This exception usely deals with trying to set a missing shader variable or setting a shader variable that is not used.
The only one I found in the UpdateMaterialColor() method is : _diffuseColorParameter.SetValue(diffuseColorVector4). If I declare a variable float4 DiffuseColor; then I must use it and I don’t know what to do with it.
(if I try to use it I end up with a IndexOutOfRangeException in MonoGame.Extended.Graphics.Effects.DefaultEffect.OnApply(), so I guess it’s not a good way to go…)
I must misanderstanding something in the TiledMapEffect working and about shaders. Could you put me on the right way ?
Note that MonoGame has XNA heritage which has roots in DirectX; OpenGL traditionally calls the first matrix “Model” where DirectX calls it “World”. They pragmatically have the same meaning.
My recommendation is to either not use DefaultEffect and write your own C# code or make sure your shader matches what is expected of TiledMapEffect and it’s inherited classes such as DefaultEffect, and MatrixChainEffect.
I choosed the 2nd option, “make sure your shader matches what is expected of TiledMapEffect” as it’s far more simple and it preserves the mechanic of TiledMapEffect, and it’s works
Here is what I did following your advices
C# code remains the same :
Create the TiledMapEffect
mapEffect = new TiledMapEffect(Content.Load(“MapEffect”));
I am trying to get the Map effects working too. Man I tried for hours @wakaja could you maybe upload the code snippet of the effect, the effect class in c# and so one? It would really help ^^ I would love to see it as a minimalistic working example and would like to add it to the Monogame-extended wiki too
I don’t anderstand what missing you as everything you need is in my previous post. What is not working for you ?
The code of the effect is DefaultEffect.fx that you can find in MonoGame Extended Github repo (see link in previous post) with “PixelShaderFunctionPositionTexture” method changed as explained to turn color into black and white. I renamed the file MapEffect.fx
The effect class is “TiledMapEffect”, also from Monogame Extended framework, instanciated with the clone constructor : new TiledMapEffect(Content.Load(“MapEffect”));
When you draw your TiledMapRenderer in your game, just pass the effect in the draw method.