Project that tries to apply and remove a material from a Craft Orc model:
-- setup function
function setup()
--make scene
craft.scene.main = craft.scene()
local scene = craft.scene.main
scene.sky.material.sky = color(235, 158, 72)
scene.sky.material.horizon = color(66, 40, 30)
scene.sky.material.ground = color(130, 50, 50)
--make entity
orcEntity = scene:entity()
orcEntity.model = craft.model(asset.builtin.Blocky_Characters.Orc)
--make camera
camView = scene.camera:add(OrbitViewer,vec3(0,9,0), 50, 0,1000)
camView.rx = 20
camView.ry = -180
--controls for adding/removing material
parameter.action("Add Material to Orc", function()
addMaterialToOrc()
end)
parameter.action("Remove Material from Orc", function()
removeMaterialFromOrc()
end)
end
function addMaterialToOrc()
if orcEntity then
local newMaterial = craft.material(asset.builtin.Materials.Specular)
newMaterial.diffuse = color(255, 0, 0) -- Example: Set to red color
orcEntity.material = newMaterial
end
end
function removeMaterialFromOrc()
if orcEntity then
orcEntity.material = nil -- Reset to default material
end
end
function draw()
end
function touched(touch)
touches.touched(touch)
end
…currently crashes upon pressing the “Remove Material” button.
How would I make it work correctly? I want to get the Orc texture back when I remove the material.