Codea 1.5 Beta

Just thinking ahead - picking a model format, then allowing import/export/picking of models like you do for images and shaders would be keen.

I’d like to say ‘m = mesh(“Documents:Teapot”)’

I’d also like to say ‘m = mesh(“http://home.bortels.us/teapot.ply”)’

or image() or shader() for that matter. Just suggestions.

I can load the 2.7 meg teapot in under 2 seconds. NOT TOO SHABBY. Haven’t parsed it yet.

So - not all PLY is PLY. Just sayin.

PS. I just caught myself; this happens every time. What I want to be doing is learning shaders and finding bugs in 1.5 - and what I’ve done for the last hour is research into writing a PLY loader (a robust one) in lua. Good stuff - worth doing - and completely irrelevant to something that might be useful for a beta test.

Yak Shaving. It is my life.

Works really nice, good job! Will try it out a bit more this weekend! It would be nice if the shader lab populated the time variable if it exists in the shader.

Bug with watch. The first two watches are a bit funny. The first gets an extra table, the second is always nil. After that they straighten out.

.@tnlogy the plan is to have those variables become interactive — like the colour picker in the main editor. So you could tap the time variable and slide a slider to change it. Or tap a texture to choose a new sample texture, and so on.

.@Andrew_Stacey so any sequence of two watch() expressions causes odd behaviour?

No, it was the first two in a project and after those two then all the others were fine.

I was experimenting with the camera project. I added watch("camWidth") and watch("camHeight") and got the first as table:<junk> 480 and the second as nil. This puzzled me somewhat as if I printed those two then I got 0 and 0 (the discrepancy on values is because I was printing from setup before the camera had been initialised). So then I added dummy variables testa = camWidth and testb= camHeight to the camera setup routine and watched those. They were fine and gave the expected result. So then I added watch("nothing") watch("nothingelse") at the start and those two were table:<junk> nil and nil and all the other watches then worked just fine (there were six in total by this stage).

I am running out of superlatives for Codea. Christmas has come early.

It would be good if the final version of version 1.5 could fix issue #203, so that it is easy again to take screen shots of what can be produced with the extended API.

.@mpilgrem yeah we know what is causing this particular issue and will fix it for the 1.5 release.

Hi @Simeon - my impression of the beta is that it is great! I liked how you kept the examples simple. I would like the code arguments things to stick around rather than fade - I like to mull over my options. :slight_smile:

The camera is wonderful and shaders will be an interesting area of growth. I presume you will ship a few more sample ones with it. Although I will have to learn how shaders work, I was able to mess around and alter the colours. A question: if your shader is like the ripple one, how does the preview work if there is no frequency input that you get with parameter() in the actual project?

Interactive sounds great :slight_smile:

It’s nice to easely edit the shaders from the Lua code view. But, maybe there is a need for a pause button in the shader view. When I modified an ink shader (used for my profile picture) I crashed Codea when I changed the step parameter in a for-loop and it was x+= .0 before I changed it to 1.0.

@Fred Simeon mentioned above that they will be interactive with sliders for the parameters.

.@tnlogy yeah the immediate re-compilation can cause issues like that. I was thinking to have Codea recompile the shader on a short timer, about 0.2-0.5 seconds after your last key press. So rapid typing doesn’t continuously recompile at every keystroke.

However I definitely think we need a pause button too (this would also be a way of hiding obtrusive error messages).

that, or have a “see it now” button. (Plus maybe “revert to last working” option)

tween() confuses me. I can interrupt it (in the example) - but not always? I think it’s a bug in the “tapCount” count in the example, actually. (Ah - yes. A bug in me - I tend to double-tap, which set the count > 1. Changing “== 1” to “> 0” made it work better for me)

Just trying to understand it - you tween(), and that essentially runs as a background thread, until the animation is complete - right? Unless you tween.resetAll(). Is there a way to reset a specific tween?

To reset a particular tween you can use the ID returned from tween()

local tid = tween( 1.0, someObject, {someTarget} )

tween.reset( tid )
tween.stop( tid )

```


Tween doesn't run in a background thread, but it does get updated automatically at the same rate as the draw() function is called.

Is there a good tutorial on shaders somewhere? I’m trying some experiments and I don’t know if it is shaders that I don’t understand or Codea’s implementation of them.

Nope.

But having said that, this one is better than most: http://www.aerotwist.com/tutorials/an-introduction-to-shaders-part-1/

it’s based on webgl and three.js, so at least it’s accessible. (Part 2 is actually more “meaty”, but you go thru part 1 to get there)

Okay, here’s my first go.

Codea program

function setup()
    m = mesh()
    local nsteps = 200
    local w = 5
    local h = 1/nsteps
    for n=1,nsteps do
        m:addRect(0,(n-.5)*h,w,h)
    end
    m.shader = shader("Documents:ExperimentOne")
    m.shader.time = 1
    a = vec2(0,0)
    b = vec2(100,0)
    c = vec2(200,200)
    d = vec2(200,300)
    m.shader.a = a
    m.shader.b = b
    m.shader.c = c
    m.shader.d = d
    m:setColors(255,255,25)
end

function draw()
    background(75, 104, 90, 255)
    strokeWidth(5)
    line(a.x,a.y,b.x,b.y)
    line(b.x,b.y,c.x,c.y)
    line(c.x,c.y,d.x,d.y)
    fill(160, 172, 22, 255)
    noStroke()
    ellipse(a.x,a.y,20)
    ellipse(b.x,b.y,20)
    ellipse(c.x,c.y,20)
    ellipse(d.x,d.y,20)
    m:draw()
end

function touched(touch)
    if touch.state == BEGAN then
    for k,v in ipairs({a,b,c,d}) do
        if v:distSqr(vec2(touch.x,touch.y)) < 900 then
            pt = v
        end
    end
    elseif pt then
        pt.x = touch.x
        pt.y = touch.y
    m.shader.a = a
    m.shader.b = b
    m.shader.c = c
    m.shader.d = d
    end
end

Vertex Shader


//
// A basic vertex shader
//

//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;

uniform vec2 a;
uniform vec2 b;
uniform vec2 c;
uniform vec2 d;
uniform float time;

void main()
{
    highp float t = position.y/time;
    highp float tt = 1.0 - t;
    highp vec2 bpos = tt*tt*tt*a + 3.0*tt*tt*t*b 
    + 3.0*tt*t*t*c + t*t*t*d;
    highp vec2 bdir = tt*tt*(b-a) + 2.0*tt*t*(c-b) + t*t*(d-c);
    bdir = vec2(bdir.y,-bdir.x);
    bdir = position.x*bdir/length(bdir);
    bpos = bpos + bdir;
    highp vec4 bzpos = vec4(bpos.x,bpos.y,0,1);
    //Pass the mesh color to the fragment shader
    vColor = color;
    vTexCoord = vec2(texCoord.x, 1.0 - texCoord.y);
    //Multiply the vertex position by our combined transform
    gl_Position = modelViewProjection * bzpos;
}

Fragment shader


//
// A basic fragment shader
//

//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 = vColor;
    if (vTexCoord.x < .2)
        col.a = col.a*vTexCoord.x/.2;
    if (vTexCoord.x > .8)
        col.a = col.a*(1.-vTexCoord.x)/.2;
    //Set the output color to the texture color
    gl_FragColor = col;
}

What I need now is a list of available functions, a bit of guidence on syntax (what language is this?) and an explanation of what lowp and highp mean.

In other news:

  1. camera + harmony = fun
  2. the shader lab doesn’t like it if you exit Codea while editing a shader. It crashes when I go back to Codea.