» ShaderSnippets
This site relies heavily on Javascript. You should enable it if you want the full experience. Learn more.

ShaderSnippets

Why reinvent the wheel? There're some handy little functions for the everyday HLSL writer. Please add your snippets in this wiki page.

General snippets

get clipping planes of a Perspective Projection

float4x4 tPI: PROJECTIONINVERSE;
float FarPlane = 1 / (tPI[2][3] + tPI[3][3]);
float NearPlane = 1 / distance(tPI[2][3], tPI[3][3]);

VertexShader snippets

apply rotation to vertices via texture

rotation function:

float4x4 Vrotate(float rotX, 
         float rotY, 
         float rotZ)
  {
   rotX *= TwoPi;
   rotY *= TwoPi;
   rotZ *= TwoPi;
   float sx = sin(rotX);
   float cx = cos(rotX);
   float sy = sin(rotY);
   float cy = cos(rotY);
   float sz = sin(rotZ);
   float cz = cos(rotZ);
 
   return float4x4( cz*cy+sz*sx*sy, sz*cx, cz*-sy+sz*sx*cy, 0,
                   -sz*cy+cz*sx*sy, cz*cx, sz*sy+cz*sx*cy , 0,
                    cx * sy       ,-sx   , cx * cy        , 0,
                    0             , 0    , 0              , 1);
  }

use the function like this:

PosO = mul(PosO, Vrotate(Sampler.r, Sampler.g, Sampler.b));
NormO = mul(NormO, Vrotate(Sampler.r, Sampler.g, Sampler.b));

Sampler.rgb are the corresponding values from the texture

PixelShader snippets for Image processing

invert

rgb-inversion

col.rgb = 1 - col.rgb;

contrast

col.rgb = ((col.rgb - 0.5) * var) + 0.5;

if var > 1, the contrast is greater. var = 0 gives a grey picture
The 0.5 can be replaced with a variable to give a gamma or centre
point for the contrast as well, for more fine tuning...

black&white

const float4 lumcoeff = {0.299, 0.587, 0.114, 0};
col = dot(col, lumcoeff);

the dot product of the color (col) and the luminace coefficients results in a correct b&w image

lumakey

const float4 lumcoeff = {0.299, 0.587, 0.114, 0};
float luma = length(dot(col, lumcoeff));
col.a = step(var, luma);

var is the keying amount. the rest works like the b&w conversion.

scene alpha

return(tex2D(Samp, float2(IN.TexCd.xy)).wwww);

Texture input must be in A8R8G8B8 format.

working alpha channel in 3d

needs 2 passes so connect an iobox with 2 rows to the pass pin and fill it with 0,1

technique workingalpha
{
    pass P0
    {
        VertexShader = compile vs_1_0 VS();
        PixelShader  = compile ps_2_0 PS();
        AlphaBlendEnable = false;
 
        AlphaTestEnable = true;
        AlphaFunc = Greater;
        AlphaRef = 245;
 
        ZEnable = true;
        ZWriteEnable = true;
 
        CullMode = None;
    }
    pass P1
    {
        VertexShader = compile vs_1_0 VS();
        PixelShader  = compile ps_2_0 PS();
        AlphaBlendEnable = true;
        SrcBlend = SrcAlpha;
        DestBlend = InvSrcAlpha;
 
        AlphaTestEnable = true;
        AlphaFunc = LessEqual;
        AlphaRef = 245;
 
        ZEnable = true;
        ZWriteEnable = false;
 
        CullMode = None;
    }
}

anonymous user login

Shoutbox

~2h ago

microdee: however non-conductive objects are invisible for this so the pencil and the sticks in the video are still a mysteries

~2h ago

microdee: hey i just found a possible solution behind leap's black magic http://www.quora.com/Leap-Motion/What-is-the-technology-behind-Leap-Motion

~3h ago

mediadog: @u7agel: red node, no connections; can' drag&drop into patch either - 27.2

~7h ago

christosk: Thanx Elliot! UnixPath worked fine :-)

~7h ago

elliotwoods: or 'unix path' (for forward slash). also custom deliminators are accepted on other pins

~7h ago

elliotwoods: @christosk : check for 'dos path'

~8h ago

christosk: What's the node to use in order to separate elements inside a string by using slashes? Separate doesn't give a Slash enumeration.

~10h ago

metrowave: vvvv not loading, it says 'no DLLs were found in the waterfall procedure' to do with error at loading ippSP library. any ideas?

~13h ago

fjen: Kaiser #BYOB this saturday ... check forum for details

~13h ago

catweasel: is there a max number of audio outs? I seem to have found one...