Skip to content

Geometries

Video Lecture

Geometries Geometries

Description

<>

Three.js has many classes for creating common geometries.

Start Scripts

./dist/client/index.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>
            Three.js TypeScript Tutorials by Sean Bradley :
            https://sbcode.net/threejs
        </title>
        <style>
            body {
                overflow: hidden;
                margin: 0px;
            }

            #debug1 {
                font-family: monospace;
                font-size: larger;
                position: absolute;
                left: 0px;
                top: 50px;
                border: 1px solid red;
                width: 180px;
                height: 310px;
                color: white;
                pointer-events: none;
            }
        </style>
    </head>

    <body>
        <div id="debug1"></div>
        <script type="module" src="bundle.js"></script>
    </body>
</html>

./src/client/client.ts

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import Stats from 'three/examples/jsm/libs/stats.module'
import { GUI } from 'dat.gui'

const scene = new THREE.Scene()
scene.add(new THREE.AxesHelper(5))

const camera = new THREE.PerspectiveCamera(
    75,
    window.innerWidth / window.innerHeight,
    0.1,
    1000
)
camera.position.x = -2
camera.position.y = 4
camera.position.z = 5

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

new OrbitControls(camera, renderer.domElement)

const boxGeometry = new THREE.BoxGeometry()
// const sphereGeometry = new THREE.SphereGeometry()
// const icosahedronGeometry = new THREE.IcosahedronGeometry()

//console.log(boxGeometry)

const material = new THREE.MeshBasicMaterial({
    color: 0x00ff00,
    wireframe: true,
})

const cube = new THREE.Mesh(boxGeometry, material)
//cube.position.x = 5
scene.add(cube)

// const sphere = new THREE.Mesh(sphereGeometry, material)
// sphere.position.x = -5
// scene.add(sphere)

// const icosahedron = new THREE.Mesh(icosahedronGeometry, material)
// scene.add(icosahedron)

window.addEventListener('resize', onWindowResize, false)
function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight
    camera.updateProjectionMatrix()
    renderer.setSize(window.innerWidth, window.innerHeight)
    render()
}

const stats = new Stats()
document.body.appendChild(stats.dom)

const gui = new GUI()
const cubeFolder = gui.addFolder('Cube')
const cubeRotationFolder = cubeFolder.addFolder('Rotation')
cubeRotationFolder.add(cube.rotation, 'x', 0, Math.PI * 2, 0.01)
cubeRotationFolder.add(cube.rotation, 'y', 0, Math.PI * 2, 0.01)
cubeRotationFolder.add(cube.rotation, 'z', 0, Math.PI * 2, 0.01)
const cubePositionFolder = cubeFolder.addFolder('Position')
cubePositionFolder.add(cube.position, 'x', -10, 10)
cubePositionFolder.add(cube.position, 'y', -10, 10)
cubePositionFolder.add(cube.position, 'z', -10, 10)
const cubeScaleFolder = cubeFolder.addFolder('Scale')
cubeScaleFolder.add(cube.scale, 'x', -5, 5, 0.1) //.onFinishChange(() => console.dir(cube.geometry))
cubeScaleFolder.add(cube.scale, 'y', -5, 5, 0.1)
cubeScaleFolder.add(cube.scale, 'z', -5, 5, 0.1)
cubeFolder.add(cube, 'visible', true)
cubeFolder.open()

// const cubeData = {
//     width: 1,
//     height: 1,
//     depth: 1,
//     widthSegments: 1,
//     heightSegments: 1,
//     depthSegments: 1,
// }
// const cubePropertiesFolder = cubeFolder.addFolder('Properties')
// cubePropertiesFolder
//     .add(cubeData, 'width', 1, 30)
//     .onChange(regenerateBoxGeometry)
//     .onFinishChange(() => console.dir(cube.geometry))
// cubePropertiesFolder.add(cubeData, 'height', 1, 30).onChange(regenerateBoxGeometry)
// cubePropertiesFolder.add(cubeData, 'depth', 1, 30).onChange(regenerateBoxGeometry)
// cubePropertiesFolder.add(cubeData, 'widthSegments', 1, 30).onChange(regenerateBoxGeometry)
// cubePropertiesFolder.add(cubeData, 'heightSegments', 1, 30).onChange(regenerateBoxGeometry)
// cubePropertiesFolder.add(cubeData, 'depthSegments', 1, 30).onChange(regenerateBoxGeometry)

// function regenerateBoxGeometry() {
//     const newGeometry = new THREE.BoxGeometry(
//         cubeData.width,
//         cubeData.height,
//         cubeData.depth,
//         cubeData.widthSegments,
//         cubeData.heightSegments,
//         cubeData.depthSegments
//     )
//     cube.geometry.dispose()
//     cube.geometry = newGeometry
// }

