Empty Vertex + Pixel Shader

Structure

The code starts with #defines we already know from C or C++. We basically map HLSL and GLSL constants (shader and vertex models, etc.) into a variable; beeing chosed during the compile time, in order to support HLSL and GLSL in one single .fx file.

Variables

The first thing we place below the defines are variables. Those variables are automatically “public” and can be set from outer space. Quick reference to variable declarations.

MonoGame does apply on each drawn Texture2D a “default” shader. Hence, the default shader already takes in account the projection and view matrix. However, while using a custom shader, the proj and view matrix must be provided in order to render the Texture2D at the correct position. Therefore, we declare two variables: projectionMatrix and viewMatrix.

Vertex Logic

The parameters of the method (or function) must equal exactly this syntax! Even a different order of the variables will cause an exception.

  • position: The vertex positions
  • color: The colors on the texture
  • texCoord: The coordinates of the texture

Since we receive those values, we can modify each one of them in order to apply our custom shader effect. We return a custom struct (VertexShaderOutput) which is arbitrary. But since the pixel-shader builds up on the result of the vertex shader, texCoord and color are at least required.

The variables “viewMatrix” and “projectionMatrix” are multiplied by the position of the object. (Position = float4 = (x,y,z,w))

Pixel Logic

The result of the vertex shader will be passed on to the pixel shader. The pixel shader picks the color for each pixel and returns a float4.

I did create a struct for the return. Also possible would be a simple: “return tex2D(…);”

Technique

The technique contains n passes. Thus, a shader may contain several vertex and pixel manipulations. Since MonoGame only allows one shader per spriteBatch.Draw to be applied, we not only need one pass, but n passes to call as many shader functions as we want.

Vertex vs Pixel Shader

As the name already indicates, the vertex shader modifies the vertices and the pixel shader modifies the pixels drawn onto the screen. Based on the required effect, the code belongs into the vertex and/or pixel function. As an example

PixelShader

  • Color adjustments
  • Texture2D combinations
  • Pixel removal
  • Lights

VertexShader

  • Distortion
  • Position adjustments

Source

One Comment

  1. Pingback: Wind Shader

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.