Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
local function makeGenerator()
local kernel = {
category = "generator",
name = "stars",
isTimeDependent = false
}
kernel.vertexData = {
{
name = 'resolutionX',
default = display.pixelWidth,
index = 0
},
{
name = 'resolutionY',
default = display.pixelHeight,
index = 1
},
{
name = 'seed',
default = 234203212011.0,
index = 2
}
}
kernel.fragment =
[[
#ifdef GL_ES
precision P_NORMAL float;
#endif
// based off http://glslsandbox.com/e#32816.0
float rand(vec2 co)
{
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 437.585453);
}
vec4 FragmentKernel( P_UV vec2 texCoord ){
//Play with prob and resolution to tweak effect depending on your tile size
float prob = 0.998;
vec2 resolution = vec2(15);
//if you want 'twinkle' effect, turn on isTimeDependent for kernel, and set
//seed to be CoronaTotalTime
float seed = CoronaVertexUserData.z;
float color = 0.0;
if (rand(texCoord.xy / resolution.xy) > prob)
{
float r = rand(texCoord.xy);
color = r * (0.85 * sin(seed * (r * 5.0) + 720.0 * r) + 0.95);
}
P_COLOR vec4 ret = vec4(vec3(color), 1.0);
return CoronaColorScale(ret);
}
]]
graphics.defineEffect(kernel)
end
makeGenerator()
local tile = display.newRect(
display.contentCenterX, display.contentCenterY,
display.pixelWidth, display.contentHeight
)
tile.fill.effect = "generator.custom.stars"
tile.fill.effect.resolutionX = display.pixelWidth
tile.fill.effect.resolutionY = display.pixelHeight
-- tile.fill.effect = "generator.sunbeams"
-- tile.fill.effect.posX = 0.5
-- tile.fill.effect.posY = 0.5
-- tile.fill.effect.aspectRatio = ( tile.width / tile.height )
local function tap( )
print("tap")
tile.fill.effect.seed = math.random( 10000000 )
end
Runtime:addEventListener("tap", tap)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.