// const sphereData = {
//     radius: 1,
//     widthSegments: 8,
//     heightSegments: 6,
//     phiStart: 0,
//     phiLength: Math.PI * 2,
//     thetaStart: 0,
//     thetaLength: Math.PI,
// }
// const sphereFolder = gui.addFolder('Sphere')
// const spherePropertiesFolder = sphereFolder.addFolder('Properties')
// spherePropertiesFolder.add(sphereData, 'radius', 0.1, 30).onChange(regenerateSphereGeometry)
// spherePropertiesFolder.add(sphereData, 'widthSegments', 1, 32).onChange(regenerateSphereGeometry)
// spherePropertiesFolder.add(sphereData, 'heightSegments', 1, 16).onChange(regenerateSphereGeometry)
// spherePropertiesFolder
//     .add(sphereData, 'phiStart', 0, Math.PI * 2)
//     .onChange(regenerateSphereGeometry)
// spherePropertiesFolder
//     .add(sphereData, 'phiLength', 0, Math.PI * 2)
//     .onChange(regenerateSphereGeometry)
// spherePropertiesFolder.add(sphereData, 'thetaStart', 0, Math.PI).onChange(regenerateSphereGeometry)
// spherePropertiesFolder.add(sphereData, 'thetaLength', 0, Math.PI).onChange(regenerateSphereGeometry)

// function regenerateSphereGeometry() {
//     const newGeometry = new THREE.SphereGeometry(
//         sphereData.radius,
//         sphereData.widthSegments,
//         sphereData.heightSegments,
//         sphereData.phiStart,
//         sphereData.phiLength,
//         sphereData.thetaStart,
//         sphereData.thetaLength
//     )
//     sphere.geometry.dispose()
//     sphere.geometry = newGeometry
// }

// const icosahedronData = {
//     radius: 1,
//     detail: 0,
// }
// const icosahedronFolder = gui.addFolder('Icosahedron')
// const icosahedronPropertiesFolder = icosahedronFolder.addFolder('Properties')
// icosahedronPropertiesFolder
//     .add(icosahedronData, 'radius', 0.1, 10)
//     .onChange(regenerateIcosahedronGeometry)
// icosahedronPropertiesFolder
//     .add(icosahedronData, 'detail', 0, 5)
//     .step(1)
//     .onChange(regenerateIcosahedronGeometry)

// function regenerateIcosahedronGeometry() {
//     const newGeometry = new THREE.IcosahedronGeometry(
//         icosahedronData.radius,
//         icosahedronData.detail
//     )
//     icosahedron.geometry.dispose()
//     icosahedron.geometry = newGeometry
// }

//const debug = document.getElementById('debug1') as HTMLDivElement

function animate() {
    requestAnimationFrame(animate)

    render()

    //debug.innerText = 'Matrix\n' + cube.matrix.elements.toString().replace(/,/g, '\n')

    stats.update()
}

function render() {
    renderer.render(scene, camera)
}

animate()

Final Script

./src/client/client.ts

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import Stats from 'three/examples/jsm/libs/stats.module'
import { GUI } from 'dat.gui'

const scene = new THREE.Scene()
scene.add(new THREE.AxesHelper(5))

const camera = new THREE.PerspectiveCamera(
    75,
    window.innerWidth / window.innerHeight,
    0.1,
    1000
)
camera.position.x = -2
camera.position.y = 4
camera.position.z = 5

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

new OrbitControls(camera, renderer.domElement)

const boxGeometry = new THREE.BoxGeometry()
const sphereGeometry = new THREE.SphereGeometry()
const icosahedronGeometry = new THREE.IcosahedronGeometry()

//console.dir(boxGeometry)
const material = new THREE.MeshBasicMaterial({
    color: 0x00ff00,
    wireframe: true,
})

const cube = new THREE.Mesh(boxGeometry, material)
cube.position.x = 5
scene.add(cube)

const sphere = new THREE.Mesh(sphereGeometry, material)
sphere.position.x = -5
scene.add(sphere)

const icosahedron = new THREE.Mesh(icosahedronGeometry, material)
scene.add(icosahedron)

window.addEventListener('resize', onWindowResize, false)
function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight
    camera.updateProjectionMatrix()
    renderer.setSize(window.innerWidth, window.innerHeight)
    render()
}

const stats = new Stats()
document.body.appendChild(stats.dom)

const gui = new GUI()
const cubeFolder = gui.addFolder('Cube')
const cubeRotationFolder = cubeFolder.addFolder('Rotation')
cubeRotationFolder.add(cube.rotation, 'x', 0, Math.PI * 2, 0.01)
cubeRotationFolder.add(cube.rotation, 'y', 0, Math.PI * 2, 0.01)
cubeRotationFolder.add(cube.rotation, 'z', 0, Math.PI * 2, 0.01)
const cubePositionFolder = cubeFolder.addFolder('Position')
cubePositionFolder.add(cube.position, 'x', -10, 10)
cubePositionFolder.add(cube.position, 'y', -10, 10)
cubePositionFolder.add(cube.position, 'z', -10, 10)
const cubeScaleFolder = cubeFolder.addFolder('Scale')
cubeScaleFolder
    .add(cube.scale, 'x', -5, 5, 0.1)
    .onFinishChange(() => console.dir(cube.geometry))
