Adding a vertex shader to a material


#1

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.


#2

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


#3

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.


#4

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:


#5

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: