Shaders

And what happens when you do this? I mean this will render single texture and you are actually overwriting texture you set as paramater with texture that is assigned by spriteBatch on draw call when flushing this:

spriteBatch.Draw(texture, new Vector2(0, 0), Color.White);

thus this will simply render “texture”.

What result are you getting?

and also header should look like this:

float4 PixelShaderFunction(float4 position : SV_Position, float4 color : COLOR0, float2 coords : TEXCOORD0) : COLOR0

error unexpected token TEXCOORD0

Umm, just double check it… that´s definitely aint unexpected token, it should be on single line, obviously.

my bad, heres what i’m trying to do

I have this

want to put this

in it as an overlay
reason being i intend to manipulate the second texture via a color table to produce scrolling color bars al la C64

Define overlay…
also again, what results are you getting atm? What´s the issue, do you have issue to render underlay texture? Or that part is working fine?

i can print the first sprite easy, i want the texture inside the letters to = the rainbow texture
all it’s printing right now is the original image

texture rainbowpic;
sampler Sampler0 : register(s0)
{

};
sampler Sampler1  : register(s1)
{
  Texture = (rainbowpic);
};

float4 PixelShaderFunction(float4 position : SV_Position, float4 color : COLOR0, float2 coords : TEXCOORD0) : COLOR0
{
  float4 color = tex2D(Sampler0, coords);
  float4 rainbow_color = tex2D(Sampler1 , coords);
  color = lerp(color, rainbow_color, 0.5f)

  return color;
}
technique Technique1
{
  pass Pass1
  {
    PixelShader = compile ps_3_0 PixelShaderFunction();
  }
}

And again, you can do this, this will blend color between textures with 0.5f bias (half - half), if you want to pan one texture it is easy enough but we need to make sure we are on same page and everything else is working, results are as expected at that stage and everyone understand code so far.

wanted to replace the pixels in the text with pixels from the second texture

good but that doesnt answer important question: Where are we right now, are you having trouble rendering single texture? If you want to replace based on alpha, so for instance you want to use second texture if alpha of first texture is 0.2 or lower then:

texture rainbowpic;
sampler Sampler0 : register(s0)
{

};
sampler Sampler1  : register(s1)
{
  Texture = (rainbowpic);
};

float4 PixelShaderFunction(float4 position : SV_Position, float4 color : COLOR0, float2 coords : TEXCOORD0) : COLOR0
{
  float4 color = tex2D(Sampler0, coords);
  float4 rainbow_color = tex2D(Sampler1 , coords);
  if(color.a < 0.2)
  {
  color = rainbow_color;
  }
  
  //or if based on second texture which is probably what you mean by overlay
  //if(rainbow_color.a > 0.0)
  //{
  //color = rainbow_color;
  //}

  return color;
}
technique Technique1
{
  pass Pass1
  {
    PixelShader = compile ps_3_0 PixelShaderFunction();
  }
}

single texture

displayed no problem
want the second text displayed in the first basically switch texture A with texture B and keep the lettering

ok got it sorted

texture text;
texture rainbowpic;
sampler Sampler0 : register(s0)
{
  Texture = (text);
};
sampler Sampler1  : register(s1)
{
  Texture = (rainbowpic);
};

float4 PixelShaderFunction (float4 position : SV_Position, float4 color : COLOR0, float2 coords : TEXCOORD0) : COLOR0
{
  color = tex2D (Sampler0, coords);
  float4 rainbow_color = tex2D (Sampler1 , coords);
  if (color.a )
  {
    color = rainbow_color;
  }

  //or if based on second texture which is probably what you mean by overlay
  //if(rainbow_color.a > 0.0)
  //{
  //color = rainbow_color;
  //}

  return color;
}

produces

Thank you SOOOOOOOOOOOO much you da man and todays hero

So what you wanted to do was to use alpha of one texture as alpha of another, basically you wanted to mask second texture. Just for future reference.

Your code is very poorly optimized for such a simple task, I would like to recommend:

float4 PixelShaderFunction (float4 position : SV_Position, float4 color : COLOR0, float2 coords : TEXCOORD0) : COLOR0
{
  //sample alpha from first texture
  float mask = tex2D (Sampler0, coords).a;
  //sample rgb and alpha from second
  float4 rainbow_color = tex2D (Sampler1 , coords);
  //multiply alpha channels, only pixels that are visible on both should remain visible AND operation
  rainbow_color.a *= mask;
  //render
  return rainbow_color;
}

This would also preserve antialiasing from original text if there would be any.

no what i wanted to do was use the alpha of one texture as a mask for the other texture, basically this…

if pixel from tex1 visible then use entire pixel from tex2

tex2 is basically going to be a background of anything i see fit to put on it, scroller, starfield, color bars, anything, probably moving in someway without a ton of graphic animation, i just needed to be able to use a shader to do it

Many thanks, once done i’ll upload a you-tube so you can see

I know… just try the code I posted in my previous comment, you will see…

yours gives me this

I need this

In addition, using shaders like this allows me to have animation within animation, a bouncing ball with a fully random starfield or scrolling banner etc
the only reason i need the original alpha is to “cookie cut” another texture that i could mess with
Thanks

You probably meant ‘declare’ when you said ‘use’.
This has come up before… I think it’s a MojoShader bug, or maybe it’s a bug in how 2mgfx interprets MojoShader reflection data. Either way, it’s a bug.

Then there is something wrong with alpha of your texture for text, well, doesnt matter.

this is what i was wanting to do, can also do starfields, scrolling backgrounds etc

and this it the texture being used by the shader

Yes, this fixed my problem getting the tutorials to work. Thanks!