Home > Web Front-end > JS Tutorial > Three.js drawing font model sample code

Three.js drawing font model sample code

小云云
Release: 2018-02-01 09:56:02
Original
2016 people have browsed it

This article mainly introduces you to the relevant information about how Three.js draws font models. The effect achieved through the method introduced in the article is very good. Friends in need can refer to it. Let’s take a look together.

  • First you need to instantiate THREE.FontLoader() to load the text format in json format, and create the grid in the callback function that loads successfully.

  • Then create the grid through the THREE.TextBufferGeometry or THREE.TextGeometry method and pass in the issues that need to be set.

  • Set a texture and create a graphic through the THREE.Mesh() function and add it to the scene.

Sample code:

var fontModel; 
 function initModel() { 
 var font; 
 var loader = new THREE.FontLoader(); 
 loader.load("examples/fonts/gentilis_regular.typeface.json", function (res) { 
  font = new THREE.TextBufferGeometry("fdsfasd", { 
  font: res, 
  size: 100, 
  height: 60 
  }); 
 
  font.computeBoundingBox(); // 运行以后设置font的boundingBox属性对象,如果不运行无法获得。 
  //font.computeVertexNormals(); 
 
  var map = new THREE.TextureLoader().load("examples/textures/UV_Grid_Sm.jpg"); 
  var material = new THREE.MeshLambertMaterial({map:map,side:THREE.DoubleSide}); 
 
  fontModel = new THREE.Mesh(font,material); 
 
  //设置位置 
  fontModel.position.x = - (font.boundingBox.max.x - font.boundingBox.min.x)/2; //计算出整个模型的宽度的一半 
  fontModel.position.y = - 50; 
  fontModel.position.z = - 30; 
 
  scene.add(fontModel); 
 }); 
 }
Copy after login

Finally, I adjusted the position and it became the code like this.

Finally put all the code:

 
 
 
  
 Title 
  

              <script>   var renderer;   function initRender() {   renderer = new THREE.WebGLRenderer({antialias: true});   renderer.setSize(window.innerWidth, window.innerHeight);   document.body.appendChild(renderer.domElement);   }     var camera;   function initCamera() {   camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000);   camera.position.set(0, 0, 400);   }     var scene;   function initScene() {   scene = new THREE.Scene();   }     var light;   function initLight() {   scene.add(new THREE.AmbientLight(0x404040));     light = new THREE.DirectionalLight(0xffffff);   light.position.set(1, 1, 1);   scene.add(light);   }     var fontModel;   function initModel() {   var font;   var loader = new THREE.FontLoader();   loader.load(&quot;examples/fonts/gentilis_regular.typeface.json&quot;, function (res) {    font = new THREE.TextBufferGeometry(&quot;fdsfasd&quot;, {    font: res,    size: 100,    height: 60    });      font.computeBoundingBox(); // 运行以后设置font的boundingBox属性对象,如果不运行无法获得。    //font.computeVertexNormals();      var map = new THREE.TextureLoader().load(&quot;examples/textures/UV_Grid_Sm.jpg&quot;);    var material = new THREE.MeshLambertMaterial({map:map,side:THREE.DoubleSide});      fontModel = new THREE.Mesh(font,material);      //设置位置    fontModel.position.x = - (font.boundingBox.max.x - font.boundingBox.min.x)/2; //计算出整个模型的宽度的一半    fontModel.position.y = - 50;    fontModel.position.z = - 30;      scene.add(fontModel);   });   }     //初始化性能插件   var stats;   function initStats() {   stats = new Stats();   document.body.appendChild(stats.dom);   }     //用户交互插件 鼠标左键按住旋转,右键按住平移,滚轮缩放   var controls;   function initControls() {     controls = new THREE.OrbitControls(camera, renderer.domElement);     // 如果使用animate方法时,将此函数删除   //controls.addEventListener( 'change', render );   // 使动画循环使用时阻尼或自转 意思是否有惯性   controls.enableDamping = true;   //动态阻尼系数 就是鼠标拖拽旋转灵敏度   //controls.dampingFactor = 0.25;   //是否可以缩放   controls.enableZoom = true;   //是否自动旋转   controls.autoRotate = false;   //设置相机距离原点的最远距离   controls.minDistance = 200;   //设置相机距离原点的最远距离   controls.maxDistance = 600;   //是否开启右键拖拽   controls.enablePan = true;   }     function render() {   renderer.render(scene, camera);   }     //窗口变动触发的函数   function onWindowResize() {   camera.aspect = window.innerWidth / window.innerHeight;   camera.updateProjectionMatrix();   render();   renderer.setSize(window.innerWidth, window.innerHeight);     }     function animate() {   //更新控制器   controls.update();   render();     //更新性能插件   stats.update();   requestAnimationFrame(animate);   }     function draw() {   initRender();   initScene();   initCamera();   initLight();   initModel();   initControls();   initStats();     animate();   window.onresize = onWindowResize;   }  </script> 
Copy after login

Related recommendations:

How to solve the blurry problem of html5 canvas drawing fonts, pictures and graphics

The above is the detailed content of Three.js drawing font model sample code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template