Skip to content

Commit ee8a285

Browse files
committed
Edit shader to fix gears demo; fix bounding box calculation in Shape.getBounds when the shape is transformed, to fix sporadic frustum culling issue in gears demo; add vec3abs, vec3absInPlace, vec4abs, vec4absInPlace, vec4negateInPlace, boxDimensions, and boxCenter to H3DU.Math
1 parent 6eb7dc6 commit ee8a285

File tree

9 files changed

+348
-76
lines changed

9 files changed

+348
-76
lines changed

demos/gears.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,20 @@
122122
}
123123
return mesh;
124124
}
125-
// TODO: Back of gears looks speckled
125+
126126
window.onload = function() {
127127
"use strict";
128128
var scene = new H3DU.Scene3D(document.getElementById("canvas"));
129129
var sub = new H3DU.Batch3D();
130+
scene.cullFace(H3DU.Scene3D.BACK);
130131
sub.getLights().setDirectionalLight(0, [5, 5, 10]);
131132
var gear1 = createGear(1.0, 4.0, 1.0, 20, 0.7);
132133
var gear2 = createGear(0.5, 2.0, 2.0, 10, 0.7);
133134
var gear3 = createGear(1.3, 2.0, 0.5, 10, 0.7);
134135
gear1 = new H3DU.Shape(gear1).setColor("red");
135136
gear2 = new H3DU.Shape(gear2).setColor("lime");
136137
gear3 = new H3DU.Shape(gear3).setColor("blue");
137-
scene.addShape(gear1).addShape(gear2).addShape(gear3);
138+
sub.addShape(gear1).addShape(gear2).addShape(gear3);
138139
var viewRotx = 20;
139140
var viewRoty = 30;
140141
var viewRotz = 0;
@@ -147,9 +148,9 @@
147148
.setOrientation(viewRotx, 1, 0, 0)
148149
.multOrientation(viewRoty, 0, 1, 0)
149150
.multOrientation(viewRotz, 0, 0, 1);
150-
scene.setProjectionMatrix(H3DU.Math.mat4frustum(
151+
sub.setProjectionMatrix(H3DU.Math.mat4frustum(
151152
-1, 1, -h, h, 5, 60));
152-
scene.setViewMatrix(view.getMatrix());
153+
sub.setViewMatrix(view.getMatrix());
153154
gear1.getTransform()
154155
.setPosition(-3, -2, 0)
155156
.setOrientation(angle, 0, 0, 1);
@@ -159,7 +160,7 @@
159160
gear3.getTransform()
160161
.setPosition(-3.1, 4.2, 0)
161162
.setOrientation(-2 * angle - 25, 0, 0, 1);
162-
scene.render();
163+
scene.render(sub);
163164
var frames = H3DU.newFrames(info, time);
164165
angle += 4 / 2.0 * frames;
165166
viewRotx += 3 / 2.0 * frames;

doc/H3DU.Math.md

Lines changed: 112 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ See the tutorial "<a href="tutorial-glmath.md">H3DU's Math Functions</a>" for mo
4747
* [.quatNormInPlace](#H3DU.Math.quatNormInPlace)
4848
* [.quatScale](#H3DU.Math.quatScale)
4949
* [.quatScaleInPlace](#H3DU.Math.quatScaleInPlace)
50+
* [boxCenter](#H3DU.Math.boxCenter)
51+
* [boxDimensions](#H3DU.Math.boxDimensions)
5052
* [boxIsEmpty](#H3DU.Math.boxIsEmpty)
5153
* [frustumHasBox](#H3DU.Math.frustumHasBox)
5254
* [frustumHasPoint](#H3DU.Math.frustumHasPoint)
@@ -99,6 +101,8 @@ See the tutorial "<a href="tutorial-glmath.md">H3DU's Math Functions</a>" for mo
99101
* [quatToMat4](#H3DU.Math.quatToMat4)
100102
* [quatToTaitBryan](#H3DU.Math.quatToTaitBryan)
101103
* [quatTransform](#H3DU.Math.quatTransform)
104+
* [vec3abs](#H3DU.Math.vec3abs)
105+
* [vec3absInPlace](#H3DU.Math.vec3absInPlace)
102106
* [vec3add](#H3DU.Math.vec3add)
103107
* [vec3addInPlace](#H3DU.Math.vec3addInPlace)
104108
* [vec3assign](#H3DU.Math.vec3assign)
@@ -121,12 +125,15 @@ See the tutorial "<a href="tutorial-glmath.md">H3DU's Math Functions</a>" for mo
121125
* [vec3subInPlace](#H3DU.Math.vec3subInPlace)
122126
* [vec3toWindowPoint](#H3DU.Math.vec3toWindowPoint)
123127
* [vec3triple](#H3DU.Math.vec3triple)
128+
* [vec4abs](#H3DU.Math.vec4abs)
129+
* [vec4absInPlace](#H3DU.Math.vec4absInPlace)
124130
* [vec4assign](#H3DU.Math.vec4assign)
125131
* [vec4copy](#H3DU.Math.vec4copy)
126132
* [vec4dot](#H3DU.Math.vec4dot)
127133
* [vec4length](#H3DU.Math.vec4length)
128134
* [vec4lerp](#H3DU.Math.vec4lerp)
129135
* [vec4negate](#H3DU.Math.vec4negate)
136+
* [vec4negateInPlace](#H3DU.Math.vec4negateInPlace)
130137
* [vec4norm](#H3DU.Math.vec4norm)
131138
* [vec4normInPlace](#H3DU.Math.vec4normInPlace)
132139
* [vec4scale](#H3DU.Math.vec4scale)
@@ -415,9 +422,42 @@ The parameter "a". (Type: Array.&lt;Number>)
415422

416423
<a href="H3DU.Math.md#H3DU.Math.vec4scaleInPlace">H3DU.Math.vec4scaleInPlace</a>
417424

425+
### (static) H3DU.Math.boxCenter(box) <a id='H3DU.Math.boxCenter'></a>
426+
427+
Finds the center of a 3D bounding box.
428+
429+
#### Parameters
430+
431+
* `box` (Type: Array.&lt;Number>)<br>
432+
An axis-aligned bounding box, which is an array of six values. The first three values are the smallest X, Y, and Z coordinates, and the last three values are the largest X, Y, and Z coordinates.
433+
434+
#### Return Value
435+
436+
A 3-element array containing the
437+
X, Y, and Z coordinates, respectively, of the bounding box's
438+
center. (Type: Array.&lt;Number>)
439+
440+
### (static) H3DU.Math.boxDimensions(box) <a id='H3DU.Math.boxDimensions'></a>
441+
442+
Finds the dimensions of a 3D bounding box. This is done by subtracting
443+
the first three values of the given array with its last three values.
444+
445+
#### Parameters
446+
447+
* `box` (Type: Array.&lt;Number>)<br>
448+
An axis-aligned bounding box, which is an array of six values. The first three values are the smallest X, Y, and Z coordinates, and the last three values are the largest X, Y, and Z coordinates.
449+
450+
#### Return Value
451+
452+
A 3-element array containing the
453+
width, height, and depth of the bounding box, respectively. If
454+
at least one of the minimum coordinates is greater than its
455+
corresponding maximum coordinate, the array can contain
456+
negative values. (Type: Array.&lt;Number>)
457+
418458
### (static) H3DU.Math.boxIsEmpty(box) <a id='H3DU.Math.boxIsEmpty'></a>
419459

420-
Determines whether a bounding box is empty.
460+
Determines whether a 3D bounding box is empty.
421461
This is determined if the minimum coordinate
422462
is larger than the corresponding maximum coordinate.
423463

@@ -1492,6 +1532,34 @@ the transformed vector. The fourth element will be 1.0.
14921532
If the input vector has 3 elements, a 3-element vector will
14931533
be returned instead. (Type: Array.&lt;Number>)
14941534

1535+
### (static) H3DU.Math.vec3abs(a) <a id='H3DU.Math.vec3abs'></a>
1536+
1537+
Returns a new 3-element
1538+
vector with the absolute value of each of its components.
1539+
1540+
#### Parameters
1541+
1542+
* `a` (Type: Array.&lt;Number>)<br>
1543+
A 3-element vector.
1544+
1545+
#### Return Value
1546+
1547+
The resulting 3-element vector. (Type: Array.&lt;Number>)
1548+
1549+
### (static) H3DU.Math.vec3absInPlace(a) <a id='H3DU.Math.vec3absInPlace'></a>
1550+
1551+
Sets each component of the given 3-element
1552+
vector to its absolute value.
1553+
1554+
#### Parameters
1555+
1556+
* `a` (Type: Array.&lt;Number>)<br>
1557+
A 3-element vector.
1558+
1559+
#### Return Value
1560+
1561+
The vector "a". (Type: Array.&lt;Number>)
1562+
14951563
### (static) H3DU.Math.vec3add(a, b) <a id='H3DU.Math.vec3add'></a>
14961564

14971565
Adds two 3-element vectors and returns a new
@@ -1564,11 +1632,7 @@ The following are properties of the cross product:<ul>
15641632
vector with the same length but opposite direction.
15651633
<li>If the cross product's length is 0, then A and B are parallel vectors.
15661634
<li>Let there be a triangle formed by point A, point B, and the point (0,0,0) in that order.
1567-
<<<<<<< HEAD
15681635
The cross product vector will be such that, while the vector points toward the viewer,
1569-
=======
1570-
The cross product vector will be such that, whenever the vector points toward the viewer,
1571-
>>>>>>> 46648dcbdc510ab994e4eb259cdce8eae656c30c
15721636
the triangle's vertices are oriented counterclockwise for right-handed coordinate systems,
15731637
or clockwise for left-handed systems. The triangle's area is half of the cross product's length.
15741638
<li>If A and B are unit vectors (<a href="H3DU.Math.md#H3DU.Math.vec3norm">"normalized" vectors</a> with a length of 1), the absolute value
@@ -1987,6 +2051,34 @@ two vectors that lie on a plane with the given normal.
19872051
// and in a clockwise direction for left-handed systems.
19882052
angle*=(triple<0 ? -1.0 : 1.0);
19892053

2054+
### (static) H3DU.Math.vec4abs(a) <a id='H3DU.Math.vec4abs'></a>
2055+
2056+
Returns a new 4-element
2057+
vector with the absolute value of each of its components.
2058+
2059+
#### Parameters
2060+
2061+
* `a` (Type: Array.&lt;Number>)<br>
2062+
A 4-element vector.
2063+
2064+
#### Return Value
2065+
2066+
The resulting 4-element vector. (Type: Array.&lt;Number>)
2067+
2068+
### (static) H3DU.Math.vec4absInPlace(a) <a id='H3DU.Math.vec4absInPlace'></a>
2069+
2070+
Sets each component of the given 4-element
2071+
vector to its absolute value.
2072+
2073+
#### Parameters
2074+
2075+
* `a` (Type: Array.&lt;Number>)<br>
2076+
A 4-element vector.
2077+
2078+
#### Return Value
2079+
2080+
The vector "a". (Type: Array.&lt;Number>)
2081+
19902082
### (static) H3DU.Math.vec4assign(dst, src) <a id='H3DU.Math.vec4assign'></a>
19912083

19922084
Assigns the values of a 4-element vector into another
@@ -2091,6 +2183,21 @@ is the same as reversing the sign of each of its components.
20912183

20922184
The resulting 4-element vector. (Type: Array.&lt;Number>)
20932185

2186+
### (static) H3DU.Math.vec4negateInPlace(a) <a id='H3DU.Math.vec4negateInPlace'></a>
2187+
2188+
Negates a 4-element vector in place.
2189+
Negating a vector
2190+
is the same as reversing the sign of each of its components.
2191+
2192+
#### Parameters
2193+
2194+
* `a` (Type: Array.&lt;Number>)<br>
2195+
A 4-element vector.
2196+
2197+
#### Return Value
2198+
2199+
The parameter "a". (Type: Array.&lt;Number>)
2200+
20942201
### (static) H3DU.Math.vec4norm(vec) <a id='H3DU.Math.vec4norm'></a>
20952202

20962203
Returns a normalized version of a 4-element vector.

doc/tutorial-meshexamples.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ This method creates a ring or disk striped in two colors.
8989
function stripedDisk(inner,outer,color1,color2,sections,sectionCount){
9090
if(sectionCount==null)sectionCount=4
9191
var firstColor=true
92-
var ret=new Mesh()
92+
var ret=new H3DU.Mesh()
9393
var sweep=360.0/sections;
9494
for(var i=0;i<sections;i++){
9595
var angle=360.0*(i*1.0/sections);
@@ -152,4 +152,17 @@ This method creates a washer-shaped 3D model.
152152
return mesh
153153
}
154154

155+
function setBoxSizeAndBounds(shape,box){
156+
shape.setPosition(H3DU.Math.boxCenter(box))
157+
shape.setScale(H3DU.Math.boxDimensions(box))
158+
return shape
159+
}
160+
function wireBox(box, color){
161+
if(!color)color="gray";
162+
var boxMesh=H3DU.Meshes.createBox(1,1,1).toWireFrame()
163+
var shape=new H3DU.Shape(boxMesh).setColor(color)
164+
.setMaterialParams({"basic":true})
165+
return setBoxSizeAndBounds(shape,box)
166+
}
167+
155168
[Back to documentation index.](index.md)

h3du-batch3d.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ H3DU.Batch3D.prototype._renderShape = function(shape, renderContext) {
347347
for(var i = 0;i < shape.shapes.length;i++) {
348348
this._renderShape(shape.shapes[i], renderContext);
349349
}
350-
} else if(shape.visible && !shape.isCulled(this._getFrustum())) {
350+
} else if(!shape.isCulled(this._getFrustum())) {
351351
var prog = null;
352352
var flags = 0;
353353
if(shape.material instanceof H3DU.Material &&

0 commit comments

Comments
 (0)