第一章 用three.js创建你的第一个3D场景

到官网下载three.js的源码和示例。

创建HTML框架界面

第一个示例的代码如下: 01-basic-skeleton.html 位于

  1. Learning Three.js- The JavaScript 3D Library for WebGL\chapter-01

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Example 01.01 - Basic skeleton</title>
  5. <script type="text/javascript"
  6. src="../libs/three.js"></script>
  7. <script type="text/javascript"
  8. src="../libs/jquery-1.9.0.js"></script>
  9. <style>
  10. body{
  11. /* set margin to 0 and overflow to hidden,
  12. to use the complete page */
  13. margin: 0;
  14. overflow: hidden;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <!-- Div which will hold the Output -->
  20. <div id="WebGL-output">
  21. </div>
  22. <!-- Javascript code that runs our Three.js examples -->
  23. <script type="text/javascript">
  24. // once everything is loaded, we run our Three.js stuff.
  25. $(function () {
  26. // here we'll put the Three.js stuff
  27. });
  28. </script>
  29. </body>
  30. </html>

Three.js 有两个版本:

• Three.min.js:压缩版本,去掉了空格和换行,大小变小了,加载时占用系统资源小。

• Three.js:未压缩版本,易于阅读和调试,但加载时占用系统资源较大。

渲染和展示一个三维物体

我们接下来将会创建我们的第一个场景并添加一系列的物体。其中包括:

Plane :有两个面的矩形,作为我们场景的地面,将会在场景中部渲染成一个灰色的矩形。

Cube :正方体,我们将渲染成红色。

Sphere :球体,我们将渲染成蓝色。

Camera :相机决定了看到了什么。

Axes :坐标轴,x,y,z轴。一个很好的调试工具,能够让你知道物体渲染在哪里。

第二个示例的代码如下 02-first-scene.html 位于

  1. Learning Three.js- The JavaScript 3D Library for WebGL\chapter-01

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Example 01.02 - First Scene</title>
  5. <script type="text/javascript" src="../libs/three.js"></script>
  6. <script type="text/javascript" src="../libs/jquery-1.9.0.js"></script>
  7. <style>
  8. body{
  9. /* set margin to 0 and overflow to hidden, to go fullscreen */
  10. margin: 0;
  11. overflow: hidden;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <!-- Div which will hold the Output -->
  17. <div id="WebGL-output">
  18. </div>
  19. <!-- Javascript code that runs our Three.js examples -->
  20. <script type="text/javascript">
  21. // once everything is loaded, we run our Three.js stuff.
  22. $(function () {
  23. // create a scene, that will hold all our elements such as objects, cameras and lights.
  24. var scene = new THREE.Scene();
  25. // create a camera, which defines where we're looking at.
  26. var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
  27. // create a render and set the size
  28. var renderer = new THREE.WebGLRenderer();
  29. renderer.setClearColorHex(0xEEEEEE);
  30. renderer.setSize(window.innerWidth, window.innerHeight);
  31. var axes = new THREE.AxisHelper( 20 );
  32. scene.add(axes);
  33. // create the ground plane
  34. var planeGeometry = new THREE.PlaneGeometry(60,20);
  35. var planeMaterial = new THREE.MeshBasicMaterial({color: 0xcccccc});
  36. var plane = new THREE.Mesh(planeGeometry,planeMaterial);
  37. // rotate and position the plane
  38. plane.rotation.x=-0.5*Math.PI;
  39. plane.position.x=15
  40. plane.position.y=0
  41. plane.position.z=0
  42. // add the plane to the scene
  43. scene.add(plane);
  44. // create a cube
  45. var cubeGeometry = new THREE.CubeGeometry(4,4,4);
  46. var cubeMaterial = new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: true});
  47. var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  48. // position the cube
  49. cube.position.x=-4;
  50. cube.position.y=3;
  51. cube.position.z=0;
  52. // add the cube to the scene
  53. scene.add(cube);
  54. var sphereGeometry = new THREE.SphereGeometry(4,20,20);
  55. var sphereMaterial = new THREE.MeshBasicMaterial({color: 0x7777ff, wireframe: true});
  56. var sphere = new THREE.Mesh(sphereGeometry,sphereMaterialMaterial);
  57. // position the sphere
  58. sphere.position.x=20;
  59. sphere.position.y=4;
  60. sphere.position.z=2;
  61. // add the sphere to the scene
  62. scene.add(sphere);
  63. // position and point the camera to the center of the scene
  64. camera.position.x = -30;
  65. camera.position.y = 40;
  66. camera.position.z = 30;
  67. camera.lookAt(scene.position);
  68. // add the output of the renderer to the html element
  69. $("#WebGL-output").append(renderer.domElement);
  70. // render the scene
  71. renderer.render(scene, camera);
  72. });
  73. </script>
  74. </body>
  75. </html>

