|
| 1 | +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <script type="text/javascript" src="../lib/three.js"></script> |
| 5 | + <script type="text/javascript" src="../lib/helvetiker_regular.typeface.js"></script> |
| 6 | + |
| 7 | + <script type="text/javascript"> |
| 8 | + function init() { |
| 9 | + var renderer = new THREE.WebGLRenderer({ |
| 10 | + canvas: document.getElementById('mainCanvas') |
| 11 | + }); |
| 12 | + renderer.setClearColor(0x000000); |
| 13 | + var scene = new THREE.Scene(); |
| 14 | + |
| 15 | + // camera |
| 16 | + var camera = new THREE.OrthographicCamera(-5, 5, 3.75, -3.75, 0.1, 100); |
| 17 | + camera.position.set(25, 25, 25); |
| 18 | + camera.lookAt(new THREE.Vector3(0, 0, 0)); |
| 19 | + scene.add(camera); |
| 20 | + |
| 21 | + // draw axes to help you understand the coordinate |
| 22 | + drawAxes(scene); |
| 23 | + |
| 24 | + var material = new THREE.MeshBasicMaterial({ |
| 25 | + color: 0xffff00, |
| 26 | + wireframe: true |
| 27 | + }); |
| 28 | + |
| 29 | + // init an empty geometry |
| 30 | + var geometry = new THREE.Geometry(); |
| 31 | + |
| 32 | + // set vertices |
| 33 | + // 4 vertices on top |
| 34 | + geometry.vertices.push(new THREE.Vector3(-1, 2, -1)); |
| 35 | + geometry.vertices.push(new THREE.Vector3(1, 2, -1)); |
| 36 | + geometry.vertices.push(new THREE.Vector3(1, 2, 1)); |
| 37 | + geometry.vertices.push(new THREE.Vector3(-1, 2, 1)); |
| 38 | + // 4 vertices on bottom |
| 39 | + geometry.vertices.push(new THREE.Vector3(-2, 0, -2)); |
| 40 | + geometry.vertices.push(new THREE.Vector3(2, 0, -2)); |
| 41 | + geometry.vertices.push(new THREE.Vector3(2, 0, 2)); |
| 42 | + geometry.vertices.push(new THREE.Vector3(-2, 0, 2)); |
| 43 | + |
| 44 | + // set faces |
| 45 | + // top face |
| 46 | + geometry.faces.push(new THREE.Face4(0, 1, 2, 3)); |
| 47 | + // bottom face |
| 48 | + geometry.faces.push(new THREE.Face4(4, 5, 6, 7)); |
| 49 | + // side faces |
| 50 | + geometry.faces.push(new THREE.Face4(0, 1, 5, 4)); |
| 51 | + geometry.faces.push(new THREE.Face4(1, 2, 6, 5)); |
| 52 | + geometry.faces.push(new THREE.Face4(2, 3, 7, 6)); |
| 53 | + geometry.faces.push(new THREE.Face4(3, 0, 4, 7)); |
| 54 | + |
| 55 | + var mesh = new THREE.Mesh(geometry, material); |
| 56 | + scene.add(mesh); |
| 57 | + |
| 58 | + // render |
| 59 | + renderer.render(scene, camera); |
| 60 | + } |
| 61 | + |
| 62 | + function drawAxes(scene) { |
| 63 | + // x-axis |
| 64 | + var xGeo = new THREE.Geometry(); |
| 65 | + xGeo.vertices.push(new THREE.Vector3(0, 0, 0)); |
| 66 | + xGeo.vertices.push(new THREE.Vector3(1, 0, 0)); |
| 67 | + var xMat = new THREE.LineBasicMaterial({ |
| 68 | + color: 0xff0000 |
| 69 | + }); |
| 70 | + var xAxis = new THREE.Line(xGeo, xMat); |
| 71 | + scene.add(xAxis); |
| 72 | + |
| 73 | + // y-axis |
| 74 | + var yGeo = new THREE.Geometry(); |
| 75 | + yGeo.vertices.push(new THREE.Vector3(0, 0, 0)); |
| 76 | + yGeo.vertices.push(new THREE.Vector3(0, 1, 0)); |
| 77 | + var yMat = new THREE.LineBasicMaterial({ |
| 78 | + color: 0x00ff00 |
| 79 | + }); |
| 80 | + var yAxis = new THREE.Line(yGeo, yMat); |
| 81 | + scene.add(yAxis); |
| 82 | + |
| 83 | + // z-axis |
| 84 | + var zGeo = new THREE.Geometry(); |
| 85 | + zGeo.vertices.push(new THREE.Vector3(0, 0, 0)); |
| 86 | + zGeo.vertices.push(new THREE.Vector3(0, 0, 1)); |
| 87 | + var zMat = new THREE.LineBasicMaterial({ |
| 88 | + color: 0x00ccff |
| 89 | + }); |
| 90 | + var zAxis = new THREE.Line(zGeo, zMat); |
| 91 | + scene.add(zAxis); |
| 92 | + } |
| 93 | + </script> |
| 94 | + </head> |
| 95 | + |
| 96 | + <body onload="init()"> |
| 97 | + <canvas id="mainCanvas" width="400px" height="300px" ></canvas> |
| 98 | + </body> |
| 99 | +</html> |
0 commit comments