cubeScaleFolder.add(cube.scale, 'y', -5, 5, 0.1)
cubeScaleFolder.add(cube.scale, 'z', -5, 5, 0.1)
cubeFolder.add(cube, 'visible', true)
cubeFolder.open()

const cubeData = {
    width: 1,
    height: 1,
    depth: 1,
    widthSegments: 1,
    heightSegments: 1,
    depthSegments: 1,
}
const cubePropertiesFolder = cubeFolder.addFolder('Properties')
cubePropertiesFolder
    .add(cubeData, 'width', 1, 30)
    .onChange(regenerateBoxGeometry)
    .onFinishChange(() => console.dir(cube.geometry))
cubePropertiesFolder
    .add(cubeData, 'height', 1, 30)
    .onChange(regenerateBoxGeometry)
cubePropertiesFolder
    .add(cubeData, 'depth', 1, 30)
    .onChange(regenerateBoxGeometry)
cubePropertiesFolder
    .add(cubeData, 'widthSegments', 1, 30)
    .onChange(regenerateBoxGeometry)
cubePropertiesFolder
    .add(cubeData, 'heightSegments', 1, 30)
    .onChange(regenerateBoxGeometry)
cubePropertiesFolder
    .add(cubeData, 'depthSegments', 1, 30)
    .onChange(regenerateBoxGeometry)

function regenerateBoxGeometry() {
    const newGeometry = new THREE.BoxGeometry(
        cubeData.width,
        cubeData.height,
        cubeData.depth,
        cubeData.widthSegments,
        cubeData.heightSegments,
        cubeData.depthSegments
    )
    cube.geometry.dispose()
    cube.geometry = newGeometry
}

const sphereData = {
    radius: 1,
    widthSegments: 8,
    heightSegments: 6,
    phiStart: 0,
    phiLength: Math.PI * 2,
    thetaStart: 0,
    thetaLength: Math.PI,
}
const sphereFolder = gui.addFolder('Sphere')
const spherePropertiesFolder = sphereFolder.addFolder('Properties')
spherePropertiesFolder
    .add(sphereData, 'radius', 0.1, 30)
    .onChange(regenerateSphereGeometry)
spherePropertiesFolder
    .add(sphereData, 'widthSegments', 1, 32)
    .onChange(regenerateSphereGeometry)
spherePropertiesFolder
    .add(sphereData, 'heightSegments', 1, 16)
    .onChange(regenerateSphereGeometry)
spherePropertiesFolder
    .add(sphereData, 'phiStart', 0, Math.PI * 2)
    .onChange(regenerateSphereGeometry)
spherePropertiesFolder
    .add(sphereData, 'phiLength', 0, Math.PI * 2)
    .onChange(regenerateSphereGeometry)
spherePropertiesFolder
    .add(sphereData, 'thetaStart', 0, Math.PI)
    .onChange(regenerateSphereGeometry)
spherePropertiesFolder
    .add(sphereData, 'thetaLength', 0, Math.PI)
    .onChange(regenerateSphereGeometry)

function regenerateSphereGeometry() {
    const newGeometry = new THREE.SphereGeometry(
        sphereData.radius,
        sphereData.widthSegments,
        sphereData.heightSegments,
        sphereData.phiStart,
        sphereData.phiLength,
        sphereData.thetaStart,
        sphereData.thetaLength
    )
    sphere.geometry.dispose()
    sphere.geometry = newGeometry
}

const icosahedronData = {
    radius: 1,
    detail: 0,
}
const icosahedronFolder = gui.addFolder('Icosahedron')
const icosahedronPropertiesFolder = icosahedronFolder.addFolder('Properties')
icosahedronPropertiesFolder
    .add(icosahedronData, 'radius', 0.1, 10)
    .onChange(regenerateIcosahedronGeometry)
icosahedronPropertiesFolder
    .add(icosahedronData, 'detail', 0, 5)
    .step(1)
    .onChange(regenerateIcosahedronGeometry)

function regenerateIcosahedronGeometry() {
    const newGeometry = new THREE.IcosahedronGeometry(
        icosahedronData.radius,
        icosahedronData.detail
    )
    icosahedron.geometry.dispose()
    icosahedron.geometry = newGeometry
}

const debug = document.getElementById('debug1') as HTMLDivElement

function animate() {
    requestAnimationFrame(animate)

    render()

    debug.innerText =
        'Matrix\n' + cube.matrix.elements.toString().replace(/,/g, '\n')

    stats.update()
}

function render() {
    renderer.render(scene, camera)
}

animate()

Transformation matrix : Wikipedia

THREE.Matrix4

Comments