一个简单的场景

  1. 准备
    a. 准备HTML页面
    b. 引入Three.js
    c. 添加Dom容器
    2. 渲染三维对象
    a. 创建场景
    new THREE.Scene()
    b. 创建相机
    new THREE.PerspectiveCamera(45, window.innerWidth /window.innerHeight, 0.1, 1000);
    c. 创建渲染器
    new THREE.WebGLRenderer()
    d. 创建一个立方体
    new THREE.BoxGeometry(4, 4, 4)
    e. 创建一种材质
    new THREE.MeshBasicMaterial({color: 0xcccccc})
    f. 物体和材质构成一个Mesh(网格对象)
    new THREE.Mesh(planeGeometry, planeMaterial)
    g. 将Mesh加入场景
    scene.add(cube)
    h. 配置相机的位置
    camera.position.x =-30
    i. 配置相机的朝向
    camera.lookAt(scene.position)
    j. 将渲染器的输出添加到页面中
    document.getElementById(“webgl”).appendChild(renderer.domElement)
    k. 渲染三维场景
    renderer.render(scene, camera)
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="../libs/three.js"></script>
  5. <style>
  6. body {
  7. margin: 0; overflow: hidden;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <div id="webgl"></div>
  13. <script>
  14. function init() {
  15. // 创建场景
  16. // create a scene, that will hold all our elements such as objects, cameras and lights.
  17. var scene = new THREE.Scene();
  18. // 创建相机
  19. // create a camera, which defines where we're looking
  20. var camera = new THREE.PerspectiveCamera(45,
  21. window.innerWidth / window.innerHeight, 0.1, 1000);
  22. // 创建渲染器
  23. // create a render and set the size
  24. var renderer = new THREE.WebGLRenderer(); renderer.setClearColor(new THREE.Color(0xEEEEEE)); renderer.setSize(window.innerWidth,
  25. window.innerHeight);
  26. // 创建坐标系
  27. // show axes in the screen
  28. var axes = new THREE.AxisHelper(20);
  29. // scene.add(axes);
  30. // 创建场地
  31. // create the ground plane
  32. var planeGeometry = new THREE.PlaneGeometry(60, 20);
  33. // 创建材料
  34. var planeMaterial = new THREE.MeshBasicMaterial({color: 0xcccccc});
  35. // 合成物体
  36. var plane = new THREE.Mesh(planeGeometry, planeMaterial);
  37. // 位置、旋转
  38. // rotate and position the plane plane.rotation.x = -0.5 * Math.PI; plane.position.x = 15;
  39. plane.position.y = 0;
  40. plane.position.z = 0;
  41. // add the plane to the scene scene.add(plane);
  42. // 创建立方体
  43. // create a cube
  44. var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
  45. var cubeMaterial = new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: true});
  46. var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  47. // position the cube cube.position.x = -4;
  48. cube.position.y = 3;
  49. cube.position.z = 0;
  50. // add the cube to the scene scene.add(cube);
  51. // 创建球体
  52. // create a sphere
  53. var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);
  54. var sphereMaterial = new
  55. THREE.MeshBasicMaterial({color: 0x7777ff, wireframe: true}); var sphere = new THREE.Mesh(sphereGeometry,
  56. sphereMaterial);
  57. // position the sphere sphere.position.x = 20;
  58. sphere.position.y = 4;
  59. sphere.position.z = 2;
  60. // add the sphere to the scene scene.add(sphere);
  61. // position and point the camera to the center of the scene
  62. camera.position.x = -30;
  63. camera.position.y = 40;
  64. camera.position.z = 30; camera.lookAt(scene.position);
  65. // add the output of the renderer to the html element
  66. // 把渲染器绘制的Dom加到页面中
  67. document.getElementById("webgl").appendChild(renderer.domElement);
  68. // render the scene renderer.render(scene, camera);
  69. }
  70. window.onload = init ;
  71. </script>
  72. </body>
  73. </html>

