-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIsometricPlaneGeometry.js
162 lines (135 loc) · 4.49 KB
/
IsometricPlaneGeometry.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
module.exports = function(THREE) {
const {
Geometry,
BufferGeometry,
Float32BufferAttribute
} = THREE;
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
// IsometricPlaneGeometry
function IsometricPlaneGeometry(width, height, widthSegments, heightSegments) {
Geometry.call(this);
this.type = 'IsometricPlaneGeometry';
this.parameters = {
width: width,
height: height,
widthSegments: widthSegments,
heightSegments: heightSegments
};
this.fromBufferGeometry(new IsometricPlaneBufferGeometry(width, height, widthSegments, heightSegments));
this.mergeVertices();
}
IsometricPlaneGeometry.prototype = Object.create(Geometry.prototype);
IsometricPlaneGeometry.prototype.constructor = IsometricPlaneGeometry;
// IsometricPlaneBufferGeometry
function IsometricPlaneBufferGeometry(width, height, widthSegments, heightSegments) {
BufferGeometry.call(this);
this.type = 'IsometricPlaneBufferGeometry';
this.parameters = {
width: width,
height: height,
widthSegments: widthSegments,
heightSegments: heightSegments
};
let width_half = width / 2;
let height_half = height / 2;
// buffers
let indices = [];
let vertices = [];
let normals = [];
let uvs = [];
// generate vertices, normals and uvs
let prevDict = {};
let currDict = {};
let index = 0;
let x, y, x1, x2, x3, x4, t, m, b;
for (x = 0; x <= widthSegments; x++) {
x1 = clamp((x - 0.5) / widthSegments, 0, 1);
x2 = clamp((x + 0.0) / widthSegments, 0, 1);
x3 = clamp((x + 0.5) / widthSegments, 0, 1);
x4 = clamp((x + 1.0) / widthSegments, 0, 1);
for (y = 0; y < heightSegments; y += 2) {
t = (y + 0) / heightSegments;
m = (y + 1) / heightSegments;
b = (y + 2) / heightSegments;
let data = [];
data.push({
vertex: [x1 * width - width_half, t * height - height_half],
uv: [x1, t]
});
data.push({
vertex: [x3 * width - width_half, t * height - height_half],
uv: [x3, t]
});
data.push({
vertex: [x2 * width - width_half, m * height - height_half],
uv: [x2, m]
});
if (x < widthSegments) {
data.push({
vertex: [x2 * width - width_half, m * height - height_half],
uv: [x2, m]
});
data.push({
vertex: [x3 * width - width_half, t * height - height_half],
uv: [x3, t]
});
data.push({
vertex: [x4 * width - width_half, m * height - height_half],
uv: [x4, m]
});
}
data.push({
vertex: [x1 * width - width_half, b * height - height_half],
uv: [x1, b]
});
data.push({
vertex: [x2 * width - width_half, m * height - height_half],
uv: [x2, m]
});
data.push({
vertex: [x3 * width - width_half, b * height - height_half],
uv: [x3, b]
});
if (x < widthSegments) {
data.push({
vertex: [x2 * width - width_half, m * height - height_half],
uv: [x2, m]
});
data.push({
vertex: [x4 * width - width_half, m * height - height_half],
uv: [x4, m]
});
data.push({
vertex: [x3 * width - width_half, b * height - height_half],
uv: [x3, b]
});
}
for (let i = 0; i < data.length; i++) {
if (!(data[i].vertex in currDict)) {
if (!(data[i].vertex in prevDict)) {
currDict[data[i].vertex] = index++;
vertices.push(...data[i].vertex, 0);
uvs.push(...data[i].uv);
normals.push(0, 0, 1);
} else {
currDict[data[i].vertex] = prevDict[data[i].vertex];
}
}
indices.push(currDict[data[i].vertex]);
}
}
prevDict = currDict;
currDict = {};
}
this.setIndex(indices);
this.addAttribute('position', new Float32BufferAttribute(vertices, 3));
this.addAttribute('normal', new Float32BufferAttribute(normals, 3));
this.addAttribute('uv', new Float32BufferAttribute(uvs, 2));
}
IsometricPlaneBufferGeometry.prototype = Object.create(BufferGeometry.prototype);
IsometricPlaneBufferGeometry.prototype.constructor = IsometricPlaneBufferGeometry;
return {
IsometricPlaneGeometry,
IsometricPlaneBufferGeometry
};
};