Question

Summary

I'm trying to apply a displacement map (Height map) to a rather simple object (Hexagonal plane) and I'm having some unexpected results. I am using grayscale and as such, I was under the impression my height map should only be affecting the Z values of my mesh. However, the displacement map I've created stretches the mesh across the X and Y planes. Furthermore, it doesn't seem to use the UV mapping I've created that all other textures are successfully applied to.

Model and UV Map

Here are reference images of my hexagonal mesh and its corresponding UV map in Blender.

Hexagonal Model and UV Map

Diffuse and Displacement Textures

These are the diffuse and displacement map textures I am applying to my mesh through Three.JS.

Hexagonal Diffuse and Displacement Textures

Renders

When I render the plane without a displacement map, you can see that the hexagonal plane stays within the lines. However, when I add the displacement map it clearly affects the X and Y positions of the vertices rather than affecting only the Z, expanding the plane well over the lines.

Hexagonal Plane Renders -- With & Without Displacement Map

Code

Here's the relevant Three.js code:

    // Textures
    var diffuseTexture = THREE.ImageUtils.loadTexture('diffuse.png', null, loaded);
    var displacementTexture = THREE.ImageUtils.loadTexture('displacement.png', null, loaded);

    // Terrain Uniform
    var terrainShader = THREE.ShaderTerrain["terrain"];
    var uniformsTerrain = THREE.UniformsUtils.clone(terrainShader.uniforms);

  //uniformsTerrain["tNormal"].value = null;
  //uniformsTerrain["uNormalScale"].value = 1;

    uniformsTerrain["tDisplacement"].value = displacementTexture;
    uniformsTerrain["uDisplacementScale"].value = 1;

    uniformsTerrain[ "tDiffuse1" ].value = diffuseTexture;
  //uniformsTerrain[ "tDetail" ].value = null;
    uniformsTerrain[ "enableDiffuse1" ].value = true;
  //uniformsTerrain[ "enableDiffuse2" ].value = true;
  //uniformsTerrain[ "enableSpecular" ].value = true;

  //uniformsTerrain[ "uDiffuseColor" ].value.setHex(0xcccccc);
  //uniformsTerrain[ "uSpecularColor" ].value.setHex(0xff0000);
  //uniformsTerrain[ "uAmbientColor" ].value.setHex(0x0000cc);

  //uniformsTerrain[ "uShininess" ].value = 3;
  //uniformsTerrain[ "uRepeatOverlay" ].value.set(6, 6);

    // Terrain Material
    var material = new THREE.ShaderMaterial({
        uniforms:uniformsTerrain,
        vertexShader:terrainShader.vertexShader,
        fragmentShader:terrainShader.fragmentShader,
        lights:true,
        fog:true
    });

    // Load Tile
    var loader = new THREE.JSONLoader();
    loader.load('models/hextile.js', function(g) {
      //g.computeFaceNormals();
      //g.computeVertexNormals();
        g.computeTangents();
        g.materials[0] = material;

        tile = new THREE.Mesh(g, new THREE.MeshFaceMaterial());
        scene.add(tile);
    });

Hypothesis

I'm currently juggling three possibilities as to why this could be going wrong:

  1. The UV map is not applying to my displacement map.
  2. I've made the displacement map incorrectly.
  3. I've missed a crucial step in the process that would lock the displacement to Z-only.

And of course, secret option #4 which is none of the above and I just really have no idea what I'm doing. Or any mixture of the aforementioned.

Live Example

You can view a live example here.

If anybody with more knowledge on the subject could guide me I'd be very grateful!

Edit 1: As per suggestion, I've commented out computeFaceNormals() and computeVertexNormals(). While it did make a slight improvement, the mesh is still being warped.

Was it helpful?

Solution

In your terrain material, set wireframe = true, and you will be able to see what is happening.

Your code and textures are basically fine. The problem occurs when you compute vertex normals in the loader callback function.

The computed vertex normals for the outer ring of your geometry point somewhat outward. This is most likely because in computeVertexNormals() they are computed by averaging the face normals of each neighboring face, and the face normals of the "sides" of your model (the black part) are averaged into the vertex normal calculation for those vertices that make up the outer ring of the "cap".

As a result, the outer ring of the "cap" expands outward under the displacement map.

EDIT: Sure enough, straight from your model, the vertex normals of the outer ring point outward. The vertex normals for the inner rings are all parallel. Perhaps Blender is using the same logic to generate vertex normals as computeVertexNormals() does.

vertex normals

OTHER TIPS

The problem is how your object is constructed becuase the displacement happens along the normal vector.

the code is here.

https://github.com/mrdoob/three.js/blob/master/examples/js/ShaderTerrain.js#L348-350

"vec3 dv = texture2D( tDisplacement, uvBase ).xyz;",

This takes a the rgb vector of the displacement texture.

"float df = uDisplacementScale * dv.x + uDisplacementBias;",

this takes only red value of the vector becuase uDisplacementScale is normally 1.0 and uDisplacementBias is 0.0.

"vec3 displacedPosition = normal * df + position;",

This displaces the postion along the normal vector.

so to solve you either update the normals or the shader.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top