📄 object.js
字号:
dojo.declare("dojox.gfx3d.Path3d", dojox.gfx3d.Object, { // This object is still very immature ! constructor: function(){ // summary: a generic line // (this is a helper object, which is defined for convenience) this.object = dojo.clone(dojox.gfx3d.defaultPath3d); this.segments = []; this.absolute = true; this.last = {}; this.path = ""; }, _collectArgs: function(array, args){ // summary: converts an array of arguments to plain numeric values // array: Array: an output argument (array of numbers) // args: Array: an input argument (can be values of Boolean, Number, dojox.gfx.Point, or an embedded array of them) for(var i = 0; i < args.length; ++i){ var t = args[i]; if(typeof(t) == "boolean"){ array.push(t ? 1 : 0); }else if(typeof(t) == "number"){ array.push(t); }else if(t instanceof Array){ this._collectArgs(array, t); }else if("x" in t && "y" in t){ array.push(t.x); array.push(t.y); } } }, // a dictionary, which maps segment type codes to a number of their argemnts _validSegments: {m: 3, l: 3, z: 0}, _pushSegment: function(action, args){ // summary: adds a segment // action: String: valid SVG code for a segment's type // args: Array: a list of parameters for this segment var group = this._validSegments[action.toLowerCase()]; if(typeof(group) == "number"){ if(group){ if(args.length >= group){ var segment = {action: action, args: args.slice(0, args.length - args.length % group)}; this.segments.push(segment); } }else{ var segment = {action: action, args: []}; this.segments.push(segment); } } }, moveTo: function(){ // summary: formes a move segment var args = []; this._collectArgs(args, arguments); this._pushSegment(this.absolute ? "M" : "m", args); return this; // self }, lineTo: function(){ // summary: formes a line segment var args = []; this._collectArgs(args, arguments); this._pushSegment(this.absolute ? "L" : "l", args); return this; // self }, closePath: function(){ // summary: closes a path this._pushSegment("Z", []); return this; // self }, render: function(camera){ // TODO: we need to get the ancestors' matrix var m = dojox.gfx3d.matrix.multiply(camera, this.matrix); // iterate all the segments and convert them to 2D canvas // TODO consider the relative mode var path = "" var _validSegments = this._validSegments; dojo.forEach(this.segments, function(item){ path += item.action; for(var i = 0; i < item.args.length; i+= _validSegments[item.action.toLowerCase()] ){ var pt = dojox.gfx3d.matrix.multiplyPoint(m, item.args[i], item.args[i+1], item.args[i+2]) path += " " + pt.x + " " + pt.y; } }); this.cache = path; }, _draw: function(){ return this.parent.createPath(this.cache); }});dojo.declare("dojox.gfx3d.Triangles", dojox.gfx3d.Object, { constructor: function(){ // summary: a generic triangle // (this is a helper object, which is defined for convenience) this.object = dojo.clone(dojox.gfx3d.defaultTriangles); }, setObject: function(newObject, /* String, optional */ style){ // summary: setup the object // newObject: Array of points || Object // style: String, optional if(newObject instanceof Array){ this.object = dojox.gfx.makeParameters(this.object, { points: newObject, style: style } ); } else { this.object = dojox.gfx.makeParameters(this.object, newObject); } return this; }, render: function(camera){ var m = dojox.gfx3d.matrix.multiply(camera, this.matrix); var c = dojo.map(this.object.points, function(item){ return dojox.gfx3d.matrix.multiplyPoint(m, item); }); this.cache = []; var pool = c.slice(0, 2); var center = c[0]; if(this.object.style == "strip"){ dojo.forEach(c.slice(2), function(item){ pool.push(item); pool.push(pool[0]); this.cache.push(pool); pool = pool.slice(1, 3); }, this); } else if(this.object.style == "fan"){ dojo.forEach(c.slice(2), function(item){ pool.push(item); pool.push(center); this.cache.push(pool); pool = [center, item]; }, this); } else { for(var i = 0; i < c.length; ){ this.cache.push( [ c[i], c[i+1], c[i+2], c[i] ]); i += 3; } } }, draw: function(lighting){ // use the BSP to schedule this.cache = dojox.gfx3d.scheduler.bsp(this.cache, function(it){ return it; }); if(this.shape){ this.shape.clear(); } else { this.shape = this.renderer.createGroup(); } dojo.forEach(this.cache, function(item){ this.shape.createPolyline(item) .setStroke(this.strokeStyle) .setFill(this.toStdFill(lighting, dojox.gfx3d.vector.normalize(item))); }, this); }, getZOrder: function(){ var zOrder = 0; dojo.forEach(this.cache, function(item){ zOrder += (item[0].z + item[1].z + item[2].z) / 3; }); return (this.cache.length > 1) ? zOrder / this.cache.length : 0; }});dojo.declare("dojox.gfx3d.Quads", dojox.gfx3d.Object, { constructor: function(){ // summary: a generic triangle // (this is a helper object, which is defined for convenience) this.object = dojo.clone(dojox.gfx3d.defaultQuads); }, setObject: function(newObject, /* String, optional */ style){ // summary: setup the object // newObject: Array of points || Object // style: String, optional this.object = dojox.gfx.makeParameters(this.object, (newObject instanceof Array) ? { points: newObject, style: style } : newObject ); return this; }, render: function(camera){ var m = dojox.gfx3d.matrix.multiply(camera, this.matrix); var c = dojo.map(this.object.points, function(item){ return dojox.gfx3d.matrix.multiplyPoint(m, item); }); this.cache = []; if(this.object.style == "strip"){ var pool = c.slice(0, 2); for(var i = 2; i < c.length; ){ pool = pool.concat( [ c[i], c[i+1], pool[0] ] ); this.cache.push(pool); pool = pool.slice(2,4); i += 2; } }else{ for(var i = 0; i < c.length; ){ this.cache.push( [c[i], c[i+1], c[i+2], c[i+3], c[i] ] ); i += 4; } } }, draw: function(lighting){ // use the BSP to schedule this.cache = dojox.gfx3d.scheduler.bsp(this.cache, function(it){ return it; }); if(this.shape){ this.shape.clear(); }else{ this.shape = this.renderer.createGroup(); } // using naive iteration to speed things up a bit by avoiding function call overhead for(var x=0; x<this.cache.length; x++){ this.shape.createPolyline(this.cache[x]) .setStroke(this.strokeStyle) .setFill(this.toStdFill(lighting, dojox.gfx3d.vector.normalize(this.cache[x]))); } /* dojo.forEach(this.cache, function(item){ this.shape.createPolyline(item) .setStroke(this.strokeStyle) .setFill(this.toStdFill(lighting, dojox.gfx3d.vector.normalize(item))); }, this); */ }, getZOrder: function(){ var zOrder = 0; // using naive iteration to speed things up a bit by avoiding function call overhead for(var x=0; x<this.cache.length; x++){ var i = this.cache[x]; zOrder += (i[0].z + i[1].z + i[2].z + i[3].z) / 4; } /* dojo.forEach(this.cache, function(item){ zOrder += (item[0].z + item[1].z + item[2].z + item[3].z) / 4; }); */ return (this.cache.length > 1) ? zOrder / this.cache.length : 0; }});dojo.declare("dojox.gfx3d.Polygon", dojox.gfx3d.Object, { constructor: function(){ // summary: a generic triangle // (this is a helper object, which is defined for convenience) this.object = dojo.clone(dojox.gfx3d.defaultPolygon); }, setObject: function(newObject){ // summary: setup the object // newObject: Array of points || Object this.object = dojox.gfx.makeParameters(this.object, (newObject instanceof Array) ? {path: newObject} : newObject) return this; }, render: function(camera){ var m = dojox.gfx3d.matrix.multiply(camera, this.matrix); this.cache = dojo.map(this.object.path, function(item){ return dojox.gfx3d.matrix.multiplyPoint(m, item); }); // add the first point to close the polyline this.cache.push(this.cache[0]); }, draw: function(lighting){ if(this.shape){ this.shape.setShape({points: this.cache}); }else{ this.shape = this.renderer.createPolyline({points: this.cache}); } this.shape.setStroke(this.strokeStyle) .setFill(this.toStdFill(lighting, dojox.gfx3d.matrix.normalize(this.cache))); }, getZOrder: function(){ var zOrder = 0; // using naive iteration to speed things up a bit by avoiding function call overhead for(var x=0; x<this.cache.length; x++){ zOrder += this.cache[x].z; } return (this.cache.length > 1) ? zOrder / this.cache.length : 0; }, getOutline: function(){ return this.cache.slice(0, 3); }});dojo.declare("dojox.gfx3d.Cube", dojox.gfx3d.Object, { constructor: function(){ // summary: a generic triangle // (this is a helper object, which is defined for convenience) this.object = dojo.clone(dojox.gfx3d.defaultCube); this.polygons = []; }, setObject: function(newObject){ // summary: setup the object // newObject: Array of points || Object this.object = dojox.gfx.makeParameters(this.object, newObject); }, render: function(camera){ // parse the top, bottom to get 6 polygons: var a = this.object.top; var g = this.object.bottom; var b = {x: g.x, y: a.y, z: a.z}; var c = {x: g.x, y: g.y, z: a.z}; var d = {x: a.x, y: g.y, z: a.z}; var e = {x: a.x, y: a.y, z: g.z}; var f = {x: g.x, y: a.y, z: g.z}; var h = {x: a.x, y: g.y, z: g.z}; var polygons = [a, b, c, d, e, f, g, h]; var m = dojox.gfx3d.matrix.multiply(camera, this.matrix); var p = dojo.map(polygons, function(item){ return dojox.gfx3d.matrix.multiplyPoint(m, item); }); a = p[0]; b = p[1]; c = p[2]; d = p[3]; e = p[4]; f = p[5]; g = p[6]; h = p[7]; 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]]; }, draw: function(lighting){ // use bsp to sort. this.cache = dojox.gfx3d.scheduler.bsp(this.cache, function(it){ return it; }); // only the last 3 polys are visible. var cache = this.cache.slice(3); if(this.shape){ this.shape.clear(); }else{ this.shape = this.renderer.createGroup(); } for(var x=0; x<cache.length; x++){ this.shape.createPolyline(cache[x]) .setStroke(this.strokeStyle) .setFill(this.toStdFill(lighting, dojox.gfx3d.vector.normalize(cache[x]))); } /* dojo.forEach(cache, function(item){ this.shape.createPolyline(item) .setStroke(this.strokeStyle) .setFill(this.toStdFill(lighting, dojox.gfx3d.vector.normalize(item))); }, this); */ }, getZOrder: function(){ var top = this.cache[0][0]; var bottom = this.cache[1][2]; return (top.z + bottom.z) / 2; }});dojo.declare("dojox.gfx3d.Cylinder", dojox.gfx3d.Object, { constructor: function(){ this.object = dojo.clone(dojox.gfx3d.defaultCylinder); }, render: function(camera){ // get the bottom surface first var m = dojox.gfx3d.matrix.multiply(camera, this.matrix); var angles = [0, Math.PI/4, Math.PI/3]; var center = dojox.gfx3d.matrix.multiplyPoint(m, this.object.center);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -