OGRE CG教程 (四): 全息衍射图效果

Description

As they are many holo... hologram... and holographic shaders, this one is called "holographe". 
有一种全息效果叫做"holographe".

This shader contains several effects:

  • one color gradient from the center vertical axis of the object to the border. (In my example: dark when pixel is near the center and brighter when away).
  • one transparency gradient from the center vertical axis of the object to the border. (In my example: transparent when pixel is near the center).
  • noise: dark bands, light bands, transparent bands scolling along the object.
这样的着色器包含几个效果:
  • 从物体的中心垂直轴到物体的边界有一个颜色梯度(在我的例子中:当像素靠近中心时变黑,远离时变亮)
  • 从物体的中心垂直轴到物体的边界有一个透明梯度(在我的例子中:靠近中心时,像素变得透明).
  • 噪声: 暗带,亮带,透明带会沿着物体滚动.
I have tried several ways to do it: with a fixed colour, or with a palette file, or with custom parameters... 
我曾尝试过一些方法去实现它:颜色混合,调色文件,或者custom参数.
Finally I think that the pictures files offer more flexibility.
最后我认为,使用图片文件比较灵活.
As this is one of my first shader creation, feel free to make it better, or to give remarks in the discussion page.
作为我第一次的shader教程,你可以让它变得更好,也可以在讨论去进行评论.

Usage

  • You need to pass the object radius as a custom parameter to the shader, for every object that will use this material.
  • You need to have 2 TGA files (RGB+A) : one for the color gradient, and one for the noise.
  • The holoColour texture: the picture size is 1 x 256 for instance. The right pixels are the color/transparency at the center of the object, and the rightest pixel at the border of the object.
  • The holoNoise texture: the picture size is 1 x 256. This picture is filled with noise. Black dots will give a dark line, white dots a bright line, and alpha dots a transparent line.
  • 你需要把物体半径作为用户参数传给shader,每个物体都会使用这种材质
  • 你需要有两个TGA 文件(RGB+A) : 颜色梯度和噪声各需要一个.
  • holoColour 纹理:对于实例,你需要一张1 x 256的纹理. 右边的像素是物体中心的透明度和颜色, 最右边的像素是物体的边界.
  • holoNoise 纹理: 图片的大小是 1 x 256. 图片里面装满了噪声. 黑色的点用来画黑线,白色的点用来画白线, 透明的点用来画透明的线

Material

vertex_program shader/holographeVP cg
 {
     source holographe3.cg
     entry_point main_vp
     profiles vs_1_1
 
     default_params
     {
         param_named_auto worldViewProj worldviewproj_matrix
        }
 }
 
 fragment_program shader/holographeFP cg
 {
     source holographe3.cg
     entry_point main_fp
     profiles ps_2_x
 }
 
 material shader/hologram
 {
     technique
     {
         pass
         {
            scene_blend alpha_blend
            vertex_program_ref shader/holographeVP
             {
             param_named_auto customParamRadius custom 0
             }
            fragment_program_ref shader/holographeFP
             {
                       param_named_auto Time time
             }
            texture_unit  holoColour
             {
             texture holoColour1D.tga 1d
             }
            texture_unit  holoNoise
             {
             texture holographe1D.tga 1d
             tex_address_mode wrap
             }
         }
     }
 }

Vertex Program

// --------------------------------------------------------------------
 // ''Author : David de Lorenzo''
 // ''Holographe Shader - Vertex Program''
 // -----------------------------------------------------------------
 void main_vp(    in      float4   position  : POSITION,
                  uniform float4x4 worldViewProj,
                  uniform float4   customParamRadius,
                  out     float4   oPosition : POSITION,
                  out     float4   oMisc     : TEXCOORD0
              )
 {
     // ''calculate output position''
     oPosition = mul(worldViewProj, position);
 
     // ''we pass the vertex position to the fragment shader''
     oMisc   = position;
     oMisc.w = customParamRadius.x;
 }

Fragment Program

// -----------------------------------------------------------------
 // ''Author : David de Lorenzo''
 // ''Holographe Shader - Fragment Program''
 // ''Effect #1: Area near the center of the object are more transparent''
 // ''Effect #2: Light and dark bands are moving upwards on the object surface''
 // ''param iPos : the position of the fragment, supplied by the VP''
 // ''param Time  : the time in [0....]''
 // ''param hologNoise  : a RGBA file (1x256 pixels) with little noise''
 // ''param hologColour : a RGBA file (1x256 pixels) with a color and transparency gradient''
 // -----------------------------------------------------------------
 float4 main_fp(
                   in        float4    iPos        : TEXCOORD0,
                   uniform   float     Time,
                   uniform   sampler1D holoNoise,
                   uniform   sampler1D holoColour
                ) :  COLOR0
 {
    float4 oColor;
 
     float objectradius = iPos.w;
 
     // ''Cylinder type hologram''
     // ''Distance from actual Vertex to center''
     float  Vdist =  (iPos.x*iPos.x) + (iPos.z*iPos.z);
     // ''Normalize distance to [0..1]''
     float  ratio  =  Vdist / (objectradius*objectradius);
     oColor   = tex1D(holoColour, ratio);
 
     float  index     = (iPos.y-Time) * 0.05;
     float4 colorBand = tex1D(holoNoise,index);
     // ''We add light bands and dark bands''
     oColor.rgb = oColor.rgb * colorBand.rgb;
     // ''We add bands with transparency''
     oColor.a   = oColor.a * colorBand.a;
 
     return (oColor);
 }







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值