|
35 | 35 |
|
36 | 36 | import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; |
37 | 37 | import { SVGLoader } from 'three/addons/loaders/SVGLoader.js'; |
38 | | - import { FontLoader } from 'three/addons/loaders/FontLoader.js'; |
| 38 | + import { Font } from 'three/addons/loaders/FontLoader.js'; |
| 39 | + import { unzipSync, strFromU8 } from 'three/addons/libs/fflate.module.js'; |
39 | 40 |
|
40 | 41 | let camera, scene, renderer; |
41 | 42 |
|
|
44 | 45 | function init( ) { |
45 | 46 |
|
46 | 47 | camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 ); |
47 | | - camera.position.set( 0, - 400, 600 ); |
| 48 | + camera.position.set( 0, - 400, 1000 ); |
48 | 49 |
|
49 | 50 | scene = new THREE.Scene(); |
50 | 51 | scene.background = new THREE.Color( 0xf0f0f0 ); |
51 | 52 |
|
52 | | - const loader = new FontLoader(); |
53 | | - loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) { |
| 53 | + new THREE.FileLoader() |
| 54 | + .setResponseType( 'arraybuffer' ) |
| 55 | + .load( 'fonts/MPLUSRounded1c/MPLUSRounded1c-Regular.typeface.json.zip', function ( data ) { |
| 56 | + const zip = unzipSync( new Uint8Array( data ) ); |
| 57 | + const strArray = strFromU8( new Uint8Array( zip[ 'MPLUSRounded1c-Regular.typeface.json' ].buffer ) ); |
54 | 58 |
|
55 | | - const color = new THREE.Color( 0x006699 ); |
| 59 | + const font = new Font( JSON.parse( strArray ) ); |
| 60 | + const color = new THREE.Color( 0x006699 ); |
56 | 61 |
|
57 | | - const matDark = new THREE.MeshBasicMaterial( { |
58 | | - color: color, |
59 | | - side: THREE.DoubleSide |
60 | | - } ); |
| 62 | + const matDark = new THREE.MeshBasicMaterial( { |
| 63 | + color: color, |
| 64 | + side: THREE.DoubleSide |
| 65 | + } ); |
61 | 66 |
|
62 | | - const matLite = new THREE.MeshBasicMaterial( { |
63 | | - color: color, |
64 | | - transparent: true, |
65 | | - opacity: 0.4, |
66 | | - side: THREE.DoubleSide |
67 | | - } ); |
| 67 | + const matLite = new THREE.MeshBasicMaterial( { |
| 68 | + color: color, |
| 69 | + transparent: true, |
| 70 | + opacity: 0.4, |
| 71 | + side: THREE.DoubleSide |
| 72 | + } ); |
68 | 73 |
|
69 | | - const message = ' Three.js\nStroke text.'; |
| 74 | + const material = { |
| 75 | + dark: matDark, |
| 76 | + lite: matLite, |
| 77 | + color: color |
| 78 | + }; |
70 | 79 |
|
71 | | - const shapes = font.generateShapes( message, 100 ); |
| 80 | + const english = ' Three.js\nStroke text.'; // Left to right |
72 | 81 |
|
73 | | - const geometry = new THREE.ShapeGeometry( shapes ); |
| 82 | + const hebrew = 'טקסט קו'; // Right to left |
74 | 83 |
|
75 | | - geometry.computeBoundingBox(); |
| 84 | + const chinese = '文字描邊'; // Top to bottom |
76 | 85 |
|
77 | | - const xMid = - 0.5 * ( geometry.boundingBox.max.x - geometry.boundingBox.min.x ); |
| 86 | + const message1 = generateStrokeText( font, material, english, 80, 'ltr' ); |
78 | 87 |
|
79 | | - geometry.translate( xMid, 0, 0 ); |
| 88 | + const message2 = generateStrokeText( font, material, hebrew, 80, 'rtl' ); |
80 | 89 |
|
81 | | - // make shape ( N.B. edge view not visible ) |
| 90 | + const message3 = generateStrokeText( font, material, chinese, 80, 'tb' ); |
82 | 91 |
|
83 | | - const text = new THREE.Mesh( geometry, matLite ); |
84 | | - text.position.z = - 150; |
85 | | - scene.add( text ); |
| 92 | + message1.position.x = - 100; |
86 | 93 |
|
87 | | - // make line shape ( N.B. edge view remains visible ) |
| 94 | + message2.position.x = - 100; |
| 95 | + message2.position.y = - 300; |
88 | 96 |
|
89 | | - const holeShapes = []; |
| 97 | + message3.position.x = 300; |
| 98 | + message3.position.y = - 300; |
90 | 99 |
|
91 | | - for ( let i = 0; i < shapes.length; i ++ ) { |
| 100 | + scene.add( message1, message2, message3 ); |
92 | 101 |
|
93 | | - const shape = shapes[ i ]; |
| 102 | + render(); |
94 | 103 |
|
95 | | - if ( shape.holes && shape.holes.length > 0 ) { |
| 104 | + } ); //end load function |
96 | 105 |
|
97 | | - for ( let j = 0; j < shape.holes.length; j ++ ) { |
| 106 | + renderer = new THREE.WebGLRenderer( { antialias: true } ); |
| 107 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 108 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 109 | + document.body.appendChild( renderer.domElement ); |
98 | 110 |
|
99 | | - const hole = shape.holes[ j ]; |
100 | | - holeShapes.push( hole ); |
| 111 | + const controls = new OrbitControls( camera, renderer.domElement ); |
| 112 | + controls.target.set( 0, 0, 0 ); |
| 113 | + controls.update(); |
101 | 114 |
|
102 | | - } |
| 115 | + controls.addEventListener( 'change', render ); |
103 | 116 |
|
104 | | - } |
| 117 | + window.addEventListener( 'resize', onWindowResize ); |
105 | 118 |
|
106 | | - } |
| 119 | + } // end init |
| 120 | + |
| 121 | + function generateStrokeText(font, material, message, size, direction = 'ltr') { |
| 122 | + |
| 123 | + const shapes = font.generateShapes( message, size, direction ); |
| 124 | + |
| 125 | + const geometry = new THREE.ShapeGeometry( shapes ); |
| 126 | + |
| 127 | + const strokeText = new THREE.Group(); |
| 128 | + |
| 129 | + geometry.computeBoundingBox(); |
| 130 | + |
| 131 | + const xMid = - 0.5 * ( geometry.boundingBox.max.x - geometry.boundingBox.min.x ); |
| 132 | + |
| 133 | + geometry.translate( xMid, 0, 0 ); |
| 134 | + |
| 135 | + // make shape ( N.B. edge view not visible ) |
107 | 136 |
|
108 | | - shapes.push( ...holeShapes ); |
| 137 | + const text = new THREE.Mesh( geometry, material.lite ); |
109 | 138 |
|
110 | | - const style = SVGLoader.getStrokeStyle( 5, color.getStyle() ); |
| 139 | + text.position.z = - 150; |
111 | 140 |
|
112 | | - const strokeText = new THREE.Group(); |
| 141 | + strokeText.add(text); |
113 | 142 |
|
114 | | - for ( let i = 0; i < shapes.length; i ++ ) { |
| 143 | + // make line shape ( N.B. edge view remains visible ) |
115 | 144 |
|
116 | | - const shape = shapes[ i ]; |
| 145 | + const holeShapes = []; |
117 | 146 |
|
118 | | - const points = shape.getPoints(); |
| 147 | + for ( let i = 0; i < shapes.length; i ++ ) { |
119 | 148 |
|
120 | | - const geometry = SVGLoader.pointsToStroke( points, style ); |
| 149 | + const shape = shapes[ i ]; |
121 | 150 |
|
122 | | - geometry.translate( xMid, 0, 0 ); |
| 151 | + if ( shape.holes && shape.holes.length > 0 ) { |
123 | 152 |
|
124 | | - const strokeMesh = new THREE.Mesh( geometry, matDark ); |
125 | | - strokeText.add( strokeMesh ); |
| 153 | + for ( let j = 0; j < shape.holes.length; j ++ ) { |
| 154 | + |
| 155 | + const hole = shape.holes[ j ]; |
| 156 | + holeShapes.push( hole ); |
| 157 | + |
| 158 | + } |
126 | 159 |
|
127 | 160 | } |
128 | 161 |
|
129 | | - scene.add( strokeText ); |
| 162 | + } |
130 | 163 |
|
131 | | - render(); |
| 164 | + shapes.push( ...holeShapes ); |
132 | 165 |
|
133 | | - } ); //end load function |
| 166 | + const style = SVGLoader.getStrokeStyle( 5, material.color.getStyle() ); |
134 | 167 |
|
135 | | - renderer = new THREE.WebGLRenderer( { antialias: true } ); |
136 | | - renderer.setPixelRatio( window.devicePixelRatio ); |
137 | | - renderer.setSize( window.innerWidth, window.innerHeight ); |
138 | | - document.body.appendChild( renderer.domElement ); |
| 168 | + for ( let i = 0; i < shapes.length; i ++ ) { |
139 | 169 |
|
140 | | - const controls = new OrbitControls( camera, renderer.domElement ); |
141 | | - controls.target.set( 0, 0, 0 ); |
142 | | - controls.update(); |
| 170 | + const shape = shapes[ i ]; |
143 | 171 |
|
144 | | - controls.addEventListener( 'change', render ); |
| 172 | + const points = shape.getPoints(); |
145 | 173 |
|
146 | | - window.addEventListener( 'resize', onWindowResize ); |
| 174 | + const geometry = SVGLoader.pointsToStroke( points, style ); |
147 | 175 |
|
148 | | - } // end init |
| 176 | + geometry.translate( xMid, 0, 0 ); |
| 177 | + |
| 178 | + const strokeMesh = new THREE.Mesh( geometry, material.dark ); |
| 179 | + strokeText.add( strokeMesh ); |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | + return strokeText; |
| 184 | + |
| 185 | + } |
149 | 186 |
|
150 | 187 | function onWindowResize() { |
151 | 188 |
|
|
0 commit comments