I am importing .obj files with textures but it seems like the lighting doesn’t update properly on these models. I have imported the same file in the editor and it works fine, so far I have been unable to find any differences between the two components using console.logging them (left is runtime import)
How can I solve this?
Can you check if your obj contains normals? It’s the vn attribute.
The obj parser will find and assign them if available, if not I don’t see it doing any normals calculations. I imagine the editor importer might be doing some additional things on imported models.
If that is the issue, there is a method in the engine to calculate normals on a mesh, if you would like to extend the obj parser to include it. All PRs are welcome:
var model = new pc.Model();
var groupNames = Object.keys(parsed);
var root = new pc.GraphNode();
// create a new mesh instance for each "group"
for (i = 0; i < groupNames.length; i++) {
var currentGroup = parsed[groupNames[i]];
if (!currentGroup.verts.length) continue;
if (currentGroup.verts.length > 65535) {
console.warn("Warning: mesh with more than 65535 vertices");
}
var indices = currentGroup.verts.map(function (x, i) { return i; }); // (((i/3)|0)*3)+[0,1,2][(i%3)]
var norms = pc.calculateNormals(currentGroup.verts, indices); // generate normals
var mesh = pc.createMesh(this._device, currentGroup.verts, {
normals: norms,
uvs: currentGroup.uvs
});
var mi = new pc.MeshInstance(new pc.GraphNode(), mesh, this._defaultMaterial);
model.meshInstances.push(mi);
root.addChild(mi.node);
}
model.graph = root;
model.getGraph().syncHierarchy();
return model;