Urho3D Wiki
(Created page with "== Custom shader parameters == You can define and use own shader parameters like this: ==== '''Define the parameter in the material.xml:''' ==== <material> <technique n...")
Tags: Visual edit apiedit
 
No edit summary
Tags: Visual edit apiedit
Line 1: Line 1:
== Custom shader parameters ==
 
 
You can define and use own shader parameters like this:
 
You can define and use own shader parameters like this:
   
Line 26: Line 25:
 
==== '''You can also modify the parameters dynamically in you code:''' ====
 
==== '''You can also modify the parameters dynamically in you code:''' ====
 
materialSkySphere->SetShaderParameter("SunDir",Vector3(0,1,0));
 
materialSkySphere->SetShaderParameter("SunDir",Vector3(0,1,0));
== Expand default terrain material to 4 textures using alpha channel ==
 
It's possible to also use the alpha channel as a color weight to get an additional texture for the terrain (or something else). (Using alpha/transparency makes drawing (at least in Gimp) difficult, see the next section for a better editable improvement with six colors&textures)
 
 
Add another texture to the terrain material "Data/Materials/Terrain.xml" like:
 
<material>
 
<technique name="Techniques/TerrainBlend.xml" />
 
<texture unit="0" name="Textures/TerrainWeights.png" />
 
<texture unit="1" name="Textures/TerrainDetail1.dds" />
 
<texture unit="2" name="Textures/TerrainDetail2.dds" />
 
<texture unit="3" name="Textures/TerrainDetail3.dds" />
 
<texture unit="4" name="Textures/StoneDiffuse.dds" />
 
<parameter name="MatSpecColor" value="0.5 0.5 0.5 16" />
 
<parameter name="DetailTiling" value="16 16" />
 
</material>
 
For OpenGL: Change the "CoreData/Shaders/GLSL/TerrainBlend.glsl" to additionally use the alpha channel:
 
...
 
uniform sampler2D sWeightMap0;
 
uniform sampler2D sDetailMap1;
 
uniform sampler2D sDetailMap2;
 
uniform sampler2D sDetailMap3;
 
uniform sampler2D sDetailMap4; // <- added
 
...
 
void PS()
 
{
 
// Get material diffuse albedo
 
vec4 weights = texture2D(sWeightMap0, vTexCoord).rgba; // <- changed
 
weights.a=1.0-weights.a; // <- added. Alpha should be weight in reverse (easier editing)
 
// with 0 (fully transparent) being full weight.
 
float sumWeights = weights.r + weights.g + weights.b + weights.a; // <- changed
 
weights /= sumWeights;
 
vec4 diffColor = cMatDiffColor * (
 
weights.r * texture2D(sDetailMap1, vDetailTexCoord) +
 
weights.g * texture2D(sDetailMap2, vDetailTexCoord) +
 
weights.b * texture2D(sDetailMap3, vDetailTexCoord) + // <- changed
 
weights.a * texture2D(sDetailMap4, vDetailTexCoord) // <- added
 
);
 
...
 
If you want to use DirectX, also change the HLSL accordingly.
 
 
Use another splatting image (watch out for high red/green/blue values inside the alpha, it should be black with high alpha). My texture weight image and the result look like this:
 
[[File:Rgba terrain.jpg|centre|thumb|730x730px]]
 

Revision as of 23:38, 25 May 2015

You can define and use own shader parameters like this:

Define the parameter in the material.xml:

<material>
    <technique name="Techniques/skysphere.xml" />
    <parameter name="SunDir" value="0.64 0.64 0.64"/>
    <parameter name="SkyColor" value="0.9 0.5 0.5 1" />
</material>

Here I'm adding a Vector3/vec3 called SunDir and a RGBA color called SkyColor. Hint: there is no difference between RGB colors and Vector3/vec3 or between RGBA colors and Vector4/vec4, both are types with 3 respectively 4 floats.

In the Shader (here GLSL):

// at the top above and outside all functions:
uniform vec3 cSunDir;
uniform vec4 cSkyColor;
...
void PS()    // the parameters can also be used in the vertex shader (VS())
{
  float sun_intensity=clamp(dot(vNormal,cSunDir),0.0,1.0);   // calculate a float with the dot product of the SunDir parameter and the vertex normal
  gl_FragColor=cSunColor*sun_intensity;                      // multiplying a vec4 / RGBA-Color with a float
}

In shaders the parameter names need to be preceded with a 'c'. See http://urho3d.github.io/documentation/1.32/_shaders.html.

See http://en.wikibooks.org/wiki/GLSL_Programming/Vector_and_Matrix_Operations for an explanation of some of the shader functions.

You can also modify the parameters dynamically in you code:

materialSkySphere->SetShaderParameter("SunDir",Vector3(0,1,0));