three.js 简介

What is Three.js?

Let‘s try to describe it briefly:

Three.js is a library that makes WebGL - 3D in the browser - easy to use. While a simple cube in raw WebGL would turn out hundreds of lines of Javascript and shader code, a Three.js equivalent is only a fraction of that.

Before we start

Before you can use Three.js, you need somewhere to display it. Save the following HTML to a file on your computer, along with a copy ofthree.min.js in the js/ directory, and open it in your browser.

<html> <head> <title>My first Three.js app</title> <style> body { margin: 0; } canvas { width: 100%; height: 100% } </style> </head> <body> <script src="js/three.min.js"></script> <script> // Our Javascript will go here. </script> </body> </html>

That‘s all. All the code below goes into the empty <script> tag.

Creating the scene

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 scene with the camera.

1 var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); var renderer = new THREE.WebGLRenderer(); renderer.setSize( window.innerWidth, window.innerHeight ); document.body.appendChild( renderer.domElement );

Let‘s take a moment to explain what‘s going on here. We have now set up the scene, our camera and the renderer. There are a few different cameras in Three.js. For now, let‘s use a PerspectiveCamera. The first attribute is the field of view.

The second one is the aspect ratio. You almost always want to use the width of the element divided by the height, or you‘ll get the same result as when you play old movies on a widescreen TV - the image looks squished.

The next two attributes are the near and far clipping plane. What that means, is that objects further away from the camera than the value of far or closer than near won‘t be rendered. You don‘t have to worry about this now, but you may want to use other values in your games to get better performance.

Next up is the renderer. This is where the magic happens. In addition to the WebGLRenderer we use here, Three.js comes with a few others, often used as fallbacks for users with older browsers or for those who don‘t have WebGL support for some reason.

In addition to creating the renderer instance, we also need to set the size at which we want it to render our app. It‘s a good idea to use the width and height of the area we want to fill with our game - in this case, the width and height of the browser window. For performance intensive games, you can also give setSize smaller values, like window.innerWidth/2 and window.innerHeight/2, for half the resolution. This does not mean that the game will only fill half the window, but rather look a bit blurry and scaled up.

Last but not least, we add the renderer element to our HTML document. This is a <canvas> element the renderer uses to display the scene to us.

"That‘s all good, but where‘s that cube you promised?" Let‘s add it now.

1 var geometry = new THREE.BoxGeometry( 1, 1, 1 ); var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } ); var cube = new THREE.Mesh( geometry, material ); scene.add( cube ); camera.position.z = 5;

To create a cube, we need a BoxGeometry. This is an object that contains all the points (vertices) and fill (faces) of the cube. We‘ll explore this more in the future.

In addition to the geometry, we need a material to color it. Three.js comes with several materials, but we‘ll stick to the MeshBasicMaterialfor now. All materials take an object of properties which will be applied to them. To keep things very simple, we only supply a color attribute of 0x00ff00, which is green. This works the same way that colors work in CSS or Photoshop (hex colors).

The third thing we need is a Mesh. A mesh is an object that takes a geometry, and applies a material to it, which we then can insert to our scene, and move freely around.

By default, when we call scene.add(), the thing we add will be added to the coordinates (0,0,0). This would cause both the camera and the cube to be inside each other. To avoid this, we simply move the camera out a bit.

Rendering the scene

If you copied the code from above into the HTML file we created earlier, you wouldn‘t be able to see anything. This is because we‘re not actually rendering anything yet. For that, we need what‘s called a render loop.

function render() { requestAnimationFrame( render ); renderer.render( scene, camera ); } render();

This will create a loop that causes the renderer to draw the scene 60 times per second. If you‘re new to writing games in the browser, you might say "why don‘t we just create a setInterval? The thing is - we could, but requestAnimationFrame has a number of advantages. Perhaps the most important one is that it pauses when the user navigates to another browser tab, hence not wasting their precious processing power and battery life.

Animating the cube

If you insert all the code above into the file you created before we began, you should see a green box. Let‘s make it all a little more interesting by rotating it.

Add the following right above the renderer.render call in your render function:

1 cube.rotation.x += 0.1; cube.rotation.y += 0.1;

This will be run every frame (60 times per second), and give the cube a nice rotation animation. Basically, anything you want to move or change while the game / app is running has to go through the render loop. You can of course call other functions from there, so that you don‘t end up with a render function that‘s hundreds of lines.

The result

Congratulations! You have now completed your first Three.js application. It‘s simple, you have to start somewhere.

The full code is available below. Play around with it to get a better understanding of how it works.