可知有如下几个步骤:

  1. 创建并设置scene、render、camera。

  2. 创建对象,指定Geometry和Material,设置position并加入到scene中

  3. 把渲染的结果显示在 $('div#WebGL-output')对象中。

  4. 开始渲染。

加上材质、灯光和阴影

第三个示例的代码如下: 03-materials-light.html 位于

  1. Learning Three.js- The JavaScript 3D Library for WebGL\chapter-01

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Example 01.03 - Materials and light</title>
  5. <script type="text/javascript" src="../libs/three.js"></script>
  6. <script type="text/javascript" src="../libs/jquery-1.9.0.js"></script>
  7. <script type="text/javascript" src="../libs/stats.js"></script>
  8. <script type="text/javascript" src="../libs/dat.gui.js"></script>
  9. <style>
  10. body{
  11. /* set margin to 0 and overflow to hidden, to go fullscreen */
  12. margin: 0;
  13. overflow: hidden;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <!-- Div which will hold the Output -->
  19. <div id="WebGL-output">
  20. </div>
  21. <!-- Javascript code that runs our Three.js examples -->
  22. <script type="text/javascript">
  23. // once everything is loaded, we run our Three.js stuff.
  24. $(function () {
  25. // create a scene, that will hold all our elements such as objects, cameras and lights.
  26. var scene = new THREE.Scene();
  27. // create a camera, which defines where we're looking at.
  28. var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
  29. // create a render and set the size
  30. var renderer = new THREE.WebGLRenderer();
  31. renderer.setClearColorHex(0xEEEEEE, 1.0);
  32. renderer.setSize(window.innerWidth, window.innerHeight);
  33. renderer.shadowMapEnabled = true;
  34. // create the ground plane
  35. var planeGeometry = new THREE.PlaneGeometry(60,20);
  36. var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});
  37. var plane = new THREE.Mesh(planeGeometry,planeMaterial);
  38. plane.receiveShadow = true;
  39. // rotate and position the plane
  40. plane.rotation.x=-0.5*Math.PI;
  41. plane.position.x=15
  42. plane.position.y=0
  43. plane.position.z=0
  44. // add the plane to the scene
  45. scene.add(plane);
  46. // create a cube
  47. var cubeGeometry = new THREE.CubeGeometry(4,4,4);
  48. var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff0000});
  49. var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  50. cube.castShadow = true;
  51. // position the cube
  52. cube.position.x=-4;
  53. cube.position.y=3;
  54. cube.position.z=0;
  55. // add the cube to the scene
  56. scene.add(cube);
  57. var sphereGeometry = new THREE.SphereGeometry(4,20,20);
  58. var sphereMaterial = new THREE.MeshLambertMaterial({color: 0x7777ff});
  59. var sphere = new THREE.Mesh(sphereGeometry,sphereMaterial);
  60. // position the sphere
  61. sphere.position.x=20;
  62. sphere.position.y=4;
  63. sphere.position.z=2;
  64. sphere.castShadow=true;
  65. // add the sphere to the scene
  66. scene.add(sphere);
  67. // position and point the camera to the center of the scene
  68. camera.position.x = -30;
  69. camera.position.y = 40;
  70. camera.position.z = 30;
  71. camera.lookAt(scene.position);
  72. // add spotlight for the shadows
  73. var spotLight = new THREE.SpotLight( 0xffffff );
  74. spotLight.position.set( -40, 60, -10 );
  75. spotLight.castShadow = true;
  76. scene.add( spotLight );
  77. // add the output of the renderer to the html element
  78. $("#WebGL-output").append(renderer.domElement);
  79. // call the render function
  80. renderer.render(scene, camera);
  81. });
  82. </script>
  83. </body>
  84. </html>

