Skip to content

Instantly share code, notes, and snippets.

@ggcrunchy
Last active May 8, 2016 18:31
Show Gist options
  • Save ggcrunchy/9164d7bf40386b0336c2a36920b81cd2 to your computer and use it in GitHub Desktop.
Save ggcrunchy/9164d7bf40386b0336c2a36920b81cd2 to your computer and use it in GitHub Desktop.
Glowy noise-based ball, camping it here until I give it a proper home (it's been lingering in a random main.lua)
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
-- [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
--
do
local kernel = { category = "generator", group = "a", name = "b" }
kernel.isTimeDependent=true
kernel.fragment = require("corona_shader.loader").FragmentShader{
prelude = [[ // see e.g. https://github.com/ggcrunchy/corona_shader_glsl/blob/master/functions/sphere.lua
#define SPHERE_PINGPONG_ANGLE
#define SPHERE_NO_DISCARD
]], main = [[
// Simplex Noise by IQ
P_POSITION vec2 hash(P_POSITION vec2 p )
{
p = vec2( dot(p,vec2(127.1,311.7)),
dot(p,vec2(269.5,183.3)) );
return -1.0 + 2.0*fract(sin(p)*437.585453123);
}
P_POSITION float noise( P_POSITION vec2 p )
{
const P_POSITION float K1 = 0.366025404; // (sqrt(3)-1)/2;
const P_POSITION float K2 = 0.211324865; // (3-sqrt(3))/6;
P_POSITION vec2 i = floor( p + (p.x+p.y)*K1 );
P_POSITION vec2 a = p - i + (i.x+i.y)*K2;
P_POSITION vec2 o = (a.x>a.y) ? vec2(1.0,0.0) : vec2(0.0,1.0); //vec2 of = 0.5 + 0.5*vec2(sign(a.x-a.y), sign(a.y-a.x));
P_POSITION vec2 b = a - o + K2;
P_POSITION vec2 c = a - 1.0 + 2.0*K2;
P_POSITION vec3 h = max( 0.5-vec3(dot(a,a), dot(b,b), dot(c,c) ), 0.0 );
P_POSITION vec3 n = h*h*h*h*vec3( dot(a,hash(i+0.0)), dot(b,hash(i+o)), dot(c,hash(i+1.0)));
return dot( n, vec3(70.0) );
}
const P_POSITION mat2 m = mat2( 0.80, 0.60, -0.60, 0.80 );
P_POSITION float fbm4( P_POSITION vec2 p )
{
P_POSITION float f = 0.0;
f += 0.5000*noise( p ); p = m*p*2.02;
f += 0.2500*noise( p ); p = m*p*2.03;
f += 0.1250*noise( p ); p = m*p*2.01;
f += 0.0625*noise( p );
return f;
}
P_POSITION float turb4( P_POSITION vec2 p )
{
P_POSITION float f = 0.0;
f += 0.5000*abs(noise(p)); p = m*p*2.02;
f += 0.2500*abs(noise(p)); p = m*p*2.03;
f += 0.1250*abs(noise(p)); p = m*p*2.01;
f += 0.0625*abs(noise(p));
return f;
}
P_POSITION float marble(P_POSITION vec2 p)
{
return cos(p.x+fbm4(p));
}
P_COLOR vec4 FragmentKernel (P_UV vec2 uv)
{
P_POSITION vec2 uv2 = GetUV_PhiDelta(2. * uv - 1., CoronaTotalTime * .081);
P_POSITION vec2 uv3 = GetUV_PhiDelta(2. * uv - 1., CoronaTotalTime * .012);
P_POSITION float r = turb4(vec2(marble(uv2.yx * 3.1), marble(uv3.yx * 2.1) + fbm4(uv.yx * 2.4)));
P_POSITION float g = turb4(vec2(marble(1. - uv2.yx * 3.1), marble(uv3.yx * 2.1) + fbm4(uv * 1.4)));
P_POSITION float b = turb4(vec2(marble(1. - uv3 * 7.1), marble(uv2 * 6.2) + fbm4(uv * 1.7)));
P_POSITION float scale = smoothstep(.03, .44, (r + g + b) / 3.);
#if 1
return vec4(mix(vec3(1.), vec3(r, g, b), scale), 1.);
#else
return vec4(r, g, b, 1.) * scale;
#endif
}
]]
}
graphics.defineEffect(kernel)
end
--local rr=display.newRect(display.contentCenterX, display.contentCenterY,300,200)
--rr:setFillColor(.4)
local r = display.newCircle(display.contentCenterX, display.contentCenterY, 70)
r.fill.effect="generator.a.b"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment