> For the complete documentation index, see [llms.txt](https://bimwhale.gitbook.io/ifc-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bimwhale.gitbook.io/ifc-js/optimization/three.js-basics.md).

# Three.js Basics

Welcome to the Three.js ~~Fundamentals~~ basics. Now that we understand the basics of 3D grpahics, let's move onto Three.js.&#x20;

## Example Scene

Let's take a look at the [example scene](https://threejs.org/docs/index.html#manual/en/introduction/Creating-a-scene).

```markup
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>My first three.js app</title>
		<style>
			body { margin: 0; }
		</style>
	</head>
	<body>
		<script src="js/three.js"></script>
		<script>
			const scene = new THREE.Scene();
			const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

			const renderer = new THREE.WebGLRenderer();
			renderer.setSize( window.innerWidth, window.innerHeight );
			document.body.appendChild( renderer.domElement );

			const geometry = new THREE.BoxGeometry();
			const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
			const cube = new THREE.Mesh( geometry, material );
			scene.add( cube );

			camera.position.z = 5;

			const animate = function () {
				requestAnimationFrame( animate );

				cube.rotation.x += 0.01;
				cube.rotation.y += 0.01;

				renderer.render( scene, camera );
			};

			animate();
		</script>
	</body>
</html>
```

Let's remove the HTML and just focus on the JavaScript.&#x20;

```javascript
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
const cube = new THREE.Mesh( geometry, material );
scene.add( cube );
```

There are some concpets that might be new.

|                           |                                                                 |
| ------------------------- | --------------------------------------------------------------- |
| THREE.Scene()             | Where you place objects, lights, cameras, etc.                  |
| THREE.PerspectiveCamera() | A type of camera                                                |
| THREE.WebGLRenderer()     | The renderer that makes it possible to render 3D in the browser |
| THREE.BoxGeometry()       | A type of geometry (a box)                                      |
| THREE.MeshBasicMaterial() | A type of material (not texture)                                |
| THREE.Mesh()              | The mesh consists of a **geometry** and **material**            |

In the snippet above, we have created a **green cube**.&#x20;

## Important Concepts

### BufferGeometry

In short, `BufferGeometry` is a "faster" way to represent geomtry. It uses `math` ~~and  magic~~ to represent the 3D geometry. Take a look at the [docs](https://threejs.org/docs/#api/en/core/BufferGeometry) or [Three.js Fundamentals](https://threejsfundamentals.org/threejs/lessons/threejs-custom-buffergeometry.html) for a deep dive into this topic.

### BufferGeometryUtils

Take a look at the [docs](https://threejs.org/docs/index.html#examples/en/utils/BufferGeometryUtils) for more information.

### **Materials**

Three offers many types of materials, see the [docs](https://threejs.org/docs/index.html#api/en/materials/Material) for more info.

![](https://1338860669-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MSCCfuWwSmlrlrlRZ9a%2F-MSga_V1N0KfZnfAQ5Ya%2F-MSgcGgIaqIaTy07o6BL%2Fimage.png?alt=media\&token=01b99510-1956-43a4-b875-6b96c56c09b6)

Notice that some meterials are linked to a type of 3D object. For example, `LineBasicMaterial` and `LineDashedMaterial` belogns to a line.

Recall the chapter about [3D Basics](/ifc-js/optimization/3d-basics.md). We are intrested in `edges` and `meshes`. You can find more information over at the [Three.js Fundamentals.](https://threejsfundamentals.org/threejs/lessons/threejs-materials.html)

## Stuff to Consider

* [THREE.Geometry will be removed from core with r125](https://discourse.threejs.org/t/three-geometry-will-be-removed-from-core-with-r125/22401)
