Codea Craft 2.5 (84) Beta - Updated 8/9/2017

@Simeon Setting it every frame from within the program was just an example to show that the parameter.text variable can be updated until you touch the parameter.text area. After that, the variable can no longer be updated from within the program. What I’m after is to use the text area as an input, have the user press a parameter.action button, process what was entered in the text area, then set the parameter.text variable to “” for the next input. Right now, you have to tap the text area and backspace what’s in the text area to clear it before you enter something else. The other parameter variables, (number, integer, boolean) allow you to alter their values from within the program. I don’t understand why the text variable can’t be altered after it’s first use. I can use parameter.clear, but if I have several other parameter variables being used, it also clears them.

@dave1707 I agree with you, I think your use case is a pretty good one. Just trying to think about how to implement it :slight_smile:

@John @Simeon I’ve just found that Codea crashes if I supply incorrect parameters to craft.rigidbody functions in update specifically. The same code in setup, touched or draw halts with an error message as normal.

function update() 
   local body = entity:get(craft.rigidbody)
   body:applyForce()
   body:applyImpulse(vec3(0,0,0))
   body:applyTorque("crash")
end

The new beta build (B77) is out! There are a number of bug-fixes and changes to example projects as well as a few new ones.

BETA NOTES
- After installing a new version you should be prompted to restore examples, please make sure to press the restore button
- For feedback / questions please visit codea.io/talk (PM simeon or john for access to the beta thread)

NEW FEATURES - Craft
- A new advanced feature-set with support for 3D environments, lighting, physics, voxels and more!
- OBJ model loading (support for other formats coming in future updates)
- Directional, point and spot lights
- Advanced material system with support for physically based rendering
- Voxel rendering and collisions with support for fully custom block types and scripting
- Advanced noise system for terrain generation and other procedural content
- 3D physics, with support for spheres, boxes, capsules and mesh shapes
New Examples
New Assets

CHANGE LOG

28/05/2017
Examples
- Added new example: Stacker
- Added new example: Models
- Added support for fog in Voxel Terrain
Assets
- Added new asset pack: Racing Kit by Kenney
Runtime
- rendering
	- Added support for fog
	- Added fog options to craft.scene (fogEnabled, fogColor, forNear, fogFar)
	- Added fog to voxel material
	- Changed fog distance calculation to radial
	- Obj model materials should load more consistently now
- voxels
	- Reused chunks are no longer visible before being meshed for the first time
	- Fixed crashes related to setting and getting properties on dynamic blocks
- physics
	- Dynamic rigidbodies now ignore their parent transforms when being simulated (unless the parent is moving)
	- Static and Kinematic rigidbodies now take their parent world space transforms into account when being simulated

@John or @Simeon Can you help me with this really quickly please. I just cant figure it out. How can i map a surface texture around a icoSphere, and how can I increase the farClipping plane beyond 400?

@John @Simeon Nothing to do with Craft, but still an existing issue in the current code. If you do a long press on a project, then export, then copy project, if the code contains --# Main or any --# tab name, when you paste into project, multiple tabs are created for each --# in the new project.

EDIT: If I edit a project then do a select all and copy, then do a paste into project, the new project doesn’t have the --# Main line of code. The --# are removed.

@GR00G0 The best way to map a texture onto an ico sphere would be to use a cube map (as regular 2D texture mapping won’t work due to seams and distortion). I’m working on an example that uses a thing called a quad cube to achieve this, however rendering the custom cube map isn’t supported at the moment and the current materials do not allow cube maps to be used for this purpose.

There’s a nice article on this subject that goes into a lot of depth on how to achieve planet rendering at multiple scales: https://acko.net/blog/making-worlds-1-of-spheres-and-cubes/ which should be possible to do in Codea soon.

@John, @Simeon I was extending quat to rotate vec3 via multiply and found something strange. The following error occurs if I attempt to compare a vec3 metatable to a quat metatable or equivocally quat.___class.

print(getmetatable(quat(1,0,0,0)) == getmetatable(quat(1,0,0,0)))
print(getmetatable(quat(1,0,0,0)) == getmetatable(vec3(0,0,0))) -- error

Main:43: expect userdata, got table
stack traceback:
	[C]: in metamethod '__eq'
	...

