Please login or register.

Login with username, password and session length
Advanced search  

News:

This forum provides RSS feed. To query recent posts use this url. More...


Author Topic: Shader sharing. Part I: BorderGlow.fx  (Read 5523 times)

0 Members and 1 Guest are viewing this topic.

cremen

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 26
    • View Profile
    • Pink Town
Shader sharing. Part I: BorderGlow.fx
« 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:


To be continued, stay turned.
« Last Edit: October 04, 2009, 01:27:56 AM by Jyujinkai »
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 35
  • Offline Offline
  • Gender: Male
  • Posts: 5643
    • View Profile
    • Dead:Code Site
Re: Shader sharing. Part I: BorderGlow.fx
« Reply #1 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.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

cremen

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 26
    • View Profile
    • Pink Town
Re: Shader sharing. Part I: BorderGlow.fx
« Reply #2 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)

 
Logged

FogGobbler

  • Regular poster
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 228
    • View Profile
Re: Shader sharing. Part I: BorderGlow.fx
« Reply #3 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?
« Last Edit: October 04, 2009, 04:58:58 PM by FogGobbler »
Logged

cremen

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 26
    • View Profile
    • Pink Town
Re: Shader sharing. Part I: BorderGlow.fx
« Reply #4 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.
Logged

FogGobbler

  • Regular poster
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 228
    • View Profile
Re: Shader sharing. Part I: BorderGlow.fx
« Reply #5 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 :-)
Logged

SoundGuy

  • Regular poster
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 196
    • View Profile
Re: Shader preview
« Reply #6 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?
Logged

Jyujinkai

  • Global Moderator
  • Frequent poster
  • *
  • Karma: 1
  • Offline Offline
  • Posts: 350
    • View Profile
    • Jyujinkai's WME Development Blog
Re: Shader sharing. Part I: BorderGlow.fx
« Reply #7 on: December 13, 2009, 08:43:24 AM »

always post screenshots :)
Logged
<Antoine de Saint-Exupéry> In any thing at all, perfection is finally attained not when there is no longer anything to add, but when there is no longer anything to take away...
<Carl Sagan> If you want to make a apple pie from scratch. You must first... invent the universe

SoundGuy

  • Regular poster
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 196
    • View Profile
Re: Shader sharing. Part I: BorderGlow.fx
« Reply #8 on: December 15, 2009, 11:33:33 AM »

example:

Logged

GW-Arch

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 2
    • View Profile
Re: Shader sharing. Part I: BorderGlow.fx
« Reply #9 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"
  }
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 35
  • Offline Offline
  • Gender: Male
  • Posts: 5643
    • View Profile
    • Dead:Code Site
Re: Shader sharing. Part I: BorderGlow.fx
« Reply #10 on: May 25, 2010, 06:59:04 AM »

GW-Arch: Yes, that's right.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave
 

Page created in 0.605 seconds with 22 queries.