-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHeatmap3dPlaneBufferGeometry.js
90 lines (53 loc) · 2.16 KB
/
Heatmap3dPlaneBufferGeometry.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
// PlaneBufferGeometry
Heatmap3dPlaneBufferGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
Heatmap3dPlaneBufferGeometry.prototype.constructor = Heatmap3dPlaneBufferGeometry;
function Heatmap3dPlaneBufferGeometry( width, height, widthSegments, heightSegments, vals, skipZeros ) {
THREE.BufferGeometry.call( this );
var width_half = width / 2;
var height_half = height / 2;
var gridX = Math.floor( widthSegments ) || 1;
var gridY = Math.floor( heightSegments ) || 1;
var gridX1 = gridX + 1;
var gridY1 = gridY + 1;
var segment_width = width / gridX;
var segment_height = height / gridY;
var ix, iy;
// buffers
var indices = [];
var vertices = [];
var normals = [];
var uvs = [];
// generate vertices, normals and uvs
for ( iy = 0; iy < gridY1; iy ++ ) {
var y = iy * segment_height - height_half;
for ( ix = 0; ix < gridX1; ix ++ ) {
var x = ix * segment_width - width_half;
vertices.push( x, - y, vals[iy*gridY1 + ix ] );
normals.push( 0, 0, 1 );
uvs.push( ix / gridX );
uvs.push( 1 - ( iy / gridY ) );
}
}
// indices
for ( iy = 0; iy < gridY; iy ++ ) {
for ( ix = 0; ix < gridX; ix ++ ) {
var a = ix + gridX1 * iy;
var b = ix + gridX1 * ( iy + 1 );
var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
var d = ( ix + 1 ) + gridX1 * iy;
// faces
// If skipZeros is false we draw the face no matter what
// If skipZeros is true, we draw the face only if we have one vertex.z>0
if (!skipZeros || Math.max(vertices[a*3+2], vertices[b*3+2],vertices[d*3+2])>0)
indices.push( a, b, d );
if (!skipZeros || Math.max(vertices[b*3+2], vertices[c*3+2],vertices[d*3+2])>0)
indices.push( b, c, d );
}
}
// build geometry
// Note: setIndex has different code in AFRAME 0.5.0 than at current THREE github.
this.setIndex( new THREE.Uint32BufferAttribute( indices, 1) );
this.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
this.addAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
this.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
}