-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstancedGeometries.ts
More file actions
131 lines (119 loc) · 5.12 KB
/
Copy pathinstancedGeometries.ts
File metadata and controls
131 lines (119 loc) · 5.12 KB
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
import * as THREE from "three";
// based on https://threejs.org/examples/#webgl_buffergeometry_instancing_lambert
declare module "three" {
export class InstancedBufferAttribute extends THREE.BufferAttribute {
// r96 added "normalized" parameter
constructor(data: ArrayLike<number>, itemSize: number, normalized: boolean, meshPerAttribute?: number);
meshPerAttribute: number;
}
}
export class InstancedSimpleGeometry extends THREE.InstancedBufferGeometry {
readonly blueprint: THREE.BufferGeometry;
private readonly instanceIds: Float32Array;
private readonly instancePositions: Float32Array;
private readonly instanceQuaternions: Float32Array;
private readonly instanceScales: Float32Array;
private readonly instanceColors: Float32Array;
private readonly instanceIdToIndex = new Map<number, number>();
constructor(blueprint: THREE.BufferGeometry,
count: number,
instancePositions: Float32Array,
instanceQuaternions: Float32Array,
instanceScales: Float32Array,
instanceColors: Float32Array) {
super();
this.blueprint = blueprint;
this.index = blueprint.index;
for (let name in blueprint.attributes) {
if (blueprint.attributes.hasOwnProperty(name)) {
this.addAttribute(name, blueprint.attributes[name]);
}
}
this.instanceIds = new Float32Array(count);
for (let index = 0; index < count; index++) {
const id = (new THREE.Object3D()).id; // to get unique bufferGeometryId for each of instance.
this.instanceIds[index] = id;
this.instanceIdToIndex.set(id, index);
}
this.instancePositions = instancePositions || new Float32Array(count * 3);
this.instanceQuaternions = instanceQuaternions || new Float32Array(count * 4);
this.instanceScales = instanceScales || new Float32Array(count * 3);
if (!instanceScales) {
this.instanceScales.fill(1, 0, this.instanceScales.length);
}
this.instanceColors = instanceColors || new Float32Array(count * 3);
if (!instanceColors) {
this.instanceColors.fill(1, 0, this.instanceColors.length);
}
this.addAttribute("instanceId", new THREE.InstancedBufferAttribute(new Float32Array(this.instanceIds), 1, true));
this.addAttribute("instancePosition", new THREE.InstancedBufferAttribute(new Float32Array(this.instancePositions), 3, true));
this.addAttribute("instanceQuaternion", new THREE.InstancedBufferAttribute(new Float32Array(this.instanceQuaternions), 4, true));
this.addAttribute("instanceScale", new THREE.InstancedBufferAttribute(new Float32Array(this.instanceScales), 3, true));
this.addAttribute("instanceColor", new THREE.InstancedBufferAttribute(new Float32Array(this.instanceColors), 3, true));
}
getInstancePosition(index: number): THREE.Vector3 {
return new THREE.Vector3(
this.instancePositions[index * 3 + 0],
this.instancePositions[index * 3 + 1],
this.instancePositions[index * 3 + 2]);
}
findIndexById(id: number): number {
return this.instanceIdToIndex.get(id);
}
}
function populateQuaternionArray<T>(itemList: T[], getQuaternion?: (item: T) => THREE.Quaternion): Float32Array {
if (getQuaternion) {
const array = new Float32Array(itemList.length * 4);
let i = 0;
for (let item of itemList) {
const vec = getQuaternion(item);
array[i++] = vec.x;
array[i++] = vec.y;
array[i++] = vec.z;
array[i++] = vec.w;
}
return array;
}
return null;
}
function populateVector3Array<T>(itemList: T[], getVector3?: (item: T) => THREE.Vector3): Float32Array {
if (getVector3) {
const array = new Float32Array(itemList.length * 3);
let i = 0;
for (let item of itemList) {
const vec = getVector3(item);
array[i++] = vec.x;
array[i++] = vec.y;
array[i++] = vec.z;
}
return array;
}
return null;
}
export class InstancedMappedGeometry<T> extends InstancedSimpleGeometry {
private readonly indexToSource: T[];
constructor(blueprint: THREE.BufferGeometry,
sourceList: T[],
getPosition?: (item: T) => THREE.Vector3,
getQuaternion?: (item: T) => THREE.Quaternion,
getScale?: (item: T) => THREE.Vector3,
getColor?: (item: T) => THREE.Vector3) {
super(
blueprint,
sourceList.length,
populateVector3Array(sourceList, getPosition),
populateQuaternionArray(sourceList, getQuaternion),
populateVector3Array(sourceList, getScale),
populateVector3Array(sourceList, getColor));
this.indexToSource = sourceList.slice(0);
}
findSourceByindex(index: number): T {
return this.indexToSource[index];
}
findSourceById(id: number): T {
const index = this.findIndexById(id);
if (index === undefined || index === null)
return null;
return this.indexToSource[index];
}
}