Skip to content

Commit 383ed9d

Browse files
committed
make objmtl.js compatible with more MTL files; fix bug there; edit demos some more
1 parent 47c7586 commit 383ed9d

File tree

9 files changed

+87
-56
lines changed

9 files changed

+87
-56
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ Examples
6969
var shape=scene.makeShape(mesh).setColor("red");
7070
// Add the shape to the scene
7171
scene.addShape(shape);
72-
// Set the starting rotation
73-
var rotation=[0,0,0];
7472
// Create a timer
7573
var timer={};
7674
// Set up the render loop
@@ -80,12 +78,13 @@ Examples
8078
scene.setPerspective(45,scene.getClientAspect(),0.1,100);
8179
// Move the camera back 40 units.
8280
scene.setLookAt([0,0,40]);
83-
// Adjust x-rotation
84-
rotation[0]=360*GLUtil.getTimePosition(timer,time,6000);
85-
// Adjust y-rotation
86-
rotation[1]=360*GLUtil.getTimePosition(timer,time,12000);
8781
// Update the shape's rotation
88-
shape.setQuaternion(GLMath.quatFromTaitBryan(rotation));
82+
var q=GLMath.quatFromTaitBryan(
83+
360*GLUtil.getTimePosition(timer,time,6000),
84+
360*GLUtil.getTimePosition(timer,time,12000),
85+
0
86+
);
87+
shape.setQuaternion(q);
8988
// Render the scene
9089
scene.render();
9190
});

demos/compositeMesh.html

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@
6464
// to Scene3D.
6565
var scene=new Scene3D(document.getElementById("canvas"));
6666
scene.setDirectionalLight(0,[5,5,10])
67-
// Set the perspective view. Camera has a 45-degree field of view
68-
// and will see objects from 0.1 to 100 units away.
69-
scene.setPerspective(45,scene.getClientAspect(),0.1,100);
70-
// Move the camera back 40 units.
71-
scene.setLookAt([0,0,40]);
7267
// Create an arrow mesh 10 units in size
7368
var mesh1=multiColoredArrow(10,2,1,2);
7469
var mesh2=arrow(10,2,1,2);
@@ -79,14 +74,23 @@
7974
scene.addShape(shape1);
8075
// Set the starting rotation
8176
var rotation=[0,0,0];
77+
// Create a timer
8278
var timer={};
8379
// Set up the render loop
8480
GLUtil.renderLoop(function(time){
85-
rotation[0]=360*GLUtil.getTimePosition(timer,time,6000);
86-
rotation[1]=360*GLUtil.getTimePosition(timer,time,12000);
81+
// Set the perspective view. Camera has a 45-degree field of view
82+
// and will see objects from 0.1 to 100 units away.
83+
scene.setPerspective(45,scene.getClientAspect(),0.1,100);
84+
// Move the camera back 40 units.
85+
scene.setLookAt([0,0,40]);
8786
// Update the shape's rotation
88-
var q=GLMath.quatFromTaitBryan(rotation);
89-
scene.shapes[0].setQuaternion(q);
87+
var q=GLMath.quatFromTaitBryan(
88+
360*GLUtil.getTimePosition(timer,time,6000),
89+
360*GLUtil.getTimePosition(timer,time,12000),
90+
0
91+
);
92+
shape1.setQuaternion(q);
93+
shape2.setQuaternion(q);
9094
// Render the scene
9195
scene.render();
9296
});

demos/simple.html

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
var shape=scene.makeShape(mesh).setColor("red");
2929
// Add the shape to the scene
3030
scene.addShape(shape);
31-
// Set the starting rotation
32-
var rotation=[0,0,0];
3331
// Create a timer
3432
var timer={};
3533
// Set up the render loop
@@ -39,12 +37,12 @@
3937
scene.setPerspective(45,scene.getClientAspect(),0.1,100);
4038
// Move the camera back 40 units.
4139
scene.setLookAt([0,0,40]);
42-
// Adjust x-rotation
43-
rotation[0]=360*GLUtil.getTimePosition(timer,time,6000);
44-
// Adjust y-rotation
45-
rotation[1]=360*GLUtil.getTimePosition(timer,time,12000);
4640
// Update the shape's rotation
47-
var q=GLMath.quatFromTaitBryan(rotation);
41+
var q=GLMath.quatFromTaitBryan(
42+
360*GLUtil.getTimePosition(timer,time,6000),
43+
360*GLUtil.getTimePosition(timer,time,12000),
44+
0
45+
);
4846
shape.setQuaternion(q);
4947
// Render the scene
5048
scene.render();

demos/surfaces.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
<script type="text/javascript" src="demoutil.js"></script>
1111
</head>
1212
<body>
13+
<p style="position:absolute;left:0;top:1em">
1314
<a href="javascript:link1()">Superellipsoid</a>,
1415
<a href="javascript:link2()">Klein bottle</a>,
1516
<a href="javascript:link3()">M&ouml;bius strip</a>,
1617
<a href="javascript:link4()">Surface of revolution for f(x) = sin x</a>,
1718
<a href="javascript:link5()">Surface of revolution for f(x) = x<sup>2</sup></a>,
1819
<a href="javascript:link6()">Supertoroid</a>
19-
<br>
20+
</p>
2021
<canvas id=canvas></canvas>
2122
<script id="demo">
2223
//<!--

