Tile texture on mesh?

Blitz3D Forums/Blitz3D Programming/Tile texture on mesh?

LamptonWorm(Posted 2008) [#1]
Hi,

Is it possible to set up a texture that when applied to multiple meshes of differrent sizes, the texture tiles? Perhaps not my best explanation ;)

Ok, we have a 64x64 texture with some bricks on. We have a mesh that is 64x64, which when textured with with the bricks looks great. If we make a different mesh of 128x64, and use the same texture, I'd like the new mesh to tile the texture so the texture isn't stretched, it is tiled.

Basically the idea is the have a single texture that can be used on meshes of differrent sizes, and the texture tiles as apposed to stretches.

I'm tired.. forgive me!

Cheers,
LW.


Gabriel(Posted 2008) [#2]
How a texture is applied to a mesh depends entirely on the UV coordinates of the mesh. So yes, of course, you can have the texture applied differently to different meshes. Just UV map the meshes accordingly in your modeller.


LamptonWorm(Posted 2008) [#3]
Thanks. Bit more awake now :)... I assume I can do the UV mapping direct in b3d? I'm creating a basic model builder and I want to create a block and have the user scale it how they like, with the texture tiling accordingly.. I think this is possible I just need to get familiar with the commands.

Cheers,
LW.


Ross C(Posted 2008) [#4]
You can do UV mapping in blitz via the VertexTexCoords() command.

If you are building your mesh in blitz, and assuming the tiles are square, getting the texture to not stretch is just a case of working out a ratio of height to width, and matching the texture co-ords to it, or scaling the texture accordingly.

For instance 64x128 would meaning you having to scale the texture 2 times along the x axis to 1 of the y axis, so:

ScaleTexture texture,2,1

would sort that problem out.

If you are building the tile mesh in blitz, you will have to set the texture co-ords of each vertex as you build it anyway. If not, don't worry about texturecoords and just use the scale texture.

However, if you use this texture on another mesh, it will be scaled, as scaletexture affects all meshes using this texture.


LamptonWorm(Posted 2008) [#5]
Thanks Ross C.

Last night I found that command but I wasn't using it correctly, just found the online version with more info - http://www.blitzbasic.com/b3ddocs/command.php?name=VertexTexCoords&ref=3d_cat - hopefully that'll get things moving nicely.

I guess I'll also need some way to pick the side of the shape so I adjust the right vertex, perhaps some 'select face' code. The plot thickens ;)

Cheers,
LW.


LamptonWorm(Posted 2008) [#6]
Ps. yes, I'm building the mesh in Blitz, and I want to re-use the same texture on different sizes meshes, so if I've got this right scaletexture won't be for me, I need to change the vertex texture coords on each mesh specifically.


Stevie G(Posted 2008) [#7]
I think he want to use the same texture on both meshes so Ross's method won't work for the reasons he explained.

Say you had a quad 64 x 64 with uv's set as 0,0 ; 1,0 ; 1,1 ; 0,1 from top left corner going clockwise. To make the same texture repeat x 2 on a 128 x 64 quad set the uv's as 0,0 ; 2,0 ; 2,1 ; 0,1.

You will also have to ensure that you don't set the clamp U and clamp V flags otherwise it won't work.

Make sense?


Ross C(Posted 2008) [#8]
Isn't there a segmented plane example in the code archives? I'm sure it lays out the UV co-ords as well, whilst letting you choose the segmentation.


LamptonWorm(Posted 2008) [#9]
Hi,

Just to confirm what I'm trying to do, I have 1 texture that I want to apply to multiple meshes. I want the texture to tile as needed when the meshes are scaled out. Scaling will be done in increments that snap to the texture size. Pretty much how CShop works, in terms of the texture scaling/tiling.

If I have a single box, and a texture applied to it and all is well, I want to then copy that mesh and scale the copy to twice the original size on x-axis. The texture should then be repeated twice, rather than stretched.

I'm trying to get to a point where I can draw/scale wall meshes and the texture auto tiles.

Cheers,
LW.


Stevie G(Posted 2008) [#10]
Sometimes I think I'm invisible here. Below is a function I knocked up for you and example usage ...

Graphics3D 640,480,16,1

;create texture
tex = CreateTexture( 64, 64 )
SetBuffer TextureBuffer( tex )
For y = 0 To 1
	For x = 0 To 1
		If ( x + y ) Mod 2 = 0
			Color 255,0,0
		Else
			Color 0,0,255
		EndIf
		Rect x*32,y*32,32,32,1
	Next
Next
SetBuffer BackBuffer()

;set up camera
camera = CreateCamera()
PositionEntity camera, 0,0,-10
pivot = CreatePivot()

;create normal cube
cube = CreateCube( pivot )
PositionEntity cube, 5,0,0
EntityTexture cube, tex

;create wide cube
widecube = CopyMesh( cube, pivot )
PositionEntity widecube, -5,0,0
ScaleMesh widecube, 3,1,1
MESHscaleUV( widecube, 1 , 3,1 )
EntityTexture widecube, tex

;create tall cube
tallcube = CopyMesh( cube, pivot )
PositionEntity tallcube, 0,0,5
ScaleMesh tallcube, 1,4,1
MESHscaleUV( tallcube, 2, 1, 4 )
EntityTexture tallcube, tex

;create long cube
longcube = CopyMesh( cube, pivot )
PositionEntity longcube, 0,0,-5
ScaleMesh longcube, 1,1,2
MESHscaleUV( longcube, 3, 2, 1 )
EntityTexture longcube, tex

;show
While Not KeyDown(1)

	TurnEntity pivot, 0,1,0
	RenderWorld()
	Flip

Wend

;========================================================================
;========================================================================
;========================================================================

Function MESHscaleUV( mesh , Axis , ScaleU#, ScaleV# )

	;axis = 1 = x
	;axis = 2 = y
	;axis = 3 = z
	
	s = GetSurface( mesh ,1 )
	
	For v = 0 To CountVertices( s ) - 1
	
		Select Axis
			Case 1
				CHANGE = ( VertexNY( s, v ) <> 0 Or VertexNZ( s, v ) <> 0 )
			Case 2
				CHANGE = ( VertexNX( s, v ) <> 0 Or VertexNZ( s, v ) <> 0 )
			Case 3
				CHANGE = ( VertexNX( s, v ) <> 0 Or VertexNY( s, v ) <> 0 )
		End Select

		If CHANGE VertexTexCoords s, v, VertexU( s, v ) * ScaleU , VertexV( s, v ) * ScaleV
	
	Next
	
End Function			



LamptonWorm(Posted 2008) [#11]
@Stevie G, thank you for taking time to help me out here - sorry you feel invisible. Can't wait to get back to my dev env later tonight to have a play with this. Appreciate your help, nice one.

Cheers,
LW.


Ross C(Posted 2008) [#12]
Stevie Wonder!

Sorry, that was poor :o)


Newbunkle(Posted 2008) [#13]
EntityAlpha Steve,1


LamptonWorm(Posted 2008) [#14]
lol ;)