Shader Model 2 Stone        

Shader Model 2 Stone Shader

Description

This shader is exported from rendermonkey 1.61 with one change to one line of code in the .material file as noted below.

It should work with gpu's that support shader model 2 and above. A live Openspace scene (that uses Ogre for 3d, Newton for physics, OpenAL for sound) is posted to: http://ifam.net/openspace/moon2/web/. You need win os or linux running wine and the scol voyager plugin (from http://scolring.org/index.php/logiciels ) to view the scene. If the plugin is not installed on your system you will be prompted for an install. Openspace like Ogre is opensource.

The upside to this shader like any of the rendermonkey shaders is that you can fool around with the variables in rendermonkey and preview them instantly in that app.

I think there is a bug in the rendermonkey exporter. With some help of one of the developers of openspace, I found that you should change the line:

param_named_auto 0 world_viewProj viewproj_matrix


to

param_indexed_auto 0  worldviewproj_matrix


otherwise if you transform the object that is painted with the shader, the shader won't move or scale along with the node that holds the entity on which the shader is painted.


colleagues: feel free to remove my commented out code, -h



Here is a screenshot. This variation that I exported from rendermonkey is good for moonlike terrain methinks:
Image

Usage


Just add the material terrain_material to your object.I have not modified it to work with hardware skinnig. Anyone who wants to do that go ahead and edit this page an add the hardware skinning version below mine.


Material file

//DirectX 9.0 HLSL Vertex Shader vs_1_1
vertex_program HLSL_Stone_Effects.Stones_Effect_Group.terrain_material.Elephant_Body.Vertex_Shader hlsl
{
	source HLSL_Stone_Effects.Stones_Effect_Group.terrain_material.Elephant_Body.Vertex_Shader.program.cg
	target vs_1_1
	entry_point main
}

//DirectX 9.0 HLSL Pixel Shader ps_2_0
fragment_program HLSL_Stone_Effects.Stones_Effect_Group.terrain_material.Elephant_Body.Pixel_Shader hlsl
{
	source HLSL_Stone_Effects.Stones_Effect_Group.terrain_material.Elephant_Body.Pixel_Shader.program.cg
	target ps_2_0
	entry_point hlsl_granite
}



//Effect: terrain_material
material terrain_material
{
	technique
	{
		//Rendering Pass: Elephant Body (pass index: #0 )
		pass
		{
			//DirectX 9.0 HLSL Pixel Shader ps_2_0
			fragment_program_ref HLSL_Stone_Effects.Stones_Effect_Group.terrain_material.Elephant_Body.Pixel_Shader
			{
				//Shader Constant: Ka
				param_indexed 3 float4 0.433333 0.433333 0.433333 1.000000
				//Shader Constant: Kd
				param_indexed 0 float4 0.833333 0.833333 0.833333 0.5
				//Shader Constant: granite_color
				param_indexed 1 float4 0.500000 1.000000 1.000000 1.000000
				//Shader Constant: light_color
				param_indexed 2 float4 1.30000 1.000000 1.000000 1.000000
				//Shader Constant: light_pos
				param_indexed 4 float4 5.600000 100.000000 2.600000 1.000000
			}
			//DirectX 9.0 HLSL Vertex Shader vs_1_1
			vertex_program_ref HLSL_Stone_Effects.Stones_Effect_Group.terrain_material.Elephant_Body.Vertex_Shader
			{
				//Shader Constant: noise_frequency
				param_indexed 8 float4 1.50000 0.5 0.2 0.2
				//Shader Constant: view_matrix
				param_indexed_auto 4 view_matrix
				//Shader Constant: view_proj_matrix
				param_indexed_auto 0 worldviewproj_matrix
			}
			//Texture Stage 0
			//Noise: "..MediaTexturesNoiseVolume.dds"
			//State: D3DSAMP_MAGFILTER, Value : D3DTEXF_LINEAR
			//State: D3DSAMP_MINFILTER, Value : D3DTEXF_LINEAR
			//State: D3DSAMP_MIPFILTER, Value : D3DTEXF_LINEAR
			texture_unit
			{
				texture NoiseVolume.dds 3d
				filtering linear linear linear
			}
		}
	}
}

This is the vertex shader source file. I know it has the .cg extension and it is declared as hlsl, but ogre doesn't seem to care.

HLSL_Stone_Effects.Stones_Effect_Group.terrain_material.Elephant_Body.Vertex_Shader.program.cg

float4x4 view_proj_matrix;
float noise_frequency;
float4x4 view_matrix;
struct VS_OUTPUT
{
   float4 Pos  : POSITION;
   float3 P    : TEXCOORD0;
   float3 Peye : TEXCOORD1;
   float3 Neye : TEXCOORD2;
};


VS_OUTPUT main (float4 vPosition: POSITION, float3 vNormal: NORMAL)
{
   VS_OUTPUT Out = (VS_OUTPUT) 0; 

   Out.Pos = mul (view_proj_matrix, vPosition);

   // Just output model coordinates for this so marble doesn't swim all over
   Out.P = vPosition * noise_frequency;

   // Put position and normal in eye space
   Out.Peye = mul (view_matrix, vPosition);
   Out.Neye = mul (view_matrix, vNormal);

   return Out;
}

This is the pixel shader source file. I know it has the .cg extension and it is declared as hlsl, but ogre doesn't seem to care.

HLSL_Stone_Effects.Stones_Effect_Group.terrain_material.Elephant_Body.Pixel_Shader.program.cg

float4 Kd;
float4 global_ambient;
float4 granite_color;
float4 light_color;
float4 Ka;
float4 light_pos;
sampler noise_volume;
float4 noise (float3 x)
{
    return tex3D (noise_volume, x);
}

float4 diffuse(float3 Neye, float3 Peye)
{
   // Compute normalized vector from vertex to light in eye space  (Leye)
   float3 Leye = (light_pos - Peye) / length(light_pos - Peye);

   return dot(Neye, Leye); // N.L
}

//
// Based on granite.sl on page 354 of The RenderMan Companion 
//
float4 hlsl_granite (float3 P : TEXCOORD0, float3 Peye : TEXCOORD1, float3 Neye : TEXCOORD2) : COLOR
{
   return abs(0.8-noise(P)) * granite_color * light_color * (Ka + Kd * diffuse(normalize(Neye), Peye));
}

Alias: Shader Model 2 Stone