Error messages
This commit is contained in:
parent
9e1a5c3b98
commit
4f68a00391
|
@ -23,10 +23,15 @@
|
|||
#percentage {
|
||||
color: white;
|
||||
}
|
||||
|
||||
#errormessage{
|
||||
color: red;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="bottombar">
|
||||
<div id="errormessage"></div>
|
||||
<progress id="progressbar" value=0 max=10000></progress>
|
||||
<span id="percentage"></span>
|
||||
</div>
|
||||
|
|
55
src/Model.js
55
src/Model.js
|
@ -1,5 +1,5 @@
|
|||
class Model extends THREE.Mesh {
|
||||
constructor() {
|
||||
constructor(path) {
|
||||
let geometry = new THREE.Geometry();
|
||||
let materials = [
|
||||
new THREE.MeshLambertMaterial( { color: 0xffffff, side: THREE.DoubleSide } ),
|
||||
|
@ -7,12 +7,50 @@ class Model extends THREE.Mesh {
|
|||
];
|
||||
super(geometry, materials);
|
||||
this.frustumCulled = false;
|
||||
this.path = path;
|
||||
this.vertices = [];
|
||||
this.currentLine = 1;
|
||||
}
|
||||
|
||||
throwError(message) {
|
||||
let e = new Error("In " + this.path + ":L" + this.currentLine + " " + message);
|
||||
e.type = "custom";
|
||||
throw e;
|
||||
}
|
||||
|
||||
checkVertex(id) {
|
||||
if (this.geometry.vertices[id] === undefined) {
|
||||
this.throwError("EditVertex requires vertex " + (id + 1) + " but there is no such vertex");
|
||||
}
|
||||
}
|
||||
|
||||
checkFaceId(id) {
|
||||
if (this.geometry.faces[id] === undefined) {
|
||||
this.throwError("EditFace requires face " + (id + 1) + " but there is no such face");
|
||||
}
|
||||
}
|
||||
|
||||
checkFace(f) {
|
||||
let vertices = this.geometry.vertices;
|
||||
|
||||
if (vertices[f.a] === undefined) {
|
||||
this.throwError("Face requires vertex " + (f.a + 1) + " but there is no such vertex");
|
||||
}
|
||||
|
||||
if (vertices[f.b] === undefined) {
|
||||
this.throwError("Face requires vertex " + (f.b + 1) + " but there is no such vertex");
|
||||
}
|
||||
|
||||
if (vertices[f.c] === undefined) {
|
||||
this.throwError("Face requires vertex " + (f.c + 1) + " but there is no such vertex");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
manageElement(element) {
|
||||
|
||||
let vertices = this.geometry.vertices;
|
||||
let f, normal;
|
||||
|
||||
switch (element.type) {
|
||||
case Element.AddVertex:
|
||||
|
@ -21,14 +59,16 @@ class Model extends THREE.Mesh {
|
|||
break;
|
||||
|
||||
case Element.EditVertex:
|
||||
this.checkVertex(element.id);
|
||||
this.geometry.vertices[element.id].copy(element.value);
|
||||
this.geometry.verticesNeedUpdate = true;
|
||||
break;
|
||||
|
||||
case Element.AddFace:
|
||||
|
||||
let f = element.value;
|
||||
let normal =
|
||||
f = element.value;
|
||||
this.checkFace(f);
|
||||
normal =
|
||||
vertices[f.b].clone().sub(vertices[f.a])
|
||||
.cross(vertices[f.c].clone().sub(vertices[f.a]));
|
||||
normal.normalize();
|
||||
|
@ -43,6 +83,8 @@ class Model extends THREE.Mesh {
|
|||
case Element.AddTriangleFan:
|
||||
|
||||
for (let f of element.value) {
|
||||
|
||||
this.checkFace(f);
|
||||
let normal =
|
||||
vertices[f.b].clone().sub(vertices[f.a])
|
||||
.cross(vertices[f.c].clone().sub(vertices[f.a]));
|
||||
|
@ -61,12 +103,16 @@ class Model extends THREE.Mesh {
|
|||
case Element.EditFace:
|
||||
|
||||
f = element.value;
|
||||
this.checkFaceId(element.id);
|
||||
this.checkFace(f);
|
||||
normal =
|
||||
vertices[f.b].clone().sub(vertices[f.a])
|
||||
.cross(vertices[f.c].clone().sub(vertices[f.a]));
|
||||
normal.normalize();
|
||||
|
||||
f.normal = normal;
|
||||
|
||||
|
||||
this.geometry.faces[element.id] = f;
|
||||
this.geometry.elementsNeedUpdate = true;
|
||||
break;
|
||||
|
@ -74,12 +120,13 @@ class Model extends THREE.Mesh {
|
|||
case Element.DeleteFace:
|
||||
this.geometry.faces[element.id].materialIndex = 1;
|
||||
this.geometry.elementsNeedUpdate = true;
|
||||
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
throw new Error("unknown element type: " + element.type);
|
||||
}
|
||||
|
||||
this.currentLine++;
|
||||
}
|
||||
}
|
||||
|
|
14
src/main.js
14
src/main.js
|
@ -5,13 +5,21 @@ animate();
|
|||
|
||||
function init() {
|
||||
|
||||
let url = document.URL.split('?')[1] || "bunny_remove.obj";
|
||||
let url = 'assets/' + document.URL.split('?')[1] || "bunny_remove.obj";
|
||||
|
||||
loader = new Loader('assets/' + url, 1024, 20);
|
||||
loader = new Loader(url, 1024, 20);
|
||||
loader.start(function(elements) {
|
||||
for (let element of elements) {
|
||||
if (element !== undefined) {
|
||||
try {
|
||||
model.manageElement(element);
|
||||
} catch(e) {
|
||||
if (e.type === "custom") {
|
||||
document.getElementById('errormessage').innerHTML = e;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -21,7 +29,7 @@ function init() {
|
|||
|
||||
scene = new THREE.Scene();
|
||||
|
||||
model = new Model();
|
||||
model = new Model(url);
|
||||
scene.add(model);
|
||||
|
||||
light1 = new THREE.AmbientLight(0x999999);
|
||||
|
|
Loading…
Reference in New Issue