Wintermute Engine Forum

Wintermute Engine => Scripts, plugins, utilities, goodies => Topic started by: cremen on October 03, 2009, 08:52:23 AM

Title: Shader sharing. Part I: BorderGlow.fx
Post by: cremen on October 03, 2009, 08:52:23 AM
First of all, feature request:
Need a script command for runtime change shader constants. Something like this (look a shader constant at border shader):
actor.SetShaderConstantVec4( "BorderColor", 1.0, 0.0, 0.0, 0.0 );
actor.SetShaderConstantVec1( "BorderWidth", 5.0 );
actor.SetTexture( "GlowTexture", "actors\glow12.jpg");
Additionaly needs the shader constant setters in actor definition file. (like texture setting, constant and others)

Border shader (Simple outer glow). Useful for some selection effects. (Sometimes for 'show hidden objects' effect with one trick)
Code: WME Script
  1. float4x4 Wm: World;                 // World matrix for object
  2. float4x4 WvpM: WorldViewProjection;    // World * View * Projection matrix
  3. float4   LightPos: ActorLightPos;
  4.  
  5. float4 BorderColor = { 1.0, 0.0, 0.0, 0.7 };
  6. float BorderWidth = 4.0;
  7.  
  8. texture g_MeshTexture<
  9.     string ResourceName = "actors\\inga\\inga.jpg";
  10. >;
  11.  
  12. // Data structure for Vertex Shaders
  13. struct invs_data {
  14.     float4 Position     : POSITION;
  15.     float3 Normal       : NORMAL;
  16.     float2 UV      : TEXCOORD0;
  17. };
  18.  
  19. // Data structure for Pixel color shader
  20. struct outvs_data {
  21.     float4 Position     : POSITION;
  22.     float2 UV      : TEXCOORD0;
  23.         float4 Diffuse  : COLOR0;
  24. };
  25.  
  26. outvs_data VertShader(invs_data IN){
  27.         float4 pos = mul(IN.Position, WvpM);
  28.         float3 lightDir = normalize(LightPos-pos);
  29.  
  30.         outvs_data OUT = (outvs_data)0;
  31.         OUT.UV = IN.UV;
  32.         OUT.Diffuse = dot( IN.Normal, lightDir );
  33.         OUT.Position = pos;
  34.         return OUT;
  35. }
  36.  
  37. sampler MeshTextureSampler =
  38. sampler_state
  39. {
  40.     Texture = <g_MeshTexture>;
  41.     MipFilter = LINEAR;
  42.     MinFilter = LINEAR;
  43.     MagFilter = LINEAR;
  44. };
  45.  
  46. float4 PixShader(outvs_data IN): COLOR0 {
  47.         float4 col = max(0.4, IN.Diffuse) * tex2D(MeshTextureSampler, IN.UV);
  48.         return float4( col.xyz, 1.0);
  49. }
  50.  
  51. float4 GlowVertShader(invs_data IN): POSITION{
  52.         float4 pos = mul(IN.Position, WvpM);
  53.         float4 nrm = normalize(mul(IN.Normal, WvpM));
  54.         return pos + nrm * BorderWidth;
  55. }
  56. float4 GlowPixShader(float4 pos: POSITION): COLOR0 {
  57.         return BorderColor;
  58. }
  59.  
  60. technique Main{
  61.          pass glowPass{
  62.                 VertexShader = compile vs_2_0 GlowVertShader();
  63.                 PixelShader = compile ps_2_0 GlowPixShader();
  64.                 ZWriteEnable=false;
  65.                 //ZTest=true;
  66.                 CullMode = None;
  67.         }
  68.         pass colorPass{
  69.                 VertexShader = compile vs_2_0 VertShader();
  70.                 PixelShader = compile ps_2_0 PixShader();
  71.                 ZEnable=true;
  72.                 ZWriteEnable=true;
  73.                 //CullMode = None;
  74.         }
  75. }
  76.  

Result:
(http://img12.imageshack.us/img12/1402/borderglow.th.jpg) (http://img12.imageshack.us/i/borderglow.jpg/)

To be continued, stay turned.
Title: Re: Shader sharing. Part I: BorderGlow.fx
Post by: Mnemonic on October 03, 2009, 05:12:11 PM
Thanks cremen!
Setting shader parameters from scripts is definitely on my roadmap.

I did a small change to the effect:

texture g_MeshTexture : DiffuseMap;

Instead of hardcoding the model texture, this semantics will automatically use the one currently assigned to the model.
Title: Re: Shader sharing. Part I: BorderGlow.fx
Post by: cremen on October 03, 2009, 06:08:02 PM
Quote
texture g_MeshTexture : DiffuseMap;
Cool. I think you need to add this trick at the help. 8)

 
Title: Re: Shader sharing. Part I: BorderGlow.fx
Post by: FogGobbler on October 04, 2009, 04:37:15 PM
Thanks for the Glow-Demo!  ::beer

Could you please show us a demo of the per-pixel lighting shader you used?!

Thanks,
Oli

P.S. I´ve just been playing around with the glow effect (using trinity and the 3Ddemo-Front) and noticed that the texture that has the glow effect doesn´t react to the light sources (spot lights). I´m a total newbie in shader things, so could anybody please help me here?
Title: Re: Shader sharing. Part I: BorderGlow.fx
Post by: cremen on October 04, 2009, 05:24:27 PM
Other shaders demo comming soon.

And light DON'T AFFECT at the border. It is not bug, it is feature.
Title: Re: Shader sharing. Part I: BorderGlow.fx
Post by: FogGobbler on October 04, 2009, 07:54:18 PM

Quote
And light DON'T AFFECT at the border. It is not bug, it is feature.

Ahhh..Eh..stupid me.. thanks! Looking forward to the new shader demos :-)
Title: Re: Shader preview
Post by: SoundGuy on October 13, 2009, 02:40:45 AM
i did the following - i changed the color of the edge to black, and the border width

float4 BorderColor = { 0.0, 0.0, 0.0, 1 };
float BorderWidth = 4.0;


i added these lines into trinity's .act3d

EFFECT
  {
    MATERIAL = "trinityskin3"
    EFFECT_FILE = "actors\trinity\outer_glow2.fx"
  }
 

and what i see are holes in the edge contour. (i can post a screenshot if you can't reproduce it yourself)
any ideas?
Title: Re: Shader sharing. Part I: BorderGlow.fx
Post by: Jyujinkai on December 13, 2009, 08:43:24 AM
always post screenshots :)
Title: Re: Shader sharing. Part I: BorderGlow.fx
Post by: SoundGuy on December 15, 2009, 11:33:33 AM
example:

(http://pizza-morgana.com/beta/trinity_hole.JPG)
Title: Re: Shader sharing. Part I: BorderGlow.fx
Post by: GW-Arch on May 24, 2010, 04:42:42 PM
Great work!
Thanx!
 ::thumbup

I'm using few files for texturing, so - i need to modify code this way?

EFFECT
  {
    MATERIAL = "skin1"
    EFFECT_FILE = "actors\<your-actor>\outer_glow.fx"
  }

EFFECT
  {
    MATERIAL = "skin2"
    EFFECT_FILE = "actors\<your-actor>\outer_glow.fx"
  }

EFFECT
  {
    MATERIAL = "skin3"
    EFFECT_FILE = "actors\<your-actor>\outer_glow.fx"
  }
Title: Re: Shader sharing. Part I: BorderGlow.fx
Post by: Mnemonic on May 25, 2010, 06:59:04 AM
GW-Arch: Yes, that's right.