Three.js: 显示自定义的中文字体

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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="apple-moible-web-app-capable" content="yes">
    <meta name="apple-moible-web-app-status-bar-style" content="black">
    <title>three.js  R125 显示中文</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <meta name="Keywords" content="塗聚文,捷為工作室,万年历,万年历查询,黄历,黄历查询,塗聚文,捷為工作室" />
    <meta name="Description" content="塗聚文,捷為工作室,信息流,物流,人力资源流,资本流的系统解决方案的开发与设计. " />
    <meta name="author" content="geovindu,塗聚文,Geovin Du" />
    <link rel="shortcut icon" href="~/favicon.ico" type="image/x-icon" />
    <link rel="icon" href="/favicon.ico" />
    <link rel="bookmark" href="/favicon.ico" type="image/gif" />
    <link type="text/css" rel="stylesheet" href="main.css">
</head>
 
<body>
    <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - custom attributes example</div>
    <div id="container"></div>
 
    <script type="x-shader/x-vertex" id="vertexshader">
 
        uniform float amplitude;
 
        attribute vec3 displacement;
        attribute vec3 customColor;
 
        varying vec3 vColor;
 
        void main() {
 
            vec3 newPosition = position + amplitude * displacement;
 
            vColor = customColor;
 
            gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );
 
        }
 
    </script>
 
    <script type="x-shader/x-fragment" id="fragmentshader">
 
        uniform vec3 color;
        uniform float opacity;
 
        varying vec3 vColor;
 
        void main() {
 
            gl_FragColor = vec4( vColor * color, opacity );
 
        }
 
    </script>
 
    <script type="module">import * as THREE from '../build/three.module.js';
 
            import Stats from './jsm/libs/stats.module.js';
 
            let renderer, scene, camera, stats;
 
            let line, uniforms;
 
            const loader = new THREE.FontLoader();
        loader.load( 'fonts/FZuanSu_regular.typeface.json', function ( font ) {
                init( font );
                animate();
 
            } );
 
            function init( font ) {
 
                camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
                camera.position.z = 400;
                scene = new THREE.Scene();
                scene.background = new THREE.Color( 0x050505 );
 
                uniforms = {
 
                    amplitude: { value: 5.0 },
                    opacity: { value: 0.3 },
                    color: { value: new THREE.Color( 0xffffff ) }
 
                };
 
                const shaderMaterial = new THREE.ShaderMaterial( {
 
                    uniforms: uniforms,
                    vertexShader: document.getElementById( 'vertexshader' ).textContent,
                    fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
                    blending: THREE.AdditiveBlending,
                    depthTest: false,
                    transparent: true
 
                } );
 
 
                const geometry = new THREE.TextGeometry( '涂聚文学习three.js', {
 
                    font: font,
 
                    size: 50,
                    height: 15,
                    curveSegments: 10,
 
                    bevelThickness: 5,
                    bevelSize: 1.5,
                    bevelEnabled: true,
                    bevelSegments: 10,
 
                } );
 
                geometry.center();
 
                const count = geometry.attributes.position.count;
 
                const displacement = new THREE.Float32BufferAttribute( count * 3, 3 );
                geometry.setAttribute( 'displacement', displacement );
 
                const customColor = new THREE.Float32BufferAttribute( count * 3, 3 );
                geometry.setAttribute( 'customColor', customColor );
 
                const color = new THREE.Color( 0xffffff );
 
                for ( let i = 0, l = customColor.count; i &lt; l; i ++ ) {
 
                    color.setHSL( i / l, 0.5, 0.5 );
                    color.toArray( customColor.array, i * customColor.itemSize );
 
                }
 
                line = new THREE.Line( geometry, shaderMaterial );
                line.rotation.x = 0.2;
                scene.add( line );
 
                renderer = new THREE.WebGLRenderer( { antialias: true } );
                renderer.setPixelRatio( window.devicePixelRatio );
                renderer.setSize( window.innerWidth, window.innerHeight );
 
                const container = document.getElementById( 'container' );
                container.appendChild( renderer.domElement );
 
                stats = new Stats();
                container.appendChild( stats.dom );
 
                //
 
                window.addEventListener( 'resize', onWindowResize );
 
            }
 
            function onWindowResize() {
 
                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();
 
                renderer.setSize( window.innerWidth, window.innerHeight );
 
            }
 
            function animate() {
 
                requestAnimationFrame( animate );
 
                render();
                stats.update();
 
            }
 
            function render() {
 
                const time = Date.now() * 0.001;
 
                line.rotation.y = 0.25 * time;
 
                uniforms.amplitude.value = Math.sin( 0.5 * time );
                uniforms.color.value.offsetHSL( 0.0005, 0, 0 );
 
                const attributes = line.geometry.attributes;
                const array = attributes.displacement.array;
 
                for ( let i = 0, l = array.length; i < l; i += 3 ) {
 
                    array[ i ] += 0.3 * ( 0.5 - Math.random() );
                    array[ i + 1 ] += 0.3 * ( 0.5 - Math.random() );
                    array[ i + 2 ] += 0.3 * ( 0.5 - Math.random() );
 
                }
 
                attributes.displacement.needsUpdate = true;
 
                renderer.render( scene, camera );
 
            }</script>
 
</body>
 
