Error messages

This commit is contained in:
Thomas Forgione 2019-12-11 17:06:34 +01:00
parent 9e1a5c3b98
commit 4f68a00391
No known key found for this signature in database
GPG Key ID: BFD17A2D71B3B5E7
3 changed files with 68 additions and 8 deletions

View File

@ -23,10 +23,15 @@
#percentage { #percentage {
color: white; color: white;
} }
#errormessage{
color: red;
}
</style> </style>
</head> </head>
<body> <body>
<div id="bottombar"> <div id="bottombar">
<div id="errormessage"></div>
<progress id="progressbar" value=0 max=10000></progress> <progress id="progressbar" value=0 max=10000></progress>
<span id="percentage"></span> <span id="percentage"></span>
</div> </div>

View File

@ -1,5 +1,5 @@
class Model extends THREE.Mesh { class Model extends THREE.Mesh {
constructor() { constructor(path) {
let geometry = new THREE.Geometry(); let geometry = new THREE.Geometry();
let materials = [ let materials = [
new THREE.MeshLambertMaterial( { color: 0xffffff, side: THREE.DoubleSide } ), new THREE.MeshLambertMaterial( { color: 0xffffff, side: THREE.DoubleSide } ),
@ -7,12 +7,50 @@ class Model extends THREE.Mesh {
]; ];
super(geometry, materials); super(geometry, materials);
this.frustumCulled = false; this.frustumCulled = false;
this.path = path;
this.vertices = []; 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) { manageElement(element) {
let vertices = this.geometry.vertices; let vertices = this.geometry.vertices;
let f, normal;
switch (element.type) { switch (element.type) {
case Element.AddVertex: case Element.AddVertex:
@ -21,14 +59,16 @@ class Model extends THREE.Mesh {
break; break;
case Element.EditVertex: case Element.EditVertex:
this.checkVertex(element.id);
this.geometry.vertices[element.id].copy(element.value); this.geometry.vertices[element.id].copy(element.value);
this.geometry.verticesNeedUpdate = true; this.geometry.verticesNeedUpdate = true;
break; break;
case Element.AddFace: case Element.AddFace:
let f = element.value; f = element.value;
let normal = this.checkFace(f);
normal =
vertices[f.b].clone().sub(vertices[f.a]) vertices[f.b].clone().sub(vertices[f.a])
.cross(vertices[f.c].clone().sub(vertices[f.a])); .cross(vertices[f.c].clone().sub(vertices[f.a]));
normal.normalize(); normal.normalize();
@ -43,6 +83,8 @@ class Model extends THREE.Mesh {
case Element.AddTriangleFan: case Element.AddTriangleFan:
for (let f of element.value) { for (let f of element.value) {
this.checkFace(f);
let normal = let normal =
vertices[f.b].clone().sub(vertices[f.a]) vertices[f.b].clone().sub(vertices[f.a])
.cross(vertices[f.c].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: case Element.EditFace:
f = element.value; f = element.value;
this.checkFaceId(element.id);
this.checkFace(f);
normal = normal =
vertices[f.b].clone().sub(vertices[f.a]) vertices[f.b].clone().sub(vertices[f.a])
.cross(vertices[f.c].clone().sub(vertices[f.a])); .cross(vertices[f.c].clone().sub(vertices[f.a]));
normal.normalize(); normal.normalize();
f.normal = normal; f.normal = normal;
this.geometry.faces[element.id] = f; this.geometry.faces[element.id] = f;
this.geometry.elementsNeedUpdate = true; this.geometry.elementsNeedUpdate = true;
break; break;
@ -74,12 +120,13 @@ class Model extends THREE.Mesh {
case Element.DeleteFace: case Element.DeleteFace:
this.geometry.faces[element.id].materialIndex = 1; this.geometry.faces[element.id].materialIndex = 1;
this.geometry.elementsNeedUpdate = true; this.geometry.elementsNeedUpdate = true;
break; break;
default: default:
throw new Error("unknown element type: " + element.type); throw new Error("unknown element type: " + element.type);
} }
this.currentLine++;
} }
} }

View File

@ -5,13 +5,21 @@ animate();
function init() { 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) { loader.start(function(elements) {
for (let element of elements) { for (let element of elements) {
if (element !== undefined) { if (element !== undefined) {
try {
model.manageElement(element); 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(); scene = new THREE.Scene();
model = new Model(); model = new Model(url);
scene.add(model); scene.add(model);
light1 = new THREE.AmbientLight(0x999999); light1 = new THREE.AmbientLight(0x999999);