Shoud MTLLoader support displacement map?

It is my understanding that the MTL format supports the “dist” (displacement map) directive:

[MTL OBJ materials file]

In fact, it is very simple to implement it, and the only problem is how to pass the scale and bias parameters. Maybe using “-mm base gain”?

Using this example displacement map example

as a motivation and creating a ninjaHead_Low.mtl file for it (ao for map_kd is kind of brute force):

newmtl default
Ka 1 1 1
Kd 0.93 0.34 0.32
Ks 0.5 0.5 0.5
Ke 0.0 0.0 0.0
Ns 250
illum 3
map_kd ao.jpg
map_bump normal.png
disp -mm 4 -0.428408 displacement.jpg

then MTLLoader can be modified:

function setMapForType

case "disp":
          // Displacement texture map

          setMapForType("displacementMap", value);

          break;

and then in function getTextureParams

pos = items.indexOf("-mm");

    if (pos >= 0) {
      matParams.displacementScale = parseFloat(items[pos + 1]);
      matParams.displacementBias = parseFloat(items[pos + 2]);
      items.splice(pos, 3);

The result is very close to what I would like to see:

Anyway, it is just a suggestion…

1 Like

Yup, -mm sounds like a way to do that if you’d like to implement it and suggest as a PR (but the order is flipped in your code snippet unless I’m mistaken) :

-mm base gain

[...]

"base" adds a base value to the texture values.

That’s what displacementBias does, you just add flat value to displacement texture color.

"gain" expands the range of the texture values.  Increasing the number 
increases the contrast.  The default is 1; the range is unlimited.

That’s kinda what displacementScale does, since it multiplies the range - so setting gain to 0.0 will be equivalent to setting displacementScale to 0.0.

1 Like

For those who might be interested, I did bother customizing loaders and what not for my OBJ+MTL Viewer, including changing the ninja files.

Here is the ninja files and the picture of it in my viewer, not sure if everything looks as it should be but this zip file can be loaded as such in the viewer.

ninja - MeshPhongMaterial.zip (1009.4 KB)

ninja - MeshPhysicalMaterial.zip (1009.4 KB)

I guess that customization could also possibly be done as you guys suggested.

I parched MTLLoader:

https://krotalias.github.io/cwdc/13-webgl/examples/three/content/MTLLoader.js

and I am using this ninjaHead_Low.mtl file:

WaveFront *.mtl file (generated by Paulo Roma)

newmtl default
Ka 1 1 1
Kd 0.93 0.34 0.32
Ks 0.5 0.5 0.5
Ke 0.0 0.0 0.0
Ns 250
illum 3
map_kd ao.jpg
map_bump normal.png
disp -mm -0.428408 4 displacement.jpg

The result can be seen here:

https://krotalias.github.io/cwdc/13-webgl/examples/three/content/stl.html?controls=trackball&file=ninja/ninjaHead_Low.obj

If the image is black, just move it a little bit with the mouse.

The change I am suggesting is very simple, indeed.

If anyone one could take a look at it, I would appreciate.

Thanks.