This code will let you switch between the shaders using parameters. On the iPad 1, it gives you a rugby ball when model=1, while on an iPad3 it makes no difference at all.
--# SphereShader
sphereShader = {
vertexShader = [[
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
vColor=color;
vTexCoord = texCoord;
gl_Position = modelViewProjection * position;
}
]],
fragmentShader = [[
precision highp float;
uniform float model;
uniform lowp sampler2D texture;
uniform float time;
uniform float fraction;
uniform float height;
uniform vec3 light;
highp float h = height*height;
highp float rh = h/(h-1.);
highp float sqrh = sqrt(rh);
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
varying vec4 vPosition;
void main()
{
vec2 p = (-1.0 + 2.0 * vTexCoord)/sqrh;
float r = dot(p,p);
if (r > 1.0) discard;
vec2 uv;
float t = (h + sqrt(r - r*h + h))/(r+h);
if (model==0.0) {
// texture coordinates according to Ignatz
float f = 0.5*(1.0-sqrt(1.0-r))/r;
uv.x = mod( fraction+p.x*f + time , 1.0 );
uv.y = mod( fraction+p.y*f + time , 1.0 );
vec4 c = texture2D( texture, uv );
}
else {
// texture coordinates according to Andrew
float phi = 1.-acos(t*p.y)/3.1415;
float theta = atan((1.-t)*height,t*p.x) / (2.*3.1415) + time ;
uv.x = mod( theta , 1.0 );
uv.y = mod( phi , 1.0 );
}
// set the texture coordinates
vec4 c = texture2D( texture, uv );
// shadow application
float l = max(0.,t*dot(p.xy,light.xy) + (1.-t)*height*light.z);
gl_FragColor = vec4(l*c.xyz, 1.0);
}
]]}
--# Main
-- sphere shader 2
-- by andrew stacey
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.height=15
m.shader.light = vec3(1,1,0):normalize()
m.shader.fraction=0.5
m:setColors(color(255))
light_azimuth = 0
light_zenith = 0
local reset_light = function()
m.shader.light = vec3(
math.cos(math.rad(light_azimuth))
* math.sin(math.rad(light_zenith)),
math.cos(math.rad(light_zenith)),
math.sin(math.rad(light_azimuth))
* math.sin(math.rad(light_zenith))
)
end
local setModel = function() m.shader.model = model end
parameter.integer("model",0,1,0,setModel)
parameter.integer("light_azimuth",-180,180,-34,reset_light)
parameter.integer("light_zenith",0,180,90,reset_light)
end
function draw()
background(40, 40, 50)
m.shader.time=ElapsedTime/10 --vary the time speed to change the rotation speed of the sphere
--m.shader.model = model
m:draw()
end