Hello there, I have built the following script.
The idea is somewhat simple :
- I loop over each element with a specific class (here
easterEgg
) - I map an event listener for
click
on each of this element - The user click on it
- I get an emoji from the attribute of the clicked elem
- I calculate the real coordinate of the click (
pixelX
andpixelY
), here I debug this position withdrawHtmlSquare
and the calculated coordinate are good - With this coordinate, I place my emoji texture (created with
createEmojiTexture()
) in the same position but in webgl, like so when the user click on a word the emoji appear just below the cursor
My problem is specifically here, I don’t find the way to get the right coordinate.
I have tried many things (latest attempts are visible here).
The most close result that I have is with :
const calcx = pixelX / (window.innerHeight / 2) - 1;
const calcy = - pixelY / (window.innerWidth / 2) + 1;
const cunproj = new THREE.Vector3(calcx, calcy, -1).unproject(camera);
emojiMesh.position.set(cunproj.x, cunproj.y, -1);
But this is not very good, in the following img I map the click (red square) to the result (emoji) :
Another problem that I have is when I want to use the vector directly in the position.set
like so - with that nothing appear on my screen :
const calcx = pixelX / (window.innerHeight / 2) - 1;
const calcy = - pixelY / (window.innerWidth / 2) + 1;
const cunproj = new THREE.Vector3(calcx, calcy, -1).unproject(camera);
emojiMesh.position.set(cunproj);
I’m pretty much a noob in webgl/three.js, so any idea is welcomed !