</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
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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="apple-moible-web-app-capable" content="yes">
    <meta name="apple-moible-web-app-status-bar-style" content="black">
    <title>自定义字体</title>
    <meta content=" Three.js r95 二元操作" name="keywords">
    <meta content=" Three.js r95 二元操作" name="description">
    <meta name="author" content="Geovin Du 涂聚文 塗聚文">
    <link rel="shortcut icon" href="~/favicon.ico" type="image/x-icon" />
    <link rel="icon" href="/favicon.ico" />
    <link rel="bookmark" href="/favicon.ico" type="image/gif" />
    <script type="text/javascript" charset="UTF-8" src="../libs/three/three.js"></script>
    <script type="text/javascript" charset="UTF-8" src="../libs/three/controls/TrackballControls.js"></script>
    <script type="text/javascript" src="../libs/util/Stats.js"></script>
    <script type="text/javascript" src="../libs/util/dat.gui.js"></script>
    <script type="text/javascript" src="js/util.js"></script>
    <link rel="stylesheet" href="../css/default.css">
    <script type="text/javascript">
 
 
        // TODO: There is also a different way to do fonts now
 
        function init() {
 
            // use the defaults
            var stats = initStats();
            var renderer = initRenderer();
            var camera = initCamera();
            var scene = new THREE.Scene();
            initDefaultLighting(scene);
            var groundPlane = addLargeGroundPlane(scene)
            groundPlane.position.y = -30;
 
            var font_bitstream;
            var font_helvetiker_bold;
            var font_helvetiker_regular;
 
            var step = 0;
            var text1;
            var text2;
 
            var fontload1 = new THREE.FontLoader();
            fontload1.load('../assets/fonts/FZuanSu_regular.typeface.json', function (response) {
                controls.font = response;
                font_bitstream = response;
                controls.redraw();
                render();
            });
 
            var fontload2 = new THREE.FontLoader();
            fontload2.load('../assets/fonts/YouYuan_bold.typeface.json', function (response) {
                font_helvetiker_bold = response;
            });
 
            var fontload3 = new THREE.FontLoader();
            fontload3.load('../assets/fonts/DFWaWaW5GB_regular.typeface.json', function (response) {
                font_helvetiker_regular = response;
            });
 
            var controls = new function () {
 
                this.appliedMaterial = applyMeshNormalMaterial
                this.castShadow = true;
                this.groundPlaneVisible = true;
 
                this.size = 90;
                this.height = 90;
                this.bevelThickness = 2;
                this.bevelSize = 0.5;
                this.bevelEnabled = true;
                this.bevelSegments = 3;
                this.bevelEnabled = true;
                this.curveSegments = 12;
                this.steps = 1;
                this.fontName = "繁篆体";
 
                // redraw function, updates the control UI and recreates the geometry.
                this.redraw = function () {
 
                    switch (controls.fontName) {
                        case '繁篆体':
                            controls.font = font_bitstream
                            break;
                        case '幼园体':
                            controls.font = font_helvetiker_regular
                            break;
                        case '娃娃体':
                            controls.font = font_helvetiker_bold
                            break;
                    }
 
                    redrawGeometryAndUpdateUI(gui, scene, controls, function () {
                        var options = {
                            size: controls.size,
                            height: controls.height,
                            weight: controls.weight,
                            font: controls.font,
                            bevelThickness: controls.bevelThickness,
                            bevelSize: controls.bevelSize,
                            bevelSegments: controls.bevelSegments,
                            bevelEnabled: controls.bevelEnabled,
                            curveSegments: controls.curveSegments,
                            steps: controls.steps
                        };
 
                        var geom = new THREE.TextGeometry("涂聚文学习", options)
                        geom.applyMatrix(new THREE.Matrix4().makeScale(0.05, 0.05, 0.05));
                        geom.center();
 
                        return geom
                    });
                };
            };
 
 
 
            var gui = new dat.GUI();
            gui.add(controls, 'size', 0, 200).onChange(controls.redraw);
            gui.add(controls, 'height', 0, 200).onChange(controls.redraw);
            gui.add(controls, 'fontName', ['繁篆体', '幼园体', '娃娃体']).onChange(controls.redraw);
            gui.add(controls, 'bevelThickness', 0, 10).onChange(controls.redraw);
            gui.add(controls, 'bevelSize', 0, 10).onChange(controls.redraw);
            gui.add(controls, 'bevelSegments', 0, 30).step(1).onChange(controls.redraw);
            gui.add(controls, 'bevelEnabled').onChange(controls.redraw);
            gui.add(controls, 'curveSegments', 1, 30).step(1).onChange(controls.redraw);
            gui.add(controls, 'steps', 1, 5).step(1).onChange(controls.redraw);
 
            // add a material section, so we can switch between materials
            gui.add(controls, 'appliedMaterial', {
                meshNormal: applyMeshNormalMaterial,
                meshStandard: applyMeshStandardMaterial
            }).onChange(controls.redraw)
 
            gui.add(controls, 'castShadow').onChange(function (e) { controls.mesh.castShadow = e })
            gui.add(controls, 'groundPlaneVisible').onChange(function (e) { groundPlane.material.visible = e })
 
            function render() {
                stats.update();
 
                controls.mesh.rotation.y = step += 0.005
                controls.mesh.rotation.x = step
                controls.mesh.rotation.z = step
 
                // render using requestAnimationFrame
                requestAnimationFrame(render);
                renderer.render(scene, camera);
            }
        }
 
 
    </script>
</head>
<body>
    <div id="webgl-output"></div>
        <script type="text/javascript">
            (function () {
                // your page initialization code here
                // the DOM will be available here
                init()
            })();
        </script>
 
 
</body>
</html >

  

posted @   ®Geovin Du Dream Park™  阅读(229)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示