demos/textured.html

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,30 @@
2727
var scene=new Scene3D(document.getElementById("canvas"))
2828
.cullFace(Scene3D.BACK);
2929
var fc=new FrameCounterDiv(scene)
30-
scene.setPerspective(45,scene.getClientAspect(),0.1,100);
31-
scene.setLookAt([0,0,40]);
32-
var timer={}
3330
scene.loadAndMapTexture("461223191.jpg").then(function(tex){
3431
var mesh=Meshes.createBox(10,20,20);
3532
var shape=scene.makeShape(mesh).setTexture(tex)
3633
scene.addShape(shape);
37-
var rotation=[0,0,0];
38-
GLUtil.renderLoop(function(time){
39-
fc.update();
40-
rotation[0]=360*GLUtil.getTimePosition(timer,time,12000);
41-
rotation[1]=360*GLUtil.getTimePosition(timer,time,6000);
42-
var q=GLMath.quatFromTaitBryan(rotation);
43-
shape.setQuaternion(q);
44-
scene.render();
45-
});
34+
// Create a timer
35+
var timer={};
36+
// Set up the render loop
37+
GLUtil.renderLoop(function(time){
38+
// Set the perspective view. Camera has a 45-degree field of view
39+
// and will see objects from 0.1 to 100 units away.
40+
scene.setPerspective(45,scene.getClientAspect(),0.1,100);
41+
// Move the camera back 40 units.
42+
scene.setLookAt([0,0,40]);
43+
// Update the shape's rotation
44+
var q=GLMath.quatFromTaitBryan(
45+
360*GLUtil.getTimePosition(timer,time,6000),
46+
360*GLUtil.getTimePosition(timer,time,12000),
47+
0
48+
);
49+
fc.update();
50+
shape.setQuaternion(q);
51+
// Render the scene
52+
scene.render();
53+
});
4654
})
4755
//-->
4856
</script>

demos/tris.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@
109109

