Skip to content

Instantly share code, notes, and snippets.

@ghiboz
Created August 5, 2014 13:24
Show Gist options
  • Save ghiboz/4045c44a3b102a1ac904 to your computer and use it in GitHub Desktop.
Save ghiboz/4045c44a3b102a1ac904 to your computer and use it in GitHub Desktop.
the grDS.mat is the main file
material grsDS
{
lod_values 0.1 2
diffuse 1 1 1 1
specular 0 0 0 0 1
ambient 1 1 1
emissive 0 0 0
scene_blend default
depth_write default
depth_check default
alpha_rejection default
transparent_sorting default
polygon_mode default
diffuseMap black.png
specMap
pass
{
vertex_program grsDS_vertex
fragment_program grsDS_fragment
shader_properties
{
diffuseMap $diffuseMap
specMap $specMap
}
diffuse $diffuse
specular $specular
ambient $ambient
emissive $emissive
scene_blend $scene_blend
depth_write $depth_write
depth_check $depth_check
alpha_rejection $alpha_rejection
transparent_sorting $transparent_sorting
polygon_mode $polygon_mode
texture_unit diffuseMap
{
texture $diffuseMap
}
texture_unit specMap
{
texture $specMap
}
texture_unit shadowMap0
{
content_type shadow
tex_address_mode clamp
filtering none
}
texture_unit shadowMap1
{
content_type shadow
tex_address_mode clamp
filtering none
}
texture_unit shadowMap2
{
content_type shadow
tex_address_mode clamp
filtering none
}
}
}
#include "core.h"
#include "spotLight.h"
// http://www.ogre3d.org/forums/viewtopic.php?f=5&t=36312
// http://www.ogre3d.org/forums/viewtopic.php?f=2&t=43696
// http://www.ogre3d.org/docs/manual/manual_23.html
// http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Brain+Damage+part+2+-+JaJDoo+Shader+Guide+-+Basics&highlight=light_direction_object_space
#define SHADOWS_PSSM @shGlobalSettingBool(shadows_pssm)
#define SPEC_MAP @shGlobalSettingBool(enable_spec_map)
#if SHADOWS_PSSM
#include "shadowsMW.h"
#endif
#if SHADOWS_PSSM
#define NEED_DEPTH
#endif
#ifdef SH_VERTEX_SHADER
//
// VVVVVVVV VVVVVVVV tttt
// V::::::V V::::::V ttt:::t
// V::::::V V::::::V t:::::t
// V::::::V V::::::V t:::::t
// V:::::V V:::::V eeeeeeeeeeee rrrrr rrrrrrrrr ttttttt:::::ttttttt eeeeeeeeeeee xxxxxxx xxxxxxx
// V:::::V V:::::V ee::::::::::::ee r::::rrr:::::::::r t:::::::::::::::::t ee::::::::::::ee x:::::x x:::::x
// V:::::V V:::::V e::::::eeeee:::::eer:::::::::::::::::r t:::::::::::::::::t e::::::eeeee:::::ee x:::::x x:::::x
// V:::::V V:::::V e::::::e e:::::err::::::rrrrr::::::rtttttt:::::::tttttt e::::::e e:::::e x:::::xx:::::x
// V:::::V V:::::V e:::::::eeeee::::::e r:::::r r:::::r t:::::t e:::::::eeeee::::::e x::::::::::x
// V:::::V V:::::V e:::::::::::::::::e r:::::r rrrrrrr t:::::t e:::::::::::::::::e x::::::::x
// V:::::V:::::V e::::::eeeeeeeeeee r:::::r t:::::t e::::::eeeeeeeeeee x::::::::x
// V:::::::::V e:::::::e r:::::r t:::::t tttttte:::::::e x::::::::::x
// V:::::::V e::::::::e r:::::r t::::::tttt:::::te::::::::e x:::::xx:::::x
// V:::::V e::::::::eeeeeeee r:::::r tt::::::::::::::t e::::::::eeeeeeee x:::::x x:::::x
// V:::V ee:::::::::::::e r:::::r tt:::::::::::tt ee:::::::::::::e x:::::x x:::::x
// VVV eeeeeeeeeeeeee rrrrrrr ttttttttttt eeeeeeeeeeeeee xxxxxxx xxxxxxx
//
SH_BEGIN_PROGRAM
shUniform(float4x4, wvp) @shAutoConstant(wvp, worldviewproj_matrix)
shUniform(float4x4, textureMatrix0) @shAutoConstant(textureMatrix0, texture_matrix, 0)
shVertexInput(float2, uv0)
shOutput(float4, UV)
shNormalInput(float4)
shOutput(float3, normalPassthrough)
// Depth in w
shOutput(float4, objSpacePositionPassthrough)
#if SHADOWS_PSSM
@shForeach(3)
shOutput(float4, lightSpacePos@shIterator)
shUniform(float4x4, texViewProjMatrix@shIterator) @shAutoConstant(texViewProjMatrix@shIterator, texture_viewproj_matrix, @shIterator)
@shEndForeach
shUniform(float4x4, worldMatrix) @shAutoConstant(worldMatrix, world_matrix)
#endif
// _ __ _ __ ___ __ _ _ __ __ _ _ __ ___
// | '_ \ | '__| / _ \ / _` | | '__| / _` | | '_ ` _ \
// | |_) | | | | (_) | | (_| | | | | (_| | | | | | | |
// | .__/ |_| \___/ \__, | |_| \__,_| |_| |_| |_|
// | | __/ |
// |_| |___/
//
SH_START_PROGRAM
{
shOutputPosition = shMatrixMult(wvp, shInputPosition);
UV.xy = shMatrixMult (textureMatrix0, float4(uv0,0,1)).xy;
normalPassthrough = normal.xyz;
#ifdef NEED_DEPTH
objSpacePositionPassthrough.w = shOutputPosition.z;
#endif
objSpacePositionPassthrough.xyz = shInputPosition.xyz;
#if SHADOWS_PSSM
float4 wPos = shMatrixMult(worldMatrix, shInputPosition);
@shForeach(3)
lightSpacePos@shIterator = shMatrixMult(texViewProjMatrix@shIterator, wPos);
@shEndForeach
#endif
}
#else
//
// FFFFFFFFFFFFFFFFFFFFFF tttt
// F::::::::::::::::::::F ttt:::t
// F::::::::::::::::::::F t:::::t
// FF::::::FFFFFFFFF::::F t:::::t
// F:::::F FFFFFFrrrrr rrrrrrrrr aaaaaaaaaaaaa ggggggggg ggggg mmmmmmm mmmmmmm eeeeeeeeeeee nnnn nnnnnnnn ttttttt:::::ttttttt
// F:::::F r::::rrr:::::::::r a::::::::::::a g:::::::::ggg::::g mm:::::::m m:::::::mm ee::::::::::::ee n:::nn::::::::nn t:::::::::::::::::t
// F::::::FFFFFFFFFF r:::::::::::::::::r aaaaaaaaa:::::a g:::::::::::::::::gm::::::::::mm::::::::::m e::::::eeeee:::::een::::::::::::::nn t:::::::::::::::::t
// F:::::::::::::::F rr::::::rrrrr::::::r a::::a g::::::ggggg::::::ggm::::::::::::::::::::::me::::::e e:::::enn:::::::::::::::ntttttt:::::::tttttt
// F:::::::::::::::F r:::::r r:::::r aaaaaaa:::::a g:::::g g:::::g m:::::mmm::::::mmm:::::me:::::::eeeee::::::e n:::::nnnn:::::n t:::::t
// F::::::FFFFFFFFFF r:::::r rrrrrrr aa::::::::::::a g:::::g g:::::g m::::m m::::m m::::me:::::::::::::::::e n::::n n::::n t:::::t
// F:::::F r:::::r a::::aaaa::::::a g:::::g g:::::g m::::m m::::m m::::me::::::eeeeeeeeeee n::::n n::::n t:::::t
// F:::::F r:::::r a::::a a:::::a g::::::g g:::::g m::::m m::::m m::::me:::::::e n::::n n::::n t:::::t tttttt
// FF:::::::FF r:::::r a::::a a:::::a g:::::::ggggg:::::g m::::m m::::m m::::me::::::::e n::::n n::::n t::::::tttt:::::t
// F::::::::FF r:::::r a:::::aaaa::::::a g::::::::::::::::g m::::m m::::m m::::m e::::::::eeeeeeee n::::n n::::n tt::::::::::::::t
// F::::::::FF r:::::r a::::::::::aa:::a gg::::::::::::::g m::::m m::::m m::::m ee:::::::::::::e n::::n n::::n tt:::::::::::tt
// FFFFFFFFFFF rrrrrrr aaaaaaaaaa aaaa gggggggg::::::g mmmmmm mmmmmm mmmmmm eeeeeeeeeeeeee nnnnnn nnnnnn ttttttttttt
// g:::::g
// gggggg g:::::g
// g:::::gg gg:::::g
// g::::::ggg:::::::g
// gg:::::::::::::g
// ggg::::::ggg
// gggggg
//
SH_BEGIN_PROGRAM
shSampler2D(diffuseMap)
#if SPEC_MAP
shSampler2D(specMap)
#endif
shInput(float4, UV)
shInput(float3, normalPassthrough)
shInput(float4, objSpacePositionPassthrough)
#if SHADOWS_PSSM
@shForeach(3)
shInput(float4, lightSpacePos@shIterator)
shSampler2D(shadowMap@shIterator)
shUniform(float2, invShadowmapSize@shIterator) @shAutoConstant(invShadowmapSize@shIterator, inverse_texture_size, @shIterator(1))
@shEndForeach
shUniform(float3, pssmSplitPoints) @shSharedParameter(pssmSplitPoints)
#endif
#if SHADOWS_PSSM
shUniform(float4, shadowFar_fadeStart) @shSharedParameter(shadowFar_fadeStart)
#endif
shUniform(float4, lightPosition[@shGlobalSettingString(num_lights)]) @shAutoConstant(lightPosition, light_position_view_space_array, @shGlobalSettingString(num_lights))
shUniform(float4, lightDirection[@shGlobalSettingString(num_lights)]) @shAutoConstant(lightDirection, light_direction_view_space_array, @shGlobalSettingString(num_lights))
shUniform(float4, lightDiffuse[@shGlobalSettingString(num_lights)]) @shAutoConstant(lightDiffuse, light_diffuse_colour_array, @shGlobalSettingString(num_lights))
shUniform(float4, lightAttenuation[@shGlobalSettingString(num_lights)]) @shAutoConstant(lightAttenuation, light_attenuation_array, @shGlobalSettingString(num_lights))
shUniform(float4, lightAmbient) @shAutoConstant(lightAmbient, ambient_light_colour)
shUniform(float4, spotLightParams[@shGlobalSettingString(num_lights)]) @shAutoConstant(spotLightParams, spotlight_params_array, @shGlobalSettingString(num_lights))
shUniform(float4x4, worldView) @shAutoConstant(worldView, worldview_matrix)
shUniform(float4, materialAmbient) @shAutoConstant(materialAmbient, surface_ambient_colour)
shUniform(float4, materialDiffuse) @shAutoConstant(materialDiffuse, surface_diffuse_colour)
shUniform(float4, materialEmissive) @shAutoConstant(materialEmissive, surface_emissive_colour)
shUniform(float3, cameraPosObjSpace) @shAutoConstant(cameraPosObjSpace, camera_position_object_space)
shUniform(float3, lightSpec0) @shAutoConstant(lightSpec0, light_specular_colour, 0)
shUniform(float3, lightPosObjSpace0) @shAutoConstant(lightPosObjSpace0, light_position_object_space, 0)
shUniform(float, materialShininess) @shAutoConstant(materialShininess, surface_shininess)
shUniform(float3, materialSpecular) @shAutoConstant(materialSpecular, surface_specular_colour)
// _ __ _ __ ___ __ _ _ __ __ _ _ __ ___
// | '_ \ | '__| / _ \ / _` | | '__| / _` | | '_ ` _ \
// | |_) | | | | (_) | | (_| | | | | (_| | | | | | | |
// | .__/ |_| \___/ \__, | |_| \__,_| |_| |_| |_|
// | | __/ |
// |_| |___/
//
SH_START_PROGRAM
{
float4 newUV = UV;
#ifdef NEED_DEPTH
float depthPassthrough = objSpacePositionPassthrough.w;
#endif
float3 normal = normalPassthrough;
float4 diffuse = shSample(diffuseMap, newUV.xy);
shOutputColour(0) = diffuse;
float3 viewPos = shMatrixMult(worldView, float4(objSpacePositionPassthrough.xyz,1)).xyz;
float3 viewNormal = normalize(shMatrixMult(worldView, float4(normal.xyz, 0)).xyz);
float3 pixelToLight;
float d;
float4 lightResult = float4(0,0,0,1);
float spotlightFactor;
@shForeach(@shGlobalSettingString(num_lights))
pixelToLight = lightPosition[@shIterator].xyz - (viewPos * lightPosition[@shIterator].w);
d = length(pixelToLight);
pixelToLight = normalize(pixelToLight);
spotlightFactor = 1.f;
if (spotLightParams[@shIterator].x != 1 || spotLightParams[@shIterator].y != 0 || spotLightParams[@shIterator].z != 0 || spotLightParams[@shIterator].w != 1)
{
spotlightFactor = shSaturate(
(dot(pixelToLight, normalize(-lightDirection[@shIterator].xyz)) - spotLightParams[@shIterator].y) /
(spotLightParams[@shIterator].x - spotLightParams[@shIterator].y));
}
lightResult.xyz += materialDiffuse.xyz * lightDiffuse[@shIterator].xyz
* shSaturate(1.0 / ((lightAttenuation[@shIterator].y) + (lightAttenuation[@shIterator].z * d) + (lightAttenuation[@shIterator].w * d * d)))
* max(dot(viewNormal.xyz, pixelToLight), 0) * spotlightFactor;
#if @shIterator == 0
float3 directionalResult = lightResult.xyz;
#endif
@shEndForeach
lightResult.xyz += lightAmbient.xyz * materialAmbient.xyz + materialEmissive.xyz;
lightResult.a *= materialDiffuse.a;
// shadows only for the first (directional) light
#if SHADOWS_PSSM
float shadow = pssmDepthShadow (lightSpacePos0, invShadowmapSize0, shadowMap0, lightSpacePos1, invShadowmapSize1, shadowMap1, lightSpacePos2, invShadowmapSize2, shadowMap2, depthPassthrough, pssmSplitPoints);
#endif
#if SHADOWS_PSSM
float fadeRange = shadowFar_fadeStart.x - shadowFar_fadeStart.y;
float fade = 1-((depthPassthrough - shadowFar_fadeStart.y) / fadeRange);
shadow = (depthPassthrough > shadowFar_fadeStart.x) ? 1.0 : ((depthPassthrough > shadowFar_fadeStart.y) ? 1.0-((1.0-shadow)*fade) : shadow);
#endif
#if !SHADOWS_PSSM
float shadow = 1.0;
#endif
#if SHADOWS_PSSM
shOutputColour(0) *= (lightResult - float4(directionalResult * (1.0-shadow),0));
#else
shOutputColour(0) *= lightResult;
#endif
// specular
float3 eyeDir = normalize(cameraPosObjSpace.xyz - objSpacePositionPassthrough.xyz);
float3 light0Dir = normalize(lightPosObjSpace0.xyz);
float3 specular = float3(0,0,0);
float shininess = materialShininess;
float3 matSpec = materialSpecular.xyz;
#if SPEC_MAP
// con mappa
float4 specTex = shSample(specMap, UV.xy);
float dotNL = dot ( light0Dir, normal );
float3 ref = (normal * 2 * dotNL) - light0Dir;
float dotRV = dot(ref, eyeDir);
float spec = pow(saturate(dotRV), shininess * specTex.a);
specular = spec * specTex.xyz * matSpec;
#else
// senza mappa
float dotNL = dot ( light0Dir, normal );
float3 ref = (normal * 2 * dotNL) - light0Dir;
float dotRV = dot(ref, eyeDir);
float spec = pow(saturate(dotRV), shininess);
specular = spec * matSpec;
#endif
shOutputColour(0).xyz += specular * lightSpec0 * shadow;
// prevent negative colour output (for example with negative lights)
shOutputColour(0).xyz = max(shOutputColour(0).xyz, float3(0,0,0));
}
#endif
lod_configuration 1
{
enable_spec_map false
}
lod_configuration 2
{
enable_spec_map false
}
material MT_Test
{
parent grsDS
ambient 1 1 1 1
diffuse 1 1 1 1
specular 1 1 1 1 4
emissive 0 0 0 1
diffuseMap textures/metalrust_D.jpg
specMap textures/metalrust_S.jpg
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment