Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created February 24, 2014 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermotbalson/9181463 to your computer and use it in GitHub Desktop.
Save dermotbalson/9181463 to your computer and use it in GitHub Desktop.
color text
function setup()
t={"This is some ","fancily colored ","text message thingy,","which is a ","mixture ","of ","several totally different ","colors, for your delectation",}
c={color(0),color(255,0,0),color(0,0,255),color(0),color(255,255,0),color(255),
color(255, 0, 0, 255),color(0)}
p=WriteText(t,c,300,300,18)
end
function draw()
background(150)
sprite(p,500,500)
end
--t = table of text, c = table of colors, w,h=wrap width,text height, s=font size
function WriteText(t,c,w,h,s)
textMode(CORNER)
fontSize(s)
textWrapWidth(w)
textAlign(LEFT)
local tt={}
tt[1]=t[1]
for i=2,#t do tt[i]=tt[i-1]..t[i] end
local img0=image(w,h)
for i=1,#t do
local img1=image(w,h)
setContext(img1)
local ww,hh=textSize(tt[i])
fill(c[i])
text(tt[i],0,200-hh)
setContext()
local m=mesh()
m:addRect(w/2,h/2,w,h)
m.shader=shader(textShader.vertexShader,textShader.fragmentShader)
m.texture=img0
m.shader.texture2=img1
local img2=image(w,h)
setContext(img2)
m:draw()
setContext()
img0=img2
end
return img0
end
textShader = {
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 lowp sampler2D texture;
uniform lowp sampler2D texture2;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
float z=0.1;
void main()
{
lowp vec4 c1 = texture2D(texture, vTexCoord) ;
lowp vec4 c2 = texture2D(texture2, vTexCoord) ;
if (c1.a==0. && c2.a>0.) gl_FragColor=c2;
else gl_FragColor=c1;
}
]]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment