| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/_base/lang", |
| 5 |
"dojox/gfx", |
| 6 |
"dojox/gfx/matrix", |
| 7 |
"./_base", |
| 8 |
"./scheduler", |
| 9 |
"./gradient", |
| 10 |
"./vector", |
| 11 |
"./matrix", |
| 12 |
"./lighting" |
| 13 |
|
| 14 |
], function(arrayUtil,declare,lang,gfx,matrixUtil2d,gfx3d,schedulerExtensions,Gradient,VectorUtil,matrixUtil,lightUtil){ |
| 15 |
|
| 16 |
var scheduler = schedulerExtensions.scheduler; |
| 17 |
|
| 18 |
|
| 19 |
var out = function(o, x){ |
| 20 |
if(arguments.length > 1){ |
| 21 |
|
| 22 |
o = x; |
| 23 |
} |
| 24 |
var e = {}; |
| 25 |
for(var i in o){ |
| 26 |
if(i in e){ continue; } |
| 27 |
|
| 28 |
} |
| 29 |
}; |
| 30 |
|
| 31 |
declare("dojox.gfx3d.Object", null, { |
| 32 |
constructor: function(){ |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
this.object = null; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
this.matrix = null; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
this.cache = null; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
this.renderer = null; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
this.parent = null; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
this.strokeStyle = null; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
this.fillStyle = null; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
this.shape = null; |
| 74 |
}, |
| 75 |
|
| 76 |
setObject: function(newObject){ |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
this.object = gfx.makeParameters(this.object, newObject); |
| 88 |
return this; |
| 89 |
}, |
| 90 |
|
| 91 |
setTransform: function(matrix){ |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
this.matrix = matrixUtil.clone(matrix ? matrixUtil.normalize(matrix) : gfx3d.identity, true); |
| 100 |
|
| 101 |
return this; |
| 102 |
}, |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
applyRightTransform: function(matrix){ |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
return matrix ? this.setTransform([this.matrix, matrix]) : this; |
| 115 |
}, |
| 116 |
applyLeftTransform: function(matrix){ |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
return matrix ? this.setTransform([matrix, this.matrix]) : this; |
| 125 |
}, |
| 126 |
|
| 127 |
applyTransform: function(matrix){ |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
return matrix ? this.setTransform([this.matrix, matrix]) : this; |
| 135 |
}, |
| 136 |
|
| 137 |
setFill: function(fill){ |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
this.fillStyle = fill; |
| 150 |
return this; |
| 151 |
}, |
| 152 |
|
| 153 |
setStroke: function(stroke){ |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
this.strokeStyle = stroke; |
| 161 |
return this; |
| 162 |
}, |
| 163 |
|
| 164 |
toStdFill: function(lighting, normal){ |
| 165 |
return (this.fillStyle && typeof this.fillStyle['type'] != "undefined") ? |
| 166 |
lighting[this.fillStyle.type](normal, this.fillStyle.finish, this.fillStyle.color) |
| 167 |
: this.fillStyle; |
| 168 |
}, |
| 169 |
|
| 170 |
invalidate: function(){ |
| 171 |
this.renderer.addTodo(this); |
| 172 |
}, |
| 173 |
|
| 174 |
destroy: function(){ |
| 175 |
if(this.shape){ |
| 176 |
var p = this.shape.getParent(); |
| 177 |
if(p){ |
| 178 |
p.remove(this.shape); |
| 179 |
} |
| 180 |
this.shape = null; |
| 181 |
} |
| 182 |
}, |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
render: function(camera){ |
| 188 |
throw "Pure virtual function, not implemented"; |
| 189 |
}, |
| 190 |
|
| 191 |
draw: function(lighting){ |
| 192 |
throw "Pure virtual function, not implemented"; |
| 193 |
}, |
| 194 |
|
| 195 |
getZOrder: function(){ |
| 196 |
return 0; |
| 197 |
}, |
| 198 |
|
| 199 |
getOutline: function(){ |
| 200 |
return null; |
| 201 |
} |
| 202 |
|
| 203 |
}); |
| 204 |
|
| 205 |
declare("dojox.gfx3d.Scene", gfx3d.Object, { |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
constructor: function(){ |
| 213 |
|
| 214 |
|
| 215 |
this.objects= []; |
| 216 |
this.todos = []; |
| 217 |
this.schedule = scheduler.zOrder; |
| 218 |
this._draw = gfx3d.drawer.conservative; |
| 219 |
}, |
| 220 |
|
| 221 |
setFill: function(fill){ |
| 222 |
this.fillStyle = fill; |
| 223 |
arrayUtil.forEach(this.objects, function(item){ |
| 224 |
item.setFill(fill); |
| 225 |
}); |
| 226 |
return this; |
| 227 |
}, |
| 228 |
|
| 229 |
setStroke: function(stroke){ |
| 230 |
this.strokeStyle = stroke; |
| 231 |
arrayUtil.forEach(this.objects, function(item){ |
| 232 |
item.setStroke(stroke); |
| 233 |
}); |
| 234 |
return this; |
| 235 |
}, |
| 236 |
|
| 237 |
render: function(camera, deep){ |
| 238 |
var m = matrixUtil.multiply(camera, this.matrix); |
| 239 |
if(deep){ |
| 240 |
this.todos = this.objects; |
| 241 |
} |
| 242 |
arrayUtil.forEach(this.todos, function(item){ item.render(m, deep); }); |
| 243 |
}, |
| 244 |
|
| 245 |
draw: function(lighting){ |
| 246 |
this.objects = this.schedule(this.objects); |
| 247 |
this._draw(this.todos, this.objects, this.renderer); |
| 248 |
}, |
| 249 |
|
| 250 |
addTodo: function(newObject){ |
| 251 |
|
| 252 |
if(arrayUtil.every(this.todos, function(item){ return item != newObject; })){ |
| 253 |
this.todos.push(newObject); |
| 254 |
this.invalidate(); |
| 255 |
} |
| 256 |
}, |
| 257 |
|
| 258 |
invalidate: function(){ |
| 259 |
this.parent.addTodo(this); |
| 260 |
}, |
| 261 |
|
| 262 |
getZOrder: function(){ |
| 263 |
var zOrder = 0; |
| 264 |
arrayUtil.forEach(this.objects, function(item){ zOrder += item.getZOrder(); }); |
| 265 |
return (this.objects.length > 1) ? zOrder / this.objects.length : 0; |
| 266 |
} |
| 267 |
}); |
| 268 |
|
| 269 |
|
| 270 |
declare("dojox.gfx3d.Edges", gfx3d.Object, { |
| 271 |
constructor: function(){ |
| 272 |
|
| 273 |
|
| 274 |
this.object = lang.clone(gfx3d.defaultEdges); |
| 275 |
}, |
| 276 |
|
| 277 |
setObject: function( newObject, style){ |
| 278 |
|
| 279 |
|
| 280 |
this.object = gfx.makeParameters(this.object, (newObject instanceof Array) ? { points: newObject, style: style } : newObject); |
| 281 |
return this; |
| 282 |
}, |
| 283 |
|
| 284 |
getZOrder: function(){ |
| 285 |
var zOrder = 0; |
| 286 |
arrayUtil.forEach(this.cache, function(item){ zOrder += item.z;} ); |
| 287 |
return (this.cache.length > 1) ? zOrder / this.cache.length : 0; |
| 288 |
}, |
| 289 |
|
| 290 |
render: function(camera){ |
| 291 |
var m = matrixUtil.multiply(camera, this.matrix); |
| 292 |
this.cache = arrayUtil.map(this.object.points, function(item){ |
| 293 |
return matrixUtil.multiplyPoint(m, item); |
| 294 |
}); |
| 295 |
}, |
| 296 |
|
| 297 |
draw: function(){ |
| 298 |
var c = this.cache; |
| 299 |
if(this.shape){ |
| 300 |
this.shape.setShape("") |
| 301 |
}else{ |
| 302 |
this.shape = this.renderer.createPath(); |
| 303 |
} |
| 304 |
var p = this.shape.setAbsoluteMode("absolute"); |
| 305 |
|
| 306 |
if(this.object.style == "strip" || this.object.style == "loop"){ |
| 307 |
p.moveTo(c[0].x, c[0].y); |
| 308 |
arrayUtil.forEach(c.slice(1), function(item){ |
| 309 |
p.lineTo(item.x, item.y); |
| 310 |
}); |
| 311 |
if(this.object.style == "loop"){ |
| 312 |
p.closePath(); |
| 313 |
} |
| 314 |
}else{ |
| 315 |
for(var i = 0; i < this.cache.length; ){ |
| 316 |
p.moveTo(c[i].x, c[i].y); |
| 317 |
i ++; |
| 318 |
p.lineTo(c[i].x, c[i].y); |
| 319 |
i ++; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
p.setStroke(this.strokeStyle); |
| 324 |
} |
| 325 |
}); |
| 326 |
|
| 327 |
declare("dojox.gfx3d.Orbit", gfx3d.Object, { |
| 328 |
constructor: function(){ |
| 329 |
|
| 330 |
|
| 331 |
this.object = lang.clone(gfx3d.defaultOrbit); |
| 332 |
}, |
| 333 |
|
| 334 |
render: function(camera){ |
| 335 |
var m = matrixUtil.multiply(camera, this.matrix); |
| 336 |
var angles = [0, Math.PI/4, Math.PI/3]; |
| 337 |
var center = matrixUtil.multiplyPoint(m, this.object.center); |
| 338 |
var marks = arrayUtil.map(angles, function(item){ |
| 339 |
return {x: this.center.x + this.radius * Math.cos(item), |
| 340 |
y: this.center.y + this.radius * Math.sin(item), z: this.center.z}; |
| 341 |
}, this.object); |
| 342 |
|
| 343 |
marks = arrayUtil.map(marks, function(item){ |
| 344 |
return matrixUtil.multiplyPoint(m, item); |
| 345 |
}); |
| 346 |
|
| 347 |
var normal = VectorUtil.normalize(marks); |
| 348 |
|
| 349 |
marks = arrayUtil.map(marks, function(item){ |
| 350 |
return VectorUtil.substract(item, center); |
| 351 |
}); |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
var A = { |
| 361 |
xx: marks[0].x * marks[0].y, xy: marks[0].y * marks[0].y, xz: 1, |
| 362 |
yx: marks[1].x * marks[1].y, yy: marks[1].y * marks[1].y, yz: 1, |
| 363 |
zx: marks[2].x * marks[2].y, zy: marks[2].y * marks[2].y, zz: 1, |
| 364 |
dx: 0, dy: 0, dz: 0 |
| 365 |
}; |
| 366 |
var B = arrayUtil.map(marks, function(item){ |
| 367 |
return -Math.pow(item.x, 2); |
| 368 |
}); |
| 369 |
|
| 370 |
|
| 371 |
var X = matrixUtil.multiplyPoint(matrixUtil.invert(A),B[0], B[1], B[2]); |
| 372 |
var theta = Math.atan2(X.x, 1 - X.y) / 2; |
| 373 |
|
| 374 |
|
| 375 |
var probes = arrayUtil.map(marks, function(item){ |
| 376 |
return matrixUtil2d.multiplyPoint(matrixUtil2d.rotate(-theta), item.x, item.y); |
| 377 |
}); |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
var a = Math.pow(probes[0].x, 2); |
| 385 |
var b = Math.pow(probes[0].y, 2); |
| 386 |
var c = Math.pow(probes[1].x, 2); |
| 387 |
var d = Math.pow(probes[1].y, 2); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
var rx = Math.sqrt( (a*d - b*c)/ (d-b) ); |
| 392 |
var ry = Math.sqrt( (a*d - b*c)/ (a-c) ); |
| 393 |
|
| 394 |
this.cache = {cx: center.x, cy: center.y, rx: rx, ry: ry, theta: theta, normal: normal}; |
| 395 |
}, |
| 396 |
|
| 397 |
draw: function(lighting){ |
| 398 |
if(this.shape){ |
| 399 |
this.shape.setShape(this.cache); |
| 400 |
} else { |
| 401 |
this.shape = this.renderer.createEllipse(this.cache); |
| 402 |
} |
| 403 |
this.shape.applyTransform(matrixUtil2d.rotateAt(this.cache.theta, this.cache.cx, this.cache.cy)) |
| 404 |
.setStroke(this.strokeStyle) |
| 405 |
.setFill(this.toStdFill(lighting, this.cache.normal)); |
| 406 |
} |
| 407 |
}); |
| 408 |
|
| 409 |
declare("dojox.gfx3d.Path3d", gfx3d.Object, { |
| 410 |
|
| 411 |
constructor: function(){ |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
this.object = lang.clone(gfx3d.defaultPath3d); |
| 417 |
this.segments = []; |
| 418 |
this.absolute = true; |
| 419 |
this.last = {}; |
| 420 |
this.path = ""; |
| 421 |
}, |
| 422 |
|
| 423 |
_collectArgs: function(array, args){ |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
for(var i = 0; i < args.length; ++i){ |
| 431 |
var t = args[i]; |
| 432 |
if(typeof(t) == "boolean"){ |
| 433 |
array.push(t ? 1 : 0); |
| 434 |
}else if(typeof(t) == "number"){ |
| 435 |
array.push(t); |
| 436 |
}else if(t instanceof Array){ |
| 437 |
this._collectArgs(array, t); |
| 438 |
}else if("x" in t && "y" in t){ |
| 439 |
array.push(t.x); |
| 440 |
array.push(t.y); |
| 441 |
} |
| 442 |
} |
| 443 |
}, |
| 444 |
|
| 445 |
|
| 446 |
_validSegments: {m: 3, l: 3, z: 0}, |
| 447 |
|
| 448 |
_pushSegment: function(action, args){ |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
var group = this._validSegments[action.toLowerCase()], segment; |
| 456 |
if(typeof(group) == "number"){ |
| 457 |
if(group){ |
| 458 |
if(args.length >= group){ |
| 459 |
segment = {action: action, args: args.slice(0, args.length - args.length % group)}; |
| 460 |
this.segments.push(segment); |
| 461 |
} |
| 462 |
}else{ |
| 463 |
segment = {action: action, args: []}; |
| 464 |
this.segments.push(segment); |
| 465 |
} |
| 466 |
} |
| 467 |
}, |
| 468 |
|
| 469 |
moveTo: function(){ |
| 470 |
|
| 471 |
|
| 472 |
var args = []; |
| 473 |
this._collectArgs(args, arguments); |
| 474 |
this._pushSegment(this.absolute ? "M" : "m", args); |
| 475 |
return this; |
| 476 |
}, |
| 477 |
lineTo: function(){ |
| 478 |
|
| 479 |
|
| 480 |
var args = []; |
| 481 |
this._collectArgs(args, arguments); |
| 482 |
this._pushSegment(this.absolute ? "L" : "l", args); |
| 483 |
return this; |
| 484 |
}, |
| 485 |
|
| 486 |
closePath: function(){ |
| 487 |
|
| 488 |
|
| 489 |
this._pushSegment("Z", []); |
| 490 |
return this; |
| 491 |
}, |
| 492 |
|
| 493 |
render: function(camera){ |
| 494 |
|
| 495 |
var m = matrixUtil.multiply(camera, this.matrix); |
| 496 |
|
| 497 |
|
| 498 |
var path = ""; |
| 499 |
var _validSegments = this._validSegments; |
| 500 |
arrayUtil.forEach(this.segments, function(item){ |
| 501 |
path += item.action; |
| 502 |
for(var i = 0; i < item.args.length; i+= _validSegments[item.action.toLowerCase()] ){ |
| 503 |
var pt = matrixUtil.multiplyPoint(m, item.args[i], item.args[i+1], item.args[i+2]) |
| 504 |
path += " " + pt.x + " " + pt.y; |
| 505 |
} |
| 506 |
}); |
| 507 |
|
| 508 |
this.cache = path; |
| 509 |
}, |
| 510 |
|
| 511 |
_draw: function(){ |
| 512 |
return this.parent.createPath(this.cache); |
| 513 |
} |
| 514 |
}); |
| 515 |
|
| 516 |
declare("dojox.gfx3d.Triangles", gfx3d.Object, { |
| 517 |
constructor: function(){ |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
this.object = lang.clone(gfx3d.defaultTriangles); |
| 523 |
}, |
| 524 |
|
| 525 |
setObject: function( newObject, style){ |
| 526 |
|
| 527 |
|
| 528 |
if(newObject instanceof Array){ |
| 529 |
this.object = gfx.makeParameters(this.object, { points: newObject, style: style } ); |
| 530 |
} else { |
| 531 |
this.object = gfx.makeParameters(this.object, newObject); |
| 532 |
} |
| 533 |
return this; |
| 534 |
}, |
| 535 |
render: function(camera){ |
| 536 |
var m = matrixUtil.multiply(camera, this.matrix); |
| 537 |
var c = arrayUtil.map(this.object.points, function(item){ |
| 538 |
return matrixUtil.multiplyPoint(m, item); |
| 539 |
}); |
| 540 |
this.cache = []; |
| 541 |
var pool = c.slice(0, 2); |
| 542 |
var center = c[0]; |
| 543 |
if(this.object.style == "strip"){ |
| 544 |
arrayUtil.forEach(c.slice(2), function(item){ |
| 545 |
pool.push(item); |
| 546 |
pool.push(pool[0]); |
| 547 |
this.cache.push(pool); |
| 548 |
pool = pool.slice(1, 3); |
| 549 |
}, this); |
| 550 |
} else if(this.object.style == "fan"){ |
| 551 |
arrayUtil.forEach(c.slice(2), function(item){ |
| 552 |
pool.push(item); |
| 553 |
pool.push(center); |
| 554 |
this.cache.push(pool); |
| 555 |
pool = [center, item]; |
| 556 |
}, this); |
| 557 |
} else { |
| 558 |
for(var i = 0; i < c.length; ){ |
| 559 |
this.cache.push( [ c[i], c[i+1], c[i+2], c[i] ]); |
| 560 |
i += 3; |
| 561 |
} |
| 562 |
} |
| 563 |
}, |
| 564 |
|
| 565 |
draw: function(lighting){ |
| 566 |
|
| 567 |
this.cache = scheduler.bsp(this.cache, function(it){ return it; }); |
| 568 |
if(this.shape){ |
| 569 |
this.shape.clear(); |
| 570 |
} else { |
| 571 |
this.shape = this.renderer.createGroup(); |
| 572 |
} |
| 573 |
arrayUtil.forEach(this.cache, function(item){ |
| 574 |
this.shape.createPolyline(item) |
| 575 |
.setStroke(this.strokeStyle) |
| 576 |
.setFill(this.toStdFill(lighting, VectorUtil.normalize(item))); |
| 577 |
}, this); |
| 578 |
}, |
| 579 |
|
| 580 |
getZOrder: function(){ |
| 581 |
var zOrder = 0; |
| 582 |
arrayUtil.forEach(this.cache, function(item){ |
| 583 |
zOrder += (item[0].z + item[1].z + item[2].z) / 3; }); |
| 584 |
return (this.cache.length > 1) ? zOrder / this.cache.length : 0; |
| 585 |
} |
| 586 |
}); |
| 587 |
|
| 588 |
declare("dojox.gfx3d.Quads", gfx3d.Object, { |
| 589 |
constructor: function(){ |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
this.object = lang.clone(gfx3d.defaultQuads); |
| 595 |
}, |
| 596 |
|
| 597 |
setObject: function( newObject, style){ |
| 598 |
|
| 599 |
|
| 600 |
this.object = gfx.makeParameters(this.object, (newObject instanceof Array) ? |
| 601 |
{ points: newObject, style: style } |
| 602 |
: newObject ); |
| 603 |
return this; |
| 604 |
}, |
| 605 |
render: function(camera){ |
| 606 |
var m = matrixUtil.multiply(camera, this.matrix), i; |
| 607 |
var c = arrayUtil.map(this.object.points, function(item){ |
| 608 |
return matrixUtil.multiplyPoint(m, item); |
| 609 |
}); |
| 610 |
this.cache = []; |
| 611 |
if(this.object.style == "strip"){ |
| 612 |
var pool = c.slice(0, 2); |
| 613 |
for(i = 2; i < c.length; ){ |
| 614 |
pool = pool.concat( [ c[i], c[i+1], pool[0] ] ); |
| 615 |
this.cache.push(pool); |
| 616 |
pool = pool.slice(2,4); |
| 617 |
i += 2; |
| 618 |
} |
| 619 |
}else{ |
| 620 |
for(i = 0; i < c.length; ){ |
| 621 |
this.cache.push( [c[i], c[i+1], c[i+2], c[i+3], c[i] ] ); |
| 622 |
i += 4; |
| 623 |
} |
| 624 |
} |
| 625 |
}, |
| 626 |
|
| 627 |
draw: function(lighting){ |
| 628 |
|
| 629 |
this.cache = gfx3d.scheduler.bsp(this.cache, function(it){ return it; }); |
| 630 |
if(this.shape){ |
| 631 |
this.shape.clear(); |
| 632 |
}else{ |
| 633 |
this.shape = this.renderer.createGroup(); |
| 634 |
} |
| 635 |
|
| 636 |
for(var x=0; x<this.cache.length; x++){ |
| 637 |
this.shape.createPolyline(this.cache[x]) |
| 638 |
.setStroke(this.strokeStyle) |
| 639 |
.setFill(this.toStdFill(lighting, VectorUtil.normalize(this.cache[x]))); |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
}, |
| 649 |
|
| 650 |
getZOrder: function(){ |
| 651 |
var zOrder = 0; |
| 652 |
|
| 653 |
for(var x=0; x<this.cache.length; x++){ |
| 654 |
var i = this.cache[x]; |
| 655 |
zOrder += (i[0].z + i[1].z + i[2].z + i[3].z) / 4; |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
return (this.cache.length > 1) ? zOrder / this.cache.length : 0; |
| 662 |
} |
| 663 |
}); |
| 664 |
|
| 665 |
declare("dojox.gfx3d.Polygon", gfx3d.Object, { |
| 666 |
constructor: function(){ |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
this.object = lang.clone(gfx3d.defaultPolygon); |
| 672 |
}, |
| 673 |
|
| 674 |
setObject: function( newObject){ |
| 675 |
|
| 676 |
|
| 677 |
this.object = gfx.makeParameters(this.object, (newObject instanceof Array) ? {path: newObject} : newObject) |
| 678 |
return this; |
| 679 |
}, |
| 680 |
|
| 681 |
render: function(camera){ |
| 682 |
var m = matrixUtil.multiply(camera, this.matrix); |
| 683 |
this.cache = arrayUtil.map(this.object.path, function(item){ |
| 684 |
return matrixUtil.multiplyPoint(m, item); |
| 685 |
}); |
| 686 |
|
| 687 |
this.cache.push(this.cache[0]); |
| 688 |
}, |
| 689 |
|
| 690 |
draw: function(lighting){ |
| 691 |
if(this.shape){ |
| 692 |
this.shape.setShape({points: this.cache}); |
| 693 |
}else{ |
| 694 |
this.shape = this.renderer.createPolyline({points: this.cache}); |
| 695 |
} |
| 696 |
|
| 697 |
this.shape.setStroke(this.strokeStyle) |
| 698 |
.setFill(this.toStdFill(lighting, matrixUtil.normalize(this.cache))); |
| 699 |
}, |
| 700 |
|
| 701 |
getZOrder: function(){ |
| 702 |
var zOrder = 0; |
| 703 |
|
| 704 |
for(var x=0; x<this.cache.length; x++){ |
| 705 |
zOrder += this.cache[x].z; |
| 706 |
} |
| 707 |
return (this.cache.length > 1) ? zOrder / this.cache.length : 0; |
| 708 |
}, |
| 709 |
|
| 710 |
getOutline: function(){ |
| 711 |
return this.cache.slice(0, 3); |
| 712 |
} |
| 713 |
}); |
| 714 |
|
| 715 |
declare("dojox.gfx3d.Cube", gfx3d.Object, { |
| 716 |
constructor: function(){ |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
this.object = lang.clone(gfx3d.defaultCube); |
| 722 |
this.polygons = []; |
| 723 |
}, |
| 724 |
|
| 725 |
setObject: function( newObject){ |
| 726 |
|
| 727 |
|
| 728 |
this.object = gfx.makeParameters(this.object, newObject); |
| 729 |
}, |
| 730 |
|
| 731 |
render: function(camera){ |
| 732 |
|
| 733 |
var a = this.object.top; |
| 734 |
var g = this.object.bottom; |
| 735 |
var b = {x: g.x, y: a.y, z: a.z}; |
| 736 |
var c = {x: g.x, y: g.y, z: a.z}; |
| 737 |
var d = {x: a.x, y: g.y, z: a.z}; |
| 738 |
var e = {x: a.x, y: a.y, z: g.z}; |
| 739 |
var f = {x: g.x, y: a.y, z: g.z}; |
| 740 |
var h = {x: a.x, y: g.y, z: g.z}; |
| 741 |
var polygons = [a, b, c, d, e, f, g, h]; |
| 742 |
var m = matrixUtil.multiply(camera, this.matrix); |
| 743 |
var p = arrayUtil.map(polygons, function(item){ |
| 744 |
return matrixUtil.multiplyPoint(m, item); |
| 745 |
}); |
| 746 |
a = p[0]; b = p[1]; c = p[2]; d = p[3]; e = p[4]; f = p[5]; g = p[6]; h = p[7]; |
| 747 |
this.cache = [[a, b, c, d, a], [e, f, g, h, e], [a, d, h, e, a], [d, c, g, h, d], [c, b, f, g, c], [b, a, e, f, b]]; |
| 748 |
}, |
| 749 |
|
| 750 |
draw: function(lighting){ |
| 751 |
|
| 752 |
this.cache = gfx3d.scheduler.bsp(this.cache, function(it){ return it; }); |
| 753 |
|
| 754 |
var cache = this.cache.slice(3); |
| 755 |
|
| 756 |
if(this.shape){ |
| 757 |
this.shape.clear(); |
| 758 |
}else{ |
| 759 |
this.shape = this.renderer.createGroup(); |
| 760 |
} |
| 761 |
for(var x=0; x<cache.length; x++){ |
| 762 |
this.shape.createPolyline(cache[x]) |
| 763 |
.setStroke(this.strokeStyle) |
| 764 |
.setFill(this.toStdFill(lighting, VectorUtil.normalize(cache[x]))); |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
}, |
| 774 |
|
| 775 |
getZOrder: function(){ |
| 776 |
var top = this.cache[0][0]; |
| 777 |
var bottom = this.cache[1][2]; |
| 778 |
return (top.z + bottom.z) / 2; |
| 779 |
} |
| 780 |
}); |
| 781 |
|
| 782 |
|
| 783 |
declare("dojox.gfx3d.Cylinder", gfx3d.Object, { |
| 784 |
constructor: function(){ |
| 785 |
this.object = lang.clone(gfx3d.defaultCylinder); |
| 786 |
}, |
| 787 |
|
| 788 |
render: function(camera){ |
| 789 |
|
| 790 |
var m = matrixUtil.multiply(camera, this.matrix); |
| 791 |
var angles = [0, Math.PI/4, Math.PI/3]; |
| 792 |
var center = matrixUtil.multiplyPoint(m, this.object.center); |
| 793 |
var marks = arrayUtil.map(angles, function(item){ |
| 794 |
return {x: this.center.x + this.radius * Math.cos(item), |
| 795 |
y: this.center.y + this.radius * Math.sin(item), z: this.center.z}; |
| 796 |
}, this.object); |
| 797 |
|
| 798 |
marks = arrayUtil.map(marks, function(item){ |
| 799 |
return VectorUtil.substract(matrixUtil.multiplyPoint(m, item), center); |
| 800 |
}); |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
var A = { |
| 810 |
xx: marks[0].x * marks[0].y, xy: marks[0].y * marks[0].y, xz: 1, |
| 811 |
yx: marks[1].x * marks[1].y, yy: marks[1].y * marks[1].y, yz: 1, |
| 812 |
zx: marks[2].x * marks[2].y, zy: marks[2].y * marks[2].y, zz: 1, |
| 813 |
dx: 0, dy: 0, dz: 0 |
| 814 |
}; |
| 815 |
var B = arrayUtil.map(marks, function(item){ |
| 816 |
return -Math.pow(item.x, 2); |
| 817 |
}); |
| 818 |
|
| 819 |
|
| 820 |
var X = matrixUtil.multiplyPoint(matrixUtil.invert(A), B[0], B[1], B[2]); |
| 821 |
var theta = Math.atan2(X.x, 1 - X.y) / 2; |
| 822 |
|
| 823 |
|
| 824 |
var probes = arrayUtil.map(marks, function(item){ |
| 825 |
return matrixUtil2d.multiplyPoint(matrixUtil2d.rotate(-theta), item.x, item.y); |
| 826 |
}); |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
var a = Math.pow(probes[0].x, 2); |
| 834 |
var b = Math.pow(probes[0].y, 2); |
| 835 |
var c = Math.pow(probes[1].x, 2); |
| 836 |
var d = Math.pow(probes[1].y, 2); |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
var rx = Math.sqrt((a * d - b * c) / (d - b)); |
| 841 |
var ry = Math.sqrt((a * d - b * c) / (a - c)); |
| 842 |
if(rx < ry){ |
| 843 |
var t = rx; |
| 844 |
rx = ry; |
| 845 |
ry = t; |
| 846 |
theta -= Math.PI/2; |
| 847 |
} |
| 848 |
|
| 849 |
var top = matrixUtil.multiplyPoint(m, |
| 850 |
VectorUtil.sum(this.object.center, {x: 0, y:0, z: this.object.height})); |
| 851 |
|
| 852 |
var gradient = this.fillStyle.type == "constant" ? this.fillStyle.color |
| 853 |
: Gradient(this.renderer.lighting, this.fillStyle, this.object.center, this.object.radius, Math.PI, 2 * Math.PI, m); |
| 854 |
if(isNaN(rx) || isNaN(ry) || isNaN(theta)){ |
| 855 |
|
| 856 |
rx = this.object.radius, ry = 0, theta = 0; |
| 857 |
} |
| 858 |
this.cache = {center: center, top: top, rx: rx, ry: ry, theta: theta, gradient: gradient}; |
| 859 |
}, |
| 860 |
|
| 861 |
draw: function(){ |
| 862 |
var c = this.cache, v = VectorUtil, m = matrixUtil2d, |
| 863 |
centers = [c.center, c.top], normal = v.substract(c.top, c.center); |
| 864 |
if(v.dotProduct(normal, this.renderer.lighting.incident) > 0){ |
| 865 |
centers = [c.top, c.center]; |
| 866 |
normal = v.substract(c.center, c.top); |
| 867 |
} |
| 868 |
|
| 869 |
var color = this.renderer.lighting[this.fillStyle.type](normal, this.fillStyle.finish, this.fillStyle.color), |
| 870 |
d = Math.sqrt( Math.pow(c.center.x - c.top.x, 2) + Math.pow(c.center.y - c.top.y, 2) ); |
| 871 |
|
| 872 |
if(this.shape){ |
| 873 |
this.shape.clear(); |
| 874 |
}else{ |
| 875 |
this.shape = this.renderer.createGroup(); |
| 876 |
} |
| 877 |
|
| 878 |
this.shape.createPath("") |
| 879 |
.moveTo(0, -c.rx) |
| 880 |
.lineTo(d, -c.rx) |
| 881 |
.lineTo(d, c.rx) |
| 882 |
.lineTo(0, c.rx) |
| 883 |
.arcTo(c.ry, c.rx, 0, true, true, 0, -c.rx) |
| 884 |
.setFill(c.gradient).setStroke(this.strokeStyle) |
| 885 |
.setTransform([m.translate(centers[0]), |
| 886 |
m.rotate(Math.atan2(centers[1].y - centers[0].y, centers[1].x - centers[0].x))]); |
| 887 |
|
| 888 |
if(c.rx > 0 && c.ry > 0){ |
| 889 |
this.shape.createEllipse({cx: centers[1].x, cy: centers[1].y, rx: c.rx, ry: c.ry}) |
| 890 |
.setFill(color).setStroke(this.strokeStyle) |
| 891 |
.applyTransform(m.rotateAt(c.theta, centers[1])); |
| 892 |
} |
| 893 |
} |
| 894 |
}); |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
declare("dojox.gfx3d.Viewport", gfx.Group, { |
| 899 |
constructor: function(){ |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
this.dimension = null; |
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
this.objects = []; |
| 914 |
|
| 915 |
|
| 916 |
this.todos = []; |
| 917 |
|
| 918 |
|
| 919 |
this.renderer = this; |
| 920 |
|
| 921 |
this.schedule = gfx3d.scheduler.zOrder; |
| 922 |
this.draw = gfx3d.drawer.conservative; |
| 923 |
|
| 924 |
this.deep = false; |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
this.lights = []; |
| 929 |
this.lighting = null; |
| 930 |
}, |
| 931 |
|
| 932 |
setCameraTransform: function(matrix){ |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
this.camera = matrixUtil.clone(matrix ? matrixUtil.normalize(matrix) : gfx3d.identity, true); |
| 940 |
this.invalidate(); |
| 941 |
return this; |
| 942 |
}, |
| 943 |
|
| 944 |
applyCameraRightTransform: function(matrix){ |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
return matrix ? this.setCameraTransform([this.camera, matrix]) : this; |
| 953 |
}, |
| 954 |
|
| 955 |
applyCameraLeftTransform: function(matrix){ |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
return matrix ? this.setCameraTransform([matrix, this.camera]) : this; |
| 964 |
}, |
| 965 |
|
| 966 |
applyCameraTransform: function(matrix){ |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
return this.applyCameraRightTransform(matrix); |
| 974 |
}, |
| 975 |
|
| 976 |
setLights: function(lights, ambient, |
| 977 |
specular){ |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
this.lights = (lights instanceof Array) ? |
| 988 |
{sources: lights, ambient: ambient, specular: specular} |
| 989 |
: lights; |
| 990 |
var view = {x: 0, y: 0, z: 1}; |
| 991 |
|
| 992 |
this.lighting = new lightUtil.Model(view, this.lights.sources, |
| 993 |
this.lights.ambient, this.lights.specular); |
| 994 |
this.invalidate(); |
| 995 |
return this; |
| 996 |
}, |
| 997 |
|
| 998 |
addLights: function(lights){ |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
return this.setLights(this.lights.sources.concat(lights)); |
| 1004 |
}, |
| 1005 |
|
| 1006 |
addTodo: function(newObject){ |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
if(arrayUtil.every(this.todos, |
| 1012 |
function(item){ |
| 1013 |
return item != newObject; |
| 1014 |
} |
| 1015 |
)){ |
| 1016 |
this.todos.push(newObject); |
| 1017 |
} |
| 1018 |
}, |
| 1019 |
|
| 1020 |
invalidate: function(){ |
| 1021 |
this.deep = true; |
| 1022 |
this.todos = this.objects; |
| 1023 |
}, |
| 1024 |
|
| 1025 |
setDimensions: function(dim){ |
| 1026 |
if(dim){ |
| 1027 |
var w = lang.isString(dim.width) ? parseInt(dim.width) : dim.width; |
| 1028 |
var h = lang.isString(dim.height) ? parseInt(dim.height) : dim.height; |
| 1029 |
|
| 1030 |
if(this.rawNode){ |
| 1031 |
var trs = this.rawNode.style; |
| 1032 |
if(trs){ |
| 1033 |
trs.height = h; |
| 1034 |
trs.width = w; |
| 1035 |
}else{ |
| 1036 |
|
| 1037 |
this.rawNode.width = w; |
| 1038 |
this.rawNode.height = h; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
this.dimension = { |
| 1042 |
width: w, |
| 1043 |
height: h |
| 1044 |
}; |
| 1045 |
}else{ |
| 1046 |
this.dimension = null; |
| 1047 |
} |
| 1048 |
}, |
| 1049 |
|
| 1050 |
render: function(){ |
| 1051 |
|
| 1052 |
|
| 1053 |
if(!this.todos.length){ return; } |
| 1054 |
|
| 1055 |
var m = matrixUtil; |
| 1056 |
|
| 1057 |
|
| 1058 |
for(var x=0; x<this.todos.length; x++){ |
| 1059 |
this.todos[x].render(matrixUtil.normalize([ |
| 1060 |
m.cameraRotateXg(180), |
| 1061 |
m.cameraTranslate(0, this.dimension.height, 0), |
| 1062 |
this.camera |
| 1063 |
]), this.deep); |
| 1064 |
} |
| 1065 |
|
| 1066 |
this.objects = this.schedule(this.objects); |
| 1067 |
this.draw(this.todos, this.objects, this); |
| 1068 |
this.todos = []; |
| 1069 |
this.deep = false; |
| 1070 |
} |
| 1071 |
|
| 1072 |
}); |
| 1073 |
|
| 1074 |
|
| 1075 |
gfx3d.Viewport.nodeType = gfx.Group.nodeType; |
| 1076 |
|
| 1077 |
gfx3d._creators = { |
| 1078 |
|
| 1079 |
|
| 1080 |
createEdges: function(edges, style){ |
| 1081 |
|
| 1082 |
|
| 1083 |
return this.create3DObject(gfx3d.Edges, edges, style); |
| 1084 |
}, |
| 1085 |
createTriangles: function(tris, style){ |
| 1086 |
|
| 1087 |
|
| 1088 |
return this.create3DObject(gfx3d.Triangles, tris, style); |
| 1089 |
}, |
| 1090 |
createQuads: function(quads, style){ |
| 1091 |
|
| 1092 |
|
| 1093 |
return this.create3DObject(gfx3d.Quads, quads, style); |
| 1094 |
}, |
| 1095 |
createPolygon: function( points){ |
| 1096 |
|
| 1097 |
|
| 1098 |
return this.create3DObject(gfx3d.Polygon, points); |
| 1099 |
}, |
| 1100 |
|
| 1101 |
createOrbit: function(orbit){ |
| 1102 |
|
| 1103 |
|
| 1104 |
return this.create3DObject(gfx3d.Orbit, orbit); |
| 1105 |
}, |
| 1106 |
|
| 1107 |
createCube: function(cube){ |
| 1108 |
|
| 1109 |
|
| 1110 |
return this.create3DObject(gfx3d.Cube, cube); |
| 1111 |
}, |
| 1112 |
|
| 1113 |
createCylinder: function(cylinder){ |
| 1114 |
|
| 1115 |
|
| 1116 |
return this.create3DObject(gfx3d.Cylinder, cylinder); |
| 1117 |
}, |
| 1118 |
|
| 1119 |
createPath3d: function(path){ |
| 1120 |
|
| 1121 |
|
| 1122 |
return this.create3DObject(gfx3d.Path3d, path); |
| 1123 |
}, |
| 1124 |
createScene: function(){ |
| 1125 |
|
| 1126 |
|
| 1127 |
return this.create3DObject(gfx3d.Scene); |
| 1128 |
}, |
| 1129 |
|
| 1130 |
create3DObject: function(objectType, rawObject, style){ |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
var obj = new objectType(); |
| 1138 |
this.adopt(obj); |
| 1139 |
if(rawObject){ obj.setObject(rawObject, style); } |
| 1140 |
return obj; |
| 1141 |
}, |
| 1142 |
|
| 1143 |
adopt: function(obj){ |
| 1144 |
|
| 1145 |
|
| 1146 |
obj.renderer = this.renderer; |
| 1147 |
obj.parent = this; |
| 1148 |
this.objects.push(obj); |
| 1149 |
this.addTodo(obj); |
| 1150 |
return this; |
| 1151 |
}, |
| 1152 |
abandon: function(obj, silently){ |
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
for(var i = 0; i < this.objects.length; ++i){ |
| 1158 |
if(this.objects[i] == obj){ |
| 1159 |
this.objects.splice(i, 1); |
| 1160 |
} |
| 1161 |
} |
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
obj.parent = null; |
| 1167 |
return this; |
| 1168 |
}, |
| 1169 |
|
| 1170 |
|
| 1171 |
setScheduler: function(scheduler){ |
| 1172 |
this.schedule = scheduler; |
| 1173 |
}, |
| 1174 |
|
| 1175 |
setDrawer: function(drawer){ |
| 1176 |
this.draw = drawer; |
| 1177 |
} |
| 1178 |
}; |
| 1179 |
|
| 1180 |
lang.extend(gfx3d.Viewport, gfx3d._creators); |
| 1181 |
lang.extend(gfx3d.Scene, gfx3d._creators); |
| 1182 |
delete gfx3d._creators; |
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
lang.extend(gfx.Surface, { |
| 1189 |
createViewport: function(){ |
| 1190 |
|
| 1191 |
var viewport = this.createObject(gfx3d.Viewport, null, true); |
| 1192 |
|
| 1193 |
viewport.setDimensions(this.getDimensions()); |
| 1194 |
return viewport; |
| 1195 |
} |
| 1196 |
}); |
| 1197 |
|
| 1198 |
return gfx3d.Object; |
| 1199 |
}); |