分析示例二和示例一可以发现不同之处。

首先,示例二添加的灯光 ,类型是点光源,并且打开了阴影效果。

  1. // add spotlight for the shadows
  2. var spotLight = new THREE.SpotLight( 0xffffff );
  3. spotLight.position.set( -40, 60, -10 );
  4. spotLight.castShadow = true;
  5. scene.add( spotLight );

然后,示例一采用的是BasicMaterial材质(效果为三角网格),而示例二采用的是LambertMaterial

材质(效果为一般的纸质效果)。

用动画拓展你的第一个场景

示例四的源码如下: 04-materials-light-animation.html 位于

  1. Learning Three.js- The JavaScript 3D Library for WebGL\chapter-01

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Example 01.04 - Materials, light and animation</title>
  5. <script type="text/javascript" src="../libs/three.js"></script>
  6. <script type="text/javascript" src="../libs/jquery-1.9.0.js"></script>
  7. <script type="text/javascript" src="../libs/stats.js"></script>
  8. <style>
  9. body{
  10. /* set margin to 0 and overflow to hidden, to go fullscreen */
  11. margin: 0;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="Stats-output">
  18. </div>
  19. <!-- Div which will hold the Output -->
  20. <div id="WebGL-output">
  21. </div>
  22. <!-- Javascript code that runs our Three.js examples -->
  23. <script type="text/javascript">
  24. // once everything is loaded, we run our Three.js stuff.
  25. $(function () {
  26. var stats = initStats();
  27. // create a scene, that will hold all our elements such as objects, cameras and lights.
  28. var scene = new THREE.Scene();
  29. // create a camera, which defines where we're looking at.
  30. var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
  31. // create a render and set the size
  32. var renderer = new THREE.WebGLRenderer();
  33. renderer.setClearColorHex(0xEEEEEE, 1.0);
  34. renderer.setSize(window.innerWidth, window.innerHeight);
  35. renderer.shadowMapEnabled = true;
  36. // create the ground plane
  37. var planeGeometry = new THREE.PlaneGeometry(60,20,1,1);
  38. var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});
  39. var plane = new THREE.Mesh(planeGeometry,planeMaterial);
  40. plane.receiveShadow = true;
  41. // rotate and position the plane
  42. plane.rotation.x=-0.5*Math.PI;
  43. plane.position.x=15
  44. plane.position.y=0
  45. plane.position.z=0
  46. // add the plane to the scene
  47. scene.add(plane);
  48. // create a cube
  49. var cubeGeometry = new THREE.CubeGeometry(4,4,4);
  50. var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff0000});
  51. var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  52. cube.castShadow = true;
  53. // position the cube
  54. cube.position.x=-4;
  55. cube.position.y=3;
  56. cube.position.z=0;
  57. // add the cube to the scene
  58. scene.add(cube);
  59. var sphereGeometry = new THREE.SphereGeometry(4,20,20);
  60. var sphereMaterial = new THREE.MeshLambertMaterial({color: 0x7777ff});
  61. var sphere = new THREE.Mesh(sphereGeometry,sphereMaterial);
  62. // position the sphere
  63. sphere.position.x=20;
  64. sphere.position.y=0;
  65. sphere.position.z=2;
  66. sphere.castShadow=true;
  67. // add the sphere to the scene
  68. scene.add(sphere);
  69. // position and point the camera to the center of the scene
  70. camera.position.x = -30;
  71. camera.position.y = 40;
  72. camera.position.z = 30;
  73. camera.lookAt(scene.position);
  74. // add subtle ambient lighting
  75. var ambientLight = new THREE.AmbientLight(0x0c0c0c);
  76. scene.add(ambientLight);
  77. // add spotlight for the shadows
  78. var spotLight = new THREE.SpotLight( 0xffffff );
  79. spotLight.position.set( -40, 60, -10 );
  80. spotLight.castShadow = true;
  81. scene.add( spotLight );
  82. // add the output of the renderer to the html element
  83. $("#WebGL-output").append(renderer.domElement);
  84. // call the render function
  85. var step=0;
  86. render();
  87. function render() {
  88. stats.update();
  89. // rotate the cube around its axes
  90. cube.rotation.x += 0.02;
  91. cube.rotation.y += 0.02;
  92. cube.rotation.z += 0.02;
  93. // bounce the sphere up and down
  94. step+=0.04;
  95. sphere.position.x = 20+( 10*(Math.cos(step)));
  96. sphere.position.y = 2 +( 10*Math.abs(Math.sin(step)));
  97. // render using requestAnimationFrame
  98. requestAnimationFrame(render);
  99. renderer.render(scene, camera);
  100. }
  101. function initStats() {
  102. var stats = new Stats();
  103. stats.setMode(0); // 0: fps, 1: ms
  104. // Align top-left
  105. stats.domElement.style.position = 'absolute';
  106. stats.domElement.style.left = '0px';
  107. stats.domElement.style.top = '0px';
  108. $("#Stats-output").append( stats.domElement );
  109. return stats;
  110. }
  111. });
  112. </script>
  113. </body>
  114. </html>

