Jump to content

How to remove/reset displacement map on mesh


RobS
 Share

Recommended Posts

Hi,

I've been messing with mesh.applyDisplacementMapFromBuffer() using a separate canvas to create a dynamic image (just messing with gradients at the moment). I'm hoping to animate the result but currently when you apply applyDisplacementMapFromBuffer() repeatedly it adds the new displacement map to the existing displaced map so things get a bit crazy looking. So basically I need something to call to before applyDisplacementMapFromBuffer() that restores the mesh to it's original state. I've looked at setVerticesData, updateVerticesData, setVertexBuffer and not got anywhere, once they're called the applyDisplacementMapFromBuffer has no effect.

Here's some vague example code:

var plane = BABYLON.Mesh.CreateGround("plane", 192, 108, 100, scene, true);
var originalVertices = plane.getVerticesData(BABYLON.VertexBuffer.PositionKind);

mergeCanvas = document.getElementById("mergemap")
var mergeCTX = mergeCanvas.getContext('2d');
var mw = mergeCanvas.width;
var mh = mergeCanvas.height;


engine.runRenderLoop(function() {

   // imagine some stuff happens here to draw into the mergeCTX

   var buffer = mergeCTX.getImageData(0, 0, mw, mh).data;

   // reset needs to happen here
   plane.updateVerticesData(BABYLON.VertexBuffer.PositionKind,originalVertices)

   plane.applyDisplacementMapFromBuffer(buffer, mw, mh, 0, 1);

});

Sorry for the fairly vague info but any pointers will be much appreciated.

Cheers,
Rob

Link to comment
Share on other sites

Typical, 5 minutes after posting I find a solution!

var plane = BABYLON.Mesh.CreateGround("plane", 192, 108, 100, scene, true);
var originalVertices = plane.getVerticesData(BABYLON.VertexBuffer.PositionKind).slice(0);

mergeCanvas = document.getElementById("mergemap")
var mergeCTX = mergeCanvas.getContext('2d');
var mw = mergeCanvas.width;
var mh = mergeCanvas.height;

var updatePositions = function (positions) {
	for (var idx = 0; idx < positions.length; idx++) {
		positions[idx] = originalVertices[idx]
	}
};

engine.runRenderLoop(function() {

	// imagine some stuff happens here to draw into the mergeCTX

	var buffer = mergeCTX.getImageData(0, 0, mw, mh).data;

	plane.updateMeshPositions(updatePositions);

	plane.applyDisplacementMapFromBuffer(buffer, mw, mh, 0, 1);

});

Is that the best way?

Cheers,
Rob

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...