Adding a vertex shader to a material

I can create a shader that inherits from ShaderBase, override the VSMain function, then put things in here. The issue is: how do I actually use the shader? I’ve tried adding the shader to various material properties, but if the shader inherits from shaderbase, it has no effect on the mesh.

what kind of effect do you want to achieve with your custom shader? from what i know, currently you can only use shaders that inherit from ComputeColor directly in the editor.

if you want to do custom rendering then you have to setup the shader/effect by code.

here is an example of a custom render pipeline:

you can also implement your own material, i did that for particles but don’t know enough to be of much help here. i basically followed this tutorial:
https://doc.xenko.com/latest/en/manual/particles/tutorials/particle-materials.html

I’m looking to displace the vertices of some meshes depending on their y axis value.

Thanks for those links. I’ll look into them.

have a look at the displacement map material feature, might easy enough to make your own that you can add to a material in the editor:

1 Like

The MaterialShader sample might be an example of what you’re looking for. You probably will want to have the Xenko VS plugin installed if not already. While it’s not mandatory to have it for this, you will have to build and run to review your shader in-game instead of seeing it update inside Xenko Studio as it’s auto compiled. Big time saver.

I think you basically just want to create a displacement map shader? If so that’s pretty easy to get some default functionality, the basic steps are:

  1. Create a new shader that inherits from ComputeColor at minimum, and if you want to make use of sampling a texture (heightmap, etc), ineheriting from Texturing is also helpful.
  2. Create a new material. Under it’s properties in the Geometry section, assign a Displacement Map in the Displacement slot.
  3. Within the displacement slot properties, use the name of the shader you just created in the open text field (type assist should suggest it if it’s being compiled by the VS plugin correctly and available).

That will get your shader into the system and running. Even though it’s a ComputeColor shader, since it’s attached to the Displacement Map, it’s really modifying the vertex’s position not the color.

The example from the MaterialShader sample uses this pretty basic shader, and all it does is displace the vertices in a sin wave pattern, roughly looking like a wave in water. Note: the generic parameters (frequency and such) are not required, but if the generic’s are a primitive numeric you can get them passed into the shader very easily, but don’t try and pass in a material or texture or anything complex there, or you’ll just bang your head against the wall fighting the engine, I say this from experience :slight_smile:

shader ComputeColorWave<float Frequency, float Amplitude, float Speed> : ComputeColor, Texturing
{
    override float4 Compute()
    {
        float phase = length(streams.TexCoord - 0.5);
        return sin((phase + Global.Time * Speed) * 2 * 3.14 * Frequency) * Amplitude;
    }
};

If you want more that this simple control, then you need to implement your own RootRenderFeature. tonfilm’s example is excellent. I used his example + the MaterialShader and CustomEffect samples to improve an example of my own I can provide that implements very simplistic water using flow maps + with configurable displacement.

I hope that helps and answers your actual question, and I didnt misunderstand and give you tangent advice :slight_smile:

1 Like

Thanks, but I believe the displacement map can only displace along the normal.

I’m currently messing with creating a new material feature based on the displacement map one, but it seems like it might be kind of difficult… it seems rather tightly bound to just displacement along the normal and mixing in a new stream you can write to seems … difficult?

It might just be that I don’t understand this well enough yet, but this is about as far along as I’ve gotten:
image

(this doesn’t actually do what you’d want yet but… it’s there, all without modifying the engine thus far, but it feels like maybe modifying the engine might be the only reasonable path forward soon)

… or maybe not, looking at the shader code I managed to get it to spit out, it’s very close, but then I spectacularly broke something and it stopped compiling :smiley:

1 Like