Also now with build 77, Codea crashes for seemingly any assert or similar error during setup with Craft enabled. It appears to happen only when Craft is loading because causing an error later in touched halts and prints as normal and Codea doesn’t crash when Craft is disabled. Also wrapping the code in an xpcall prevents the crash.

In case anyone is interested I extended quat to rotate vec3 with the following code in the end. The equation is from https://gamedev.stackexchange.com/questions/28395/rotating-vector3-by-a-quaternion.

function hook(base,func)
    return function(...) return func(base,...) end
end

function setup()
    local types = {}
    types[vec3] = getmetatable(vec3())
    types[quat] = quat.___class
    quat.___class.__mul = hook(quat.___class.__mul,function(base,left,right)

        local mt = getmetatable(right)
        local result, isQuat = xpcall(function() return mt == types[quat] end,
        function() end)
        isQuat = result and isQuat 
    
        if isQuat then
           return base(left,right)
        elseif mt == types[vec3] then
           local u = vec3(left.x,left.y,left.z)
           local s = left.w
           return 2 * u:dot(right) * u + (s*s - u:dot(u)) * right + 2 * s * right:cross(u) 
        end
    end)
end

@John Ok thank you. Thats kind of how I already do it, but just not woth craft.

Hi All,

Thanks for the update - looking at the craft demos:

First 3D Planet - looks great but ran into couple of snags - first zooming into planet goes below surface to show clear sky and all the dark-side objects, not sure but they may be facing upward - shouldn’t we see inside of sphere? Seems to lock on the z(y?) axis can zoom out but not further in.

Also when running step 1 just zooms into surface and doesn’t show the models or allow movement - am I missing something?

Second assets - I’ve seen blank items in the resources list but they are obviously there as we can see them in the demo. The only one that shows icons is the UI resource. Do we need to install/sync these in some way?

Thirdly Voxel Terrain - works fine on my iPad Pro but partially runs then bombs out to iOS on my iPad 2. Is there a memory limitation or clash with OpenGL?

@Bri_G Voxel Terrain works just fine on my 16GB iPad Air. I’m on iOS 10.3.1 and I haven’t uploaded version 10.3.2 yet. I usually wait a week or so before I update to the latest iOS just to see if anyone reports problems.

@dave1707

iPad Pro (9.7). iOS 10.3.2 128GB
iPad 2 iOS 9.3.5 32GB

@Bri_G Maybe there’s a difference in the iOS. It seems to work OK on the 10.3 versions. If you haven’t run into any problems with iOS 10.3.2, maybe I’ll update both my iPads.

@XanDDemoX Nice. I’ll have to look into those bugs. Do you have a minimal project that causes the crash? The metatable equality check is probably having issues as the metatable is its own metatable (so it’s actually trying to do the quat equality check :open_mouth:).
I can add native support for multiplying quats and vec3’s though.

@GR00G0 Fair enough. Craft is still in beta and a lot of these features are still in development so certain things just aren’t possible at the moment.

@Bri_G The Planet3D demo definitely has a few issues like that. You can’t see the inside purely because the material it renders with is one-sided. Two-sided materials are something I’ll add in future if anyone needs it.
The ‘steps’ feature is supposed to allow you to view different progressive examples, but it’s broken at the moment as you can’t reset the whole scene at once.
The Voxel Terrain demo is very memory intensive, however I think it is using more than it should so I’ll look into that. I wouldn’t expect it to run any better than Minecraft does as it’s not nearly as optimised.

@John Ahhh I should have guessed :smiley: Native support for multiplying to rotate vec3s by quat would certainly be handy.

It could also be useful for building/generating meshes to be able to obtain a rotation matrix directly from a quat. Although currently there is already a couple of options, obtain with matrix.rotate and quat.angles or directly as below.

-- http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/geometric/orthogonal/index.htm
-- http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/
function quaternionToMatrix(q)
    local x,y,z,w = q.x,q.y,q.z,q.w
    
    local xs2 = 2*(x*x)
    local ys2 = 2*(y*y)
    local zs2 = 2*(z*z)
    
    local xy2 = 2*(x*y)
    local xz2 = 2*(x*z)
    local yz2 = 2*(y*z)
    
    local wx2 = 2*(w*x)
    local wy2 = 2*(w*y)
    local wz2 = 2*(w*z)
    
    return matrix(
        1-ys2-zs2,xy2-wz2,xz2+wy2,0,
        xy2+wz2,1-xs2-zs2,yz2-wx2,0,
        xz2-wy2,yz2+wx2,1-xs2-ys2,0,
        0,0,0,1
    )
