How do I apply Texture over a Flat Mesh Ive Created?

Started by Hardcoal, May 12, 2020, 23:12:32

Previous topic - Next topic

Hardcoal

Hi. Ive created a flat mesh using triangles..
I dont understand much about UV mapping..
All I want is that the texture will appear on the 2D Mesh.. (Like a Sprite)

When  I Say a 2D mesh I mean a Shape with one surface that has one side visible.. like a polygon..

Is there a code that applies autmatically texture over?

When i try doing it, it either doesnt appear or get looking messed..

I need some process that auto sort the UV Map of a Flat Mesh
Code

MrmediamanX

#1
depending on what your using, the easiest method is to open the [quad mesh] in a modeling tool/create a material from the texture and apply it to the quad mesh.
method 2 is load in and create a texture by coding it in as an asset [ loadtexture("texturename.png") ] and apply it to the quad mesh by way coding also [EntityTexture,quadmesh,texturename]
If the programming tool in question is capable of texture scale you may need to use it.
this is how I do most of my sprites or water/cloud planes. :)
The syntax may differ regarding code but the methodology should remain the same.
It's a thing that doe's when it don't..

Santiago

hello, sometimes UV and W can be confusing, but it is very useful when you learn to use it.

I always solved it, drawing the vertices on a piece of paper and seeing how coordinated they should be.
In Blitz, you can make it show you a text with the number of each vertex if you don't know which is which.
Even so, I always ended up doing it with the trial and error method.



This function, i make a plane, and i put over the screen, using the screenwindows corners, for UI and effects

here you can se how i apply the UV coords to my 4 vertexs

; WINDOWS SYSTEM

Global windows
Global w_texture
Global w_t_tam
Global w_mx
Global w_my
Global w_pick_pivot
Global w_dist
Global w_relacion#
Global w_ancho#
Global w_alto#
Global w_margen
Global w_tam
Global w_cam_zoom#
Global w_pointer_pivot

Function update_windows(tam,force_fix_vertex_position,zoom_ajust)

;Esta funcion Crea y actualiza la ventana mesh,
;pero la textura la manipula cada funcion independientemente de esta funcion.

;El plano esta sobre el 0,0,0
;la camara se aleja del plano siempre
;el plano debe estar como el MAR

w_dist = 5
w_tam = tam
w_margen = 5


Local plano ;objeto temporal

If windows = 0 Then
If ground <> 0 Then EntityPickMode ground,0

DebugLog "PLANO WINDOWS creado!"
PositionEntity cam,0,5,0
plano = CreatePlane()
PointEntity cam,plano
EntityColor plano,50,50,50
EntityPickMode plano,2
EntityAlpha plano,1
CameraZoom cam,1

PointEntity cam,plano



windows = CreateMesh()
EntityFX windows,1
surf = CreateSurface(windows)

pick  = CameraPick(cam,0,0)
v0 = AddVertex (surf, PickedX(),PickedY(),PickedZ(), 0 ,0)
DebugLog "V0 x: " + PickedX()
DebugLog "V0 y: " + PickedY()
DebugLog "V0 z: " + PickedZ()

pick  = CameraPick(cam,gancho,0)
v1 = AddVertex (surf, PickedX(),PickedY(),PickedZ(), 1 ,0)
DebugLog "V1 x: " + PickedX()
DebugLog "V1 y: " + PickedY()
DebugLog "V1 z: " + PickedZ()

