Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, 7th_zorro, Ayumi), 749 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Tile-ing Textures #399546
04/17/12 00:23
04/17/12 00:23
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
I started a thread in the "starting with gamestudio" section, but since I am going the shader route I decided it would be best to continue here. Some backround on what I am trying to achieve with this shader:

Originally Posted By: Carlos3DGS
Hard to describe this clearly in the title, so let me try get the idea across in more detailed explanation:
I have lots of square cube blocks in my game, the player can place and scale the height of these blocks, kind of like a simplified minecraft. These blocks all have tileable textures so they look contiguos side by side.
To build a castle-looking structure you could try to achieve it by scaleing a block to a height of x12, the block beside to height x10, and continue altering heights of your blocks between x12 and x10 till you have built the complete structure of the castle out of different height blocks.

My problem is this: When scaling the height of a block, the texture on the sides (walls) are streched, so the tileable textures on the sides (wall) no longer match the wall textures of the blocks beside it.

My question is, to avoid this "stretching" problem on the textures... Is there any way to tile the texture on the objects depending on it's size instead of stretching it?


I have started to think on how to achieve this and so far I have this pseudo-code planning out the shader logic:

Code:
//THIS FIRST PART WORKS FOR HORIZONTAL PLANES (TOP OF BLOCK)
float3 object_world_pos = my object's origin;
float3 pixel_local_pos = position relative to object's origin;

float3 pixel_world_pos = object_world_pos + pixellocal__pos;
float2 texture_pos = (pixel_world_pos.x, pixel_world_pos.y);//texcoord

//THIS PART ALSO TAKES INTO ACCOUNT VERTICAL WALLS (SIDES OF THE CUBES)
float3 normal = pixel surface's normal;
texture_pos.x += pixel_world_pos.z * normal.x;
texture_pos.y += pixel_world_pos.z * normal.y;

float4 pixel = texture_sampler(texture_pos);//wrap?



So how does the logic look so far? Am I heading in the right direction?
Any help would be great.


"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: Tile-ing Textures [Re: Carlos3DGS] #399569
04/17/12 11:15
04/17/12 11:15
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hello,

You can multiply the texture coords by the scale factor into the shader, and I think that is all what you need.

tiling.fx
Code:
float4x4 matWorld;
float4x4 matWorldViewProj;
float4 vecSkill41;

texture entSkin1;

sampler TexSampler = sampler_state
{
	Texture = <entSkin1>;
	MipFilter = Linear;
	AddressU = Wrap;
	Addressv = Wrap;
};

struct out_tiling
{
	float4 Pos	: POSITION;
	float2 Tex0	: TEXCOORD0;
};

out_tiling vs_20
(
	in float4 inPos	        : POSITION,
	in float2 inTex0	: TEXCOORD0
)
{
	out_tiling Out;
	
	Out.Pos = mul(inPos,matWorldViewProj);
	Out.Tex0 = inTex0;

	return Out;
}
	
float4 ps_20 ( out_tiling In ): COLOR
{
	float2 Coord;
	Coord.x = In.Tex0.x * vecSkill41.x;
	Coord.y = In.Tex0.y * vecSkill41.y;
	
	return tex2D ( TexSampler, Coord );
}

technique TexTile
{
	pass one
	{
		VertexShader = compile vs_2_0 vs_20();
		PixelShader = compile ps_2_0 ps_20();			
	}		
}



main.c
Code:
#include <acknex.h>
#include <default.c>

MATERIAL *mtl_tiling =
{
	effect = "tiling.fx";
}

function main ()
{
	wait (2);
	level_load ( "" );
	wait(3);
	
	camera.x = -500;
	
	you = ent_create ( "cube.mdl", nullvector, NULL );
	you.material = mtl_tiling;
	
	you.skill41 = floatv(2);
	you.skill42 = floatv(2);
	
	while ( !key_esc )
	{
		
		wait(1);
	}
	
	sys_exit ( NULL );
}



