CesiumGS / obj2gltf

Convert OBJ assets to glTF
Apache License 2.0
1.71k stars 307 forks source link

OBJ Kd material attribute incorrectly converted to emission value #80

Open erich666 opened 7 years ago

erich666 commented 7 years ago

Download the OBJ version of this free model: https://www.turbosquid.com/FullPreview/Index.cfm/ID/691507

When converted by obj2gltf and displayed using three.js's gltf loader, I get the result https://s26.postimg.org/6maqtr28p/bad_gltf.jpg

The materials are all very bright, there is no shading. This occurs because all materials' Kd values are used to set the emission, not the diffuse value.

Here is a snippet of the original material, .mtl file:

newmtl canopy2 Ns 25 d 0.8 illum 2 Kd 0.8 0.796863 0.793726 Ks 0 0 0 Ka 0.3 0.298824 0.297647

This gets converted to:

"canopy2": {
  "technique": "technique1",
  "values": {
    "ambient": [
      0.3,
      0.298824,
      0.297647,
      1
    ],
    "diffuse": [
      0,
      0,
      0,
      1
    ],
    "emission": [
      0.8,
      0.796863,
      0.793726,
      0.8
    ],
    "specular": [
      0,
      0,
      0,
      1
    ],
    "shininess": 25,
    "transparency": 1
  }
},

Note how the emission is set, but diffuse is not. This gives the unshaded model view shown in https://s26.postimg.org/6maqtr28p/bad_gltf.jpg

My guess is that perhaps the "illum 2", which is officially "2 Highlight on", is causing this. In practice, I would ignore "illum 2" - people misuse it. Or, perhaps it's the "d 0.8" value, which should be interpreted as 20% transparent, another factor missing from the translation. Whatever the case, the diffuse color is missing, emission is set instead, and that's incorrect.

Wavefront OBJ materials are a nightmare to interpret, so expect more bugs along these lines.

lilleyse commented 7 years ago

I believe I know why this is happening...

The final result of obj2gltf is a model using the KHR_materials_common extension. It then passes that along to gltf-pipeline to create proper materials/shaders/techniques.

If the model doesn't contain normals it uses the CONSTANT lighting technique which only accepts emission and ambient sources link. So to honor this extension yet still show the colors that are part of the model, the diffuse color is placed into the emission color slot. It's a hack and I don't like it...

erich666 commented 7 years ago

As a data point, I believe the model does contain normals (I just generated it again with the "-n" option to make sure - that said, I won't guarantee it, since this software's unfamiliar to me). I recommend downloading the OBJ model and trying it out.