Improved Area Lighting in WebGL & ThreeJS

NOTE: three.js now supports THREE.RectAreaLight. This answer pertains to legacy versions of three.js. Your approach of using the point that maximizes the dot product is fundamentally flawed, and not physically plausible. In your first illustration above, suppose that your area light consisted of only the left half. The “purple” point — the one that maximizes … Read more

How do you save an image from a Three.js canvas?

Since the toDataURL is a method of canvas html element, that will work for 3d context too. But you have to take care of couple of things. Make sure when the 3D context is initialized you set preserveDrawingBuffer flag to true, like so: var context = canvas.getContext(“experimental-webgl”, {preserveDrawingBuffer: true}); Then user canvas.toDataURL() to get the … Read more

Any way to get a bounding box from a three.js Object3D?

You don’t need to iterate over all children of the object; there’s a method in the library to do this: THREE.Box3#setFromObject: see the docs. For example, you can do: var bbox = new THREE.Box3().setFromObject(obj); to get the bounding box of obj, including all of its children, and accounting for any translations, rotations, etc. Note that … Read more

How to find the unicode of the subscript alphabet?

Take a look at the wikipedia article Unicode subscripts and superscripts. It looks like these are spread out across different ranges, and not all characters are available. Consolidated for cut-and-pasting purposes, the Unicode standard defines complete sub- and super-scripts for numbers and common mathematical symbols ( ⁰ ¹ ² ³ ⁴ ⁵ ⁶ ⁷ ⁸ … Read more

ThreeJS: Remove object from scene

I think seeing your usage for addEntity and removeEntity code would be helpful, but my first thought is are you actually setting the object.name? Try in your loader just before scene.add(object); something like this: object.name = “test_name”; scene.add(object); What might be happening is the default “name” for an Object3D is “”, so when you then … Read more

threejs how to rotate around object’s own center,instead of world center

If your mesh is not rotating around its center, it is because the geometry vertices are offset from the origin. You can automate repositioning by using a bounding box to define a reasonable center, and then offset the mesh’s position like so: var box = new THREE.Box3().setFromObject( mesh ); box.center( mesh.position ); // this re-sets … Read more

Using textures in THREE.js

By the time the image is loaded, the renderer has already drawn the scene, hence it is too late. The solution is to change texture = THREE.ImageUtils.loadTexture(‘crate.gif’), into texture = THREE.ImageUtils.loadTexture(‘crate.gif’, {}, function() { renderer.render(scene); }),