matTexture not getting calculated

Posted By: Error014

matTexture not getting calculated - 08/04/12 20:55

Hello!

There's a strange bug concerning matTexture - it seems that it's not getting calculated. DoTexture (nor, of course, the direct multiplication of the vector with matTexture) does nothing.

Here is a rar that has a simple test model and code ready for testing: link.

The important code is:

Code:
MATERIAL* uvcheck = {
	effect =
	"
		const float4x4 matWorldViewProj;
		float4x4 matTexture;
		
		texture entSkin1;

		sampler sBaseTex = sampler_state { 
			Texture = <entSkin1>;
			AddressU = Wrap;
			Addressv = Wrap;
		};

		void vs_uvshift( 
		   in float4 InPos: POSITION, 
		   in float2 InTex: TEXCOORD0, 
		   out float4 OutPos: POSITION, 
		   out float2 OutTex: TEXCOORD0) 
		{ 
		   OutPos = mul(InPos, matWorldViewProj); 
		   OutTex = mul(float4(InTex.x,InTex.y,1,1),matTexture).xy;
		} 
	
		float4 ps_uvshift(in float2 InTex: TEXCOORD0): COLOR
		{
			return tex2D(sBaseTex,InTex);
		}	
	
		technique uvtest {
			pass p0 {
				VertexShader = compile vs_2_0 vs_uvshift();
				PixelShader = compile ps_2_0 ps_uvshift();					
			}	
		}
	";
}


function shiftuv() {
        my.material = uvcheck;
	while(1) {
		my.u += time_step;
		wait(1);
	}
}



It's worth pointing out that if you don't assign a new material to an entity, u and v work as expected (the right entity in the demo).

This has been tested in both 8.30.5, and, superku reports the same issue in the new 8.40 version in the original thread.


Additionally, it'd be great if the manual could be a bit more detailed about u,v and matTexture. Simply writing that matTexture is (what we assume, at least)

Code:
1 0 u 0
0 1 0 v
0 0 1 0
0 0 0 1



and then saying that DoTexture is a simple way to do texture shifting would be enough and much appreciated.

Thanks in advance!

Posted By: jcl

Re: matTexture not getting calculated - 08/06/12 08:21

If matTexture would "not be calculated", you would not see a texture, just a single pixel.

A texture matrix looks like this:

1 0 0 0
0 1 0 0
u v 1 0
0 0 0 1

I've added this to the manual.
Posted By: Error014

Re: matTexture not getting calculated - 08/06/12 09:52

Thanks for adding that to the manual! laugh


Have you tried the demo, though? While u is being changed in the entity's action, it doesn't give the expected texture shift. Which is the reason why I assumed that "it's not getting calculated", meaning it looks like matTexture is just the identity matrix.
Posted By: jcl

Re: matTexture not getting calculated - 08/06/12 11:22

Don't you see an u texture shift without your shader?
Posted By: Error014

Re: matTexture not getting calculated - 08/06/12 11:45

Yeah, there's texture shifting for that one - the right entity in the demo.


And while I normally would think that this suggests an error in the shader, I can't find any. laugh
Checking matTexture[0][2] or matTexture[2][0] directly also results in zeros - for that, I've simply returned

Code:
return float4(matTexture[0][2], matTexture[2][0], 0, 1);



in the Pixel shader. By changing u, we'd expect the thing to become more red or more green (I used both to make sure I don't accidentally use the wrong convention :)), but it's actually staying black.
Posted By: jcl

Re: matTexture not getting calculated - 08/06/12 12:30

The engine uses matTexture for uv shifting, so there's obviously nothing wrong with the matrix. Check the transformations in your shader. The _31 and _32 matrix coordinates determine the uv offsets. They must have a range between 0 and 1.
Posted By: Error014

Re: matTexture not getting calculated - 08/06/12 13:19

The transformation may very well be wrong, but so is it in DoTexture then.

This material - fundamentally the same - uses DoTexture, and thus hopefully eliminates any doubt that this is just a mistake on my end. laugh

Code:
#include <texture>

const float4x4 matWorldViewProj;

texture entSkin1;

sampler sBaseTex = sampler_state { 
	Texture = <entSkin1>;
	AddressU = Wrap;
	Addressv = Wrap;
};

void vs_uvshift( 
   in float4 InPos: POSITION, 
   in float2 InTex: TEXCOORD0, 
   out float4 OutPos: POSITION, 
   out float2 OutTex: TEXCOORD0) 
{ 
   OutPos = mul(InPos, matWorldViewProj); 
   OutTex = DoTexture(InTex);
} 

float4 ps_uvshift(in float2 InTex: TEXCOORD0): COLOR
{
	return tex2D(sBaseTex,InTex);
        //Feel free to use that line for a more direct test:
	//return float4(matTexture[0][2], matTexture[2][0], 0, 1);
}	

technique uvtest {
	pass p0 {
		VertexShader = compile vs_2_0 vs_uvshift();
		PixelShader = compile ps_2_0 ps_uvshift();					
	}	
}




Here's a picture:



And, once more, I refer to the demo that would probably make testing easier.


EDIT: In fairness, it might be that I'm missing something - do I need to do anything but change u,v outside the shader?


In the meantime, I'm using skill41 as a workaround. I still think it's wrong behaviour, though.
Posted By: jcl

Re: matTexture not getting calculated - 08/06/12 13:54

I see now your problem: you just copied your shader code from DoTexture. But DoTexture does not do a texture translation. It is a scaling function.

A texture has 2 coordinates, u and v. That's why matTexture has the translation coordinates in the 3rd row. So you need to get a 3x3 matrix from it and multiply it with a float3 extended texture coordinate vector (x,y,1). The result are the translated coordinates. Hope this helps. There is no shader library function for texture translation, but if you have problems, let me know and I'll write one.
Posted By: Error014

Re: matTexture not getting calculated - 08/06/12 15:12

That would be great, as I have to admit I'm a bit lost here; specifically on why the current version doesn't work.


As far as I understand, this line:

Code:
OutTex = mul(float4(InTex.x,InTex.y,1,1),matTexture).xy;



(which is just what DoTexture does)

would, together with this matrix:

Code:
1 0 0 0
0 1 0 0
u v 1 0
0 0 0 1



result in

(x+u, y+v, 1, 1)

so, using .xy of that should give us texture translation.

Scaling, however, doesn't seem to be something that could be done with that matrix structure, at least not with the vector DoTexture multiplies it with.

It also doesn't quite explain why the components [0][2]/[2][0] of matTexture are all zero ([3][1]/[1][3] are also zero, by the way)


My apologies - I probably come across as thick here. Your help is much appreciated!
Posted By: jcl

Re: matTexture not getting calculated - 08/07/12 12:04

I've changed the DoTexture function so that it now also supports translation. There were also several other small bugs in the recent version that were fixed. You can download the new version, 8.40.1, with the new DoTexture function from the announcements forum.
Posted By: Error014

Re: matTexture not getting calculated - 08/07/12 12:40

Everything works as expected now. Even the previously given lines now result in the wanted texture shift. Very nice laugh

Thank you!
Posted By: Hummel

Re: matTexture not getting calculated - 08/08/12 08:06

What kind of texture shift does the matrix produce you can not achieve with simple offsets?
Posted By: MasterQ32

Re: matTexture not getting calculated - 08/08/12 08:17

i think it's only about the u and v parameters of the entity
so the matrix translates the texture with those offsets and not with a material skill as the offset
© 2023 lite-C Forums