Skip to content

Instantly share code, notes, and snippets.

@bj0
Last active August 29, 2015 14:13
Show Gist options
  • Save bj0/fa4cd0a7c2cda2b4f607 to your computer and use it in GitHub Desktop.
Save bj0/fa4cd0a7c2cda2b4f607 to your computer and use it in GitHub Desktop.
generate a texture from Perlin Noise in kivy 1.8
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.graphics.texture import Texture
from kivy.core.window import Window
from noise import pnoise2, snoise2
kv='''
#:import math math
Widget:
canvas:
Rectangle:
pos: self.pos
size: self.size
texture: app.tex
'''
class TexApp(App):
tex = ObjectProperty(None)
def get_buf(self):
return [ int(x*255/size) for x in range(size)]
def get_noise(self):
octave = 6
freq = 24.0
resolution = 255 # number of steps to use (<256)
size = 64*64*3
map = [int(snoise2(x / freq, y / freq, octave, repeatx=64, repeaty=64, persistence=.4)*(resolution/2)+(resolution/2-1))
for x in range(64) for y in range(64)]
return [map[i//3]*(255/resolution) for i in range(size) ]
def build(self):
buf = self.get_noise()
buf = b''.join(map(chr,buf))
self.tex = Texture.create(size=(64,64), colorfmt='rgb')
self.tex.blit_buffer(buf, colorfmt='rgb',bufferfmt='ubyte')
return Builder.load_string(kv)
if __name__ == '__main__':
TexApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment