Home  >  Article  >  Web Front-end  >  Basic Introduction to Three.js JS Library

Basic Introduction to Three.js JS Library

巴扎黑
巴扎黑Original
2017-09-15 09:15:562307browse

three.js is a webGL framework that is widely used due to its ease of use. Below, the editor of Script House will explain the basic configuration method of three.js through a case. Please refer to this article for details

Opening Statement

webGL allows us to achieve 3D effects on canvas. Three.js is a webGL framework that is widely used due to its ease of use. If you want to learn webGL, it is a good choice to abandon those complicated native interfaces and start with this framework.

The blogger is also currently learning three.js, and found that the relevant information is very scarce, and even the official API documentation is very rough. Many effects require you to slowly type the code and explore it yourself. So the purpose of writing this tutorial is to summarize it myself, and to share it with everyone.

This article is the first in a series of tutorials: Getting Started. In this article, I will take a simple demo as an example to explain the basic configuration method of three.js. After studying this article, you will learn how to draw a three-dimensional graphic in the browser!

Preparation work

Before writing code, you must first download the latest three.js framework package and introduce it into your page three.js, of course, there are many demos in the file package for everyone to learn;

Chrome is currently the best browser that supports webGL, Firefow is second, and domestic Aoyou and Cheetah can also run after testing.

Start with an example!

First we build the html, as follows:


<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>lesson1-by-shawn.xie</title>
 <!--引入Three.js-->
 <script src="Three.js"></script>
 <style type="text/css">
 p#canvas-frame{
  border: none;
  cursor: move;
  width: 1400px;
  height: 600px;
  background-color: #EEEEEE;
 }
 </style>
 </head>
 <body>
 <!--盛放canvas的容器-->
 <p id="container"></p>
 </body>
</html>

Prepare an area consistent with the size of the canvas frame for WebGL drawing. Specifically:

(1) Add the p element with the id "canvas3d" to the body tag.

(2) Specify the CSS style of "canvas3d" in the style tag.

It should be noted that we do not need to write a 5ba626b379994d53f7acf72a64f9b697 tag, we only need to define the p that holds the canvas. The canvas is dynamically generated by three.js!

Let’s start writing the script. We will build a simple three-dimensional model through the following five steps, which are also the basic steps of three.js:

1. Set up the three.js renderer

The process of mapping objects in three-dimensional space to a two-dimensional plane is called three-dimensional rendering. Generally speaking, we call the software that performs rendering operations a renderer. Specifically, the following processing is required.

(0) Declare global variables (objects);

(1) Get the height and width of the canvas "canvas-frame";

(2) Generate a renderer object ( Attributes: The anti-aliasing effect is valid for settings);

(3) Specify the height and width of the renderer (the same as the canvas frame size);

