3D organization, better Round function, arrayOperation function
This commit is contained in:
parent
2b3ce55a1d
commit
bc578c80b9
@ -27,7 +27,7 @@ Saku = {
|
|||||||
this.focal = foc || 0.35;
|
this.focal = foc || 0.35;
|
||||||
this.size = size || 0.32;
|
this.size = size || 0.32;
|
||||||
},
|
},
|
||||||
Object: function(vertices, options) {
|
Face: function(vertices, options) {
|
||||||
if(vertices === undefined) throw TypeError("Not enough arguments; Expected Array as argument.");
|
if(vertices === undefined) throw TypeError("Not enough arguments; Expected Array as argument.");
|
||||||
var invalid = [[varType(vertices) !== "Array", varType(vertices)]];
|
var invalid = [[varType(vertices) !== "Array", varType(vertices)]];
|
||||||
invalid[1] = vertices.every(function(element) { return varType(element) !== "Array"; });
|
invalid[1] = vertices.every(function(element) { return varType(element) !== "Array"; });
|
||||||
@ -36,7 +36,7 @@ Saku = {
|
|||||||
if(invalid[1]) throw TypeError("Expected Arrays as 1st dimensional element.");
|
if(invalid[1]) throw TypeError("Expected Arrays as 1st dimensional element.");
|
||||||
if(invalid[2]) throw TypeError("Expected Numbers as 2nd dimensional element.");
|
if(invalid[2]) throw TypeError("Expected Numbers as 2nd dimensional element.");
|
||||||
|
|
||||||
this.name = options.name || "Unnamed Object";
|
this.name = options.name || "Unnamed Object Face";
|
||||||
this.vertices = vertices;
|
this.vertices = vertices;
|
||||||
this.origin = options.origin || getOrigin(vertices);
|
this.origin = options.origin || getOrigin(vertices);
|
||||||
|
|
||||||
@ -65,26 +65,68 @@ Saku = {
|
|||||||
arr[index][2] = ele[2] + value;
|
arr[index][2] = ele[2] + value;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
Model: function(faces, connections, name) {
|
||||||
|
this.name = options.name || "Unnamed Object Model";
|
||||||
|
this.faces = faces;
|
||||||
|
this.connections = connections;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Saku["Triangle"] = function(name) {
|
var ref = ["Triangle", "Square", "Pentagon", "Hexagon", "Heptagon", "Octagon", "Nonagon", "Decagon"];
|
||||||
Saku.Object.call(this, [
|
|
||||||
[0,50,0],
|
Saku["Polygon"] = function(sides, name, size) {
|
||||||
[0,50,5],
|
var defArray = [];
|
||||||
[3,50,3]
|
var offset = !(sides%2)*Math.PI/sides;
|
||||||
], {name: (name || "Triangle")});
|
for(var i = 0; i < sides; i++) {
|
||||||
this.prototype = Object.create(Saku.Object.prototype);
|
defArray.push([
|
||||||
|
Math.sin(i*-2*Math.PI/sides+offset),
|
||||||
|
0,
|
||||||
|
Math.cos(i*-2*Math.PI/sides+offset)
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Saku["Square"] = function(name) {
|
defArray = Rnd(defArray,5);
|
||||||
Saku.Object.call(this, [
|
Saku.Face.call(this, defArray, {name: (name || (sides > 10) ? sides+"-gon" : ref[sides-3])});
|
||||||
[0,50,5],
|
this.prototype = Object.create(Saku.Face.prototype);
|
||||||
[5,50,5],
|
}
|
||||||
[5,50,10],
|
|
||||||
[0,50,10]
|
ref.forEach(function(ele, index) {
|
||||||
], {name: (name || "Square")});
|
Saku[ele] = function(name) {
|
||||||
this.prototype = Object.create(Saku.Object.prototype);
|
Saku.Polygon.call(this, index+3, name);
|
||||||
|
this.prototype = Object.create(Saku.Face.prototype);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/*Saku["Cube"] = function(name) {
|
||||||
|
var defArray = [
|
||||||
|
[-0.5, -0.5, 0.5],
|
||||||
|
[0.5, -0.5, 0.5],
|
||||||
|
[]
|
||||||
|
]
|
||||||
|
}*/
|
||||||
|
|
||||||
|
function arrayOperation(item, operator, amount) {
|
||||||
|
var operators = {
|
||||||
|
"+": function(x,y) {return x+y},
|
||||||
|
"-": function(x,y) {return x-y},
|
||||||
|
"*": function(x,y) {return x*y},
|
||||||
|
"/": function(x,y) {return x/y},
|
||||||
|
"^": function(x,y) {return Math.pow(x,y)},
|
||||||
|
"log": function(x,y) {return Math.log(x) / Math.log(y || 10)}
|
||||||
|
}
|
||||||
|
var type = varType(item);
|
||||||
|
if(type === "Array") {
|
||||||
|
var arr = [];
|
||||||
|
for(var i = 0; i < item.length; i++) {
|
||||||
|
arr[i] = arrayOperation(item[i], operator, amount);
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
} else if(type === "Number") {
|
||||||
|
return operators[operator](item, amount);
|
||||||
|
} else {
|
||||||
|
throw new TypeError("Expected Numbers, got " + type + ".");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function varType(variable) {
|
function varType(variable) {
|
||||||
@ -219,8 +261,18 @@ function toRad(deg) {
|
|||||||
return deg/180*Math.PI;
|
return deg/180*Math.PI;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Rnd(num,fig) {
|
function Rnd(item,fig) {
|
||||||
return Math.round(num*Math.pow(10,fig))/Math.pow(10,fig);
|
if(varType(item) === "Array") {
|
||||||
|
var arr = [];
|
||||||
|
for(var i = 0; i < item.length; i++) {
|
||||||
|
arr[i] = Rnd(item[i],fig);
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
} else if(varType(item) === "Number") {
|
||||||
|
return Math.round(item*Math.pow(10,fig))/Math.pow(10,fig);
|
||||||
|
} else {
|
||||||
|
throw new TypeError("Expected Integers, got " + varType(item) + ".");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function dot(vecOne, vecTwo) {
|
function dot(vecOne, vecTwo) {
|
||||||
@ -286,13 +338,12 @@ drawObjects = [];
|
|||||||
verbose = false; // [[Position],[Rotation(x,z)],Focal];
|
verbose = false; // [[Position],[Rotation(x,z)],Focal];
|
||||||
|
|
||||||
scene = new Saku.Scene("glCanvas");
|
scene = new Saku.Scene("glCanvas");
|
||||||
var triangle = new Saku.Triangle();
|
var polygon = new Saku.Polygon(3);
|
||||||
var square = new Saku.Square();
|
|
||||||
var camera = new Saku.Camera();
|
var camera = new Saku.Camera();
|
||||||
scene.setCamera(camera);
|
scene.setCamera(camera);
|
||||||
|
|
||||||
scene.addObject(triangle);
|
scene.addObject(polygon);
|
||||||
scene.addObject(square);
|
polygon.translateY(10);
|
||||||
|
|
||||||
setInterval(function() { updateFrame(scene); }, 500);
|
setInterval(function() { updateFrame(scene); }, 500);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user