Ooops
January 15, 2021, 05:45:34 PM

Author Topic: [bb] Seamless tile creator by Rob Farley [ 1+ years ago ]  (Read 403 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Seamless tile creator by Rob Farley [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Seamless tile creator
Author : Rob Farley
Posted : 1+ years ago

Description : This is not designed to take a texture and make it seamless.

It creates a texture from a complete image, ie, if you've got a picture of a lawn you load the lawn into this, pick the area you want to turn into a texture and it does the rest.

Once you've picked the area it will save out a BMP file called tile_[yourfilename].bmp

There's a couple of variables to monkey with in this code:
Filename$: The name of the image file you want to create your texture from.
Size: The size of the texture you want to create
QSize: The size of the overhang used to make the texture seamless
Noise: This makes the blend imperfect which can sometimes look better on some textures, 0 is no noise, 1 is 100% noise, you usually get best results around .1 or .2.

Please add to and improve this code!

A few bits need doing to improve this:
* contrast needs to increase on the blendy bits
* Front end
* Able to use images bigger than the screen


Code :
Code: BlitzBasic
  1. ; Seamless texture creator.
  2. ; 2005 MentalIllusion.
  3. ; http://www.mentalillusion.co.uk
  4. ;
  5. ;
  6. ; This is not designed to take a texture and make it seamless. It creates a texture from
  7. ; a complete image, ie, if you've got a picture of a lawn you load the lawn into this,
  8. ; pick the area you want to turn into a texture and it does the rest.
  9.  
  10. ; Once you've picked the area it will save out a BMP file called tile_[yourfilename].bmp
  11.  
  12.  
  13.  
  14.  
  15. ; Please add to and improve this code!
  16. ;
  17. ;
  18. ; A few bits need doing to improve this:
  19. ; * contrast needs to increase on the blendy bits
  20. ; * Front end
  21. ; * Able to use images bigger than the screen
  22.  
  23.  
  24.  
  25. Graphics 1024,768,32,1
  26.  
  27. font = LoadFont("Arial.ttf",20,True)
  28. SetFont font
  29.  
  30. filename$="[your image.jpg]"
  31.  
  32. i = LoadImage(filename)
  33.  
  34. size = 256
  35. hsize = size/2
  36.  
  37. ; set this to adjust the overhang
  38. qsize = size/4
  39.  
  40. ; set this to make it noiser!
  41. ; The noise variable makes the blends imperfect, however, it tends to make it look better
  42. ; have a play with this value, a value of 1 will just give a dither pattern which will look crap.
  43. ; .1 or .2 on most things looks pretty good, if you're doing grass or something that's almost a
  44. ; noise pattern anyway push it up to about .5... basically have a play.
  45. noise# = .2
  46.  
  47. ; RGB Functions
  48. Global gotr#=0
  49. Global gotg#=0
  50. Global gotb#=0
  51.  
  52. SetBuffer BackBuffer()
  53.  
  54. Repeat
  55.         x=MouseX()
  56.         y=MouseY()
  57.        
  58.         If x-hsize<0 Then x=hsize
  59.         If x+hsize+qsize>ImageWidth(i) Then x = ImageWidth(i)-hsize-qsize
  60.         If y-hsize<0 Then y=hsize
  61.         If y+hsize+qsize>ImageHeight(i) Then y = ImageHeight(i)-hsize-qsize
  62.        
  63.         Color c,0,0
  64.         c=(c+10) Mod 256
  65.         DrawBlock i,0,0
  66.         Rect x-hsize,y-hsize,size+qsize,size+qsize,False
  67.         Color 0,c,0
  68.         Rect x-hsize,y-hsize,size,size,False
  69.         oText MouseX(),MouseY(),"X",True,True
  70.         oText GraphicsWidth()/2,10,"Select area for tile",True
  71.         Flip
  72.  
  73.  
  74. Until MouseDown(1)
  75.  
  76. x=x - hsize
  77. y=y - hsize
  78.  
  79. j = CreateImage(size+qsize,size+qsize)
  80. CopyRect x,y,size+qsize,size+qsize,0,0,ImageBuffer(i),ImageBuffer(j)
  81. k = CreateImage(size,size)
  82.  
  83. LockBuffer ImageBuffer(i)
  84. LockBuffer ImageBuffer(j)
  85.  
  86. ; sort left hand edge from right hand stuff
  87. For yy=0 To size+qsize
  88. For xx=0 To qsize
  89.         ; create a bit of noise to make it imperfect
  90.         disturb# = Rnd(-noise,noise)
  91.         offset# = Float(xx) / Float(qsize) + disturb
  92.         If offset>1 Then offset = 1
  93.         If offset<0 Then offset = 0
  94.         noffset# = 1 - offset
  95.        
  96.         GetRGB(i,x+size+xx,y+yy)
  97.         r1# = gotr * noffset
  98.         g1# = gotg * noffset
  99.         b1# = gotb * noffset
  100.        
  101.         GetRGB(j,xx,yy)
  102.         r2# = gotr * offset
  103.         g2# = gotg * offset
  104.         b2# = gotb * offset
  105.        
  106.         r# = r1 + r2
  107.         g# = g1 + g2
  108.         b# = b1 + b2
  109.        
  110.         WriteRGB(j,xx,yy,r,g,b)
  111. Next
  112. Next
  113.  
  114. UnlockBuffer ImageBuffer(i)
  115. UnlockBuffer ImageBuffer(j)
  116.  
  117. CopyRect 0,0,size,size,0,0,ImageBuffer(j),ImageBuffer(k)
  118.  
  119. LockBuffer ImageBuffer(j)
  120. LockBuffer ImageBuffer(k)
  121.  
  122. ; sort top edge from stuff below
  123. For xx=0 To size
  124. For yy=0 To qsize
  125.         ; create a bit of noise to make it imperfect
  126.         disturb# = Rnd(-noise,noise)
  127.         offset# = Float(yy) / Float(qsize) + disturb
  128.         If offset>1 Then offset = 1
  129.         If offset<0 Then offset = 0
  130.         noffset# = 1 - offset
  131.        
  132.         GetRGB(j,xx,yy+size)
  133.         r1# = gotr * noffset
  134.         g1# = gotg * noffset
  135.         b1# = gotb * noffset
  136.        
  137.         GetRGB(k,xx,yy)
  138.         r2# = gotr * offset
  139.         g2# = gotg * offset
  140.         b2# = gotb * offset
  141.        
  142.         r# = r1 + r2
  143.         g# = g1 + g2
  144.         b# = b1 + b2
  145.        
  146.         WriteRGB(k,xx,yy,r,g,b)
  147. Next
  148. Next
  149.  
  150. UnlockBuffer ImageBuffer(j)
  151. UnlockBuffer ImageBuffer(k)
  152.  
  153. SaveImage(k,"tile_"+Left(filename,Instr(filename,"."))+"bmp")
  154.  
  155.  
  156. Cls
  157. For x=0 To (GraphicsWidth()/size)+1
  158. For y=0 To (GraphicsHeight()/size)+1
  159. DrawBlock k,x*size,y*size
  160. Next
  161. Next
  162. oText GraphicsWidth()/2+x,GraphicsHeight()/2+y,"Tile saved, Press any Key",True
  163.  
  164. Flip
  165. WaitKey
  166.  
  167. Function Otext(xp,yp,t$,xcen=False,ycen=False)
  168.  
  169. Color 0,0,0
  170. For x=-2 To 2
  171. For y=-2 To 2
  172.         Text xp+x,yp+y,t$,xcen,ycen
  173. Next
  174. Next
  175. Color 255,255,255
  176. Text xp,yp,t$,xcen,ycen
  177.  
  178. End Function
  179.  
  180.  
  181. Function WriteRGB(image_name,x,y,red,green,blue)
  182. ; Writes a pixel to an image.
  183. ; The imagebuffer needs to be locked as it does a write pixel fast.
  184. argb=(blue Or (green Shl 8) Or (red Shl 16) Or ($ff000000))
  185. WritePixelFast x,y,argb,ImageBuffer(image_name)
  186. End Function
  187.  
  188. Function GetRGB(image_name,x,y)
  189. ; Gets the RGB components from an image.
  190. ; The imagebuffer needs to be locked as it does a read pixel fast.
  191. ; The components are put into the global varibles gotr, gotg and gotb
  192.         argb=ReadPixelFast(x,y,ImageBuffer(image_name))
  193.         gotr=(ARGB Shr 16) And $ff
  194.         gotg=(ARGB Shr 8) And $ff
  195.         gotb=ARGB And $ff
  196. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal