Color cycling using shaders

Started by
16 comments, last by Headkaze 15 years, 8 months ago
Last time I asked about this I got two suggestions to create color cycling using shaders a) using a conversion to HSL and rotate the hue and b) use a texture lookup. So I decided to try b. I can successfully make my image fade to white, but I can't seem to be able to get the texture lookup to work and so the color cycling is not working either. When I load my "PaletteTex" which is a 256 x 256 pixel texture with a spectrum of colors running across it horizontally I set it to the effect like so:
m_fx.SetValue("PaletteTex", m_paletteTexture);
And this is in my renderloop.
m_fx.Technique = "ColorCycle";
m_fx.SetValue("gOffset", m_offset);
m_fx.Begin(FX.None);
m_fx.BeginPass(0);
...
m_fx.EndPass();
m_fx.End();
If I leave the code as it is below it will fade all the colors to white. So the gOffset (0 - 1.0f) variable is working, but if I try to access the "palcolor" variable I get an error. Obviously I need to use palcolor to be able to cycle the colors. Can you see any errors in the code?
sampler2D TextureSampler;
texture PaletteTex;
float gOffset;

sampler2D PaletteSampler : TEXUNIT0 = sampler_state
{
    Texture = (PaletteTex);
};

//vertex to pixel shader structure
struct v2p
{
    float2 TexCoord : TEXCOORD0;
};

//pixel shader to screen
struct p2f
{
    float4 Color : COLOR0;
};

void CycleTextureColor(in v2p In, out p2f Out)
{
	float4 color = tex2D(TextureSampler, In.TexCoord);
	float4 palcolor = tex2D(PaletteSampler, float2(color.r + gOffset, 0));

	Out.Color = float4(color.r + gOffset, color.g + gOffset, color.b + gOffset, color.a);
};

technique ColorCycle
{
    pass P0
    {
        VertexShader = NULL;
        PixelShader  = compile ps_1_1 CycleTextureColor();
    }
}
Advertisement
Have you tried the effect in FX Composer and see what kind of error it may be generating? Do you know the error you are getting? Does it work in FX Composer?

What is the shader code when you are "trying to access palcolor"? Is it different then what you have listed there?
Enoch DagorLead DeveloperDark Sky EntertainmentBeyond Protocol
I'm quite unfamiliar with HLSL so this may be way off the mark.

How is TextureSampler setup?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:Original post by Buckeye
I'm quite unfamiliar with HLSL so this may be way off the mark.

How is TextureSampler setup?


From the looks of it, it's not explicitly bound to any sampler or texture. Which means there's no good way of setting the texture that sampler uses.
I have no idea why but TextureSampler is automatically bound to the texture in stage 0. That is how it was from the "InverseColor.fx" my code is based on.

The code is kinda working; all the colors in the texture set on stage 0 turns to white as you would expect from the code.

But if I change the code like to this, it bombs out. Notice all I do is swap color to palcolor so it will use the colors from PaletteTex.

void CycleTextureColor(in v2p In, out p2f Out){	float4 color = tex2D(TextureSampler, In.TexCoord);	float4 palcolor = tex2D(PaletteSampler, float2(color.r + gOffset, 0));	Out.Color = float4(palcolor.r + gOffset, palcolor.g + gOffset, palcolor.b + gOffset, color.a);}


I hope I don't have to install FX Composer for something so simple. Is there no obvious error in the code?
Quote:as you would expect from the code

Isn't there a possibility that, without TextureSampler bound, you'd get the same result?

You might try setting the sampler_state of TextureSampler, assigning the texture and seeing what happens. Doesn't seem like it would take but a few minutes to do.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Okay tried binding TextureSampler manually to my texture (EffectTex) and I get the same results. As soon as I try to use "palcolor" it fails to compile the code. Unfortunately the only error I get is a C++ runtime error on my Effect.FromFile() line.

texture EffectTex;texture PaletteTex;float gOffset;sampler2D TextureSampler : TEXUNIT0 = sampler_state{	Texture = (EffectTex);};sampler2D PaletteSampler : TEXUNIT1 = sampler_state{	Texture = (PaletteTex);};
Quote:the only error I get is a C++ runtime error on my Effect.FromFile() line

What's the error?

By the way, I tried your file with no problem as far as:
	strcat(strcpy( path, resPath ),"test.fx");	SAFE_RELEASE(pEffectProx);	D3DXCreateEffectFromFile(m_pDevice, path, 0, 0,                		D3DXSHADER_DEBUG, 0, &pEffectProx, &errorBuffer);    	if( errorBuffer )	{		MessageBox(NULL,(char*)errorBuffer->GetBufferPointer(),"pEffectProx creation error.",MB_OK);		SAFE_RELEASE(errorBuffer);		SAFE_RELEASE(pEffectProx);		return E_FAIL;	}	else	{		HProxTechnique = pEffectProx->GetTechniqueByName("ColorCycle");		hr = pEffectProx->ValidateTechnique(HProxTechnique);                // breakpoint. did not test beyond here. hr==S_OK


Assuming your file now looks like:
texture EffectTex;texture PaletteTex;float gOffset;sampler2D TextureSampler : TEXUNIT0 = sampler_state{	Texture = (EffectTex);};sampler2D PaletteSampler : TEXUNIT0 = sampler_state{    Texture = (PaletteTex);};//vertex to pixel shader structurestruct v2p{    float2 TexCoord : TEXCOORD0;};//pixel shader to screenstruct p2f{    float4 Color : COLOR0;};void CycleTextureColor(in v2p In, out p2f Out){	float4 color = tex2D(TextureSampler, In.TexCoord);	float4 palcolor = tex2D(PaletteSampler, float2(color.r + gOffset, 0));	Out.Color = float4(color.r + gOffset, color.g + gOffset, color.b + gOffset, color.a);};technique ColorCycle{    pass P0    {        VertexShader = NULL;        PixelShader  = compile ps_1_1 CycleTextureColor();    }}

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Buckeye: Appreciate your feedback. Like I said the code does work, the problem is when you try to change:

Out.Color = float4(color.r + gOffset, color.g + gOffset, color.b + gOffset, color.a);


to

Out.Color = float4(color.r + palcolor.r, color.g + palcolor.g, color.b + palcolor.b, color.a);


Okay I found out how to get the error, here it is:

Quote:error X4523: cannot map this dependent texture read to ps_1_1
C:\Temp\Effects\ColorCycle.fx(36): ID3DXEffectCompiler::CompileEffect: There was an error compiling expression ID3DXEffectCompiler: Compilation failed
Well, at least things are consistent. I just tried the palcolor change (apparently while you were posting) and got.. if you can believe it.. exactly the same error.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement