| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 |
1×
1×
35×
35×
35×
35×
35×
35×
210×
210×
35×
105×
35×
35×
35×
35×
210×
210×
630×
630×
630×
630×
630×
7616×
7616×
7616×
210×
210×
1644×
210×
210×
35×
35×
35×
210×
210×
210×
35×
35×
35×
35×
35×
35×
35×
35×
35×
35×
35×
35×
35×
35×
35×
35×
35×
210×
210×
| import Geometry from '../Geometry';
import Plane from './Plane';
import Matrix4 from '../math/Matrix4';
import Vector3 from '../math/Vector3';
import BoundingBox from '../math/BoundingBox';
import vendor from '../core/vendor';
var planeMatrix = new Matrix4();
/**
* @constructor clay.geometry.Cube
* @extends clay.Geometry
* @param {Object} [opt]
* @param {number} [opt.widthSegments]
* @param {number} [opt.heightSegments]
* @param {number} [opt.depthSegments]
* @param {boolean} [opt.inside]
*/
var Cube = Geometry.extend(
/**@lends clay.geometry.Cube# */
{
dynamic: false,
/**
* @type {number}
*/
widthSegments: 1,
/**
* @type {number}
*/
heightSegments: 1,
/**
* @type {number}
*/
depthSegments: 1,
/**
* @type {boolean}
*/
inside: false
}, function() {
this.build();
},
/** @lends clay.geometry.Cube.prototype */
{
/**
* Build cube geometry
*/
build: function() {
var planes = {
'px': createPlane('px', this.depthSegments, this.heightSegments),
'nx': createPlane('nx', this.depthSegments, this.heightSegments),
'py': createPlane('py', this.widthSegments, this.depthSegments),
'ny': createPlane('ny', this.widthSegments, this.depthSegments),
'pz': createPlane('pz', this.widthSegments, this.heightSegments),
'nz': createPlane('nz', this.widthSegments, this.heightSegments),
};
var attrList = ['position', 'texcoord0', 'normal'];
var vertexNumber = 0;
var faceNumber = 0;
for (var pos in planes) {
vertexNumber += planes[pos].vertexCount;
faceNumber += planes[pos].indices.length;
}
for (var k = 0; k < attrList.length; k++) {
this.attributes[attrList[k]].init(vertexNumber);
}
this.indices = new vendor.Uint16Array(faceNumber);
var faceOffset = 0;
var vertexOffset = 0;
for (var pos in planes) {
var plane = planes[pos];
for (var k = 0; k < attrList.length; k++) {
var attrName = attrList[k];
var attrArray = plane.attributes[attrName].value;
var attrSize = plane.attributes[attrName].size;
var isNormal = attrName === 'normal';
for (var i = 0; i < attrArray.length; i++) {
var value = attrArray[i];
Iif (this.inside && isNormal) {
value = -value;
}
this.attributes[attrName].value[i + attrSize * vertexOffset] = value;
}
}
var len = plane.indices.length;
for (var i = 0; i < plane.indices.length; i++) {
this.indices[i + faceOffset] = vertexOffset + plane.indices[this.inside ? (len - i - 1) : i];
}
faceOffset += plane.indices.length;
vertexOffset += plane.vertexCount;
}
this.boundingBox = new BoundingBox();
this.boundingBox.max.set(1, 1, 1);
this.boundingBox.min.set(-1, -1, -1);
}
});
function createPlane(pos, widthSegments, heightSegments) {
planeMatrix.identity();
var plane = new Plane({
widthSegments: widthSegments,
heightSegments: heightSegments
});
switch(pos) {
case 'px':
Matrix4.translate(planeMatrix, planeMatrix, Vector3.POSITIVE_X);
Matrix4.rotateY(planeMatrix, planeMatrix, Math.PI / 2);
break;
case 'nx':
Matrix4.translate(planeMatrix, planeMatrix, Vector3.NEGATIVE_X);
Matrix4.rotateY(planeMatrix, planeMatrix, -Math.PI / 2);
break;
case 'py':
Matrix4.translate(planeMatrix, planeMatrix, Vector3.POSITIVE_Y);
Matrix4.rotateX(planeMatrix, planeMatrix, -Math.PI / 2);
break;
case 'ny':
Matrix4.translate(planeMatrix, planeMatrix, Vector3.NEGATIVE_Y);
Matrix4.rotateX(planeMatrix, planeMatrix, Math.PI / 2);
break;
case 'pz':
Matrix4.translate(planeMatrix, planeMatrix, Vector3.POSITIVE_Z);
break;
case 'nz':
Matrix4.translate(planeMatrix, planeMatrix, Vector3.NEGATIVE_Z);
Matrix4.rotateY(planeMatrix, planeMatrix, Math.PI);
break;
}
plane.applyTransform(planeMatrix);
return plane;
}
export default Cube;
|