At least on my iPhone it’s currently broken, it isn’t playing well with newer paradigms.
This works:
require(asset.documents.Craft.Cameras)
-----------------------------------------
-- Models
-- Written by John Millard
-----------------------------------------
-- Description:
-- Domonstrates model loading in Craft.
-----------------------------------------
Tag = class()
function Tag:init(entity, name)
self.entity = entity
self.name = name
end
-- Use this function to perform your initial setup
function setup()
PrintExplanation()
viewer.mode = FULLSCREEN
scene = craft.scene()
packs =
{
asset.builtin.Blocky_Characters,
asset.builtin.Watercraft,
asset.builtin.RacingKit,
asset.builtin.SpaceKit,
asset.builtin.CastleKit,
asset.builtin.Primitives,
asset.builtin.Nature
}
-- Setup camera and lighting
scene.ambientColor = color(61, 61, 61, 255)
scene.sun:get(craft.light).intensity = 1.0
scene.sun.rotation = quat.eulerAngles(-45, 125, 0)
scene.sky.active = false
orbitViewer = scene.camera:add(OrbitViewer, vec3(0,0,0), 75, 10, 200)
orbitViewer.ry = 225
orbitViewer.rx = 45
AssetPack = 1
loadModels(packs[AssetPack])
parameter.watch("packs[AssetPack].name")
parameter.action("Next Pack", function()
AssetPack = math.min(AssetPack+1, #packs)
loadModels(packs[AssetPack])
end)
parameter.action("Previous Pack", function()
AssetPack = math.max(AssetPack-1, 1)
loadModels(packs[AssetPack])
end)
parameter.boolean("ShowBounds", false)
end
function loadModels(pack)
assets = {}
for _,v in pairs(pack.all) do
if v.type == "model" then
table.insert(assets, v)
end
end
if models then
for k,v in pairs(models) do
v:destroy()
end
end
-- Load models
models = {}
for k,v in pairs(assets) do
local model = scene:entity()
local mr = model:add(craft.renderer)
mr.model = craft.model(v)
-- Tag the model for later
model:add(Tag, v)
table.insert(models, model)
end
local x, z = 0,0
local maxRowDepth = 0
local center = vec3()
for k,model in pairs(models) do
local mr = model:get(craft.renderer)
local bounds = mr.model.bounds
model.x = x - bounds.max.x
model.z = z - bounds.offset.z
x = x - bounds.size.x - 2
maxRowDepth = math.max(maxRowDepth, bounds.size.z)
if x < -120 then
x = 0
z = z + maxRowDepth + 2
maxRowDepth = 0
end
center = center + model.position
end
if #models > 0 then
center = center / #models
orbitViewer.target = center
end
orbitViewer.target = center
end
function updateViewer()
local m = models[ModelNumber]
local b = m:get(craft.renderer).model.bounds
orbitViewer.target = m.position + b.center
end
function update(dt)
scene:update(dt)
if ShowBounds then
for k,v in pairs(models) do
local b = v:get(craft.renderer).model.bounds
b2 = bounds(b.min, b.max)
b2:translate(v.position)
scene.debug:bounds(b2, color(255,255,255,255))
end
end
end
-- This function gets called once every frame
function draw()
update(DeltaTime)
scene:draw()
end
function PrintExplanation()
output.clear()
print("Loading models is easy, simply create an entity and add a renderer component.")
print("Set the mesh using craft.model(asset)")
print("In this example we have used assetList to get all models from each asset pack")
print("Models will try to load a material if there is one, otherwise a blank material will be used.")
end
function touched(touch)
orbitViewer:touched(touch)
end