pick  = CameraPick(cam,0,galto*w_relacion#)
v2 = AddVertex (surf, PickedX(),PickedY(),PickedZ(), 0,1)
DebugLog "V2 x: " + PickedX()
DebugLog "V2 y: " + PickedY()
DebugLog "V2 z: " + PickedZ()

pick  = CameraPick(cam,gancho,galto*w_relacion#)
v3 = AddVertex (surf, PickedX(),PickedY(),PickedZ(), 1,1)
DebugLog "V3 x: " + PickedX()
DebugLog "V3 y: " + PickedY()
DebugLog "V3 z: " + PickedZ()


tri = AddTriangle (surf,v0,v1,v3)
tri = AddTriangle (surf,v0,v3,v2)
FreeEntity plano



w_texture = CreateTexture(w_tam,w_tam,256)
EntityOrder windows,-800
EntityBlend windows,1
EntityFX windows,1
w_pick_pivot = CreatePivot()

End If



;this metohd must be improve for speed
;reposition the plane to fit with camera without care the zoom.
If force_fix_vertex_position = 1 Then
plano = CreatePlane()
EntityPickMode plano,2

surf = GetSurface(windows,1)
pick  = CameraPick(cam,0,0)
VertexCoords surf,1,PickedX(),PickedY(),PickedZ()

pick  = CameraPick(cam,gancho,0)
VertexCoords surf,2,PickedX(),PickedY(),PickedZ()

pick  = CameraPick(cam,0,galto*relacion#)
VertexCoords surf,3,PickedX(),PickedY(),PickedZ()

pick  = CameraPick(cam,gancho,galto*relacion#)
VertexCoords surf,4,PickedX(),PickedY(),PickedZ()
FreeEntity plano
End If

If zoom_ajust = 1 Then ScaleEntity windows,  Float(1/cam_zoom),Float(1/cam_zoom),1/cam_zoom


;Define donde esta el pick del mouse sobre el plano del mundo
w_mx = mx/(Float(Float(gancho)/Float(w_ancho)))
w_my = my/(Float(Float(galto)/Float(w_alto)))
plano = CreatePlane()
EntityPickMode plano,2
pick = CameraPick(cam,mx,my)
FreeEntity plano
PositionEntity w_pick_pivot,PickedX(),PickedY(),PickedZ()


End Function


but im shure there is easy ways or best examples..

Hardcoal

Thanks Santiago for your Kind effort. But I still not fully understand this UV thing..
I decided to add my mesh and its texture, and see if someone can fit it together

this is an image of the results i want to achieve

thanks..





Code

Steve Elliott

#4
When I was using Nuclear BASIC/Fusion I used an Unwrap Program to break the model into flat 2D shapes, put my textures(sprites) over each shape and saved in B3D Format (there are lots of other formats).  This one here:  https://www.unwrap3d.com/u3d/index.aspx
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

RemiD

easiest method is to do an unwrap in a modeling software. (fragmotion!) you can do a plane projection and then manually position the vertices with texel precision...

but if you want to use only flat meshes you can also model the mesh and manually UVmap each vertex, after having drawn the texture, i also use this approach when modeling low poly meshes.

first you draw your shape on a texture, in a way that it will be made of triangles, then since you know the texel size you want in your 3dworld, you can model the flat mesh using this scale (for example 1texel corresponds to 0.1unit), and since you know the vertex position on the 2d texture,  you can easily calculate the U,V coordinate...

U,V coordinate is a number between 0 and 1.0 which allows you to calculate on which texel a vertex will be put (or rather on which vertex a texel will be put), and this is how a texture is applied on a triangle, and this is why when U,V are not set as wanted, the texture will be stretched or inverted or some texel distorted...

for example :
your texture is 128w128h
topleft is 0.0U,0.0V
topright is 1.0U,0.0V
bottomleft is 0U,1.0V
bottomright is 1.0U,1.0V

you draw a 2Dtriangle on your texture, so that V0 (50x,25y) | V1 (20x,80y) | V2 (90x,95y)
then you can calculate the U,V of each vertex in this way :
V0 (50.0/128,25.0/128) | V1 (20.0/128,80.0/128) | V2 (90.0/128,95.0/128)

useful commands for this are :
createmesh()
addsurface()
addvertex()
addtriangle()
vertextexcoords()

loadtexture()
entitytexture()
or
createbrush()
brushtexture()
paintsurface()

(also one thing to keep in mind is that a triangle must be created with vertices in a clockwise order, this is what will determine is visible face)

MrmediamanX

#6
It may be easier to simply use a quad akin to the image below. less poly's and all that jazz. :)
Nodout loading the texture in as a sprite may work as well assuming your using a fixed camera view with no horizontal or vertical rotation.
It's a thing that doe's when it don't..