Salud!

Re: Tile-ing Textures [Re: txesmi] #399578
04/17/12 12:22
04/17/12 12:22
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
you did it for me, wow, thankyou very much!!! That's a much better approach! One more thing though, if anyone has the time...
What if I have a few BMAP* declared in my script (for the different materials the blocks can be made from)? I know how to import them for the shader to use, and how to select a different BMAP to sample depending on a skill. The problem is I do it with "if" statements, and I was told if statements are slow and sloppy for shaders. How could it be done withought if statements? (I can't seem to come up with the evil formula required to do this withought using "if" statements in my shader)
If anyone could provide an example for, let's say, only 3 BMAPS I could try figure out how to adapt the formula for it to work with the ton of materials I will use, thanks.


"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: Tile-ing Textures [Re: Carlos3DGS] #399584
04/17/12 13:55
04/17/12 13:55
Joined: Dec 2000
Posts: 4,608
mk_1 Offline

Expert
mk_1  Offline

Expert

Joined: Dec 2000
Posts: 4,608
simply blend them depending on skills


Follow me on twitter
Re: Tile-ing Textures [Re: mk_1] #399603
04/17/12 16:18
04/17/12 16:18
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
The thing is, I was told the "if" are bad in shaders because it pre-calcualates each branch and then applies the if-gate logic (or something like that if I understood it correctly).

So if I have, lets say, 50 materials (BMAPS & texture_sampler calls) nested in "ifs" it will actually do 50 texture samples and then apply the if logic to see what brach it goes to? (I repeat, thats only if I understood it correctly)

But if I just blend them depending on the skill value, that will still be 50 texture samples + 50 extra lerps/blends. So I'm not sure that will be faster or slower. There has got to be some better, faster, or more effiencient way to do this? Some evil formula that eludes me, wierd saturate trick thing or... I don't know... I'm lost... Need some advice on this.


"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: Tile-ing Textures [Re: Carlos3DGS] #399604
04/17/12 16:37
04/17/12 16:37
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Why would you want to create a shader that supports 50 textures?
Simply create a few more shaders and use entity skins.


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: Tile-ing Textures [Re: Superku] #399612
04/17/12 17:53
04/17/12 17:53
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
I had a nailed thorn and needed take it off.

I found a better way, but the box has to be perfectly aligned to the system planes and all the cube faces has to be detached from each other, otherwise it will not work.

Save box scales into x->skill41, y->skill42 and z->skill43

tiled.fx
Code:
float4x4 matWorld;
float4x4 matWorldViewProj;
float4 vecSkill41;

texture entSkin1;

sampler TexSampler = sampler_state
{
	Texture = <entSkin1>;
	MipFilter = Linear;
	AddressU = Wrap;
	Addressv = Wrap;
};

struct out_tiling
{
	float4 Pos		: POSITION;
	float2 Tex0		: TEXCOORD0;
};

out_tiling vs_20
(
	in float4 inPos	 : POSITION,
	in float3 inNormal : NORMAL,
	in float2 inTex0	 : TEXCOORD0
)
{
	out_tiling Out;
	
	Out.Pos = mul(inPos,matWorldViewProj);
	Out.Tex0.x = inTex0.x * ( (vecSkill41.y*inNormal.x) + (vecSkill41.x*inNormal.y) + (vecSkill41.x*inNormal.z) );
	Out.Tex0.y = inTex0.y * ( (vecSkill41.z*inNormal.x) + (vecSkill41.y*inNormal.y) + (vecSkill41.z*inNormal.z) );
	return Out;
}
	
float4 ps_20 ( out_tiling In ): COLOR
{
	return tex2D ( TexSampler, In.Tex0 );
}

technique TexTile
{
	pass one
	{
		VertexShader = compile vs_2_0 vs_20();
		PixelShader = compile ps_2_0 ps_20();			
	}		
}



Salud!

Re: Tile-ing Textures [Re: txesmi] #399640
04/17/12 22:38
04/17/12 22:38
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
What do you mean by all the faces must be detached? Instead of a cube I have to make it out of 6 separate faces (but still all in the same model)? Or six separate entities/models?

Also... Why must they be detached?
Can you explain how the second shader works? I don't quite understand the logic behind it, but I will examine it in more detail when I get home to try to understand it.


"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: Tile-ing Textures [Re: Carlos3DGS] #399663
04/18/12 08:37
04/18/12 08:37
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Originally Posted By: Carlos3DGS
Instead of a cube I have to make it out of 6 separate faces (but still all in the same model)?

That is it. It has to be this way because the normals of the vertex of a merged cube are not perpendicular to the faces that they are part of, instead, every vertex has a normal that is an average of the normals of the three faces that it is part of. To make this shader work, every face has to be independent and share no vertex with the other faces.

Originally Posted By: Carlos3DGS

Can you explain how the second shader works?

Of course. The normal of a plane aligned to any of the orthogonal planes (XY, XZ or YZ) has one of its coords fullfilled (1 or -1) and the other two are empty (0).
XY: 0,0,1 - XZ: 0,1,0 - YZ: 1,0,0
The shader computes the texture coords with this values, so only multiplies one of those three elements of the summation to each coordinate. That is why it does not work with not aligned faces.

Hope I explained enough and sorry for my english xP

Salud!

Re: Tile-ing Textures [Re: txesmi] #399723
04/18/12 20:58
04/18/12 20:58
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
Don't appologize for your english, it is quite good. By the way I checked the links in your signature and found they lead to spanish Lite-C sites. Are you spanish? If so, what a coincidence! I am spanish too!

Back on topic: After revising your code again carefully with your explanation I think I understand it now. Thanks!

I am now in the process of adapting it, so it can tile textures withought caring about scale or if the blocks are snapped on a grid (or abrirtarily sized med models withought having to calculate the relative size compared with a "standard" block). So there can be half-width blocks or whatever and still continue to be perfectly tiled (or even bigger more complex structures withought having to calculate what the corresponding scale is. For this i am trying to take into account normals like you do, and taking into account global position instead of modifying local UV coordinates.

It helps me alot to have your shader to work on, It is generally hard for me to get the structure of a shader right from scratch but am pretty good at modifying one.



This is an example of what I am trying to achieve, thats why I am trying to modify your code to use world coordinates instead of local UV coordinates+scale.

This is what I have so far:
Code:
float4x4 matWorld;
float4x4 matWorldViewProj;
float4 vecSkill41;

texture entSkin1;

sampler TexSampler = sampler_state
{
	Texture = <entSkin1>;
	MipFilter = Linear;
	AddressU = Wrap;
	Addressv = Wrap;
};

struct out_tiling
{
	float4 Pos    : POSITION;
	float2 Tex0   : TEXCOORD0;
};

out_tiling vs_20
(
	in float4 inPos	 : POSITION,
	in float3 inNormal : NORMAL,
	in float2 inTex0	 : TEXCOORD0
)
{
	out_tiling Out;
	
	Out.Pos = mul(inPos,matWorldViewProj);
		
	Out.Tex0.x = Out.Pos.x + (Out.Pos.y*inNormal.z);
	Out.Tex0.y = Out.Pos.z + (Out.Pos.y*inNormal.x);
	return Out;
}
	
float4 ps_20 ( out_tiling In ): COLOR
{
	return tex2D ( TexSampler, In.Tex0 );
}

technique TexTile
{
	pass one
	{
		VertexShader = compile vs_2_0 vs_20();
		PixelShader = compile ps_2_0 ps_20();			
	}		
}



One thing... If I remeber correctly, in shaders y and z axis are swappped, right? (It's been a long time since I played around with shaders)


"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Page 1 of 2 1 2

Moderated by  Blink, Hummel, Superku 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1