Okay so I’ve been looking at meshes at a beginner level of course and written the following which works fine
-- Basemesh
function setup()
displayMode(FULLSCREEN)
l=5 --alter this to change resolution of mesh can handle upto about 1000+ before crashing
meshdata={}
basemesh=mesh()
for i=1,l do
for y=1,l do
posx=(i*HEIGHT/l)-HEIGHT/(2*l)
posy=(y*HEIGHT/l)-HEIGHT/(2*l)
basemesh:addRect(posx,posy,HEIGHT/l,HEIGHT/l)
end
end
for z=1,l^2 do
colconv=z*255/l^2
basemesh:setRectColor(z,255-colconv,colconv,255-colconv) --remove the 255-s for grey scale
end
end
function draw()
basemesh:draw()
end
So obviously it produces a square mesh of composite squares and you could if you wish set the dimensions using l and with l=height then in landscape mode you have a square which fits on the screen and a mesh which in theory should be composed of pixel sized rectangular meshes. Each square module has been individually coloured with a slightly different colour using a loop.
Okay so the aim of this is to plug this into a tiling program…I wrote some code for the tiling program and it worked well but an alternative much more elegant code was suggested by @dave1707 so I’ve embedded the above into that (anyone who has had the misfortune of trying to help me previously may have uploaded 512 images into their documents folder -apologies again)…the following code which includes the embedded code above does not do this but stores them in a table.
function setup()
displayMode(FULLSCREEN)
l=10 --alter this to change resolution of mesh
meshdata={} --create meshdata as table
basemesh=mesh() --create basemesh as a mesh
for i=1,l do
meshdata[i]={}
for y=1,l do --double iteration which sets up
meshdata[i][y]=0 --meshdata as a 2 way table
posx=(i*HEIGHT/l)-HEIGHT/(2*l) --generates a central x coord
posy=(y*HEIGHT/l)-HEIGHT/(2*l) --and a central y coord
--for a grid of square mesh pieces
idx = basemesh:addRect(posx,posy,HEIGHT/l,HEIGHT/l)
--inserts all rectangle meshes into basemesh and returns id
posx=math.ceil(posx/HEIGHT*l) --these two lines convert the x,y coordinate into a horizontal
posy=math.ceil(posy/HEIGHT*l) --and vertical address for each mesh tile, which along with
meshdata[posx][posy]=idx --its idx is written into the table meshdata
-- print(posx, posy, idx) --this is a check that the meshdata table has been correctly made
end
end
for z=1,l^2 do --this loop sets up colours for each mesh piece in the basemesh
colconv=z*255/l^2
basemesh:setRectColor(z,colconv,colconv,colconv)
end
tab={} --the following generates 512 tiles based on permutations
rectMode(CORNER) --of a binary coded 3x3 matrix....
spriteMode(CORNER)
base=readImage("Documents:newtile")
shady=readImage("Documents:newshade")
dy=0
z=0
end
function draw()
background(40, 40, 50)
fill(255)
if z<512 then
tab[z]=image(123,123)
setContext(tab[z])
sprite(base,0,0,123,123)
bin(z)
setContext()
text("creating image "..z,WIDTH/2,HEIGHT/2)
z=z+1
else --here all tiles have now been generated and stored
--using the bin function below
text("Slide to change",WIDTH/2+400,HEIGHT/2+200)
yy=math.floor(dy/10)
text("image "..yy,WIDTH/2+400,HEIGHT/2+100)
sprite(tab[yy],WIDTH/2+340,HEIGHT/2-61)
basemesh:draw() --here the base mesh is being drawn
end
end
function bin(z)
x,y=0,-1
for a=8,0,-1 do
y=y+1
if y>2 then
y=0
x=x+1
end
if z>=2^a then
z=z-2^a
sprite(shady,y*41,x*41,41,41)
end
end
end
function touched(t)
if t.x>=HEIGHT and t.state==MOVING then --this detects swiping in the r.h.s of the screen
dy=dy+t.deltaY
if dy>#tab*10 then
dy=#tab*10
elseif dy<0 then
dy=0
end
end
if t.x<=HEIGHT and t.tapCount==2 then do --this detects a double tap on the mesh
tileselect(t)
end
end
end
function tileselect(t)
xsel=math.ceil(t.x/HEIGHT*l) --these two lines convert the touch coordinates
ysel=math.ceil(t.y/HEIGHT*l) --into the horizontal and vertical address system
--tabulated in meshdata so that I can match the mesh piece
--selected with its identity with the aim of using the selected
--tile image on the r.h.s. as a texture map to the selected mesh
--section
basemesh.texture = tab[yy] --this selects the current generated tile as a texture map for
--the selected mesh
-- print(meshdata[xsel][ysel]) --this checks that the touch has been converted correctly
basemesh:setRectTex(meshdata[xsel][ysel],0,0,1,1)
end
--the penultimate line is supposed to map the texture to the specific tile selected but all tiles are affected...Help!
Much gratitude for any assistance or pointers. My naive thinking was that since I can individually colour each mesh section, then I should be able to individually specify a texture map to each mesh section, and indeed my interpretation of the literature seemed to suggest that one could…I changed the 0,0,1,1 into 0,0,1/l,1/l as an experiment which had a bizarre effect but did start affecting things individually…but…hmmm. Any thought gratefully accepted.