1 <html> <head> <title>My first Three.js app</title> <style> body { margin: 0; } canvas { width: 100%; height: 100% } </style> </head> <body> <script src="js/three.min.js"></script> <script> var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 ); var renderer = new THREE.WebGLRenderer(); renderer.setSize( window.innerWidth, window.innerHeight ); document.body.appendChild( renderer.domElement ); var geometry = new THREE.BoxGeometry( 1, 1, 1 ); var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } ); var cube = new THREE.Mesh( geometry, material ); scene.add( cube ); camera.position.z = 5; var render = function () { requestAnimationFrame( render ); cube.rotation.x += 0.1; cube.rotation.y += 0.1; renderer.render(scene, camera); }; render(); </script> </body> </html>
时间: 2024-10-01 04:23:57

three.js 简介的相关文章

jquery.flot.js简介

JQuery图表插件之Flot Flot是一个Jquery下图表插件,具有简单使用,交互效果,具有吸引力外观特点.目前支持 Internet Explorer 6+, Chrome, Firefox 2+, Safari 3+ and Opera 9.5+ 等浏览器,是一个基于Javascript和Jquery纯客端户的脚本库,下面看一个简单的示例,先插入js: <script language="javascript" type="text/javascript&qu

jquery.easypiechart.js简介

此插件主要是用来统计新的访问.跳出率.服务器负载.使用的RAM等,功能很强大,带有HTML5的动画效果,效果非常炫,看效果吧easyPieChart一款新型的EASY饼图数据统计Jquery插件截图: 插件下载:https://github.com/rendro/easy-pie-chart/ jquery.easypiechart.js简介,布布扣,bubuko.com

jquery.sparkline.js简介

jQuery线状图插件Sparkline 官网地址:http://omnipotent.net/jquery.sparkline/ 文档地址:http://omnipotent.net/jquery.sparkline/#docs 下载地址:http://omnipotent.net/jquery.sparkline/#download jquery.sparkline.js简介,布布扣,bubuko.com

Vue.js简介

Vue.js简介 Vue.js的作者为Evan You(尤雨溪),任职于Google Creative Lab,虽然是Vue是一个个人项目,但在发展前景上个人认为绝不输于Google的AngularJs,下面我会将Vue与Angular(Angular 1.0+版本)做一些简单的比较. Vue的主要特点就和它官网(http://cn.vuejs.org/)所介绍的那样: (1) 简洁 (2) 轻量 (3)快速 (4) 数据驱动 (5) 模块友好 (6) 组件化 简单 下面看一段Angular的实

01 Node.js简介, 安装&amp;配置

Node.js 简介 Node.js 是什么 Node.js 有着强大而灵活的包管理器(node package manager,npm) 目前, 已经有强大第三方工具模块, 例如数据库连接, 网站开发框架, CSS生成器, 操作系统API, 网络通信 等. Node.js 是什么 ? 是一个让 javascript 运行在服务器端的平台, 以前javascript只能运行在浏览器中,  node.js 可以解析 javascript. CommonJS 试图设计一套Javascript的规范.

Analytics.js简介

analytics.js JavaScript代码段是一种可用于衡量用户与您网站的互动情况的全新方式.它与之前的跟踪代码ga.js类似,但为开发者自定义实现方案提供了更大的灵活性. analytics.js代码段是Universal Analytics(目前处于公测阶段)的一部分.新用户应使用analytics.js.现有的ga.js用户应为analytics.js创建新的网络媒体资源并在他们的网站中进行两次代码实现.在同一页面上添加ga.js和analytics.js代码段并不会有任何安全问题

jquery.gritter.js简介

Gritter 是一个小型的 jQuery 消息通知插件,通知效果如下图所示: 参考网 jquery.gritter.js简介,布布扣,bubuko.com

探秘Node.js(一)——Node.js简介及安装配置

1.Node.js 简介及特点: Node.js 是一个可以让 JavaScript 运行在服务器端的平台,它可以让JavaScript 脱离浏览器的束缚运行在一般的服务器环境下,就像运行 Python. Perl. PHP. Ruby 程序一样.我们可以用 Node.js 轻松地进行服务器端应用开发,Python. Perl. PHP. Ruby 能做的事 Node.js 几乎都能做,而且可以做得更好. Node.js 最大的特点就是采用异步式 I/O 与事件驱动的架构设计.对于高并发的解决方

Gulp.js简介

Gulp.js简介 我们讨论了很多关于怎么减少页面体积,提高重网站性能的方法.有些是操作是一劳永逸的,如开启服务器的gzip压缩,使用适当的图片格式,或删除一些不必要的字符.但有一些任务是每次工作都必须反复执行的.如 新图片或修改后图片的压缩 去除调试语句如console,debugger 合并和压缩css和js 把更新的文件部署到服务器 你可能设想大家都会记住所有的这些操作,但总会有人忘记一两条.随着项目越来越大,上面的工作越来越浪费时间.只好设专人来完成这些枯燥的工作. 能不能让这些工作不用

jquery.peity.js简介

jQuery简单图表peity.js [javascript] <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>无标题页</title> <script type="text/javascript" src="jQuery.js"></script> <script type="text/ja