Description
This shader applies to an object a color that fade to dark when getting away from the camera. I use it for large object, to emphase the feeling of largeness. The object is not textured, but just colorized.
这个shader可以为物体创造渐隐效果.把它用在大的物体上,可以体现出物体的大.这个物体并没有纹理,它只是被着色了.
Usage
To use the shader, you will need to give 2 custom parameters:
- the color of the object.
- the radius of the object.
为了使用这个shader,我们需要两个custom变量
- 物体的颜色
- 物体的半径
And then apply the shader/gradient material to your object.
接下来我们在程序中应用纹理.
Example :
例子:
Vector4 vColour = Vector4(circlecolour.r, circlecolour.g, circlecolour.b, circlecolour.a);
Vector4 vRadius = Vector4(mRadius,0,0,0);
mObject->setCustomParameter(0,vColour);
mObject->setCustomParameter(1,vRadius);
mObject->setMaterialName("shader/gradient");
Gradientshader.cg
// --------------------------------------------------------------------
// Gradient shader : vertex program
// Author : David de Lorenzo
// -----------------------------------------------------------------
void gradient_vp( in float4 position : POSITION,
uniform float4x4 worldViewProj, //物体坐标->世界坐标 的转换矩阵
uniform float3 camera_position_object_space, //摄像机在物体空间下的坐标
uniform float4 customParamColour,
uniform float4 customParamRadius,
out float4 oPosition : POSITION, //输出的位置
out float4 oColor : COLOR //输出的颜色
)
{
// Get the custom parameters
float objectradius = customParamRadius.x; //获得渐隐
// calculate output position
oPosition = mul(worldViewProj, position); //计算输出的位置
// Calculate the color value, depending on the position 根据摄像机位置计算颜色
float3 vect_center_to_cam = camera_position_object_space;
float3 position_closest = normalize(vect_center_to_cam) * objectradius; //把摄像机向量单位化再乘以渐隐的半径,获得最靠近的位置
//Rc其实就是两个向量的点积 与摄像机夹角越大,值就越小
float Rc = (position_closest.z * position.z ) + (position_closest.y * position.y ) + (position_closest.x * position.x); // Ratio on Center-to-Cam axis
Rc = Rc / (objectradius * objectradius); // recalibrate (-1..1) 如果渐隐半径比物体小的话,获得的值就会超过1
float ratio = (1+Rc); // recalibrate ( 0..2) - (x2 to emphasis the color)
oColor= ratio * customParamColour;
}
// -----------------------------------------------------------------
// Gradient shader : fragment program
// Author : David de Lorenzo
// -----------------------------------------------------------------
float4 main_fp(in float4 color : COLOR) : COLOR0
{
return (color);
}
下面分析下default_params的地方:
default_params那段代码。这段定义了在执行期给着色器的参数。param_named_auto 定义了一个被Ogre 3D自动传给着色器的参数【这里的参数是指非语义指定的由渲染管线自动填充数值的参数,而是指由程序显示传进来的参数,此参数和c语义函数的参数意义一样】。
关键字uniform表示参数在每次绘图调用是恒定的【一次完整的绘制过程中是恒定的,但下次完整的绘制时可能是另一个值】。
Gradientshader.material
vertex_program shader/gradientVP cg
{
source gradientshader.cg
entry_point gradient_vp
profiles vs_1_1
default_params
{
param_named_auto worldViewProj worldviewproj_matrix
param_named_auto camera_position_object_space camera_position_object_space //摄像机在物体坐标系下的坐标
}
}
fragment_program shader/gradientFP cg
{
source gradientshader.cg
entry_point main_fp
profiles ps_1_1
}
material shader/gradient
{
technique
{
pass
{
vertex_program_ref shader/gradientVP
{
param_named_auto customParamColour custom 0
param_named_auto customParamRadius custom 1
}
fragment_program_ref shader/gradientFP
{
}
}
}
}