Hi Everyone,
My first post and brand new to Babylon.js, I’m a 3D artist with some knowledge of html and css, but very little if none javascript.
Anyways on too my question, I have started with the default playground scene. which I have created a button with the following
var threeDButton = document.createElement("button");
threeDButton.className = "label-component";
threeDButton.className += " upper";
threeDButton.style.display = "block";
threeDButton.style.position = "absolute";
threeDButton.style.top = "0";
threeDButton.style.left = "0";
threeDButton.style.transform = "translate3d(50vw, 50px, 0px)";
var threeDButtonDot = document.createElement("span");
threeDButtonDot.className = "dot-circle";
threeDButton.appendChild(threeDButtonDot);
var threeDButtonText = document.createElement("span");
threeDButtonText.textContent = "Handle";
threeDButton.appendChild(threeDButtonText);
document.body.appendChild(threeDButton);
which I want to position the button onto a certain I want to say null object is 3d space. so I am trying to convert the vector3 of that object to screen space?
which I have found the follow code
var vertex2 = new BABYLON.Vector3();
var vertex2_world = BABYLON.Vector3.TransformCoordinates(vertex2, boxSml.getWorldMatrix());
scene.registerBeforeRender(function(vertex2_view) {
var vertex2_view = BABYLON.Vector3.Project(
vertex2_world, BABYLON.Matrix.Identity(),
scene.getTransformMatrix(),
camera.viewport.toGlobal(engine.getRenderWidth(true), engine.getRenderHeight(true))
);
threeDButton.addEventListener("click", () => {
console.log('vertex2_view');
console.log(vertex2_view);
});
});
of which you can see I have an EventListener which console.logs the vertex2_view. which seems to give me some sort of screen space x,y,z. but for some reason one click gives hundreds of the same, not sure why it doesn’t just give one per click? is there a reason why it give hundreds of lines of the same values.
anyways it not a major problem just wondering why, to test if the x,y,z where correct position, I inspected the button in the chrome inspector and done a little css with the value of the console.log for instance the following
transform: translate3d(calc(1279.5000014331736px - 50%) , calc(264.1972926830056px - 50%), 0);
which places the button exactly where I want it to be, So is this the best way to get to this point so far or is there a better way of doing this? But also how will I continue this to automatically send these x,y,z to my css transform: translate3d ?
Kind regards,
FireMe