One of the interesting things that I found about craft.camera ——post effects.
It offers three post effects:
craft.bloomEffect()
craft.depthOfFieldEffect()
craft.selectionEffect()
the third one I don’t know how to use, and the other two are great, but there’s not a lot of discussion here about post effects, probably due to the lack of documentation, I only found two posts about the Craft Post Effect:
https://codea.io/talk/discussion/9849/is-it-possible-to-turn-on-the-bloo
https://codea.io/talk/discussion/12392/how-to-add-post-processing#latest
But it’s really interesting,
here’s the sample code:
-- CraftCameraTest
function setup()
-- Create a new craft scene
scene = craft.scene()
scene.ambientColor = color(218, 158, 79)
scene.sky.active = false
-- Setup camera and lighting
scene.sun.rotation = quat.eulerAngles(125, 125, 0)
pb = craft.bloomEffect()
pd = craft.depthOfFieldEffect()
ps = craft.selectionEffect()
parameter.boolean("enable",true, function() pb.enabled = enable end)
parameter.integer("iterations",1,100,5, function() pb.iterations =iterations end)
parameter.number("threshold",-1.0,3.0,2.0,function() pb.threshold = threshold end)
parameter.number("softThreshold",-1.0,3.0,1.5,function() pb.softThreshold = softThreshold end)
parameter.number("intensity", 0, 10, 0.06,function() pb.intensity = intensity end)
-- Helper class for interactive camera
myViewer = scene.camera:add(OrbitViewer, vec3( 2.0, 0.5, 0.5), 8, 1, 400)
myViewer.rx = 45
myViewer.ry = -45
c = scene.camera:get(craft.camera)
c.hdr = true
c.colorTextureEnabled = true
c:addPostEffect(pb)
c:addPostEffect(ps)
myChest = Chest()
end
function update(dt)
-- Update the scene (physics, transforms etc)
scene:update(dt)
myChest:update()
end
-- Called automatically by codea
function draw()
update(DeltaTime)
scene:draw()
end
Chest = class()
function Chest:init()
e = self.entity
self.base = scene:entity()
self.base.parent = e
self.base.position = vec3(0.5, 0.3, 0.5)
local r1 = self.base:add(craft.renderer, craft.model.cube(vec3(0.8,0.6,0.8)))
r1.material = craft.material(asset.builtin.Materials.Specular)
r1.material.diffuse = color(133, 79, 30)
self.root = scene:entity()
self.root.position = vec3(1.5, 0.3, 0.5)
local r0 = self.root:add(craft.renderer, craft.model.cube(vec3(0.8,0.2,0.8),vec3(0.3,0.1,0.4)))
self.top = scene:entity()
self.top.parent = self.root
self.top.position = vec3(0.1, 0.6, 0.1)
local r2 = self.top:add(craft.renderer, craft.model.cube(vec3(0.8,0.2,0.8),vec3(0.4,0.1,0.4)))
r2.material = craft.material(asset.builtin.Materials.Specular)
r2.material.diffuse = color(66, 47, 30, 255)
self.angle = 0
self.y = 0.6
self.ball = scene:entity()
self.ball.parent = self.root
self.ball.position = vec3(0, 0.3, 0.5)
local r3 = self.ball:add(craft.renderer, craft.model.icosphere(0.2))
r3.material = craft.material(asset.builtin.Materials.Specular)
r3.material.diffuse = color(133, 124, 30)
self.obj = scene:entity()
self.obj.parent = self.root
self.obj.position = vec3(1.5, 0.3, 0.5)
self.obj.scale = vec3(0.5, 0.5, 0.5)
local r4 = self.obj:add(craft.renderer, craft.model(asset.builtin.Primitives.RoundedCube))
r4.material = craft.material(asset.builtin.Materials.Specular)
r4.material.diffuse = color(30, 133, 101)
self.floor = scene:entity()
self.floor.parent = e
self.floor.position = vec3(0.1, 0.6, 0.1)
local r5 = self.floor:add(craft.renderer, craft.model.plane(vec2(1.2,1.2),vec3(0.6,0.0,0.6)))
r5.material = craft.material(asset.builtin.Materials.Specular)
r5.material.diffuse = color(46, 66, 30)
touches.addHandler(self, -1, false)
end
function Chest:update()
self.root.rotation = quat.eulerAngles(0, 0, self.angle)
self.top.rotation = quat.eulerAngles(0, 0, self.angle*8)
self.ball.rotation = quat.eulerAngles(0, 0, self.angle)
self.obj.rotation = quat.eulerAngles(0, 0, self.angle*10)
self.floor.rotation = quat.eulerAngles(0, 0, self.angle)
scene.debug:line(vec3(0.1, 0.6, 0.1-1),vec3(0.1, 0.6, 0.1+1),color(47, 239, 5))
scene.debug:line(vec3(1.5, 0.3, 0.5-1),vec3(1.5, 0.3, 0.5+1),color(239, 5, 5))
local a = self.ball.worldPosition + vec3(0,0,-1)
local b = self.ball.worldPosition + vec3(0,0,1)
scene.debug:line(a, b,color(239, 5, 199))
local a = self.top.worldPosition + vec3(0,0,-1)
local b = self.top.worldPosition + vec3(0,0,1)
scene.debug:line(a, b,color(16, 239, 5))
local a = self.obj.worldPosition + vec3(0,0,-1)
local b = self.obj.worldPosition + vec3(0,0,1)
scene.debug:line(a, b,color(239, 233, 5))
end
function Chest:interact()
if not self.open then
self.open = true
tween(1.6, self, {angle = 90, y=10}, {easing=tween.easing.backOut,loop = tween.loop.once })
else
self.open = false
tween(1.6, self, {angle = 0, y=0.6}, tween.easing.cubicIn)
end
end
function Chest:touched(touch)
if touch.state == BEGAN then
self:interact()
end
end
Here is the demo videos: