|
| 1 | +var Class = require('../../utils/Class'); |
| 2 | +var Matrix4 = require('../../math/Matrix4'); |
| 3 | +var Vector2 = require('../../math/Vector2'); |
| 4 | +var Vector3 = require('../../math/Vector3'); |
| 5 | + |
| 6 | +var Mesh = new Class({ |
| 7 | + |
| 8 | + initialize: |
| 9 | + |
| 10 | + function Mesh (data, x, y, z) |
| 11 | + { |
| 12 | + this.vertices = data.verts; |
| 13 | + this.faces = data.faces; |
| 14 | + |
| 15 | + this.position = new Vector3(x, y, z); |
| 16 | + this.rotation = new Vector3(); |
| 17 | + this.scale = new Vector3(1, 1, 1); |
| 18 | + |
| 19 | + this.visible = true; |
| 20 | + |
| 21 | + this.thickness = 1; |
| 22 | + this.color = 0x00ff00; |
| 23 | + this.alpha = 1; |
| 24 | + |
| 25 | + this._pA = new Vector2(); |
| 26 | + this._pB = new Vector2(); |
| 27 | + this._pC = new Vector2(); |
| 28 | + this._pD = new Vector2(); |
| 29 | + |
| 30 | + this._tempVec3 = new Vector3(); |
| 31 | + |
| 32 | + this.worldMatrix = new Matrix4(); |
| 33 | + }, |
| 34 | + |
| 35 | + draw: function (graphics) |
| 36 | + { |
| 37 | + if (!this.visible || this.alpha === 0) |
| 38 | + { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + var pa = this._pA; |
| 43 | + var pb = this._pB; |
| 44 | + var pc = this._pC; |
| 45 | + var pd = this._pD; |
| 46 | + |
| 47 | + var world = this.worldMatrix; |
| 48 | + |
| 49 | + world.setWorldMatrix(this.rotation, this.position, this.scale, graphics.viewMatrix, graphics.projectionMatrix); |
| 50 | + |
| 51 | + graphics.lineStyle(this.thickness, this.color, this.alpha); |
| 52 | + graphics.beginPath(); |
| 53 | + |
| 54 | + for (var f = 0; f < this.faces.length; f++) |
| 55 | + { |
| 56 | + var face = this.faces[f]; |
| 57 | + var verts = this.vertices; |
| 58 | + |
| 59 | + this.project(graphics, pa, verts[face.A].pos, world); |
| 60 | + this.project(graphics, pb, verts[face.B].pos, world); |
| 61 | + this.project(graphics, pc, verts[face.C].pos, world); |
| 62 | + this.project(graphics, pd, verts[face.D].pos, world); |
| 63 | + |
| 64 | + graphics.moveTo(pa.x, pa.y); |
| 65 | + graphics.lineTo(pb.x, pb.y); |
| 66 | + |
| 67 | + graphics.moveTo(pb.x, pb.y); |
| 68 | + graphics.lineTo(pc.x, pc.y); |
| 69 | + |
| 70 | + graphics.moveTo(pc.x, pc.y); |
| 71 | + graphics.lineTo(pd.x, pd.y); |
| 72 | + |
| 73 | + graphics.moveTo(pd.x, pd.y); |
| 74 | + graphics.lineTo(pa.x, pa.y); |
| 75 | + } |
| 76 | + |
| 77 | + graphics.closePath(); |
| 78 | + graphics.strokePath(); |
| 79 | + }, |
| 80 | + |
| 81 | + project: function (graphics, local, coord, transMat) |
| 82 | + { |
| 83 | + var w = graphics.viewportWidth; |
| 84 | + var h = graphics.viewportHeight; |
| 85 | + |
| 86 | + var point = this._tempVec3; |
| 87 | + |
| 88 | + point.copy(coord); |
| 89 | + |
| 90 | + point.transformCoordinates(transMat); |
| 91 | + |
| 92 | + local.x = point.x * w + w / 2 >> 0; |
| 93 | + local.y = -point.y * h + h / 2 >> 0; |
| 94 | + }, |
| 95 | + |
| 96 | + setPosition: function (x, y, z) |
| 97 | + { |
| 98 | + if (x === undefined) { x = 0; } |
| 99 | + if (y === undefined) { y = x; } |
| 100 | + if (z === undefined) { z = x; } |
| 101 | + |
| 102 | + this.position.x = x; |
| 103 | + this.position.y = y; |
| 104 | + this.position.z = z; |
| 105 | + |
| 106 | + return this; |
| 107 | + }, |
| 108 | + |
| 109 | + setRotation: function (x, y, z) |
| 110 | + { |
| 111 | + if (x === undefined) { x = 0; } |
| 112 | + if (y === undefined) { y = x; } |
| 113 | + if (z === undefined) { z = x; } |
| 114 | + |
| 115 | + this.rotation.x = x; |
| 116 | + this.rotation.y = y; |
| 117 | + this.rotation.z = z; |
| 118 | + |
| 119 | + return this; |
| 120 | + }, |
| 121 | + |
| 122 | + setScale: function (x, y, z) |
| 123 | + { |
| 124 | + if (x === undefined) { x = 1; } |
| 125 | + if (y === undefined) { y = x; } |
| 126 | + if (z === undefined) { z = x; } |
| 127 | + |
| 128 | + this.scale.x = x; |
| 129 | + this.scale.y = y; |
| 130 | + this.scale.z = z; |
| 131 | + |
| 132 | + return this; |
| 133 | + }, |
| 134 | + |
| 135 | + setColor: function (color) |
| 136 | + { |
| 137 | + this.color = color; |
| 138 | + |
| 139 | + return this; |
| 140 | + }, |
| 141 | + |
| 142 | + setAlpha: function (alpha) |
| 143 | + { |
| 144 | + this.alpha = alpha; |
| 145 | + |
| 146 | + return this; |
| 147 | + }, |
| 148 | + |
| 149 | + setLineStyle: function (color, thickness, alpha) |
| 150 | + { |
| 151 | + this.color = color; |
| 152 | + this.thickness = thickness; |
| 153 | + this.alpha = alpha; |
| 154 | + |
| 155 | + return this; |
| 156 | + }, |
| 157 | + |
| 158 | + setVisible: function (value) |
| 159 | + { |
| 160 | + this.visible = value; |
| 161 | + |
| 162 | + return this; |
| 163 | + }, |
| 164 | + |
| 165 | + x: { |
| 166 | + |
| 167 | + get: function () |
| 168 | + { |
| 169 | + return this.position.x; |
| 170 | + }, |
| 171 | + |
| 172 | + set: function (value) |
| 173 | + { |
| 174 | + this.position.x = value; |
| 175 | + } |
| 176 | + |
| 177 | + }, |
| 178 | + |
| 179 | + y: { |
| 180 | + |
| 181 | + get: function () |
| 182 | + { |
| 183 | + return this.position.y; |
| 184 | + }, |
| 185 | + |
| 186 | + set: function (value) |
| 187 | + { |
| 188 | + this.position.y = value; |
| 189 | + } |
| 190 | + |
| 191 | + }, |
| 192 | + |
| 193 | + z: { |
| 194 | + |
| 195 | + get: function () |
| 196 | + { |
| 197 | + return this.position.z; |
| 198 | + }, |
| 199 | + |
| 200 | + set: function (value) |
| 201 | + { |
| 202 | + this.position.z = value; |
| 203 | + } |
| 204 | + |
| 205 | + } |
| 206 | + |
| 207 | +}); |
| 208 | + |
| 209 | +module.exports = Mesh; |
0 commit comments