Hello everyone
I have an height map and I want to get the location of the map in terms of mouse clicked on the map.
and here is my code and my PG:
https://playground.babylonjs.com/#XB49NT#2
engine.runRenderLoop(() => {
const data = ground.getVerticesData(BABYLON.VertexBuffer.PositionKind);
console.log("data",data);
});
var avgX;
var avgY;
var avgZ;
var avg;
var vertexData2;
var vertexData3;
var colors;
var colors2;
var index0;
var index1;
var index2;
var selPos;
var selInd;
var selected;
var selectedMesh;
canvas.addEventListener("click", function (e) {
var pickResult = scene.pick(scene.pointerX, scene.pointerY);
if (pickResult.hit) {
console.log("p", pickResult.hit);
selectedMesh = pickResult.pickedMesh.getIndices();
index0 = selectedMesh[pickResult.faceId * 3];
index1 = selectedMesh[pickResult.faceId * 3 + 1];
index2 = selectedMesh[pickResult.faceId * 3 + 2];
// console.log(vertexData.positions[index1]);
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].length; j++) {
x0 = data[i][3 * index0];
y0 = data[i][3 * index0 + 1];
z0 = data[i][3 * index0 + 2];
x1 = data[i][3 * index1];
y1 = data[i][3 * index1 + 1];
z1 = data[i][3 * index1 + 2];
x2 = data[i][3 * index2];
y2 = data[i][3 * index2 + 1];
z2 = data[i][3 * index2 + 2];
}
}
var minX = Math.min(x0, x1, x2);
var minY = Math.min(y0, y1, y2);
var minZ = Math.min(z0, z1, z2);
var maxX = Math.max(x0, x1, x2);
var maxY = Math.max(y0, y1, y2);
var maxZ = Math.max(z0, z1, z2);
var minVec = new BABYLON.Vector3(minX, minY, minZ);
var maxVec = new BABYLON.Vector3(maxX, maxY, maxZ);
avgX = (x0 + x1 + x2) / 3;
avgY = (y0 + y1 + y2) / 3;
avgZ = (z0 + z1 + z2) / 3;
avg = BABYLON.Vector3(avgX, avgY, avgZ);
console.log("v1", x0, y0, z0);
console.log("v2", x1, y1, z1);
console.log("v3", x2, y2, z2);
// console.log("avg", avgX, avgY, avgZ);
selPos = [x0, y0, z0, x1, y1, z1, x2, y2, z2];
selInd = [0, 1, 2];
colors = [1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1];
colors2 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
vertexData2 = new BABYLON.VertexData();
vertexData2.positions = selPos;
vertexData2.indices = selInd;
vertexData2.colors = colors;
vertexData2.applyToMesh(customMesh2);
console.log("s", selectedMesh);
var mat2 = new BABYLON.StandardMaterial("mat", scene);
mat2.wireframe = true;
customMesh2.material = mat2;
} else {
console.log("h", pickResult.hit);
vertexData3 = new BABYLON.VertexData();
vertexData3.positions = selPos;
vertexData3.indices = selInd;
vertexData3.colors = colors2;
vertexData3.applyToMesh(customMesh3);
console.log("s", selectedMesh);
var mat3 = new BABYLON.StandardMaterial("mat", scene);
mat3.wireframe = true;
customMesh3.material = mat3;
}
});