加一个光源

  1. 添加能够产生阴影的光源
    spotLight = new THREE.SpotLight(0xffffff)
    2. 打开光源的阴影
    spotLight.castShadow =true
    3. 将光源加入场景
    scene.add(spotLight)
    4. 创建一个能够反应光源的材质
    new THREE.MeshLambertMaterial({color: 0xffffff})
    5. 使此Mesh可以投射阴影
    plane.receiveShadow =true
  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/stats.js"></script>
  7. <script type="text/javascript" src="../libs/dat.gui.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 which will hold the Output -->
  18. <div id="WebGL-output">
  19. </div>
  20. <!-- Javascript code that runs our Three.libs examples -->
  21. <script type="text/javascript">
  22. // once everything is loaded, we run our Three.libs stuff. function init() {
  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(); renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0)); renderer.setSize(window.innerWidth, window.innerHeight); renderer.shadowMapEnabled = true;
  29. // create the ground plane
  30. var planeGeometry = new THREE.PlaneGeometry(60, 20);
  31. // (MeshLambertMaterial/MeshPhongMaterial)
  32. var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});
  33. var plane = new THREE.Mesh(planeGeometry, planeMaterial);
  34. //
  35. plane.receiveShadow = true;
  36. // rotate and position the plane plane.rotation.x = -0.5 * Math.PI; plane.position.x = 15;
  37. plane.position.y = 0;
  38. plane.position.z = 0;
  39. // add the plane to the scene scene.add(plane);
  40. // create a cube
  41. var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
  42. var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff0000});
  43. var cube = new THREE.Mesh(cubeGeometry, cubeMaterial); cube.castShadow = true;
  44. // position the cube cube.position.x = -4;
  45. cube.position.y = 3;
  46. cube.position.z = 0;
  47. // add the cube to the scene scene.add(cube);
  48. var sphereGeometry = new THREE.SphereGeometry(4, 20, 20); var sphereMaterial = new THREE.MeshLambertMaterial({color:
  49. 0x7777ff});
  50. var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
  51. // position the sphere sphere.position.x = 20;
  52. sphere.position.y = 4;
  53. sphere.position.z = 2; sphere.castShadow = true;
  54. // add the sphere to the scene scene.add(sphere);
  55. // position and point the camera to the center of the scene camera.position.x = -30;
  56. camera.position.y = 40;
  57. camera.position.z = 30; camera.lookAt(scene.position);
  58. // add spotlight for the shadows
  59. var spotLight = new THREE.SpotLight(0xffffff); spotLight.position.set(-40, 60, -10);
  60. spotLight.castShadow = true; scene.add(spotLight);
  61. // add the output of the renderer to the html element
  62. document.getElementById("WebGL-output").appendChild(renderer.domElement);
  63. // call the render function renderer.render(scene, camera);
  64. }
  65. window.onload = init;
  66. </script>
  67. </body>
  68. </html>

让场景动起来

  1. 引入动画检测
    stats.js
    2. 初始化动画检测统计对象
    new Stats()
    3. 配置检测每秒传输帧数
    stats.setMode(0)
    4. 将输出加入页面
    document.getElementById(“Stats-output”).appendChild(stats.domElement)
    创建动画场景方法
    renderScene()
    a. 更新统计对象
    stats.update()
    b. Mesh的动作
    cube.rotation.x += 0.02
    c. 渲染场景
    renderer.render(scene, camera)
    d. 循环渲染
    requestAnimationFrame(renderScene)
    6. 启动动画
    renderScene()
  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. <!-- -->
  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. function init() {
  25. var stats = initStats();
  26. // create a scene, that will hold all our elements such as objects, cameras and lights.
  27. var scene = new THREE.Scene();
  28. // create a camera, which defines where we're looking at.
  29. var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
  30. // create a render and set the size
  31. var renderer = new THREE.WebGLRenderer(); renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0)); renderer.setSize(window.innerWidth, window.innerHeight); renderer.shadowMapEnabled = true;
  32. // create the ground plane
  33. var planeGeometry = new THREE.PlaneGeometry(60, 20, 1, 1); var planeMaterial = new THREE.MeshLambertMaterial({color:
  34. 0xffffff});
  35. var plane = new THREE.Mesh(planeGeometry, planeMaterial); plane.receiveShadow = true;
  36. // rotate and position the plane plane.rotation.x = -0.5 * Math.PI; plane.position.x = 15;
  37. plane.position.y = 0;
  38. plane.position.z = 0;
  39. // add the plane to the scene scene.add(plane);
  40. // create a cube
  41. var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
  42. var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff0000});
  43. var cube = new THREE.Mesh(cubeGeometry, cubeMaterial); cube.castShadow = true;
  44. // position the cube cube.position.x = -4;
  45. cube.position.y = 3;
  46. cube.position.z = 0;
  47. // add the cube to the scene scene.add(cube);
  48. var sphereGeometry = new THREE.SphereGeometry(4, 20, 20); var sphereMaterial = new THREE.MeshLambertMaterial({color:
  49. 0x7777ff});
  50. var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
  51. // position the sphere
  52. sphere.position.x = 20;
  53. sphere.position.y = 0;
  54. sphere.position.z = 2; sphere.castShadow = true;
  55. // add the sphere to the scene scene.add(sphere);
  56. // position and point the camera to the center of the scene camera.position.x = -30;
  57. camera.position.y = 40;
  58. camera.position.z = 30; camera.lookAt(scene.position);
  59. // add subtle ambient lighting
  60. var ambientLight = new THREE.AmbientLight(0x0c0c0c); scene.add(ambientLight);
  61. // add spotlight for the shadows
  62. var spotLight = new THREE.SpotLight(0xffffff); spotLight.position.set(-40, 60, -10); spotLight.castShadow = true; scene.add(spotLight);
  63. // add the output of the renderer to the html element
  64. document.getElementById("WebGL-output").appendChild(renderer.domElement);
  65. // call the render function var step = 0;
  66. // renderScene();
  67. function renderScene() { stats.update();
  68. // rotate the cube around its axes cube.rotation.x += 0.02;
  69. cube.rotation.y += 0.02;
  70. cube.rotation.z += 0.02;
  71. // bounce the sphere up and down step += 0.04;
  72. sphere.position.x = 20 + ( 10 * (Math.cos(step))); sphere.position.y = 2 + ( 10 * Math.abs(Math.sin(step)));
  73. //
  74. // render using requestAnimationFrame requestAnimationFrame(renderScene); renderer.render(scene, camera);
  75. }
  76. //
  77. function initStats() {
  78. var stats = new Stats(); stats.setMode(0); // 0: fps, 1: ms
  79. // Align top-left stats.domElement.style.position = 'absolute'; stats.domElement.style.left = '0px'; stats.domElement.style.top = '0px';
  80. document.getElementById("Stats-output").appendChild(stats.domElement); return stats;
  81. }
  82. }
  83. window.onload = init;
  84. </script>
  85. </body>
  86. </html>

