Can’t get mtl for OBJ robot to work

@dave1707 thanks for sharing that, it’s great.

We basically went the same route conceptually—using lua’s os commands to get around codea’s limitations—but you’ve done it way better.

…and the next question is, now that we’ve got the .mtl working, is it possible to get it creating nicely rounded surfaces like it does in @yojimbo2000’s OBJ loader?

@dave1707 here it is. The various properties can be changed, not just the colour.

--viewer.mode=FULLSCREEN
function setup()
    assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
    scene = craft.scene()
    scene.sun.rotation=quat.eulerAngles(30,60,0)
    skyMaterial = scene.sky.material
    skyMaterial.horizon = color(255)
    scene.ambientColor = color(255)   
    m=scene:entity()    
    m.model = craft.model(asset.."robot.obj")
    m.position=vec3(0,0,0)
    v = scene.camera:add(OrbitViewer, vec3(0,3,0), 10, 0, 2000)
    v.rx=15
    v.ry=210
    
    nMesh=m.model.submeshCount
    print("number of meshes ", nMesh)
  
    local mat1=craft.material(asset.builtin.Materials.Standard)
    mat1.diffuse=color(13, 235, 220)
    mat1.roughness=0.6
    mat1.metalness=1.0
    m:get(craft.renderer):setMaterial(mat1, 1)
    
    local mat2=craft.material(asset.builtin.Materials.Standard)
    mat2.diffuse=color(108, 241, 7)
    mat2.roughness=0.5
    mat2.metalness=1.0
    m:get(craft.renderer):setMaterial(mat2, 2)

    local mat3 = craft.material(asset.builtin.Materials.Specular) 
    mat3.diffuse = color(239, 23, 70)
    mat3.roughness=0.7
    mat3.metalness=0.5 
    m:get(craft.renderer):setMaterial(mat3, 3)
    
    local mat4 = craft.material(asset.builtin.Materials.Specular) 
    mat4.diffuse = color(128, 152, 106)
    mat4.roughness=0.7
    mat4.metalness=0.5 
    m:get(craft.renderer):setMaterial(mat3, 4)
end

function update(dt)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw()    
end

@piinthesky Thanks for the update. That’s kind of what I was able to do with just one of the mats, but since I couldn’t get the color to change I thought I was doing something wrong. The color values (Ka) would change in the .mtl file, but it’s like I didn’t have control of it and I couldn’t figure why it changed. Is it final or is there something that isn’t being done right to change the color. I’ll play around with this and see what I can or can’t come up with.

@dave1707 the colour does change, via the ‘diffuse’, so i don’t understand what is the problem?

@UberGoober if you apply the CalculateAverageNormals function in @yojimbo2000 obj loader code, you can get a sort of smoothed surface:

    vertices=m.model.positions
    m.model.normals=CalculateAverageNormals(vertices)

All,

My contribution to playing around with the robot is attached (hope it runs OK).

Not to be a dweeb but:

This is awesome how everybody’s contributing to making this one thing cooler!

@Bri_G here’s my contribution to your version. I added the code like @piinthesky suggested, so it can use @yojimbo2000’s approach to averaging the normals, and I renamed the variables to be descriptive (which I think is especially helpful in knowing which colors you’re adjusting with which control), and I made it so the color controls look like they’re the same as the colors being used on the model right from the start, and I added a verrry slight color tint to the horizon that shouldn’t bother anyone, and I made a little color tweak to the robot’s palms so they don’t just look like they’re the black of the eternal void.

And of course, again, much thanks to @dave1707 for figuring out the best way to get .mtl files into project assets.

[[also attached, just some colors I played around with—it’s really fun to play with them]]

OK All,

Bit off the wall and would probably take me weeks fiddling, but - this demo could develop into a cool tool. Take a basic non-coloured model, analyse to get the component parts (triangles) then list them. Select one and add glow to it, then for that component select and define the colour. When the model is fully painted write out an mtl file for it.

Major work there.

@UberGoober - neat update, think the smoothing works in some areas and not in others but thanks for including it, nice to see it working.

@Bri_G yeah the smoothing’s a bit funky, maybe someone who understands that stuff better can fix it… I assume it’s down to how meshes treat normals vs how craft models treat normals… you don’t know the difference by any chance?

@UberGoober - I think it’s simpler than that. Each models built up of different sections. Some sections are significantly different to others, so if you do a global average the overlaps between sections get distorted.

@Bri_G you probably know more than me, but afaik normals are what shaders use to make facets look rounded, and they mainly do that by comparing themselves with their immediate neighbors.

In which case if, craft meshes sort “neighbors” into a slightly different order than regular meshes do, the normals will generate incorrect rounding some of the time.

I know that the issue of storing things in different orders comes up sometimes because I’ve seen others talk about it, I think they talk about something like “inverting indices”, or something like that.

If the normals also need inverting that might actually be fixable. But again I only just barely know what I’m talking about.

Here the robot code that uses one set of parameters (color, roughness, metalness) for all 4 mats. Actually, this should work for any number of mats. Just slide the mat parameter to select one of the four robot mats and alter the values.

@dave1707 sweet and short and sweetly short as usual. Good idea re-using the code.

Now if I had much better Craft chops than I do I’d use raytracing or something to automatically change the selected material whenever someone tapped it on the robot itself.

Do either of you guys happen to have something like that in your toolbox?