110110
// Create the 3D scene; find the HTML canvas and pass it
111111
// to Scene3D.
112-
var scene=new Scene3D(document.getElementById("canvas"));
113-
scene.disableLighting()
112+
var scene=new Scene3D(document.getElementById("canvas"))
113+
.setClearColor("white")
114+
.disableLighting()
114115
var tris=new TriangleParticles(scene,200);
115116
// Set up the render loop
116117
GLUtil.renderLoop(function(time){

glutil-eval.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ BezierCurve.prototype.evaluate=function(u){
178178
/**
179179
* A parametric evaluator for B&eacute;zier surfaces.<p>
180180
* A B&eacute;zier surface is defined by a series of control points, where
181-
* the first and last control points on each corner define the endpoints of the surface, and
181+
* the control points on each corner define the endpoints of the surface, and
182182
* the remaining control points define the surface's shape, though they don't
183183
* necessarily cross the surface.
184184
* @class
@@ -188,11 +188,11 @@ BezierCurve.prototype.evaluate=function(u){
188188
* control point is an array with the same length as the other control points.
189189
* It is assumed that:<ul>
190190
* <li>The length of this parameter minus 1 represents the degree of the B&eacute;zier
191-
* curve along the V axis. For example, a degree-3 (cubic) curve along the V axis
191+
* surface along the V axis. For example, a degree-3 (cubic) surface along the V axis
192192
* contains 4 control points, one in each control point array. A degree of 1 on
193193
* both the U and V axes results in a flat surface.
194194
* <li>The length of the first control point array minus 1 represents the degree of the B&eacute;zier
195-
* curve along the U axis.
195+
* surface along the U axis.
196196
* <li>The first control point's length represents the size of all the control
197197
* points.
198198
* </ul>

objmtl.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ MtlData._getMaterial=function(mtl){
173173
}
174174
if(mtl.hasOwnProperty("Ke")){
175175
var ke=mtl["Ke"];
176-
emission=[ke,ke,ke];
176+
if(ke.length==1){
177+
emission=[ke,ke,ke];
178+
} else {
179+
emission=xyzToRgb(ke);
180+
}
177181
}
178182
if(mtl.hasOwnProperty("Ks")){
179183
specular=xyzToRgb(mtl["Ks"]);
@@ -212,12 +216,10 @@ ObjData.loadMtlFromUrl=function(url){
212216
Loads a WaveFront OBJ file (along with its associated MTL, or
213217
material file, if available) asynchronously.
214218
@param {string} url The URL to load.
215-
@return {Promise} A promise that:
216-
- Resolves when:
217-
The OBJ file is loaded successfully, whether or not its associated
218-
MTL is also loaded successfully. The result is an ObjData object.
219-
- Is rejected when:
220-
An error occurs when loading the OBJ file.
219+
@return {Promise} A promise that resolves when
220+
the OBJ file is loaded successfully, whether or not its associated
221+
MTL is also loaded successfully (the result is an ObjData object),
222+
and is rejected when an error occurs when loading the OBJ file.
221223
*/
222224
ObjData.loadObjFromUrl=function(url){
223225
return GLUtil.loadFileFromUrl(url).then(
@@ -251,13 +253,15 @@ ObjData.loadObjFromUrl=function(url){
251253
MtlData._loadMtl=function(str){
252254
var number="(-?(?:\\d+\\.?\\d*|\\d*\\.\\d+)(?:[Ee][\\+\\-]?\\d+)?)"
253255
var nonnegInteger="(\\d+)"
254-
var oneNumLine=new RegExp("^(Ns|d|Tr|Ni|Ke)\\s+"+number+"\\s*$")
255-
var oneIntLine=new RegExp("^(illum)\\s+"+nonnegInteger+"\\s*$")
256-
var threeNumLine=new RegExp("^(Tf)\\s+"+number+"\\s+"+number
256+
var oneNumLine=new RegExp("^[ \\t]*(Ns|d|Tr|Ni)\\s+"+number+"\\s*$")
257+
var oneIntLine=new RegExp("^[ \\t]*(illum)\\s+"+nonnegInteger+"\\s*$")
258+
var threeNumLine=new RegExp("^[ \\t]*(Tf)\\s+"+number+"\\s+"+number
257259
+"\\s+"+number+"\\s*$")
258-
var threeOrFourNumLine=new RegExp("^(Kd|Ka|Ks)\\s+"+number+"\\s+"+number
260+
var oneOrThreeNumLine=new RegExp("^[ \\t]*(Ke)\\s+"+number+"(?:\\s+"+number
261+
+"\\s+"+number+")?\\s*$")
262+
var threeOrFourNumLine=new RegExp("^[ \\t]*(Kd|Ka|Ks)\\s+"+number+"\\s+"+number
259263
+"\\s+"+number+"(?:\\s+"+number+")?\\s*$")
260-
var mapLine=new RegExp("^(map_Kd|map_bump|map_Ka|map_Ks)\\s+(.*?)\\s*$")
264+
var mapLine=new RegExp("^[ \\t]*(map_Kd|map_bump|map_Ka|map_Ks)\\s+(.*?)\\s*$")
261265
var newmtlLine=new RegExp("^newmtl\\s+([^\\s]*)$")
262266
var faceStart=new RegExp("^f\\s+")
263267
var lines=str.split(/\r?\n/)
@@ -305,6 +309,15 @@ MtlData._loadMtl=function(str){
305309
currentMat[e[1]]=[parseFloat(e[2]),parseFloat(e[3]),parseFloat(e[4])];
306310
continue;
307311
}
312+
e=oneOrThreeNumLine.exec(line)
313+
if(e){
314+
if(e[3]){
315+
currentMat[e[1]]=[parseFloat(e[2]),parseFloat(e[3]),parseFloat(e[4])];
316+
} else {
317+
currentMat[e[1]]=[parseFloat(e[2]),parseFloat(e[2]),parseFloat(e[2])];
318+
}
319+
continue;
320+
}
308321
e=oneNumLine.exec(line)
309322
if(e){
310323
currentMat[e[1]]=parseFloat(e[2]);
@@ -526,7 +539,7 @@ ObjData._loadObj=function(str){
526539
vertexKind=-1;
527540
lastPrimitiveSeen=-1;
528541
haveNormals=false;
529-
usemtl=nul;
542+
usemtl=null;
530543
mesh=new Mesh();
531544
}
532545
meshName=e[2];

tutorials/camera.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Introduction
22

3-
This article describes projection and view transforms commonly used in 3D graphics,
3+
This tip describes projection and view transforms commonly used in 3D graphics,
44
especially when using my [HTML 3D Library](http://peteroupc.github.io/html3dutil).
55

66
**Download the latest version of the library at the [HTML 3D Library's Releases page](https://github.com/peteroupc/html3dutil/releases).**
@@ -25,7 +25,7 @@ the HTML 3D library.
2525

2626
* A _world matrix_ transforms an object's own coordinates to _world space_,
2727
the coordinate system shared by every object in the scene. The world matrix
28-
is not discussed in this article.
28+
is not discussed in this page.
2929
* A _view matrix_ transforms coordinates in world space to _camera space_ or _eye space_.
3030
* A _projection matrix_ transforms coordinates in camera space to _clip space_.
3131
* Additionally, the graphics pipeline (outside the HTML 3D library) converts the
@@ -178,3 +178,10 @@ This is optional. The default is [0, 1, 0].
178178
This method allows you to set the view matrix to an arbitrary [4x4 matrix]{@tutorial glmath}.
179179

180180
* `matrix` - The 4x4 matrix to use.
181+
182+
## Other Pages
183+
184+
The following pages of mine on CodeProject also discuss this library:
185+
186+
* [_Public-Domain HTML 3D Library_](http://www.codeproject.com/Tips/896839/Public-Domain-HTML-ThreeD-Library)
187+
* [_Creating shapes using the Public Domain HTML 3D Library_](http://www.codeproject.com/Tips/987914/Creating-shapes-using-the-Public-Domain-HTML-D-Lib)

0 commit comments

Comments
 (0)