Skip to content

Examples : Engraving

Description

Realtime circular text engraving.

This examples demonstrates,

  • Using CSG Subtract to create a ring by creating a larger CylinderBufferGeometry and subtracting a smaller CylinderBufferGeometry from it.
  • Using the THREE.FontLoader.
  • Using the Raycaster with the dblclick event to focusing the controls target to the point clicked on the mesh.
  • Replacing a Mesh geometry at runtime
  • Bending a Mesh around a radius
  • Using CSG Subtract to subtract the generated text geometry from the ring geometry to create the engraved text effect

./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
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 'three/examples/jsm/libs/dat.gui.module'
import { CSG } from './utils/CSGMesh'
import { TWEEN } from 'three/examples/jsm/libs/tween.module.min'
import Bender from './bender'

const bender = new Bender()

const scene = new THREE.Scene()

const light1 = new THREE.SpotLight()
light1.position.set(6.5, 7.5, 7.5)
light1.angle = Math.PI / 4
light1.penumbra = 0.5
scene.add(light1)

const light2 = new THREE.SpotLight()
light1.position.set(-6.5, 7.5, 7.5)
light2.angle = Math.PI / 4
light2.penumbra = 0.5
scene.add(light2)

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

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

const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true
controls.target.z = -5

const data = {
    text: 'abc123'
}

const envTexture = new THREE.CubeTextureLoader().load([
    'img/px.jpg',
    'img/nx.jpg',
    'img/py.jpg',
    'img/ny.jpg',
    'img/pz.jpg',
    'img/nz.jpg'
])
envTexture.mapping = THREE.CubeReflectionMapping

const material = new THREE.MeshStandardMaterial({
    envMap: envTexture,
    metalness: 1.0,
    roughness: 0.0,
    color: 0xffd700
})

const cylinderMesh1 = new THREE.Mesh(
    new THREE.CylinderBufferGeometry(6, 6, 1.5, 64, 1, false),
    material
)
const cylinderMesh2 = new THREE.Mesh(
    new THREE.CylinderBufferGeometry(5, 5, 1.6, 64, 1, false),
    material
)
cylinderMesh1.position.set(0, 0, 0)
cylinderMesh2.geometry.rotateX(-Math.PI / 2)
cylinderMesh2.position.set(0, 0, 0)
cylinderMesh2.geometry.rotateX(-Math.PI / 2)

const cylinderCSG1 = CSG.fromMesh(cylinderMesh1)
const cylinderCSG2 = CSG.fromMesh(cylinderMesh2)

const ringCSG = cylinderCSG1.subtract(cylinderCSG2)
const ringMesh = CSG.toMesh(ringCSG, new THREE.Matrix4())

let engravedMesh = new THREE.Mesh(ringMesh.geometry, material)
scene.add(engravedMesh)
let font: THREE.Font

const loader = new THREE.FontLoader()
loader.load('fonts/helvetiker_regular.typeface.json', function (f) {
    font = f
    regenerateGeometry()
})

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

const raycaster = new THREE.Raycaster()

renderer.domElement.addEventListener('dblclick', onDoubleClick, false)
function onDoubleClick(event: MouseEvent) {
    const mouse = {
        x: (event.clientX / renderer.domElement.clientWidth) * 2 - 1,
        y: -(event.clientY / renderer.domElement.clientHeight) * 2 + 1
    }

    raycaster.setFromCamera(mouse, camera)

    const intersects = raycaster.intersectObject(engravedMesh, false)
    if (intersects.length > 0) {
        const p = intersects[0].point
        new TWEEN.Tween(controls.target)
            .to(
                {
                    x: p.x,
                    y: p.y,
                    z: p.z
                },
                200
            )
            .easing(TWEEN.Easing.Cubic.Out)
            .start()
    }
}

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

const gui = new GUI()
gui.add(data, 'text').onFinishChange(regenerateGeometry)
gui.open()

function regenerateGeometry() {
    let newGeometry

    newGeometry = new THREE.TextBufferGeometry(data.text, {
        font: font,
        size: 1,
        height: 0.2,
        curveSegments: 2
    })

    newGeometry.center()
    bender.bend(newGeometry, 'y', Math.PI / 16)
    newGeometry.translate(0, 0, -5)

    const textCSG = CSG.fromGeometry(newGeometry)
    const engravedCSG = ringCSG.subtract(textCSG)
    engravedMesh.geometry.dispose()
    engravedMesh.geometry = CSG.toMesh(
        engravedCSG,
        new THREE.Matrix4()
    ).geometry
}

function animate() {
    requestAnimationFrame(animate)
    controls.update()
    TWEEN.update()
    render()
    stats.update()
}

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

animate()