MENU

Aligning HTML Elements to 3D

0
605
0

Sometimes you'd like to display some text in your 3D scene. You have many options each with pluses and minuses.

  • Use 3D text

    If you look at the primitives you'll see TextGeometry which makes 3D text. This might be useful for flying logos but probably not so useful for stats, info, or labelling lots of things.

  • Use a texture with 2D text drawn into it.

    The article on using a Canvas as a texture shows using a canvas as a texture. You can draw text into a canvas and display it as a billboard. The plus here might be that the text is integrated into the 3D scene. For something like a computer terminal shown in a 3D scene this might be perfect.

  • Use HTML Elements and position them to match the 3D

    The benefits to this approach is you can use all of HTML. Your HTML can have multiple elements. It can by styled with CSS. It can also be selected by the user as it is actual text.

Sometimes using three.js requires coming up with creative solutions. I'm not sure this is a great solution but I thought I'd share it and you can see if it suggests any solutions for your needs.

You can see a globe here that displays country name as HTML element.

https://codesandbox.io/s/threejs-html-elements-to-3d-4wrsw

2021-06-28_7-59-31

Meet Tony - https://linktr.ee/maapps

Read More ›

Three.js Texture Shadow

0
626
0

Shadows on computers can be a complicated topic. There are various solutions and all of them have tradeoffs including the solutions available in three.js.

Three.js by default uses shadow maps. The way a shadow map works is, for every light that casts shadows all objects marked to cast shadows are rendered from the point of view of the light. READ THAT AGAIN! and let it sink in.

In other words, if you have 20 objects, and 5 lights, and all 20 objects are casting shadows and all 5 lights are casting shadows then your entire scene will be drawn 6 times. All 20 objects will be drawn for light #1, then all 20 objects will be drawn for light #2, then #3, etc and finally the actual scene will be drawn using data from the first 5 renders.

It gets worse, if you have a point light casting shadows the scene has to be drawn 6 times just for that light!

For these reasons it's common to find other solutions than to have a bunch of lights all generating shadows. One common solution is to have multiple lights but only one directional light generating shadows.

Yet another solution is to use lightmaps and or ambient occlusion maps to pre-compute the effects of lighting offline. This results in static lighting or static lighting hints but at least it's fast.

Another solution is to use fake shadows. Make a plane, put a grayscale texture in the plane that approximates a shadow, draw it above the ground below your object.

Here is an example using fake shadow. It renders 15 spheres, but it's really fast with less resources used.

image

You can play with it in codesandbox - https://codesandbox.io/s/threejs-shadow-shadow-texture-0pvvg

In some apps it's common to use a round or oval shadow for everything but of course you could also use different shaped shadow textures. You might also give the shadow a harder edge. A good example of using this type of shadow is Animal Crossing Pocket Camp where you can see each character has a simple round shadow. It's effective and cheap. Monument Valley appears to also use this kind of shadow for the main character.

Tony - https://linktr.ee/maapps

Read More ›

Render Shadertoy in Three.js – Mushroom

Today I imported a shader created by "iq" from shadertoy, and rendered it into a beautiful mushroom ever in Three.js. The shocking thing is that it made with only programming codes. You can look through the source code here - https://codesandbox.io/s/threejs-shadertoy-mushroom-eyqel Here is my https://linktr.ee/maapps

Read More ›

WebGL 2.0 Fundamental – Rectangle

0
684
0
I draw a 2D rectangle on the 3D world by using pure WebGL 2.0 API.Play on CodeSandbox: https://codesandbox.io/s/webgl-fundamental-rectangle-o5kem More fascinating models will come soon…
Read More ›

WebGL 2.0 Fundamental – Triangle

0
667
0
I draw a 2D triangle on the 3D world by using pure WebGL 2.0 API.Play on CodeSandbox: https://codesandbox.io/s/webgl-fundamental-triangle-whpn0More fascinating models will come soon…
Read More ›