Hi there!
In my application I want to change plane geometry vertices position by moving mouse over it but still be capable of raycasting on it and get the point on the changed plane.
For now it was done by changing the displacement map texture, which is canvas where user drawing when moving mouse over the plane. But when i`m raycasting on the newly created “mountains” I receive to intersections. After reading other question from forum I understand i need to change vertices positions too. Unfortunately answer from questions is depricated, since geometry.vertices is no longer in use.
here is a demo of my current version
And here is the code how i change displacement map based on canvas element for those how can`t access the demo sandbox.
function height2normal(context, contextN) {
const width = 128;
const height = 128;
const src = context.getImageData(0, 0, width, height);
const dst = contextN.createImageData(width, height);
for (let i = 0, l = width * height * 4; i < l; i += 4) {
let x1, x2, y1, y2;
if (i % (width * 4) === 0) {
// left edge
x1 = src.data[i];
x2 = src.data[i + 4];
} else if (i % (width * 4) === (width - 1) * 4) {
// right edge
x1 = src.data[i - 4];
x2 = src.data[i];
} else {
x1 = src.data[i - 4];
x2 = src.data[i + 4];
}
if (i < width * 4) {
// top edge
y1 = src.data[i];
y2 = src.data[i + width * 4];
} else if (i > width * (height - 1) * 4) {
// bottom edge
y1 = src.data[i - width * 4];
y2 = src.data[i];
} else {
y1 = src.data[i - width * 4];
y2 = src.data[i + width * 4];
}
dst.data[i] = x1 - x2 + 127;
dst.data[i + 1] = y1 - y2 + 127;
dst.data[i + 2] = 255;
dst.data[i + 3] = 255;
}
contextN.putImageData(dst, 0, 0);
}
// uv - intersection uv, point of intersection on the plane texture
function draw(uv) {
const { scene } = three();
const canvasD = document.getElementById('canvasD');
const contextD = canvasD.getContext('2d');
const canvasN = document.getElementById('canvasN');
const contextN = canvasN.getContext('2d');
const plane = scene.getObjectByName('terrain');
const material = plane.material;
contextD.fillStyle = '#FFFFFF';
contextD.fillRect(uv.x * 128, 128 - uv.y * 128, 2, 2);
material.needsUpdate = true;
material.displacementMap.needsUpdate = true;
height2normal(contextD, contextN);
material.normalMap.needsUpdate = true;
}
Please help me understand how to make geometry vertices positions same value as displacement map has.
Thank you in advance!