(4) Append the [canvas] element to [canvas3d 】 element;

(5) Set the clear color (clearColor) of the renderer.


//开启Three.js渲染器
var renderer;//声明全局变量(对象)
function initThree() {
 width = document.getElementById(&#39;canvas3d&#39;).clientWidth;//获取画布「canvas3d」的宽
 height = document.getElementById(&#39;canvas3d&#39;).clientHeight;//获取画布「canvas3d」的高
 renderer=new THREE.WebGLRenderer({antialias:true});//生成渲染器对象(属性:抗锯齿效果为设置有效)
 renderer.setSize(width, height );//指定渲染器的高宽(和画布框大小一致)
 document.getElementById(&#39;canvas3d&#39;).appendChild(renderer.domElement);//追加 【canvas】 元素到 【canvas3d】 元素中。
 renderer.setClearColorHex(0xFFFFFF, 1.0);//设置canvas背景色(clearColor)
}

2. Set up the camera camera

In OpenGL (WebGL), in the way objects in three-dimensional space are projected to two-dimensional space, there is perspective projection and orthographic projection cameras. Perspective projection is a method in which objects closer to the viewpoint are larger and objects farther away are drawn smaller. This is consistent with the way we see objects in daily life. Orthographic projection is to draw objects in a uniform size regardless of the distance from the viewpoint. In fields such as architecture and design, objects need to be drawn from various angles, so this projection is widely used. In Three.js, you can also specify cameras in perspective projection and orthographic projection. This article follows the steps below to set up the perspective projection method.

(0) Declare global variables (objects);

(1) Set the camera for perspective projection;

(2) Set the position coordinates of the camera;

(3) Set the top of the camera to the "z" axis direction;

(4) Set the center coordinates of the field of view.​​​


  //设置相机
 var camera;
 function initCamera() { 
 camera = new THREE.PerspectiveCamera( 45, width / height , 1 , 5000 );//设置透视投影的相机,默认情况下相机的上方向为Y轴,右方向为X轴,沿着Z轴朝里(视野角:fov 纵横比:aspect 相机离视体积最近的距离:near 相机离视体积最远的距离:far)
 camera.position.x = 0;//设置相机的位置坐标
 camera.position.y = 50;//设置相机的位置坐标
 camera.position.z = 100;//设置相机的位置坐标
 camera.up.x = 0;//设置相机的上为「x」轴方向
 camera.up.y = 1;//设置相机的上为「y」轴方向
 camera.up.z = 0;//设置相机的上为「z」轴方向
 camera.lookAt( {x:0, y:0, z:0 } );//设置视野的中心坐标
 }

3. Set the scene scene

The scene is a three-dimensional space. Use the [Scene] class to declare an object called [scene].​​


  //设置场景
 var scene;
 function initScene() { 
 scene = new THREE.Scene();
 }

4. Set the light source light

In the three-dimensional space of OpenGL (WebGL), there are two types of point light sources and spotlights. Furthermore, there is also a parallel light source (wireless high light source) as a special case of the point light source. In addition, as a parameter of the light source, settings such as [Ambient Light] can also be made. Correspondingly, [Point Light], [Spot Light], [Direction Light], and [Ambient Light] can be set in Three.js. Like OpenGL, multiple light sources can be set in a scene. Basically, it is a combination of ambient light and several other light sources. If you don't set ambient light, the surfaces not illuminated by light will become too dark. In this article, we first follow the steps below to set up the parallel light source, and then add ambient light.

(0) Declare global variables (objects)

(1) Set directional light source

(2) Set light source vector

(3) Add light source Go to the scene

Here we use the "DirectionalLight" class to declare an object called [light] to represent the directional light source


    //设置光源
 var light;
 function initLight() { 
 light = new THREE.DirectionalLight(0xff0000, 1.0, 0);//设置平行光源
 light.position.set( 200, 200, 200 );//设置光源向量
 scene.add(light);// 追加光源到场景
 }

5. Set the object object

   这里,我们声明一个球模型   


   //设置物体
 var sphere;
 function initObject(){ 
 sphere = new THREE.Mesh(
  new THREE.SphereGeometry(20,20), //width,height,depth
  new THREE.MeshLambertMaterial({color: 0xff0000}) //材质设定
 );
 scene.add(sphere);
 sphere.position.set(0,0,0);
 }

最后,我们写一个主函数执行以上五步:


        //执行
 function threeStart() {
 initThree();
 initCamera();
 initScene(); 
 initLight();
 initObject();
 renderer.clear(); 
 renderer.render(scene, camera);
 }

这时,测试以上程序,你会发现浏览器窗口中出现了你绘制的球形模型:

总结

以上就是three.js的入门内容,我们核心的五步就是:

1.设置three.js渲染器

2.设置摄像机camera

3.设置场景scene

4.设置光源light

5.设置物体object

可能其中有些设置你还不太清楚,没关系,后面几篇文章会对以上五个主要步骤进行详细的讲解,敬请期待~~

本例完整代码:


<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>lesson1-by-shawn.xie</title>
 <!--引入Three.js-->
 <script src="Three.js"></script>
 <script type="text/javascript">
 //开启Three.js渲染器
 var renderer;//声明全局变量(对象)
 function initThree() {
 width = document.getElementById(&#39;canvas3d&#39;).clientWidth;//获取画布「canvas3d」的宽
 height = document.getElementById(&#39;canvas3d&#39;).clientHeight;//获取画布「canvas3d」的高
 renderer=new THREE.WebGLRenderer({antialias:true});//生成渲染器对象(属性:抗锯齿效果为设置有效)
 renderer.setSize(width, height );//指定渲染器的高宽(和画布框大小一致)
 document.getElementById(&#39;canvas3d&#39;).appendChild(renderer.domElement);//追加 【canvas】 元素到 【canvas3d】 元素中。
 renderer.setClearColorHex(0xFFFFFF, 1.0);//设置canvas背景色(clearColor)
 }
 //设置相机
 var camera;
 function initCamera() { 
 camera = new THREE.PerspectiveCamera( 45, width / height , 1 , 5000 );//设置透视投影的相机,默认情况下相机的上方向为Y轴,右方向为X轴,沿着Z轴朝里(视野角:fov 纵横比:aspect 相机离视体积最近的距离:near 相机离视体积最远的距离:far)
 camera.position.x = 0;//设置相机的位置坐标
 camera.position.y = 50;//设置相机的位置坐标
 camera.position.z = 100;//设置相机的位置坐标
 camera.up.x = 0;//设置相机的上为「x」轴方向
 camera.up.y = 1;//设置相机的上为「y」轴方向
 camera.up.z = 0;//设置相机的上为「z」轴方向
 camera.lookAt( {x:0, y:0, z:0 } );//设置视野的中心坐标
 }
 //设置场景
 var scene;
 function initScene() { 
 scene = new THREE.Scene();
 }
 //设置光源
 var light;
 function initLight() { 
 light = new THREE.DirectionalLight(0xff0000, 1.0, 0);//设置平行光源
 light.position.set( 200, 200, 200 );//设置光源向量
 scene.add(light);// 追加光源到场景
 }
 //设置物体
 var sphere;
 function initObject(){ 
 sphere = new THREE.Mesh(
  new THREE.SphereGeometry(20,20), //width,height,depth
  new THREE.MeshLambertMaterial({color: 0xff0000}) //材质设定
 );
 scene.add(sphere);
 sphere.position.set(0,0,0);
 }
 //执行
 function threeStart() {
 initThree();
 initCamera();
 initScene(); 
 initLight();
 initObject();
 renderer.clear(); 
 renderer.render(scene, camera);
 }
 </script>
 <style type="text/css">
 p#canvas3d{
  border: none;
  cursor: move;
  width: 1400px;
  height: 600px;
  background-color: #EEEEEE;
 }
 </style>
 </head>
 <body onload=&#39;threeStart();&#39;>
 <!--盛放canvas的容器-->
 <p id="canvas3d"></p>
 </body>
</html>

The above is the detailed content of Basic Introduction to Three.js JS Library. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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