Skip to content

Instantly share code, notes, and snippets.

@Shchvova
Last active April 11, 2022 21:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Shchvova/c5872d699ca5f1ca0d8a to your computer and use it in GitHub Desktop.
Save Shchvova/c5872d699ca5f1ca0d8a to your computer and use it in GitHub Desktop.
Planets shader for Corona

#Corona planet shader v0.1#

Alt text

###Instructions###

  1. Download and unpack this gist into folder
  2. Add this assets two images and name them "red.jpg" and "[blue.jpg] (http://i.imgur.com/V3Tw97g.jpg)" in same folder (you will have to download and rename them)
  3. Open project in Corona Simulator 🤘
  4. [OPTIONAL]Works best with retina/hi-dpi screens

####WARNING#### This shader is very computationally expensive and slow.

--
-- For more information on build.settings see the Corona SDK Build Guide at:
-- http://docs.coronalabs.com/guide/distribution/buildSettings/index.html
--
settings =
{
orientation =
{
-- Supported values for orientation:
-- portrait, portraitUpsideDown, landscapeLeft, landscapeRight
default = "landscapeRight",
supported = { "landscapeRight", }
},
excludeFiles =
{
-- Include only the necessary icon files on each platform
iphone = { "Icon-*dpi.png", },
android = { "Icon.png", "Icon-Small-*.png", "Icon*@2x.png", },
},
--
-- iOS Section
--
iphone =
{
plist =
{
UIStatusBarHidden = false,
UIPrerenderedIcon = true, -- set to false for "shine" overlay
--UIApplicationExitsOnSuspend = true, -- uncomment to quit app on suspend
CFBundleIconFiles =
{
"Icon.png",
"Icon@2x.png",
"Icon-60.png",
"Icon-60@2x.png",
"Icon-60@3x.png",
"Icon-72.png",
"Icon-72@2x.png",
"Icon-76.png",
"Icon-76@2x.png",
"Icon-Small.png",
"Icon-Small@2x.png",
"Icon-Small@3x.png",
"Icon-Small-40.png",
"Icon-Small-40@2x.png",
"Icon-Small-50.png",
"Icon-Small-50@2x.png",
},
--[[
-- iOS app URL schemes:
CFBundleURLTypes =
{
{
CFBundleURLSchemes =
{
"fbXXXXXXXXX", -- replace XXXXXXXXX with your Facebook appId
}
}
}
--]]
}
},
--
-- Android Section
--
android =
{
usesPermissions =
{
"android.permission.INTERNET",
},
},
}
application =
{
content =
{
width = 768,
height = 1024,
scale = "adaptive",
fps = 30,
--[[
imageSuffix =
{
["@2x"] = 2,
},
--]]
},
--[[
-- Push notifications
notification =
{
iphone =
{
types =
{
"badge", "sound", "alert", "newsstand"
}
}
},
--]]
}
local kernel = {}
kernel.language = "glsl"
kernel.category = "filter"
kernel.name = "planet"
kernel.isTimeDependent = true
kernel.vertexData =
{
{
name = "vx", -- horizontal "rotation" speed
default = 0.1,
min = -2,
max = 2,
index = 0, -- This corresponds to CoronaVertexUserData.x
},
{
name = "vy", -- vertical "rotation" speed
default = 0,
min = -2,
max = 2,
index = 1, -- This corresponds to CoronaVertexUserData.y
},
{
name = "t0", -- Time offset from start time
default = 0,
min = -1000,
max = 1000,
index = 2, -- This corresponds to CoronaVertexUserData.z
},
}
kernel.fragment =
[[
P_COLOR vec4 FragmentKernel( P_UV vec2 uv )
{
P_UV vec4 fragColor; //return value
P_UV vec2 p = -1.0 + 2.0 * uv;
//p is now vector from center of texture, pointing to this texel
P_UV float r = dot(p, p);
if (r<=1.0) {
P_UV vec2 Velocity = CoronaVertexUserData.xy;
P_UV float f;
if(r>=0.00001) {
f = (1.0 - sqrt(1.00 - r)) / r;
}else
{
f = 1.0-r/2.0;
}
P_UV vec2 uv1;
uv1.x = 1.0*p.x*f;
uv1.y = 1.0*p.y*f;
P_UV float z1 = sqrt(1.0 - uv1.x*uv1.x-uv1.y*uv1.y);
P_UV float z2 = distance( vec2(0.7,0.7), uv1);
uv1 += (CoronaTotalTime+CoronaVertexUserData.z)*Velocity;
fragColor = vec4(texture2D(CoronaSampler0, uv1).rgb*z2, 1.0*z1);
} else {
fragColor = vec4(0.0,0.0,0.0, 0.0);
}
return CoronaColorScale(fragColor);
}
]]
return kernel
display.setDefault( "textureWrapX", "mirroredRepeat" )
display.setDefault( "textureWrapY", "mirroredRepeat" )
display.setDefault( 'isShaderCompilerVerbose', true )
local kernel = require("kernel_composite_planet")
graphics.defineEffect( kernel )
function setPlanet( o, planet )
local paint =
{
type="image",
filename=planet
}
o.fill = paint
o.fill.effect = "filter.custom.planet"
o.fill.effect.vx = 0.1 - math.random()*0.2
o.fill.effect.vy = 0.1 - math.random()*0.2
o.fill.effect.t0 = math.random( 2, 50 )
end
local cy = display.contentCenterY
local w = display.contentWidth;
local sz = math.min(w/2.1,cy*2)
local object1 = display.newRect( w/4, cy, sz, sz )
local object2 = display.newRect( w*3/4, cy, sz, sz )
setPlanet(object1, "blue.jpg")
setPlanet(object2, "red.jpg")
-- object1:setFillColor( 1, 0,0,0.5 ) -- uncomment after global worming
@ggcrunchy
Copy link

Nice, and an interesting solution to the north / south poles problem. (I've been playing with a sphere myself but just sort of ignore it...)

Did you compare the "fragColor = vec4(0.0,0.0,0.0, 0.0);" against "discard;" at all? Just curious since I don't have much hardware handy to make decent comparisons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment