mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
99.32k stars 35.12k forks source link

Add transparent flag to convert_obj_three.py #1089

Closed powerslave closed 12 years ago

powerslave commented 12 years ago

I was wondering why, when converting models with convert_obj_three.py, materials with mapAlpha don't have the transparent flag set. As far as I see it, mapAlpha textures are only rendered transparent when this flag is set. So why not add the transparent flag directly to the resulting model.js file whenever a mapAlpha is set, instead of setting it in the main script in the JSON callback?

I patched my convert_obj_three.py with

# Alpha texture
# map_d texture_alpha.png
if chunks[0] == "map_d" and len(chunks) == 2:
    materials[identifier]["mapAlpha"] = texture_relative_path(chunks[1])
    materials[identifier]["transparent"] = True  ## added this line

and for me it seems to do what I'd expect. The output .js simply has the transparent flag added.

"materials": [ {
...
"mapAlpha" : "earth_clouds_1024.png",
"mapDiffuse" : "earth_clouds_1024.png",
"opticalDensity" : 1.0,
"specularCoef" : 0.0,
"transparency" : 1.0,
"transparent" : true  // transparent flag is added
},

Is this a good idea, or is there a reason why the transparent flag is not set "automatically"?

mrdoob commented 12 years ago

The idea sounds good to me. Not sure if @alteredq tried this already though.

alteredq commented 12 years ago

We don't use alpha textures. It's one of the things from OBJ / MTL that is simply not implemented on our side.

It just gets passed on to JSON (in case somebody would want to handle it by their own) but there is no correspondent thing in standard materials, it'll just get ignored.

mrdoob commented 12 years ago

Ah, I thought the mapAlpha was like a flag that indicated that the diffuse texture had transparency. Closing this one then.

alteredq commented 12 years ago

Yup, alpha map is grayscale image for handling transparency separately from the diffuse map. Kinda redundant as you can bake alpha directly into the diffuse map.

It can make sense in some scenarios, but so far there was no real need to implement this feature.

powerslave commented 12 years ago

I still think it would be a good idea to add the flag.

I'd say one can assume that whenever a diffuse texture is used that contains alpha information, the texture should be rendered in a transparent way. Do you agree on that?

currently, convert_obj_three.py outputs

    "illumination" : 4,
    "mapAlpha" : "TEXTURE_WITH_TRANSPARENCY.png",
    "mapDiffuse" : "TEXTURE_WITH_TRANSPARENCY.png",
    "opticalDensity" : 1.0,
    "specularCoef" : 20.0,
    "transparency" : 1.0

will result in a material that does NOT render the texture transparent.

when adding the transparent flag

    "illumination" : 4,
    "mapAlpha" : "TEXTURE_WITH_TRANSPARENCY.png",
    "mapDiffuse" : "TEXTURE_WITH_TRANSPARENCY.png",
    "opticalDensity" : 1.0,
    "specularCoef" : 20.0,
    "transparency" : 1.0,
    "transparent" : true

the texture is rendered WITH transparency.

I therefore still suggest to add the transparent flag to convert_obj_three.py, because

Note that, as alterdeq mentioned, the mapAlpha is completely out of the equation here. convert_obj_three.py adds the mapAlpha property whenever a texture with transparency information is found, however the property is ignored for rendering. The "transparent" property is necessary to tell mapDiffuse to be rendered transparent.