Code: Select all
// Dirt
// import RTSS/NormalMapping_MultiPass from "RTShaderSystem.material"
vertex_program myDirtTileVertexShader glsl
{
source DirtTile.vert
default_params
{
param_named_auto projectionMatrix projection_matrix
param_named_auto viewMatrix view_matrix
param_named_auto worldMatrix world_matrix
param_named_auto lightMatrix texture_worldviewproj_matrix
}
}
fragment_program myDirtTileFragmentShader glsl
{
source DirtTile.frag
default_params
{
param_named_auto ambientLightColour ambient_light_colour
param_named_auto lightDiffuseColour light_diffuse_colour 0.0
param_named_auto lightSpecularColour light_specular_colour 0.0
param_named_auto lightPos light_position 0.0 0.0 0.0 0.0
param_named_auto cameraPosition camera_position
param_named_auto diffuseSurface surface_diffuse_colour
param_named shadowingEnabled bool false
}
}
material Dirt //: RTSS/NormalMapping_MultiPass
{
technique
{
pass
{
vertex_program_ref myDirtTileVertexShader
{
}
fragment_program_ref myDirtTileFragmentShader
{
param_named decalmap int 0
param_named normalmap int 1
param_named shadowmap int 2
}
// We use blending, so that we can see the underlying texture.
texture_unit decalmap
{
texture Dirt.png
}
texture_unit normalmap
{
texture DirtNormal.png
}
texture_unit shadowmap
{
content_type shadow
tex_address_mode clamp
filtering bilinear
}
}
}
}
AND also :
Code: Select all
#version 330 core
uniform sampler2D decalmap;
uniform sampler2D normalmap;
uniform sampler2D shadowmap;
uniform vec4 ambientLightColour;
uniform vec4 lightDiffuseColour;
uniform vec4 lightSpecularColour;
uniform vec4 lightPos;
uniform vec4 cameraPosition;
uniform vec4 diffuseSurface;
uniform bool shadowingEnabled;
in vec2 out_UV0;
in vec2 out_UV1;
in vec3 FragPos;
in vec4 VertexPos;
in mat3 TBN;
out vec4 color;
void main (void)
{
vec3 texelColor = texture(decalmap, out_UV0.st).rgb;
// compute Normal
vec3 Normal = texture(normalmap, out_UV1.st).rgb;
Normal.xyz = 2 * Normal.xyz - (1.0,1.0,1.0);
Normal = normalize(TBN * Normal);
vec4 shadow = vec4(1.0, 1.0, 1.0,1.0);
vec4 tmpVertexPos = VertexPos;
// compute shadowmap
if(shadowingEnabled){
if(tmpVertexPos.z > 0 ){
tmpVertexPos /= tmpVertexPos.w;
shadow = texture(shadowmap, tmpVertexPos.xy);
}
}
// compute lightDir
vec3 lightDir = normalize(lightPos.xyz - FragPos*lightPos.w);
// compute Specular
vec3 viewDirection = normalize( cameraPosition.xyz - FragPos);
vec3 reflectedLightDirection = normalize(reflect(-1.0*lightDir.xyz,Normal));
float spec = max(dot(reflectedLightDirection, viewDirection ), 0.0) ;
spec = pow(spec,16);
vec3 specular = spec * lightSpecularColour.rgb;
// compute Diffuse
float diff = max(dot(lightDir,Normal), 0.0);
vec3 diffuse = diff * lightDiffuseColour.rgb;
vec3 result;
// precompute the lighting term
vec3 lightingTerm = (diffuse + specular + ambientLightColour.rgb/2.0 )*shadow.rgb;
if(diffuseSurface != vec4(1.0,1.0,1.0,1.0))
result = lightingTerm * mix(texelColor, diffuseSurface.rgb,0.5);
else
result = lightingTerm * texelColor;
color = vec4(result.xyz, 1.0);
}
and vertex shader :
Code: Select all
#include "PerlinNoise.glsl"
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 worldMatrix;
uniform mat4 lightMatrix;
layout (location = 0) in vec4 position;
layout (location = 2) in vec3 normal;
layout (location = 14) in vec3 tangent;
layout (location = 8) in vec2 uv_0;
layout (location = 8) in vec2 uv_1;
out vec2 out_UV0;
out vec2 out_UV1;
out vec3 FragPos;
out vec4 VertexPos;
out mat3 TBN;
float freq = 3.1415;
vec3 deform(vec3 pos) {
pos.x += perlin(pos.x,pos.y);
pos.y += perlin(pos.y,pos.x);
return pos;
}
void main() {
// compute world space position, tangent, bitangent
vec3 P = (worldMatrix * position).xyz;
vec3 T = normalize(vec3(worldMatrix * vec4(tangent, 0.0)));
vec3 B = normalize(vec3(worldMatrix * vec4(cross(tangent, normal), 0.0)));
// apply deformation
vec3 PT = deform(P + T);
vec3 PB = deform(P + B);
P = deform(P);
// compute tangent frame
T = normalize(PT - P);
B = normalize(PB - P);
vec3 N = cross(B, T);
TBN = mat3(T, B, N);
gl_Position = projectionMatrix * viewMatrix * vec4(P, 1.0);
FragPos = P;
out_UV0 = uv_0;
out_UV1 = uv_1;
VertexPos = lightMatrix * position;
}