three.js绘制地球、飞机与轨迹的效果示例

对于three.js不太熟悉的朋友们可以参考这篇文章,threejs官网:https://threejs.org/

首先我们来看下要实现的效果

这个缩小后的图片,下面我们来看下近距离的动态效果。。

效果比较简陋,需要后期再处理。。。

下面进入主题,代码篇。。

HTML部分:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>全球航班</title>

 <style>
 html{overflow: hidden;}
 body { margin: 0;}
 </style>

 <script src="js/jquery.min.js"></script>
</head>
<body>
 <!-- 地国 -->
 <div id="zh_globe_container"></div> <!-- 容器 -->

 <script src="js/threejs/Detector.js"></script> <!-- webGL浏览器支持检测 -->
 <script src="js/threejs/three.min.js"></script> <!-- 核心js -->
 <script src="js/threejs/stats.min.js"></script> <!-- 性能测试 -->
 <script src="js/threejs/OrbitControls.js"></script> <!-- 地球控制 -->
 <script src="js/socketio-1.4.5.js"></script> <!-- socket -->

 <script src="js/globe.js"></script> <!-- -->
</body>
</html>

JS部分(globe.js)

1、实现地球

地球贴图(可以在网上下载)

// 地球
function globe() {
 var globeTextureLoader = new THREE.TextureLoader();
 globeTextureLoader.load('images/textures/earth.jpg', function (texture) {
 var globeGgeometry = new THREE.SphereGeometry(200, 100, 100);
 var globeMaterial = new THREE.MeshStandardMaterial({map: texture});
 var globeMesh = new THREE.Mesh(globeGgeometry, globeMaterial);
 group.add(globeMesh);
 group.rotation.x = THREE.Math.degToRad(35);
 group.rotation.y = THREE.Math.degToRad(170);
 });
}

2、添加球面光源(这里使用的是半球光)

// 光
function lights() {
 var hemisphereLight = new THREE.HemisphereLight(0xffffff, 0x333333, 2);
 hemisphereLight.position.x = 0;
 hemisphereLight.position.y = 0;
 hemisphereLight.position.z = -200;
 group.add(hemisphereLight);
}

3、添加星点

// 星点
function stars() {
 var starsGeometry = new THREE.Geometry();
 for (var i = 0; i < 2000; i ++) {
 var starVector = new THREE.Vector3(
 THREE.Math.randFloatSpread(2000),
 THREE.Math.randFloatSpread(2000),
 THREE.Math.randFloatSpread(2000)
 );
 starsGeometry.vertices.push(starVector);
 }
 var starsMaterial = new THREE.PointsMaterial({color: 0x888888})
 var starsPoint = new THREE.Points(starsGeometry, starsMaterial);
 group.add(starsPoint);
}

4、添加飞机

这里需要我们把 经纬度坐标 转成 xyz 坐标

// 获取position
function getPosition(lng, lat, alt) {
 var phi = (90-lat)*(Math.PI/180),
 theta = (lng+180)*(Math.PI/180),
 radius = alt+200,
 x = -(radius * Math.sin(phi) * Math.cos(theta)),
 z = (radius * Math.sin(phi) * Math.sin(theta)),
 y = (radius * Math.cos(phi));
 return {x: x, y: y, z: z};
}

画飞机

// 飞机形状(不想画的,可以下载个 飞机模型 使用加载器加载进来)
var planeShape = new THREE.Shape();
planeShape.moveTo( 0, 0);
planeShape.lineTo(0.2, -0.2);
planeShape.lineTo(0.2, -1.3);
planeShape.lineTo(1.6,-2.7);
planeShape.lineTo(1.6,-3);
planeShape.lineTo(0.2, -2.1);
planeShape.lineTo(0.2, -3);
planeShape.lineTo(0.5, -3.4);
planeShape.lineTo(0.5, -3.7);
planeShape.lineTo(0, -3.3);
planeShape.lineTo(-0.5, -3.7);
planeShape.lineTo(-0.5, -3.4);
planeShape.lineTo(-0.2, -3);
planeShape.lineTo(-0.2, -2.1);
planeShape.lineTo(-1.6,-3);
planeShape.lineTo(-1.6,-2.7);
planeShape.lineTo(-0.2, -1.3);
planeShape.lineTo(-0.2, -0.2);
var planeGeometry = new THREE.ShapeGeometry(planeShape);
// 飞机材质
var planeMaterial = new THREE.MeshPhongMaterial({color: 0x0FB4DD, side: THREE.DoubleSide, depthTest: true});

