# The falling matrix effect??

I want to make the falling matrix effect like this http://imgur.com/rOJRC,NPlKz#0 is that possible and how

http://twolivesleft.com/Codea/Talk/discussion/577/digital-rain

Awesome thanks!

I experimented with this myself (results below) as it chimed with my recent experiments with fonts and the UTF-8 format. This was inspired by the Handling Touches example project. (Updated)

``````
--
-- Raining Kana v2
--
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
backingMode(RETAINED)
function setup()
fontSize(28)
n = math.floor(WIDTH/40)
y = {}
for i = 0, n do
y[i] = {}
for j = 1, 3 do
y[i][j] = math.random(0, HEIGHT)
end
end
kana = {}
for i = 1, 0x5B do
local k = 0x30A0 + (i - 1)
kana[i] = kanaUnicode2UTF8(k)
end
nKana = #kana
rectMode(CORNER)
lastTime = ElapsedTime
end

function draw()
if (ElapsedTime - lastTime) < 1/20 then return end
lastTime = ElapsedTime
fill(0, 30)
rect(0, 0, WIDTH, HEIGHT)
fill(35, 184, 40)
for i = 0, n do
for j = 1, 3 do
text(kana[math.random(1, nKana)], i*WIDTH/n, y[i][j])
y[i][j] = y[i][j] - math.random(5, 30)
if y[i][j] < 0 then y[i][j] = HEIGHT end
end
end
end

function kanaUnicode2UTF8(u) -- Three-byte UTF-8 format
return string.char(
math.floor(u/0x1000) + 0xE0,
math.floor(u/0x40) % 0x40 + 0x80,
u % 0x40 + 0x80
)
end
``````

Here’s my stupid little physics matrix effect: http://twolivesleft.com/Codea/Talk/discussion/2933/physical-matrix-effect

Here’s a little program that gives a matrix drip effect to any picture, it does it while you watch

``````-- matrix

function setup()
img2=readImage("Cargo Bot:Codea Icon") --choose an image here
w,h=img2.width,img2.height
--create copy, make black background
img=image(w,h)
setContext(img)
fill(0)
rect(0,0,w,h)
setContext()
end

function draw()
background(220)
sprite(img,300,300)
end

for k=1,10 do
x,y,n,b=math.random(1,w),math.random(1,h),math.random(2,15),math.random(100,255)
for i=1,n do
local yy=y-i if yy<1 then break end
local d=.5+i/n/2
--don't draw over existing pixel
local r,g,b,a=img:get(x,yy)
if r+g+b>0 then break end
--adjust intensity to original image pixel
r,g,b,a=img2:get(x,yy)
local j=.21*r+.71*g+.08*b  --intensity weightings
img:set(x,yy,0,j*d,0)
end
end
end
``````

Wow @Ignatz - Nice.

Hey! I’ve been working my butt off, and this is my almost final result! I guess I kinda cheated with a mesh, but heck… It works! ITS ALIVE!!!

``````-- matrix
displayMode(FULLSCREEN)

-- Use this function to perform your initial setup
function setup()
cameraSource(CAMERA_FRONT)
spriteMode(CORNER)
characters = { "0", "1" }
PrevTime = 0
font("Courier")
fontSize(10)
textMode(CORNER)
dim = { { textSize("0") }, { textSize("01") }}
x, y = dim[2][1] - dim[1][1], dim[2][1]
print(x, y)
w = (WIDTH - ( { textSize("0") })[1]) / x
h = HEIGHT / y
img = image(w, h)
m = mesh()
local w, h = WIDTH, HEIGHT
m.vertices = {
vec2(0, 0), vec2(0, h), vec2(w, h),
vec2(0, 0), vec2(w, 0), vec2(w, h),
}
m.texCoords = {
vec2(0, 0), vec2(0, 1), vec2(1, 1),
vec2(0, 0), vec2(1, 0), vec2(1, 1),
}

m:setColors(255, 255, 255, 255)
m.texture = CAMERA
setContext(img)
m:draw()
setContext()
end

-- This function gets called once every frame
function draw()
translate(0, -(ElapsedTime * 10 % (y * 2)))
--if ElapsedTime > PrevTime + 0.25 then
--    PrevTime = PrevTime + 0.25

--fill(255, 255, 255, 255)
--setContext(img)
--m:draw()
--setContext()
--end
-- This sets a dark background color
background(0, 0, 0, 255)

-- This sets the line thickness
strokeWidth(5)

fill(0, 255, 17, 255)
for i = 1, h + 3 do
text(screen[i], 0, y * (i - 1))
end
resetMatrix()
--sprite(img, 0, 0, WIDTH, HEIGHT)
m:draw()
end

screen = {}
for i = 1, h + 3 do
screen[i] = characters[math.random(#characters)]
for j = 1, w do
screen[i] = screen[i] .. characters[math.random(#characters)]
end
end
end

function touched(t)
if t.state == ENDED and CurrentTouch.tapCount == 2 then
if t.y > HEIGHT / 4 then
cameraSource(3 - cameraSource())
else
m.texture = image(CAMERA)
end
end
end
vS = [[
//
//

//This is the current model * view * projection matrix
// Codea sets it automatically
uniform mat4 modelViewProjection;

//This is the current mesh vertex position, color and tex coord
// Set automatically
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;

//This is an output variable that will be passed to the fragment shader
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;

void main()
{
//Pass the mesh color to the fragment shader
vColor = color;
vTexCoord = texCoord;

//Multiply the vertex position by our combined transform
gl_Position = modelViewProjection * position;
}
]]
fS = [[
//
//

//Default precision qualifier
precision highp float;

//This represents the current texture on the mesh
uniform lowp sampler2D texture;

//The interpolated vertex color for this fragment
varying lowp vec4 vColor;

//The interpolated texture coordinate for this fragment
varying highp vec2 vTexCoord;

void main()
{
//Sample the texture at the interpolated coordinate
lowp vec4 col = texture2D( texture, vTexCoord ) * vColor;
mediump float t = col.r + col.g + col.b;
lowp float a = t / 3.0;
col.r = a;
col.g = a;
col.b = a;
//Set the output color to the texture color
gl_FragColor = vec4(0, 0, 0, a * a);
}
]]
``````

How to:
Double tap the bottom quarter of the screen to freeze the camera (permanently). Double tap anywhere else to swap the camera source.
Welcome to the real world…