end

I think the crash is another occurrence of invalid parameters in update although it shouldn’t even be called in the following situation. A print is not output suggesting it isn’t but I can affect whether the crash happens by changing variable initialisation to before or after the assert.

Crashing code

function setup()
    assert(false)
    angle = 0
end

function update()
    print("print")
    local q = quat.eulerAngles(0,0,angle)
end

Working code

function setup()
    angle = 0
    assert(false)
end

function update()
    local q = quat.eulerAngles(0,0,angle)
end

I also noticed that Codea doesn’t halt immediately when an assert is in update.

function setup()
end

function update()
    print("once not twice")
    assert(false)
end

Output

once not twice
Main:11: assertion failed!
stack traceback:
	[C]: in ?
	[C]: in function 'assert'
	Main:11: in function 'update'
once not twice
Main:11: assertion failed!
stack traceback:
        [C]: in ?
	[C]: in function 'assert'
	Main:11: in function 'update'

Edit: Just found another strange one. The entity instance returned by a raycast result doesn’t work as a key to lookup a value in a table.

function setup()
    entity = craft.entity()
    local size = vec3(20,1,20)
    local renderer = entity:add(craft.renderer,craft.mesh.cube(size))
    renderer.material = craft.material("Materials:Standard")
    renderer.material.map = readImage("Blocks:Brick Red")
    
    entity.position = vec3(0,0,0)
    
    local body = entity:add(craft.rigidbody,STATIC)
    entity:add(craft.shape.box, size, vec3(0,0,0))
    craft.scene.camera.rotation = quat.eulerAngles(45,0,180)
    craft.scene.camera.position = vec3(0,40,25)
    
    tbl = {}
    tbl[entity] = entity
end

function update()
    
end

function touched(touch)
    if touch.state ~= BEGAN then return end
    local pos,dir = craft.camera.main:screenToRay(vec2(touch.x,touch.y))
    local result = craft.physics:raycast(pos,dir,100)
    if not result then return end
    print(result.entity == entity) -- ok
    print(tbl[entity]) -- ok
    print(tbl[result.entity]) -- nil, should be entity.
    for k,v in pairs(tbl) do
        print(k == entity) -- ok
        print(v == entity) -- ok 
        print(k == result.entity) -- ok
        print(v == result.entity)  -- ok
    end
end

@GR00G0 I’m not sure what you were after in the other discussion, but here’s your program from there where I modified it a little.

displayMode(FULLSCREEN)

function setup()
    craft.scene.camera.position = vec3(0,0,5)
    craft.scene.camera.rotation = quat.eulerAngles(0,0,180)

    sphere = craft.entity()
    renderer = sphere:add(craft.renderer,craft.mesh.icoSphere(1,5,true))
    renderer.material = craft.material("Materials:Standard")

    for i = 0, renderer.mesh.vertexCount-1 do
        if noise(renderer.mesh:position(i)) <-.2 then
            renderer.mesh:color(i,0,0,255,255)
        elseif noise(renderer.mesh:position(i)) <.4 then
            renderer.mesh:color(i,255,0,255,255)
        else
            renderer.mesh:color(i,0,255,0,255)
        end
        renderer.mesh:position(i,getpos(renderer.mesh:position(i)))
    end
end

function update()
    sphere.rotation = quat.eulerAngles(10*ElapsedTime,20*ElapsedTime,10*ElapsedTime)
end

function getpos(x,y,z)
    return vec3(x,y,z)
end

Grrrrrrrr, Codea just deleted many of the tabs in my current project! (of course i had not made a backup). I’ve reported this before, once there are a lot of tabs there is a risk that the ones on the right disappear. @Simeon this really needs to be fixed urgently or at least limit the number of tabs allowed to a safe number.

@piinthesky Was this just using an iPad or were you using Aircode when it happened.

ipad pro 12.9 directly without aircode

@piinthesky Thanks for the info. Apparently they haven’t figured out why it’s happening or fixed it yet. Back in Sept 2016 you had a similar problem and posted about it, but you still didn’t create backups. It doesn’t take a lot of time to long press a project and create a duplicate before making changes, but it’s a little late now. I’ll let @Simeon or @John respond to this to see if they have any idea why, but my suggestion to everyone is to make backups of any project you’re updating until a fix is found.