depthTest作用是能否透过球体看到飞机,如果是false则旋转到球体另一面也能看到飞机

添加飞机

// 添加飞机
function addPlane(item) {
 if(item.anum && item.lng && item.lat) {
 var plane = new THREE.Mesh(planeGeometry, planeMaterial);
 // 旋转
 plane.rotation.z = THREE.Math.degToRad(item.ang);
 // 定位
 var position = getPosition(item.lng, item.lat, 5);
 plane.position.set(position.x, position.y, position.z);
 // 显示/隐藏
 // plane.visible = false;
 // 保存
 planeMarkers[item.anum] = plane;
 // 添加到场景
 group.add(plane);
 // 绘制历史轨迹
 drawHistoryTrack(item.anum);
 }
}

绘制轨迹(使用socket来获取的飞行轨迹经纬度坐标点)

// 时间段
var curTime = Date.parse(new Date())/1000;
var depTime = curTime - 30*60;
// 轨迹线质
var trackMaterial = new THREE.LineBasicMaterial({color : 0x1B94B1});
// 绘制历史轨迹
function drawHistoryTrack(anum) {
 socket.emit("fullPath", anum, depTime, curTime, function(status, data){
 if(status) {
 var dLength = data.length;
 if(dLength>=2) {
 var trackCoordArr = [];
 for(var i=0; i<dLength; i++) {
  if(data[i].lng && data[i].alt) {
  trackCoordArr.push({lng: data[i].lng, lat: data[i].lat});
  }
 }

 var tcaLength = trackCoordArr.length;
 if(tcaLength>=2) {
  var tcaHalfLength = Math.ceil(tcaLength/2),
  vertexArr = [];

  // 这里只取了三个点(起点、中点、终点)
  var p1 = getPosition(trackCoordArr[0].lng, trackCoordArr[0].lat, 0),
  p2 = getPosition(trackCoordArr[tcaHalfLength].lng, trackCoordArr[tcaHalfLength].lat, tcaLength*0.01),
  p3 = getPosition(trackCoordArr[tcaLength-1].lng, trackCoordArr[tcaLength-1].lat, 0);

  var trackCurve = new THREE.CatmullRomCurve3([
  new THREE.Vector3(p1.x, p1.y, p1.z),
  new THREE.Vector3(p2.x, p2.y, p2.z),
  new THREE.Vector3(p3.x, p3.y, p3.z)
  ]);

  var trackGeometry = new THREE.Geometry(),
  verticesArr = trackCurve.getPoints(tcaLength);

  trackGeometry.vertices = verticesArr;

  var trackLine = new THREE.Line(trackGeometry, trackMaterial);
  group.add(trackLine);

  // 动画点
  addLightPoint(p1, tcaLength, verticesArr);
 }
 }
 }
 });
}

如果要绘制所有点,且头尾是在球面上的曲线,则需要两次循环

var tcaRemainLength = tcaLength-tcaHalfLength
for(var j=0; j<tcaHalfLength; j++) { // 前一半
 var p1 = getPosition(trackCoordArr[j].lng, trackCoordArr[j].lat, j*0.05);
 vertexArr.push(new THREE.Vector3(p1.x, p1.y, p1.z));
}
for(var k=tcaRemainLength; k>0; k--) { // 后一半
 var p2 = getPosition(trackCoordArr[tcaLength-k].lng, trackCoordArr[tcaLength-k].lat, k*0.05);
 vertexArr.push(new THREE.Vector3(p2.x, p2.y, p2.z));
}

var trackCurve = new THREE.CatmullRomCurve3(vertexArr);

这个部分看看就行了。。

光点动画

