Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created October 8, 2013 14:54
Show Gist options
  • Save dermotbalson/6886024 to your computer and use it in GitHub Desktop.
Save dermotbalson/6886024 to your computer and use it in GitHub Desktop.
Sphere shader
--# Main
--[[
there are two shader examples, one in each of the next two tabs
Whichever tab is on the far right will run, so drag whichever tab you want to run, to the right
--]]
--# Emboss
-- Emboss
-- Use this function to perform your initial setup
function setup()
m=mesh()
img=readImage("Small World:House White")
m:addRect(WIDTH/2,HEIGHT/2,img.width,img.height)
m:setColors(color(255))
m.texture=img
m.shader=shader(embossShader.vertexShader,embossShader.fragmentShader)
m.shader.width=1/img.width --important, don't change
m.shader.height=1/img.height --important, don't change
end
function draw()
background(40, 40, 50)
m:draw()
end
embossShader = {
vertexShader = [[
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
varying vec4 vPosition;
void main()
{
vColor=color;
vTexCoord = texCoord;
vPosition=position;
gl_Position = modelViewProjection * position;
}
]],
fragmentShader = [[
precision highp float;
uniform lowp sampler2D texture;
uniform float width;
uniform float height;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
varying vec4 vPosition;
const vec4 lumcoeff = vec4(0.299, 0.587, 0.114, 0);
void main()
{
lowp vec4 col = texture2D( texture, vec2(vTexCoord.x, vTexCoord.y));
if (col.a>0.) {
vec4 col0 = texture2D(texture, vec2(vTexCoord.x-width,vTexCoord.y-height));
vec4 col1 = texture2D(texture, vec2(vTexCoord.x ,vTexCoord.y-height));
vec4 col2 = texture2D(texture, vec2(vTexCoord.x-width,vTexCoord.y ));
vec4 col3 = texture2D(texture, vec2(vTexCoord.x+width,vTexCoord.y ));
vec4 col4 = texture2D(texture, vec2(vTexCoord.x ,vTexCoord.y+height));
vec4 col5 = texture2D(texture, vec2(vTexCoord.x+width,vTexCoord.y+height));
vec4 sum = vec4(0.5) + (col0 + col1 + col2) - (col3 + col4 + col5);
float lum = dot(sum, lumcoeff);
gl_FragColor = vec4(lum, lum, lum, 1.0) * vColor;
}
else gl_FragColor = col;
}
]]}
--# FakeSphere
-- Fake sphere
--NOTES
--texture image has to be square
--there is one adjustable shader parameter, fraction. When set to 0.5, it seems to wrap the texture
-- right around the image. if set to 1, it wraps the image multiple times
function setup()
m=mesh()
img0=readImage("Cargo Bot:Codea Icon") --select your image here <<<<<
mm=math.min(img0.width,img0.height)
img=img0:copy(1,1,mm,mm) --make it square
m:addRect(WIDTH/2,HEIGHT/2,400,400) --add a rectangle to our mesh, the size you want (square)
m.texture=img
m.shader=shader(sphereShader.vertexShader,sphereShader.fragmentShader)
m.shader.fraction=0.5
m:setColors(color(255))
end
function draw()
background(40, 40, 50)
m.shader.time=ElapsedTime/20 --vary the time speed to change the rotation speed of the sphere
m:draw()
end
sphereShader = {
vertexShader = [[
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
varying vec4 vPosition;
void main()
{
vColor=color;
vTexCoord = texCoord;
vPosition=position;
gl_Position = modelViewProjection * position;
}
]],
fragmentShader = [[
precision highp float;
uniform lowp sampler2D texture;
uniform float time;
uniform float fraction;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
varying vec4 vPosition;
void main()
{
//vec2 tc = vTexCoord.xy;
vec2 p = -1.0 + 2.0 * vTexCoord;
float r = dot(p,p);
if (r > 1.0) discard;
float f = 0.5*(1.0-sqrt(1.0-r))/r;
vec2 uv;
uv.x = fraction+p.x*f + time;
uv.y = fraction+p.y*f + time;
vec4 c = texture2D( texture, vec2(mod(uv.x,1.0), mod(uv.y,1.0)));
gl_FragColor = vec4(c.xyz, 1.0);
}
]]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment