1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | function FluxBuildings() {
|
6 |
|
7 |
|
8 | this._inTrial = false;
|
9 |
|
10 | this._renderCount = 0;
|
11 |
|
12 | this._maxCount = 10;
|
13 |
|
14 | this._perf = new InteractivePerf();
|
15 |
|
16 | this.printMemory();
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | FluxBuildings.urls = [
|
25 | "https://storage.googleapis.com/object-library/sphere.json",
|
26 | "https://storage.googleapis.com/object-library/sphereBig.json",
|
27 | "https://storage.googleapis.com/object-library/sphereSmall.json",
|
28 | "https://storage.googleapis.com/object-library/spheres.json",
|
29 | "https://storage.googleapis.com/object-library/buildingsMedMesh.json",
|
30 | "https://storage.googleapis.com/object-library/buildingsBigMesh.json"
|
31 | ];
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | FluxBuildings.prototype.loadModel = function (url) {
|
38 | console.log("Loading model",url);
|
39 | var _this = this;
|
40 | this.requestModel(url, function (result) {
|
41 | this._perf.begin();
|
42 | fluxViewport.setGeometryEntity(result).then(function (result) {
|
43 | fluxViewport.focus();
|
44 | _this._perf.end('Load model');
|
45 | if (result.isEmpty()) {
|
46 | console.warn(result.primStatus.invalidKeySummary());
|
47 | }
|
48 | _this.countGeometry();
|
49 | _this.printMemory();
|
50 | }).catch(function (err) {
|
51 | console.log(err);
|
52 | });
|
53 | }.bind(this));
|
54 | };
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | FluxBuildings.prototype.handleRecord = function () {
|
62 | if (!fluxViewport.renderPatched) {
|
63 | var renderFunction = fluxViewport.render;
|
64 | var _this = this;
|
65 | fluxViewport.render = function() {
|
66 | _this._perf.begin();
|
67 | renderFunction.call(fluxViewport);
|
68 | _this._perf.end();
|
69 | };
|
70 | fluxViewport.renderPatched = true;
|
71 | }
|
72 | this._perf.toggleRecording();
|
73 | };
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | FluxBuildings.prototype.handleTrial = function () {
|
81 | this._testRender();
|
82 | };
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | FluxBuildings.prototype.countGeometry = function () {
|
89 | var model = fluxViewport._renderer._model;
|
90 | this.countObjects(model);
|
91 | this.countTriangles(model);
|
92 | this.computeBoundingBox(model);
|
93 | };
|
94 |
|
95 | FluxBuildings.prototype.countObjects = function (objectRoot) {
|
96 | var count = 0;
|
97 | objectRoot.traverse( function (element) {
|
98 | if (element.type === "Mesh") {
|
99 | count++;
|
100 | }
|
101 | });
|
102 | console.log("Number of objects:",count);
|
103 | };
|
104 |
|
105 | FluxBuildings.prototype.countTriangles = function (objectRoot) {
|
106 | var faceCount = 0;
|
107 | var vertCount = 0;
|
108 | objectRoot.traverse( function (element) {
|
109 | if (element.type === "Mesh" && element.visible===true) {
|
110 | if (element.geometry instanceof THREE.BufferGeometry) {
|
111 | vertCount += element.geometry.attributes.position.count;
|
112 | if (element.geometry.attributes.index) {
|
113 | faceCount += element.geometry.attributes.index.count/3;
|
114 | } else {
|
115 | faceCount += element.geometry.attributes.position.count/3;
|
116 | }
|
117 | }
|
118 | else {
|
119 | vertCount += element.geometry.vertices.length;
|
120 | faceCount += element.geometry.faces.length;
|
121 | }
|
122 | }
|
123 | });
|
124 | console.log("Number of vertices:", vertCount);
|
125 | console.log("Number of faces:", faceCount);
|
126 | };
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | FluxBuildings.prototype.computeBoundingBox = function (objectRoot) {
|
133 |
|
134 | var bbMax = new THREE.Vector3(-Infinity, -Infinity, -Infinity);
|
135 | var bbMin = new THREE.Vector3(Infinity, Infinity, Infinity);
|
136 | var tmp = new THREE.Vector3(0,0,0);
|
137 |
|
138 | var foundBufferGeometry = false;
|
139 | var foundRegularGeometry = false;
|
140 | objectRoot.traverse( function (element) {
|
141 | if (element.matrixWorldNeedsUpdate) {
|
142 |
|
143 | element.updateMatrixWorld(true);
|
144 | }
|
145 |
|
146 |
|
147 | if (element.type === "Mesh" && element.visible===true) {
|
148 |
|
149 | if (element.geometry instanceof THREE.BufferGeometry) {
|
150 | foundBufferGeometry = true;
|
151 | var positions = element.geometry.attributes.position.array;
|
152 | var i = 0;
|
153 | while (i < positions.length) {
|
154 | tmp.set(positions[i],positions[i+1],positions[i+2]);
|
155 | tmp.applyMatrix4(element.matrix);
|
156 | bbMin.min(tmp);
|
157 | bbMax.max(tmp);
|
158 | i+=3;
|
159 | }
|
160 | }
|
161 | else {
|
162 | foundRegularGeometry = true;
|
163 | var vertices = element.geometry.vertices;
|
164 | vertices.forEach( function (vert) {
|
165 | vert.applyMatrix4(element.matrix);
|
166 | bbMin.min(vert);
|
167 | bbMax.max(vert);
|
168 | });
|
169 | }
|
170 |
|
171 | }
|
172 | });
|
173 | if (foundBufferGeometry) {
|
174 | console.log("Found buffer geometry");
|
175 | }
|
176 | if (foundRegularGeometry) {
|
177 | console.log("Found regular geometry");
|
178 | }
|
179 |
|
180 | tmp.copy(bbMax);
|
181 | tmp.sub(bbMin);
|
182 | console.log("Maximum axis aligned length:",Math.max(Math.max(tmp.x,tmp.y),tmp.z));
|
183 | };
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | FluxBuildings.prototype.requestModel = function (url, callback) {
|
191 | var xhr = new XMLHttpRequest();
|
192 | xhr.addEventListener('load', function () {
|
193 | if (this.status === 200) {
|
194 | var responseObj = JSON.parse(this.response);
|
195 | callback(responseObj);
|
196 | } else {
|
197 | console.error(this.status, this.response);
|
198 | }
|
199 | });
|
200 | xhr.open("GET", url, true);
|
201 | xhr.send();
|
202 | };
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | FluxBuildings.prototype.printMemory = function () {
|
209 | if (window.performance && window.performance.memory) {
|
210 | console.log("Js Heap used: ", window.performance.memory.usedJSHeapSize/1000000,"M");
|
211 | } else {
|
212 | console.log("This browser does not support memory statistics.");
|
213 | }
|
214 | };
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | FluxBuildings.prototype._testRender = function () {
|
220 |
|
221 | this._perf.startRecording();
|
222 | this._inTrial = true;
|
223 | this._renderCount = 0;
|
224 | for (var i=0;i<this._maxCount;i++) {
|
225 | this._perf.begin();
|
226 | fluxViewport.render();
|
227 | this._perf.end();
|
228 | }
|
229 | this._perf.stopRecording();
|
230 | };
|