// 点动画
var pointGeometry = new THREE.SphereGeometry(0.2, 20, 20);
var pointMaterial = new THREE.MeshBasicMaterial({color: 0x40E0D0});
function addLightPoint(pos, coordsNum ,verArr) {
 var pointMesh = new THREE.Mesh(pointGeometry, pointMaterial);
 pointMesh.position.set(pos.x, pos.y, pos.z);
 group.add(pointMesh);

 var index = 0;
 function pointAnimate() {
 index++;
 if(index>coordsNum) {
 index = 0;
 }
 pointMesh.position.set(verArr[index].x, verArr[index].y, verArr[index].z);
 requestAnimationFrame(pointAnimate);
 }
 pointAnimate();
}

这个点使用的是sphere,,当然也可以用顶点来实现,如下

var geometry = new THREE.Geometry();
geometry.vertices.push(new THREE.Vector3(0, 0, 0))
geometry.colors.push(new THREE.Color(0xffffff));

var material = new THREE.PointsMaterial({size: 1, vertexColors: THREE.VertexColors, opacity: 0.75, sizeAttenuation: true, transparent: true});
var point = new THREE.Points(geometry, material);
point.position.set(x, y, z);
group.add(point);

另外不想用光点动画的话,也可以用线动画,实现原理是不断更新顶点坐标,如下

var curveGeometry = new THREE.Geometry();
var curveData = new THREE.CatmullRomCurve3(verArr.slice(0, 10));
curveGeometry.vertices = curveData.getPoints(10);

var curveMaterial = new THREE.LineBasicMaterial({color: 0x40E0D0});
var curveLine = new THREE.Line(curveGeometry, curveMaterial);
group.add(curveLine);

var index = 0;
function lineAnimate() {
 index++;
 if(index>coordsNum-10) {
 index = 0;
 }
 var offsetData = verArr.slice(index, 10+index);
 if(offsetData.length > 0) {
 curveData = new THREE.CatmullRomCurve3(offsetData);
 curveLine.geometry.vertices = curveData.getPoints(10);
 curveLine.geometry.verticesNeedUpdate = true;
 }
 requestAnimationFrame(lineAnimate);
}
lineAnimate();

最后就是布置场景和事件了

// 初始化
function init() {
 container = document.getElementById('zh_globe_container');

 scene = new THREE.Scene();
 var bgTexture = new THREE.TextureLoader().load("images/textures/starfield.jpg");
 scene.background = bgTexture;

 camera = new THREE.PerspectiveCamera(50, winWth/winHgt, 1, 2000);
 camera.up.x = 0;
 camera.up.y = 1;
 camera.up.z = 0;
 camera.position.x = 0;
 camera.position.y = 0;
 camera.position.z = 400;
 camera.lookAt(0,0,0);

 group = new THREE.Group();
 scene.add(group);

 // 地球
 globe();

 // 飞机
 plane();

 // 星点
 stars();

 // 半球光
 lights();

 // 渲染器
 renderer = new THREE.WebGLRenderer({antialias: true, preserveDrawingBuffer: true});
 renderer.setPixelRatio(window.devicePixelRatio);
 renderer.setSize(winWth, winHgt);
 container.appendChild(renderer.domElement);

 // 盘旋控制
 var orbitControl = new THREE.OrbitControls(camera, renderer.domElement);
 orbitControl.minDistrance = 20;
 orbitControl.maxDistrance = 50;
 orbitControl.maxPolarAngle = Math.PI/2;

 // 性能测试
 stats = new Stats();
 container.appendChild(stats.dom);

 // resize事件
 window.addEventListener('resize', onWindowResize, false);
}

// 窗口大小改变
function onWindowResize() {
 camera.aspect = window.innerWidth/window.innerHeight;
 camera.updateProjectionMatrix();
 renderer.setSize(window.innerWidth, window.innerHeight);
}

// 渲染
function render() {
 group.rotation.y -= 0.0005;
 renderer.render(scene, camera);
}

// 动画
function animate() {
 requestAnimationFrame(animate);
 render();
 stats.update();
}

init();
animate(); 

完整代码:

var log = console.log.bind(console);

