From d4fe4c108b46810399b3e8d0c3b9e2189f7e44a9 Mon Sep 17 00:00:00 2001 From: Mysterix Date: Thu, 1 May 2014 21:33:16 +0200 Subject: [PATCH] Solving multiple shapes display order with new function addOrdered --- js/isomer.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/js/isomer.js b/js/isomer.js index 0ff509a..debf5bc 100644 --- a/js/isomer.js +++ b/js/isomer.js @@ -82,6 +82,35 @@ Isomer.prototype.add = function (item, baseColor) { } }; +/** + * Adds an array of shapes to the scene, sorting their faces globally + * so that they are displayed in the right order + * + * Input is given as an array of {shape: yourShape, color: yourColor} + */ + +Isomer.prototype.addOrdered = function (item) { + var Point = Isomer.Point; + var observer = new Point(-10, -10, 10); + var pathList = []; + var index = 0; + + for (var i = 0; i < item.length; i++) { + for(var j = 0 ; j < item[i].shape.paths.length ; j++){ + pathList[index] = { + path: item[i].shape.paths[j], + color: item[i].color, + distance: item[i].shape._pathDistances(item[i].shape.paths[j], observer) + }; + index++; + } + } + pathList.sort(function(pathA, pathB){return (pathB.distance.averageDistance - pathA.distance.averageDistance)}); + + for (var i = 0 ; i < pathList.length ; i++) { + this._addPath(pathList[i].path, pathList[i].color); + } +}; /** * Adds a path to the scene