添加控制器

  1. 引入图形交互插件
    dat.gui.js
    2. 创建控制对象
    new function () {
    this.rotationSpeed = 0.02;
    this.bouncingSpeed = 0.03;
    }
    3. 初始化GUI
    new dat.GUI()
    4. 设置属性和取值范围
    gui.add(controls, ‘rotationSpeed’, 0, 0.5)
    5. 在动画中引用属性
    cube.rotation.x += controls.rotationSpeed
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Example 01.05 - Control gui</title>
  5. <script type="text/javascript" src="../libs/three.js"></script>
  6. <script type="text/javascript" src="../libs/stats.js"></script>
  7. <script type="text/javascript" src="../libs/dat.gui.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. function init() {
  25. var stats = initStats();
  26. // create a scene, that will hold all our elements such as objects, cameras and lights.
  27. var scene = new THREE.Scene();
  28. // create a camera, which defines where we're looking at.
  29. var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
  30. // create a render and set the size
  31. var renderer = new THREE.WebGLRenderer(); renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0)); renderer.setSize(window.innerWidth, window.innerHeight); renderer.shadowMapEnabled = true;
  32. // create the ground plane
  33. var planeGeometry = new THREE.PlaneGeometry(60, 20, 1, 1); var planeMaterial = new THREE.MeshLambertMaterial({color:
  34. 0xffffff});
  35. var plane = new THREE.Mesh(planeGeometry, planeMaterial); plane.receiveShadow = true;
  36. // rotate and position the plane plane.rotation.x = -0.5 * Math.PI; plane.position.x = 15;
  37. plane.position.y = 0;
  38. plane.position.z = 0;
  39. // add the plane to the scene scene.add(plane);
  40. // create a cube
  41. var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
  42. var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff0000});
  43. var cube = new THREE.Mesh(cubeGeometry, cubeMaterial); cube.castShadow = true;
  44. // position the cube cube.position.x = -4;
  45. cube.position.y = 3;
  46. cube.position.z = 0;
  47. // add the cube to the scene scene.add(cube);
  48. var sphereGeometry = new THREE.SphereGeometry(4, 20, 20); var sphereMaterial = new THREE.MeshLambertMaterial({color:
  49. 0x7777ff});
  50. var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
  51. // position the sphere sphere.position.x = 20;
  52. sphere.position.y = 0;
  53. sphere.position.z = 2; sphere.castShadow = true;
  54. // add the sphere to the scene scene.add(sphere);
  55. // position and point the camera to the center of the scene camera.position.x = -30;
  56. camera.position.y = 40;
  57. camera.position.z = 30;
  58. camera.lookAt(scene.position);
  59. // add subtle ambient lighting
  60. var ambientLight = new THREE.AmbientLight(0x0c0c0c); scene.add(ambientLight);
  61. // add spotlight for the shadows
  62. var spotLight = new THREE.SpotLight(0xffffff); spotLight.position.set(-40, 60, -10); spotLight.castShadow = true; scene.add(spotLight);
  63. // add the output of the renderer to the html element
  64. document.getElementById("WebGL-output").appendChild(renderer.domElement);
  65. // call the render function var step = 0;
  66. //
  67. var controls = new function () { this.rotationSpeed = 0.02;
  68. this.bouncingSpeed = 0.03;
  69. };
  70. var gui = new dat.GUI();
  71. //
  72. gui.add(controls, 'rotationSpeed', 0, 0.5);
  73. gui.add(controls, 'bouncingSpeed', 0, 0.5); render();
  74. function render() { stats.update();
  75. // rotate the cube around its axes
  76. //
  77. cube.rotation.x += controls.rotationSpeed; cube.rotation.y += controls.rotationSpeed; cube.rotation.z += controls.rotationSpeed;
  78. // bounce the sphere up and down step += controls.bouncingSpeed;
  79. sphere.position.x = 20 + ( 10 * (Math.cos(step))); sphere.position.y = 2 + ( 10 * Math.abs(Math.sin(step)));
  80. // render using requestAnimationFrame requestAnimationFrame(render); renderer.render(scene, camera);
  81. }
  82. function initStats() {
  83. var stats = new Stats(); stats.setMode(0); // 0: fps, 1: ms
  84. // Align top-left stats.domElement.style.position = 'absolute'; stats.domElement.style.left = '0px'; stats.domElement.style.top = '0px';
  85. document.getElementById("Stats-output").appendChild(stats.domElement); return stats;
  86. }
  87. }
  88. window.onload = init;
  89. </script>
  90. </body>
  91. </html>