var globeObj = (function() {
 'use strict';

 // 判断浏览器是否支持webgl
 if(!Detector.webgl) Detector.addGetWebGLMessage();

 var container, stats;
 var camera, scene, renderer;
 var group;
 var mouseX = 0, mouseY = 0;
 var winWth = window.innerWidth, winHgt = window.innerHeight;

 // 获取position
 function getPosition(lng, lat, alt) {
 var phi = (90-lat)*(Math.PI/180),
 theta = (lng+180)*(Math.PI/180),
 radius = alt+200,
 x = -(radius * Math.sin(phi) * Math.cos(theta)),
 z = (radius * Math.sin(phi) * Math.sin(theta)),
 y = (radius * Math.cos(phi));
 return {x: x, y: y, z: z};
 }

 // 飞机
 function plane() {
 var socket = io('https://loc.variflight.com/*****此处接口地址不能给了', {transports: ['websocket']});

 var clientBounds = [52.793056,72.427908,2.970897,135.181814];

 // 连接
 socket.on('connect', function() {
 socket.emit("sub", clientBounds, -1, '', function(){});
 });

 // 飞机标记
 var planeMarkers = {};

 // 飞机形状
 var planeShape = new THREE.Shape();
 planeShape.moveTo( 0, 0);
 planeShape.lineTo(0.2, -0.2);
 planeShape.lineTo(0.2, -1.3);
 planeShape.lineTo(1.6,-2.7);
 planeShape.lineTo(1.6,-3);
 planeShape.lineTo(0.2, -2.1);
 planeShape.lineTo(0.2, -3);
 planeShape.lineTo(0.5, -3.4);
 planeShape.lineTo(0.5, -3.7);
 planeShape.lineTo(0, -3.3);
 planeShape.lineTo(-0.5, -3.7);
 planeShape.lineTo(-0.5, -3.4);
 planeShape.lineTo(-0.2, -3);
 planeShape.lineTo(-0.2, -2.1);
 planeShape.lineTo(-1.6,-3);
 planeShape.lineTo(-1.6,-2.7);
 planeShape.lineTo(-0.2, -1.3);
 planeShape.lineTo(-0.2, -0.2);
 var planeGeometry = new THREE.ShapeGeometry(planeShape);
 // 飞机材质
 var planeMaterial = new THREE.MeshPhongMaterial({color: 0x0FB4DD, side: THREE.DoubleSide, depthTest: true});
 // 添加飞机
 function addPlane(item) {
 if(item.anum && item.lng && item.lat) {
 var plane = new THREE.Mesh(planeGeometry, planeMaterial);
 // 旋转
 plane.rotation.z = THREE.Math.degToRad(item.ang);
 // 定位
 var position = getPosition(item.lng, item.lat, 5);
 plane.position.set(position.x, position.y, position.z);
 // 显示/隐藏
 // plane.visible = false;
 // 保存
 planeMarkers[item.anum] = plane;
 // 添加到场景
 group.add(plane);
 // 绘制历史轨迹
 drawHistoryTrack(item.anum);
 }
 }

 // 时间段
 var curTime = Date.parse(new Date())/1000;
 var depTime = curTime - 30*60;
 // 轨迹线质
 var trackMaterial = new THREE.LineBasicMaterial({color : 0x1B94B1});
 // 绘制历史轨迹
 function drawHistoryTrack(anum) {
 socket.emit("fullPath", anum, depTime, curTime, function(status, data){
 if(status) {
  var dLength = data.length;
  if(dLength>=2) {
  var trackCoordArr = [];
  for(var i=0; i<dLength; i++) {
  if(data[i].lng && data[i].alt) {
  trackCoordArr.push({lng: data[i].lng, lat: data[i].lat});
  }
  }

  var tcaLength = trackCoordArr.length;
  if(tcaLength>=2) {
  var tcaHalfLength = Math.ceil(tcaLength/2),
  tcaRemainLength = tcaLength-tcaHalfLength,
  vertexArr = [];

  /* 所有点
  for(var j=0; j<tcaHalfLength; j++) {
  var p1 = getPosition(trackCoordArr[j].lng, trackCoordArr[j].lat, j*0.05);
  vertexArr.push(new THREE.Vector3(p1.x, p1.y, p1.z));
  }
  for(var k=tcaRemainLength; k>0; k--) {
  var p2 = getPosition(trackCoordArr[tcaLength-k].lng, trackCoordArr[tcaLength-k].lat, k*0.05);
  vertexArr.push(new THREE.Vector3(p2.x, p2.y, p2.z));
  }

  var trackCurve = new THREE.CatmullRomCurve3(vertexArr);
  */

  // 三个点
  var p1 = getPosition(trackCoordArr[0].lng, trackCoordArr[0].lat, 0),
  p2 = getPosition(trackCoordArr[tcaHalfLength].lng, trackCoordArr[tcaHalfLength].lat, tcaLength*0.01),
  p3 = getPosition(trackCoordArr[tcaLength-1].lng, trackCoordArr[tcaLength-1].lat, 0);

  var trackCurve = new THREE.CatmullRomCurve3([
  new THREE.Vector3(p1.x, p1.y, p1.z),
  new THREE.Vector3(p2.x, p2.y, p2.z),
  new THREE.Vector3(p3.x, p3.y, p3.z)
  ]);

  var trackGeometry = new THREE.Geometry(),
  verticesArr = trackCurve.getPoints(tcaLength);

  trackGeometry.vertices = verticesArr;

  var trackLine = new THREE.Line(trackGeometry, trackMaterial);
  group.add(trackLine);

  // 动画点
  addLightPoint(p1, tcaLength, verticesArr);
  }
  }
 }
 });
 }

 // 点动画
 var pointGeometry = new THREE.SphereGeometry(0.2, 20, 20);
 var pointMaterial = new THREE.MeshBasicMaterial({color: 0x40E0D0});
 function addLightPoint(pos, coordsNum ,verArr) {
 var pointMesh = new THREE.Mesh(pointGeometry, pointMaterial);
 pointMesh.position.set(pos.x, pos.y, pos.z);
 group.add(pointMesh);

 var index = 0;
 function pointAnimate() {
 index++;
 if(index>coordsNum) {
  index = 0;
 }
 pointMesh.position.set(verArr[index].x, verArr[index].y, verArr[index].z);
 requestAnimationFrame(pointAnimate);
 }
 pointAnimate();

 /*var curveGeometry = new THREE.Geometry();
 var curveData = new THREE.CatmullRomCurve3(verArr.slice(0, 10));
 curveGeometry.vertices = curveData.getPoints(10);

 var curveMaterial = new THREE.LineBasicMaterial({color: 0x40E0D0});
 var curveLine = new THREE.Line(curveGeometry, curveMaterial);
 group.add(curveLine);

 var index = 0;
 function lineAnimate() {
 index++;
 if(index>coordsNum-10) {
  index = 0;
 }
 var offsetData = verArr.slice(index, 10+index);
 if(offsetData.length > 0) {
  curveData = new THREE.CatmullRomCurve3(offsetData);
  curveLine.geometry.vertices = curveData.getPoints(10);
  curveLine.geometry.verticesNeedUpdate = true;
 }
 requestAnimationFrame(lineAnimate);
 }
 lineAnimate();*/
 }

 // 监听数据(添加并更新)
 socket.on('~', function(res) {
 if($.isEmptyObject(planeMarkers)) {
 $.each(res, function(i, item) {
  addPlane(item);
 });
 } else {
 $.each(res, function(i, item) {
  if(planeMarkers[item.anum]) {
  if(item.lng && item.lat) {
  var pos = getPosition(item.lng, item.lat, 5);
  planeMarkers[item.anum].position.set(pos.x, pos.y, pos.z);
  }
  } else {
  addPlane(item);
  }
 });
 }
 });
 }

 // 地球
 function globe() {
 var globeTextureLoader = new THREE.TextureLoader();
 globeTextureLoader.load('images/textures/earth.jpg', function (texture) {
 var globeGgeometry = new THREE.SphereGeometry(200, 100, 100);
 var globeMaterial = new THREE.MeshStandardMaterial({map: texture});
 var globeMesh = new THREE.Mesh(globeGgeometry, globeMaterial);
 group.add(globeMesh);
 group.rotation.x = THREE.Math.degToRad(35);
 group.rotation.y = THREE.Math.degToRad(170);
 });
 }

 // 星点
 function stars() {
 var starsGeometry = new THREE.Geometry();
 for (var i = 0; i < 2000; i ++) {
 var starVector = new THREE.Vector3(
 THREE.Math.randFloatSpread(2000),
 THREE.Math.randFloatSpread(2000),
 THREE.Math.randFloatSpread(2000)
 );
 starsGeometry.vertices.push(starVector);
 }
 var starsMaterial = new THREE.PointsMaterial({color: 0x888888})
 var starsPoint = new THREE.Points(starsGeometry, starsMaterial);
 group.add(starsPoint);
 }

 // 光
 function lights() {
 var hemisphereLight = new THREE.HemisphereLight(0xffffff, 0x333333, 2);
 hemisphereLight.position.x = 0;
 hemisphereLight.position.y = 0;
 hemisphereLight.position.z = -200;
 group.add(hemisphereLight);
 }

 // 初始化
 function init() {
 container = document.getElementById('zh_globe_container');

 scene = new THREE.Scene();
 var bgTexture = new THREE.TextureLoader().load("images/textures/starfield.jpg");
 scene.background = bgTexture;

 camera = new THREE.PerspectiveCamera(50, winWth/winHgt, 1, 2000);
 camera.up.x = 0;
 camera.up.y = 1;
 camera.up.z = 0;
 camera.position.x = 0;
 camera.position.y = 0;
 camera.position.z = 400;
 camera.lookAt(0,0,0);

 group = new THREE.Group();
 scene.add(group);

 // 地球
 globe();

 // 飞机
 plane();

 // 星点
 stars();

 // 半球光
 lights();

 // 渲染器
 renderer = new THREE.WebGLRenderer({antialias: true, preserveDrawingBuffer: true});
 renderer.setPixelRatio(window.devicePixelRatio);
 renderer.setSize(winWth, winHgt);
 container.appendChild(renderer.domElement);

 // 盘旋控制
 var orbitControl = new THREE.OrbitControls(camera, renderer.domElement);
 orbitControl.minDistrance = 20;
 orbitControl.maxDistrance = 50;
 orbitControl.maxPolarAngle = Math.PI/2;

 // 性能测试
 stats = new Stats();
 container.appendChild(stats.dom);

 // resize事件
 window.addEventListener('resize', onWindowResize, false);
 }

 // 窗口大小改变
 function onWindowResize() {
 camera.aspect = window.innerWidth/window.innerHeight;
 camera.updateProjectionMatrix();
 renderer.setSize(window.innerWidth, window.innerHeight);
 }

 // 渲染
 function render() {
 group.rotation.y -= 0.0005;
 renderer.render(scene, camera);
 }

 // 动画
 function animate() {
 requestAnimationFrame(animate);
 render();
 stats.update();
 }

 init();
 animate();
})();

场景背景图

总结

以上就是关于利用three.js绘制地球、飞机、轨迹的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

(0)

相关推荐

  • Three.js实现绘制字体模型示例代码

    前言 本文主要给大家介绍了关于利用Three.js绘制字体模型的相关内容,使用three.js绘制字体模型,没有想象当中那么难.下面话不多说了,来一起看看详细的介绍: 首先你需要实例化 THREE.FontLoader() 来进行json格式的文字格式加载,在加载成功的回调函数里面进行创建网格. 然后通过THREE.TextBufferGeometry或者THREE.TextGeometry方法进行网格创建,并将需要设置的问题传入. 再设置一个纹理,通过THREE.Mesh()函数创建成图形添加

  • Three.js如何用轨迹球插件(trackball)增加对模型的交互功能详解

    前言 之前我们已经简单的给大家介绍了关于three.js入门的一些案例,下面本文将详细介绍关于Three.js如何用轨迹球插件(trackball)增加对模型的交互功能,下面话不多说了,来一起看看详细的介绍吧. 这是three.js的一个组件,需要额外的引入文件,文件的地址是在官方下载的案例examples/js/controls/TrackballControls.js. 只需要和案例里面一样设置相关的属性,并在实例化的时候讲相机传入.就可以实现交互效果. 可以实现的效果: 鼠标按住左键可以旋

  • 利用Three.js如何实现阴影效果实例代码

    前言 众所周知作为webgl的插件,three.js肯定没有原生webgl那样,添加一个阴影这么费劲.所以,经过一小时的研究(笨人不聪明,已经是极限速度了).终于将阴影效果做了出来,并且还发现一些容易犯错的地方.话不多说了,来一起看看详细的介绍吧. 先上效果图: 实现这个效果其实很简单,只需要设置几个属性就可以实现当前的效果.而上面的材质问题我将放到下一节: (1)首先需要告诉渲染器我需要阴影,你给我生成阴影: renderer.shadowMap.enabled = true; (2)然后告诉

  • Three.js基础部分学习

    一.关于使用Three.js几点理论说明 1.请参考官网地址 https://threejs.org/ 2.使用three.js必备条件  <场景 A scene.相机a camera.渲染器 a renderer  三者缺一不可> To actually be able to display anything with Three.js, we need three things: A scene, a camera, and a renderer so we can render the

  • Three.js快速入门教程

    引言 本文主要是讲解Three.js的相关概念,帮助读者对Three.js以及相关知识形成比较完整的理解. 近年来web得到了快速的发展.随着HTML5的普及,网页的表现能力越来越强大.网页上已经可以做出很多复杂的动画,精美的效果. 但是,人总是贪的.那么,在此之上还能做什么呢?其中一种就是通过WebGL在网页中绘制高性能的3D图形. OpenGL,WebGL到Three.js OpenGL大概许多人都有所耳闻,它是最常用的跨平台图形库. WebGL是基于OpenGL设计的面向web的图形标准,

  • JS库之Three.js 简易入门教程(详解之一)

    开场白 webGL可以让我们在canvas上实现3D效果.而three.js是一款webGL框架,由于其易用性被广泛应用.如果你要学习webGL,抛弃那些复杂的原生接口从这款框架入手是一个不错的选择. 博主目前也在学习three.js,发现相关资料非常稀少,甚至官方的api文档也非常粗糙,很多效果需要自己慢慢敲代码摸索.所以我写这个教程的目的一是自己总结,二是与大家分享. 本篇是系列教程的第一篇:入门篇.在这篇文章中,我将以一个简单的demo为例,阐述three.js的基本配置方法.学完这篇文章

  • Three.js利用性能插件stats实现性能监听的方法

    前言 关于性能:测试一个程序,性能上是否有瓶颈,在3D世界里,经常使用帧数的概念,首先我们来定义一下帧数的意义. 帧数:图形处理器每秒钟能够刷新几次,通常用fps(Frames Per Second)来表示 关于性能:测试一个程序,性能上是否有瓶颈,在3D世界里,经常使用帧数的概念,首先我们来定义一下帧数的意义. 帧数:图形处理器每秒钟能够刷新几次,通常用fps(Frames Per Second)来表示 stats性能插件添加了以后,会默认在左上角显示性能帧数,每次刷新所用时间,占用内存.鼠标

  • Three.js入门之hello world以及如何绘制线

    前言 本文属于学习Three.js 的入门教程,文中通过示例介绍了hello world和线的实现,下面话不多说了,来一起看看详细的介绍吧. hello world 首先使用我们先用three.js创建一个立方体的hello world类型的案例. <!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content=

  • Three.js利用orbit controls插件(轨道控制)控制模型交互动作详解

    前言 本文主要给大家介绍了关于Three.js利用orbit controls插件(轨道控制)控制模型交互动作的相关内容,这个效果相对于第八节的轨迹球插件使用上感觉要好,虽然轨迹球插件可以来回的滚动,但是容易分辨不清楚上下左右的关系,容易混乱,适合调试,而轨道控制插件orbit则适合客户使用,还不会产生混乱效果.下面讲一下使用. (1)首先引入插件,文件地址在官方案例的examples/js/controls/OrbitControls.js. (2)然后实例化函数,把相机和渲染器的dom传入,

  • Three.js利用Detector.js插件如何实现兼容性检测详解

    前言 本文主要给大家介绍了关于Three.js用Detector.js插件实现兼容性检测的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 其实Detector.js插件的代码很短,但是功能很全, (1)判断canvas兼容. (2)判断webgl兼容性. (3)在页面添加不兼容提示信息. 这三个功能已经对兼容性检测足够了. 使用方式也很简单: 首先,将插件引入到页面: <script src="examples/js/Detector.js">&

随机推荐