[134] | 1 | function GraphicsEngine() { |
---|
| 2 | this._scene = undefined; |
---|
| 3 | this._camera = undefined; |
---|
| 4 | this._renderer = undefined; |
---|
| 5 | this._canvasWidth = 500; |
---|
| 6 | this._canvasHeight = 500; |
---|
| 7 | this._containerContext = undefined; |
---|
| 8 | this._BALL_RADIUS = 0.25; |
---|
| 9 | this._controls = undefined; |
---|
| 10 | this._parts = [] |
---|
| 11 | this._debug1 = 0 |
---|
| 12 | this._showAxis = false; |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | GraphicsEngine.prototype.showPartAxis = function () { |
---|
| 16 | this._showAxis = true; |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | GraphicsEngine.prototype._createRenderer = function () { |
---|
| 20 | this._renderer = new THREE.WebGLRenderer({antialias: true}); |
---|
| 21 | this._renderer.setClearColor(0x000000, 1); |
---|
| 22 | this._renderer.setSize(this._canvasWidth, this._canvasHeight); |
---|
| 23 | this._containerContext = $("#container"); |
---|
| 24 | this._containerContext.append(this._renderer.domElement); |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | GraphicsEngine.prototype._prepareCamera = function () { |
---|
| 28 | this._camera = new THREE.PerspectiveCamera(45, this._canvasWidth / this._canvasHeight, 1, 100); |
---|
| 29 | this._camera.position.set(0, 0, 10); |
---|
| 30 | this._camera.lookAt(this._scene.position); |
---|
| 31 | this._scene.add(this._camera); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | GraphicsEngine.prototype._addLight = function () { |
---|
| 35 | var directionalLight = new THREE.DirectionalLight(0xffffff); |
---|
| 36 | |
---|
| 37 | directionalLight.position = this._camera.position; |
---|
| 38 | directionalLight.intensity = 1; |
---|
| 39 | |
---|
| 40 | this._scene.add(directionalLight); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | GraphicsEngine.prototype._rotateObject = function (object, part) { |
---|
| 44 | object.rotateX(part.getXrot()); |
---|
| 45 | object.rotateY(part.getYrot()); |
---|
| 46 | object.rotateZ(part.getZrot()); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | GraphicsEngine.prototype._addBall = function (part) { |
---|
| 50 | var segments = 40, |
---|
| 51 | rings = 40; |
---|
| 52 | |
---|
| 53 | var sphereMaterial = |
---|
| 54 | new THREE.MeshPhongMaterial( |
---|
| 55 | { |
---|
| 56 | color: 0xffffff |
---|
| 57 | }); |
---|
| 58 | sphereMaterial.color.setRGB(part.getR(), part.getG(), part.getB()) |
---|
| 59 | |
---|
| 60 | var ball = new THREE.Mesh( |
---|
| 61 | new THREE.SphereGeometry(part.getRadius()/4, |
---|
| 62 | segments, |
---|
| 63 | rings), |
---|
| 64 | sphereMaterial); |
---|
| 65 | |
---|
| 66 | ball.position.set(part.getX(), part.getY(), part.getZ()); |
---|
| 67 | this._rotateObject(ball, part); |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | this._parts.push(ball); |
---|
| 71 | if(this._showAxis) |
---|
| 72 | ball.add(new THREE.AxisHelper(1)); |
---|
| 73 | this._scene.add(ball); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | GraphicsEngine.prototype._addEllipsoid = function (part) { |
---|
| 77 | var geometry = new THREE.SphereGeometry(1, 20, 20); |
---|
| 78 | geometry.applyMatrix(new THREE.Matrix4().makeScale(part.getXshape(), part.getYshape(), part.getZshape())); |
---|
| 79 | |
---|
| 80 | var ellipsoidMaterial = new THREE.MeshPhongMaterial({color: 0xffffff}); |
---|
| 81 | ellipsoidMaterial.color.setRGB(part.getR(), part.getG(), part.getB()) |
---|
| 82 | |
---|
| 83 | var ellipsoid = new THREE.Mesh(geometry, ellipsoidMaterial); |
---|
| 84 | ellipsoid.position.set(part.getX(), part.getY(), part.getZ()); |
---|
| 85 | this._rotateObject(ellipsoid, part); |
---|
| 86 | |
---|
| 87 | this._parts.push(ellipsoid); |
---|
| 88 | if(this._showAxis) |
---|
| 89 | ellipsoid.add(new THREE.AxisHelper(1)); |
---|
| 90 | this._scene.add(ellipsoid); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | GraphicsEngine.prototype._addCylinder = function (part) { |
---|
| 94 | var geometry = new THREE.CylinderGeometry(1, 1, 1, 10, 10, false); |
---|
| 95 | geometry.applyMatrix(new THREE.Matrix4().makeScale(part.getZshape(), part.getXshape(), part.getYshape())); |
---|
| 96 | |
---|
| 97 | var material = new THREE.MeshLambertMaterial({color: 0xffffff}); |
---|
| 98 | material.color.setRGB(part.getR(), part.getG(), part.getB()); |
---|
| 99 | |
---|
| 100 | var cylinder = new THREE.Mesh(geometry, material); |
---|
| 101 | cylinder.position.set(part.getX(), part.getY(), part.getZ()); |
---|
| 102 | cylinder.overdraw = true; |
---|
| 103 | this._rotateObject(cylinder, part); |
---|
| 104 | |
---|
| 105 | this._parts.push(cylinder); |
---|
| 106 | if(this._showAxis) |
---|
| 107 | cylinder.add(new THREE.AxisHelper(1)); |
---|
| 108 | this._scene.add(cylinder); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | GraphicsEngine.prototype._addCuboid = function (part) { |
---|
| 112 | var geometry = new THREE.CubeGeometry(2, 2, 2, 10, 10, 10); |
---|
| 113 | geometry.applyMatrix(new THREE.Matrix4().makeScale(part.getXshape(), part.getYshape(), part.getZshape())); |
---|
| 114 | |
---|
| 115 | var material = new THREE.MeshLambertMaterial({color: 0xffffff}); |
---|
| 116 | material.color.setRGB(part.getR(), part.getG(), part.getB()); |
---|
| 117 | |
---|
| 118 | var cube = new THREE.Mesh(geometry, material); |
---|
| 119 | cube.position.set(part.getX(), part.getY(), part.getZ()); |
---|
| 120 | this._rotateObject(cube, part); |
---|
| 121 | |
---|
| 122 | this._parts.push(cube); |
---|
| 123 | if(this._showAxis) |
---|
| 124 | cube.add(new THREE.AxisHelper(1)); |
---|
| 125 | this._scene.add(cube); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | GraphicsEngine.prototype.addPart = function (part) { |
---|
| 129 | switch (part.getType()) { |
---|
| 130 | case 0: |
---|
| 131 | this._addBall(part); |
---|
| 132 | break; |
---|
| 133 | case 1: |
---|
| 134 | this._addEllipsoid(part); |
---|
| 135 | break; |
---|
| 136 | case 2: |
---|
| 137 | this._addCuboid(part); |
---|
| 138 | break; |
---|
| 139 | case 3: |
---|
| 140 | this._addCylinder(part); |
---|
| 141 | break; |
---|
| 142 | default : |
---|
| 143 | throw new Error("Unknown shape of part"); |
---|
| 144 | break; |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | GraphicsEngine.prototype._addStick = function (joint) { |
---|
| 149 | |
---|
| 150 | var p1Pos = this._parts[joint.getP1()].position; |
---|
| 151 | var p2Pos = this._parts[joint.getP2()].position; |
---|
| 152 | var p1Vector = new THREE.Vector3(p1Pos.x, p1Pos.y, p1Pos.z); |
---|
| 153 | var p2Vector = new THREE.Vector3(p2Pos.x, p2Pos.y, p2Pos.z); |
---|
| 154 | |
---|
| 155 | var HALF_PI = Math.PI * .5; |
---|
| 156 | var distance = p1Vector.distanceTo(p2Vector); |
---|
| 157 | var position = p2Vector.clone().add(p1Vector).divideScalar(2); |
---|
| 158 | |
---|
| 159 | var material = new THREE.MeshPhongMaterial({color: 0x0000ff}); |
---|
| 160 | material.color.setRGB(joint.getR(), joint.getG(), joint.getB()); |
---|
| 161 | |
---|
| 162 | var cylinder = new THREE.CylinderGeometry(0.05, 0.05, distance, 20, 20, false); |
---|
| 163 | |
---|
| 164 | var orientation = new THREE.Matrix4();//a new orientation matrix to offset pivot |
---|
| 165 | var offsetRotation = new THREE.Matrix4();//a matrix to fix pivot rotation |
---|
| 166 | orientation.lookAt(p1Vector, p2Vector, new THREE.Vector3(0, 1, 0));//look at destination |
---|
| 167 | offsetRotation.makeRotationX(HALF_PI);//rotate 90 degs on X |
---|
| 168 | orientation.multiply(offsetRotation);//combine orientation with rotation transformations |
---|
| 169 | cylinder.applyMatrix(orientation); |
---|
| 170 | |
---|
| 171 | var mesh = new THREE.Mesh(cylinder, material); |
---|
| 172 | mesh.position = position; |
---|
| 173 | |
---|
| 174 | this._scene.add(mesh); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | GraphicsEngine.prototype._hasTranslation = function (joint) { |
---|
| 178 | if (joint.getXtran() != 0) |
---|
| 179 | return true; |
---|
| 180 | if (joint.getYtran() != 0) |
---|
| 181 | return true; |
---|
| 182 | if (joint.getZtran() != 0) |
---|
| 183 | return true; |
---|
| 184 | |
---|
| 185 | return false; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | GraphicsEngine.prototype._translate = function (joint) { |
---|
| 189 | |
---|
| 190 | if(this._hasTranslation(joint)) |
---|
| 191 | { |
---|
| 192 | var p1 = joint.getP1(); |
---|
| 193 | var p2 = joint.getP2(); |
---|
| 194 | |
---|
| 195 | var copy = this._parts[p1].clone(); |
---|
| 196 | |
---|
| 197 | copy.rotateX(joint.getXrot()); |
---|
| 198 | copy.rotateY(joint.getYrot()); |
---|
| 199 | copy.rotateZ(joint.getZrot()); |
---|
| 200 | |
---|
| 201 | copy.translateX(joint.getXtran()); |
---|
| 202 | copy.translateY(joint.getYtran()); |
---|
| 203 | copy.translateZ(joint.getZtran()); |
---|
| 204 | |
---|
| 205 | this._parts[p2].position = copy.position.clone(); |
---|
| 206 | this._parts[p2].rotation = copy.rotation.clone(); |
---|
| 207 | |
---|
| 208 | delete copy; |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | GraphicsEngine.prototype.addJoint = function (joint) { |
---|
| 213 | switch (joint.getType()) { |
---|
| 214 | case 0: |
---|
| 215 | this._translate(joint); |
---|
| 216 | this._addStick(joint); |
---|
| 217 | break; |
---|
| 218 | case 1: |
---|
| 219 | this._translate(joint); |
---|
| 220 | break; |
---|
| 221 | default : |
---|
| 222 | throw new Error("Unknown shape of joint"); |
---|
| 223 | break; |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | GraphicsEngine.prototype.initializeScene = function () { |
---|
| 228 | |
---|
| 229 | this._createRenderer(); |
---|
| 230 | this._scene = new THREE.Scene(); |
---|
| 231 | this._prepareCamera(); |
---|
| 232 | this._addLight(); |
---|
| 233 | this._controls = new THREE.TrackballControls(this._camera, this._renderer.domElement) |
---|
| 234 | this._debug(); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | GraphicsEngine.prototype.renderScene = function () { |
---|
| 238 | |
---|
| 239 | var self = this; |
---|
| 240 | requestAnimationFrame( |
---|
| 241 | function () { |
---|
| 242 | self.renderScene(); |
---|
| 243 | }); |
---|
| 244 | this._renderer.render(this._scene, this._camera); |
---|
| 245 | this._controls.update(); |
---|
| 246 | |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | GraphicsEngine.prototype._debug = function() |
---|
| 250 | { |
---|
| 251 | |
---|
| 252 | this._scene.add( new THREE.AxisHelper( 20 ) ); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | GraphicsEngine.prototype.debugTest = function() |
---|
| 256 | { |
---|
| 257 | |
---|
| 258 | } |
---|
| 259 | |
---|