分析源码可知与示例三的不同有:

  1. function initStats() {
  2. var stats = new Stats();
  3. stats.setMode(0); // 0: fps, 1: ms
  4. // Align top-left
  5. stats.domElement.style.position = 'absolute';
  6. stats.domElement.style.left = '0px';
  7. stats.domElement.style.top = '0px';
  8. $("#Stats-output").append( stats.domElement );
  9. return stats;
  10. }

这里这个函数创建了一个Stats,也就是左上角的显示帧数的方框。方法就是创建一个Stats,并设置

参数,然后将结果显示在$('#Stats-output')中。

然后看这段代码:

  1. var step=0;
  2. render();
  3. function render() {
  4. stats.update();
  5. // rotate the cube around its axes
  6. cube.rotation.x += 0.02;
  7. cube.rotation.y += 0.02;
  8. cube.rotation.z += 0.02;
  9. // bounce the sphere up and down
  10. step+=0.04;
  11. sphere.position.x = 20+( 10*(Math.cos(step)));
  12. sphere.position.y = 2 +( 10*Math.abs(Math.sin(step)));
  13. // render using requestAnimationFrame
  14. requestAnimationFrame(render);
  15. renderer.render(scene, camera);
  16. }

这里更改了cube和sphere的角度和位置,然后调用requestAnimationFrame函数,该函数就是调用传入的参数render,然后就进入渲染循环,就产生了动画。

运用 dat.GUI库使得实验更简易

创建一个GUI 接受rotationSpeed和bouncingSpeed,值在0~0.5之间。

  1. var gui = new dat.GUI();
  2. gui.add(controls, 'rotationSpeed',0,0.5);
  3. gui.add(controls, 'bouncingSpeed',0,0.5);

创建一个javascript对象用来包含rotationSpeed和bouncingSpeed。

  1. var controls = new function() {
  2. this.rotationSpeed = 0.02;
  3. this.bouncingSpeed = 0.03;
  4. }

然后改变的角度和位置可以采用controls的值。

