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


    <!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 < 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>
    

      

    <!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 >
    

      

    哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)
  • 相关阅读:
    卷积神经网络
    卷积神经网络导览
    初识区块链——用JS构建你自己的区块链
    揭秘菜鸟仓储体系“大脑”:智能波次如何实现仓库降本提效?
    JQuery操作Iframe
    Spring MVC 学习笔记(二)
    SpringMVC各种视图
    Spring MVC 学习笔记(一)
    为什么在MySQL数据库中无法创建外键?(MyISAM和InnoDB详解)
    数据库访问性能优化
  • 原文地址:https://www.cnblogs.com/geovindu/p/14448172.html
Copyright © 2020-2023  润新知