Old Vector and Matrix constructor functions would create all methods and
make assignments with each call. A lot of speed-ups can be made by using
shared properties through the prototype chain. Since methods and
initialized properties are shared among all instances they're only
created once.
Some tests can be seen at examples/performance-test.html
Some results:
Chrome
//6.4 times faster
performance-test.html:25 Vector2 x 5000000: 213ms
performance-test.html:30 Vector2Orig x 5000000: 1366ms
//8.3 times faster
performance-test.html:37 Vector3 x 5000000: 241ms
performance-test.html:42 Vector3Orig x 5000000: 2015ms
//3.8 times faster
performance-test.html:49 Vector4 x 5000000: 278ms
performance-test.html:54 Vector4Orig x 5000000: 1059ms
//7.5 times faster
performance-test.html:61 Matrix4 x 5000000: 937ms
performance-test.html:66 Matrix4Orig x 5000000: 7004ms
Firefox
//6.8 times faster
Vector2 x 50000: 45ms
Vector2Orig x 50000: 307ms
//11.5 times faster
Vector3 x 50000: 48ms
Vector3Orig x 50000: 550ms
//3.8 times faster
Vector4 x 50000: 61ms
Vector4Orig x 50000: 234ms
//9.4 times faster
Matrix4 x 50000: 199ms
Matrix4Orig x 50000: 1878ms
where "Orig" are old versions of the objects.
Running these tests multiple times can yeild different results due to
browser cache. However the results are always kind of similar in terms
of speedup.
three.js
Javascript 3D Engine
The aim of this project is to create a lightweight 3D engine with a very low level of abstraction (aka for dummies). Currently there is no documentation available but feel free to use the examples as a reference and/or read the source code. However, be aware that the API may change from revision to revision breaking compatibility.
The engine can render using <canvas> and <svg> and WebGL.
Other similar projects: pre3d, pvjs, jsgl, k3d, ...
Examples
Usage
Download the compressed library and include it in your html.
<script type="text/javascript" src="js/three.js"></script>
This code creates a camera, then creates a scene object, adds a bunch of random particles in it, creates a <canvas> renderer and adds its viewport in the document.body element.
<script type="text/javascript">
var camera, scene, renderer;
init();
setInterval( loop, 1000 / 60 );
function init() {
camera = new THREE.Camera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 1000;
scene = new THREE.Scene();
for (var i = 0; i < 1000; i++) {
var particle = new THREE.Particle( new THREE.ParticleCircleMaterial( Math.random() * 0x808008 + 0x808080, 1 ) );
particle.position.x = Math.random() * 2000 - 1000;
particle.position.y = Math.random() * 2000 - 1000;
particle.position.z = Math.random() * 2000 - 1000;
particle.scale.x = particle.scale.y = Math.random() * 10 + 5;
scene.addObject( particle );
}
renderer = new THREE.CanvasRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
}
function loop() {
renderer.render( scene, camera );
}
</script>
For creating a customised version of the library, including the source files in this order would be a good way to start:
<script type="text/javascript" src="js/three/Three.js"></script>
<script type="text/javascript" src="js/three/core/Color.js"></script>
<script type="text/javascript" src="js/three/core/Vector2.js"></script>
<script type="text/javascript" src="js/three/core/Vector3.js"></script>
<script type="text/javascript" src="js/three/core/Vector4.js"></script>
<script type="text/javascript" src="js/three/core/Rectangle.js"></script>
<script type="text/javascript" src="js/three/core/Matrix4.js"></script>
<script type="text/javascript" src="js/three/core/Vertex.js"></script>
<script type="text/javascript" src="js/three/core/Face3.js"></script>
<script type="text/javascript" src="js/three/core/Face4.js"></script>
<script type="text/javascript" src="js/three/core/Geometry.js"></script>
<script type="text/javascript" src="js/three/cameras/Camera.js"></script>
<script type="text/javascript" src="js/three/objects/Object3D.js"></script>
<script type="text/javascript" src="js/three/objects/Mesh.js"></script>
<script type="text/javascript" src="js/three/objects/Particle.js"></script>
<script type="text/javascript" src="js/three/objects/Line.js"></script>
<script type="text/javascript" src="js/three/materials/BitmapUVMappingMaterial.js"></script>
<script type="text/javascript" src="js/three/materials/ColorFillMaterial.js"></script>
<script type="text/javascript" src="js/three/materials/ColorStrokeMaterial.js"></script>
<script type="text/javascript" src="js/three/materials/FaceColorFillMaterial.js"></script>
<script type="text/javascript" src="js/three/materials/FaceColorStrokeMaterial.js"></script>
<script type="text/javascript" src="js/three/materials/ParticleBitmapMaterial.js"></script>
<script type="text/javascript" src="js/three/materials/ParticleCircleMaterial.js"></script>
<script type="text/javascript" src="js/three/scenes/Scene.js"></script>
<script type="text/javascript" src="js/three/renderers/Renderer.js"></script>
<script type="text/javascript" src="js/three/renderers/CanvasRenderer.js"></script>
<script type="text/javascript" src="js/three/renderers/SVGRenderer.js"></script>
<script type="text/javascript" src="js/three/renderers/WebGLRenderer.js"></script>
<script type="text/javascript" src="js/three/renderers/renderables/RenderableFace3.js"></script>
<script type="text/javascript" src="js/three/renderers/renderables/RenderableFace4.js"></script>
<script type="text/javascript" src="js/three/renderers/renderables/RenderableParticle.js"></script>
<script type="text/javascript" src="js/three/renderers/renderables/RenderableLine.js"></script>
Contributors
Thanks to the power of the internets (and github <3) these people have kindly helped out with the project.
Paul Brunt, Fernando Serrano, kikko.
Change Log
2010 07 12 - r13 (29.492 kb)
- Added
ParticleCircleMaterialandParticleBitmapMaterial Particlenow useParticleCircleMaterialinstead ofColorFillMaterialParticle.size>Particle.scale.xandParticle.scale.yParticle.rotation.zfor rotating the particleSVGRenderercurrently out of sync
2010 07 07 - r12 (28.494 kb)
- First version of the
WebGLRenderer(ColorFillMaterialandFaceColorFillMaterialby now) Matrix4.lookAtfix (CanvasRendererandSVGRenderernow handle the -Y)Colornow using 0-1 floats instead of 0-255 integers
2010 07 03 - r11 (23.541 kb)
- Blender 2.5 exporter (utils/export_threejs.py) now exports UV and normals (Thx kikko)
Scene.add>Scene.addObject- Enabled
Scene.removeObject - Removed
computeNormals()fromGeometry
2010 06 22 - r10 (23.959 kb)
- Changed Camera system. (Thx Paul Brunt)
Object3D.overdraw = trueto enable CanvasRenderer screen space point expansion hack.
2010 06 20 - r9 (23.753 kb)
- JSLinted.
autoClearproperty for renderers.- Removed SVG rgba() workaround for WebKit. (WebKit now supports it)
- Fixed matrix bug. (transformed objects outside the x axis would get infinitely tall :S)
2010 06 06 - r8 (23.496 kb)
- Moved UVs to
Geometry. CanvasRendererexpands screen space points (workaround for antialias gaps).CanvasRenderersupportsBitmapUVMappingMaterial.
2010 06 05 - r7 (22.387 kb)
- Added Line Object.
- Workaround for WebKit not supporting rgba() in SVG yet.
- No need to call updateMatrix(). Use .autoUpdateMatrix = false if needed. (Thx Gregory Athons).
2010 05 17 - r6 (21.003 kb)
- 2d clipping on
CanvasRendererandSVGRenderer clearRectoptimisations onCanvasRenderer
2010 05 16 - r5 (19.026 kb)
- Removed Class.js dependency
- Added
THREEnamespace Camera.x->Camera.position.xCamera.target.x>Camera.target.position.xColorMaterial>ColorFillMaterialFaceColorMaterial>FaceColorFillMaterial- Materials are now multipass (use array)
- Added
ColorStrokeMaterialandFaceColorStrokeMaterial geometry.faces.aare now indexes instead of references
2010 04 26 - r4 (16.274 kb)
SVGRendererParticle renderingCanvasRendererusescontext.setTransformto avoid extra calculations
2010 04 24 - r3 (16.392 kb)
- Fixed incorrect rotation matrix transforms
- Added
PlaneandCubeprimitives
2010 04 24 - r2 (15.724 kb)
- Improved
Colorhandling
2010 04 24 - r1 (15.25 kb)
- First alpha release














