Wednesday 15 July 2015

hlsl - Gradient-generating shader with arbitrary color components -



hlsl - Gradient-generating shader with arbitrary color components -

the task is: shader takes in constant color, generates pixel colors according positions replacing 2 of 4 color components (rgba) texture coordinates. hardcoded component set like:

float4 inputcolor : register(c0); float4 main(float2 uv : texcoord) : color { float4 color = 0; color.a = inputcolor.a; color.r = inputcolor.r; color.g = uv.x; color.b = uv.y; homecoming color; }

now i'd pass in parameter(s) specifying components should replaced uv.x , uv.y. let's inputcolor has -1 , -2 in these components. or there uint xindex , yindex parameters specifying positions in vector4 replaced. hlsl not allow "color[xindex] = uv.x".

currently i've done in ugly way bunch of if-else. sense there cross-product or matrix multiplication solution. ideas?

you work 2 additional vectors channelmasks. works indexing, vector operators.

float4 inputcolor : register(c0); float4 uvx_mask : register(c1); //e.g. (0,0,1,0) float4 uvy_mask : register(c2); // e.g. (0,0,0,1) float4 main(float2 uv : texcoord) : color { float4 color = 0; // replacing uvx channel uv.x color = lerp(inputcolor, uv.x * uvx_mask, uvx_mask); // replacing uvy channel uv.y color = lerp(color , uv.y * uvy_mask, uvy_mask); homecoming color; //in illustration (inputcolor.r, inputcolor.g, uv.x, uv.y) }

if need lastly bit of performance work alternative preprocessor (#define, #ifdef) build right code on demand.

hlsl

No comments:

Post a Comment