So basically, I have a cube with some texture on it. I want to add some text label/information on one side of the cube, most likely HTML. I want something like this
The only difference I’m trying to make is I want the sprite/label attached to one face of the cube, kindly tell me what should I use to make it attached to the face of the cube.
I wouldn’t build upon that example - it’s a custom solution and seems bugged for higher resolutions (see screenshot, label floats outside of the screen.)
Thank you for your example, but the problem coming is I want the HTML sticked to only side of the cube, so If I rotate the cube, the HTML disappears from camera sight. If there’s any way to do that kindly share.
You can try using CSS3DRenderer then, though mind it has quite severe limitations (you can have cubes and simple shapes, but won’t be able to use models etc.)
If you do need models + depth-tested HTML floating in 3D, I don’t know if there’s any way besides rendering said HTML onto a CanvasTexture, or using gimmicky tricks like in the example you linked:
First, add a CSS2DRenderer and render the HTML (it’s always rendered above the WebGLRenderer.)
In the WebGLRenderer, add a point in the 3D space at the same location as the HTML element.
On every frame, test whether the 3D point is occluded, if it is - hide the CSS2DObject (note it will work the same way as the example above, so mesh won’t really occlude parts of the HTML element, you’ll just be able to either hide it or unhide the HTML element entirely using opacity.)
This example works to an extent, all I want now is the label to not face the camera, basically stick to only one side/face of the cube, as of now If I rotate it the label keeps facing the camera. Is there any way I can do that?
With changing the css renderer from CSS2DRenderer to CSS3DRenderer and slight modifications of creation of html-element and its css, you’ll get this result.
@prisoner849
I have been playing around with your CSS2Renderer fiddle, thank you so much for the example!
One thing I noticed is that the position of the number icon breaks when we include responsive window resize, how can we solve this?
I was also wondering if you would like to explain the code snippet on line 55 and 56? Am I correct to assume that you are converting from 2D to 3D coordinates given the position of the 3D box?
Been trying to accomplish something similar to what you’ve produced by repurposing your code. I want HTML on every face.
I can work out the rotation angles and positions etc, but can’t get the div to appear on the outside for some reason.
For example, if I change the y rotation of the label to -0.5*pi, and change the label position to (-2,0.5,0) then the label appears in the correct place (i.e. the opposite face) but is only visible from the inside of the cube. I can’t figure out why - are you able to help at all?
Thanks!!
EDIT: looking back I can see it’s because of the way the whole display is produced, i.e. normal to side of label. Any way I can achieve labels on all sides? Thanks again.
I’m still figuring it out so this is not finished code, but the issue is visible here. Left side of cube is visible only from inside but I’m suspectful of having done something wrong somewhere.
EDIT:
Think I’ve kinda figured it out - except the back face still has an issue.
Face 6 disappears when rotating sometimes around the back but faces 1-5 seem to be fine. If I can fix that then I’m sorted.