How do I remove a material from a Craft entity?

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.

Is it possible to make a blank material that does nothing but replace another material, while applying no effects itself?

Would the getMaterial and setMaterial functions help?

Could you make your grids out of voxels?!

@piinthesky are you addressing both my recent posts in one message here? :slight_smile:

I think the relevant function would be entity.remove(craft.material)… which in my experience either does nothing or crashes.

As to my other post about voxels: as a matter of fact the raycast does work on voxels, when I look at the code it seems like it’s not actually detecting the grid entity directly at all.

Exactly, i guess the grids dont behave with voxel raycast as they are not voxel entities. This was why i suggested to construct the grids using voxels.

@piinthesky we should continue this in the post actually related to the grid, ok?