自适应浏览器

  1. 添加监听
    window.addEventListener(‘resize’, onResize, false)
    2. 重置相机长宽比
    camera.aspect= window.innerWidth /window.innerHeight
    3. 更新相机
    camera.updateProjectionMatrix()
    4. 更新渲染器尺寸
    renderer.setSize(window.innerWidth, window.innerHeight)
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Example 01.06 - Screen size change</title>
  5. <script type="text/javascript" src="../libs/three.js"></script>
  6. <script type="text/javascript" src="../libs/stats.js"></script>
  7. <script type="text/javascript" src="../libs/dat.gui.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"> var camera;
  24. var scene; var renderer;
  25. // once everything is loaded, we run our Three.js stuff.
  26. function init() {
  27. var stats = initStats();
  28. // create a scene, that will hold all our elements such as objects, cameras and lights.
  29. scene = new THREE.Scene();
  30. // create a camera, which defines where we're looking at. camera = new THREE.PerspectiveCamera(45, window.innerWidth /
  31. window.innerHeight, 0.1, 1000);
  32. // create a render and set the size renderer = new THREE.WebGLRenderer();
  33. renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0)); renderer.setSize(window.innerWidth, window.innerHeight); renderer.shadowMapEnabled = true;
  34. // create the ground plane
  35. var planeGeometry = new THREE.PlaneGeometry(60, 20, 1, 1); var planeMaterial = new THREE.MeshLambertMaterial({color:
  36. 0xffffff});
  37. var plane = new THREE.Mesh(planeGeometry, planeMaterial); plane.receiveShadow = true;
  38. // rotate and position the plane plane.rotation.x = -0.5 * Math.PI; plane.position.x = 15;
  39. plane.position.y = 0;
  40. plane.position.z = 0;
  41. // add the plane to the scene scene.add(plane);
  42. // create a cube
  43. var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
  44. var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff0000});
  45. var cube = new THREE.Mesh(cubeGeometry, cubeMaterial); cube.castShadow = true;
  46. // position the cube cube.position.x = -4;
  47. cube.position.y = 3;
  48. cube.position.z = 0;
  49. // add the cube to the scene scene.add(cube);
  50. var sphereGeometry = new THREE.SphereGeometry(4, 20, 20); var sphereMaterial = new THREE.MeshLambertMaterial({color:
  51. 0x7777ff});
  52. var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
  53. // position the sphere sphere.position.x = 20;
  54. sphere.position.y = 0;
  55. sphere.position.z = 2; sphere.castShadow = true;
  56. // add the sphere to the scene scene.add(sphere);
  57. // position and point the camera to the center of the scene camera.position.x = -30;
  58. camera.position.y = 40;
  59. camera.position.z = 30;
  60. camera.lookAt(scene.position);
  61. // add subtle ambient lighting
  62. var ambientLight = new THREE.AmbientLight(0x0c0c0c); scene.add(ambientLight);
  63. // add spotlight for the shadows
  64. var spotLight = new THREE.SpotLight(0xffffff); spotLight.position.set(-40, 60, -10); spotLight.castShadow = true; scene.add(spotLight);
  65. // add the output of the renderer to the html element
  66. document.getElementById("WebGL-output").appendChild(renderer.domElement);
  67. // call the render function var step = 0;
  68. var controls = new function () { this.rotationSpeed = 0.02;
  69. this.bouncingSpeed = 0.03;
  70. };
  71. var gui = new dat.GUI();
  72. gui.add(controls, 'rotationSpeed', 0, 0.5);
  73. gui.add(controls, 'bouncingSpeed', 0, 0.5); render();
  74. function render() { stats.update();
  75. // rotate the cube around its axes cube.rotation.x += controls.rotationSpeed; cube.rotation.y += controls.rotationSpeed; cube.rotation.z += controls.rotationSpeed;
  76. // bounce the sphere up and down step += controls.bouncingSpeed;
  77. sphere.position.x = 20 + ( 10 * (Math.cos(step))); sphere.position.y = 2 + ( 10 * Math.abs(Math.sin(step)));
  78. // render using requestAnimationFrame requestAnimationFrame(render); renderer.render(scene, camera);
  79. }
  80. function initStats() {
  81. var stats = new Stats(); stats.setMode(0); // 0: fps, 1: ms
  82. // Align top-left stats.domElement.style.position = 'absolute'; stats.domElement.style.left = '0px'; stats.domElement.style.top = '0px';
  83. document.getElementById("Stats-output").appendChild(stats.domElement); return stats;
  84. }
  85. }
  86. //
  87. function onResize() {
  88. //
  89. camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix();
  90. renderer.setSize(window.innerWidth, window.innerHeight);
  91. }
  92. window.onload = init;
  93. // listen to the resize events window.addEventListener('resize', onResize, false);
  94. </script>
  95. </body>
  96. </html>