第一章 用three.js创建你的第一个3D场景的更多相关文章

  1. Seen.js – 使用 SVG 或者 Canvas 渲染 3D 场景

    Seen.js 渲染3D场景为 SVG 或者 HTML5 画布.Seen.js 包含对于 SVG 和 HTML5 Canvas 元素的图形功能的最简单的抽象.所有这个库的其它组件都是不用关心将要渲染的 ...

  2. Node.js学习(第一章:Node.js安装和模块化理解)

    Node.js安装和简单使用 安装方法 简单的安装方式是直接官网下载,然后本地安装即可.官网地址:nodejs.org Windows系统下,选择和系统版本匹配的.msi后缀的安装文件.Mac OS ...

  3. Three-js 创建第一个3D场景

    1.一个场景至少需要的三种类型组件 相机/决定哪些东西将在屏幕上渲染    光源/他们会对材质如何显示,以及生成阴影时材质如何使用产生影响    物体/他们是在相机透视图里主要的渲染队形:方块.球体等 ...

  4. NodeJs>------->>第一章:Node.js介绍

    一:章节前言 二:Node.js概述 1:使用node.js能够解决什么问题 2:实现高性能服务器 3:非阻塞型I/O及事件环形机制 4:node.js适合开发的程序 三:node.js安装 一.No ...

  5. Node.js学习(第一章:Node.js简介)

    Node.js是什么? Node.js 诞生于 2009 年,由 Joyent 的员工 Ryan Dahl 开发而成, 目前官网最新版本已经更新到 12.0.0版本,最新稳定的是10.15.3.Nod ...

  6. Learning From Data 第一章总结

    之前上了台大的机器学习基石课程,里面用的教材是<Learning from data>,最近看了看觉得不错,打算深入看下去,内容上和台大的课程差不太多,但是有些点讲的更深入,想了解课程里面 ...

  7. css3创建3D场景

    浏览器本身是一个2维平面,对于3D的情况,实际上是增加了一个维度(深度),所以我们需要创建一个3D场景.这时浏览器不仅仅是一个平面了,更像是一个窗口,我们透过这个窗口去观察里面的三维世界.所谓的创建3 ...

  8. -Three.js开发指南---用three.js创建你的第一个三维场景(第一章)

    本章主要做了下面的工作 1 生成一个简单的场景,该场景的物体只有平面和坐标轴 2 在第一个demo的基础上添加光源和方块物体,并生成阴影 3 在第二个demo的基础上,增加动画,使得方块进行旋转 4 ...

  9. 第一章 创建WEB项目

    第一章   创建WEB项目 一.Eclipse创建WEB项目 方法/步骤1 首先,你要先打开Eclipse软件,打开后在工具栏依次点击[File]>>>[New]>>&g ...

随机推荐

  1. Oracle hextoraw和rawtohex

    Oracle hextoraw和rawtohex [日期:2012-07-17] 来源:Linux社区  作者:adrain_001 [字体:大 中 小]     HEXTORAW  语法: HEXT ...

  2. iOS svn版本回退 cornerstone

    http://blog.csdn.net/x32sky/article/details/46866899   IOS开发中,SVN如何恢复到某一个版本(以Cornerstone为例) Cornerst ...

  3. OC与Swift的区别五(函数)

    13 函数 oc函数定义: 返回值类型 函数名(参数类型 参数名,参数类型 参数名){ } swift 函数定义: func 函数名(参数名:参数类型,参数名:参数类型) -> 返回值类型{ } ...

  4. javaIo流实际应用

    /*查看目录下所有的文件*/ package cn.file; import java.io.File; public class Text2 { public static void main(St ...

  5. swift基本语法

    swift种语法着实怪异,实质干的事情还是一样的,一下将对此语法做简单介绍: 1.swift语法种已经剔除“:”这个结束符号,下面将演示入门操作的hello world import Foundati ...

  6. 如何在Html的div+css中去除<li>标签前面小黑点,和ul、LI部分属性方法

    div是很多人做网站都会用到的,但在显示效果时前面总是会有一个小黑点,这个效果很多人不想要,但又不知到如何去除,然而我们可以用以下方法来清除. 1.在CSS中写入代码.找到相关性的CSS,在..li和 ...

  7. javascript进阶——测试和打包分发

    建立一个面向对象的好的代码基础后,为了达到代码重用的目的,通过调试使用适当的测试用例进行测试尤为必要,之后就是打包分发的主题. 一.调试与测试 1.调试 Firebug:包含了错误控制台.调试器.DO ...

  8. centos 6.3 64位安装php5.5及配置tengine

    PHP 用到的工具包: yum install gd-devel libjpeg-devel libpng-devel freetype-devel libxml2-devel curl-devel ...

  9. makefile中PHONY的重要性

    伪目标是这样一个目标:它不代表一个真正的文件名,在执行make时可以指定这个目标来执行所在规则定义的命令,有时也可以将一个伪目标称为标签.伪目标通过   PHONY来指明. PHONY定义伪目标的命令 ...

  10. html 5 中的 6位 十六进制颜色码 代表的意思

    人的眼睛看到的颜色有两种: ⒈ 一种是发光体发出的颜色,比如计算机显示器屏幕显示的颜色: ⒉ 另一种是物体本身不发光,而是反射的光产生 十六进制颜色码 的颜色,比如看报纸和杂志上的颜色. 我们又知道任 ...