Skip to content

Commit fb11acb

Browse files
authored
Make BufferGeometry only contain normal buffer attributes by default (#432)
* Default BufferGeometry to use normal attributes * Named types * Fix * Stricter base * Update OTHER_FILES
1 parent e9fd123 commit fb11acb

File tree

7 files changed

+184
-60
lines changed

7 files changed

+184
-60
lines changed

types/three/OTHER_FILES.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ examples/jsm/helpers/VertexNormalsHelper.d.ts
3636
examples/jsm/helpers/VertexTangentsHelper.d.ts
3737
examples/jsm/interactive/HTMLMesh.d.ts
3838
examples/jsm/interactive/InteractiveGroup.d.ts
39-
examples/jsm/libs/stats.module.d.ts
4039
examples/jsm/lights/LightProbeGenerator.d.ts
4140
examples/jsm/lines/Wireframe.d.ts
4241
examples/jsm/lines/WireframeGeometry2.d.ts
@@ -170,6 +169,7 @@ examples/jsm/shaders/VerticalTiltShiftShader.d.ts
170169
examples/jsm/shaders/VignetteShader.d.ts
171170
examples/jsm/shaders/VolumeShader.d.ts
172171
examples/jsm/shaders/WaterRefractionShader.d.ts
172+
examples/jsm/utils/BufferGeometryUtils.d.ts
173173
examples/jsm/utils/GeometryCompressionUtils.d.ts
174174
examples/jsm/utils/PackedPhongMaterial.d.ts
175175
examples/jsm/utils/SceneUtils.d.ts

types/three/src/constants.d.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -901,26 +901,3 @@ export type PixelFormatGPU =
901901
| 'DEPTH_COMPONENT32F'
902902
| 'DEPTH24_STENCIL8'
903903
| 'DEPTH32F_STENCIL8';
904-
905-
///////////////////////////////////////////////////////////////////////////////
906-
907-
export type BuiltinShaderAttributeName =
908-
| 'position'
909-
| 'normal'
910-
| 'uv'
911-
| 'color'
912-
| 'skinIndex'
913-
| 'skinWeight'
914-
| 'instanceMatrix'
915-
| 'morphTarget0'
916-
| 'morphTarget1'
917-
| 'morphTarget2'
918-
| 'morphTarget3'
919-
| 'morphTarget4'
920-
| 'morphTarget5'
921-
| 'morphTarget6'
922-
| 'morphTarget7'
923-
| 'morphNormal0'
924-
| 'morphNormal1'
925-
| 'morphNormal2'
926-
| 'morphNormal3';

types/three/src/core/BufferGeometry.d.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { BufferAttribute } from './BufferAttribute';
22
import { InterleavedBufferAttribute } from './InterleavedBufferAttribute';
3-
import { GLBufferAttribute } from './GLBufferAttribute';
4-
import { Box3 } from './../math/Box3';
5-
import { Sphere } from './../math/Sphere';
6-
import { Matrix4 } from './../math/Matrix4';
7-
import { Quaternion } from './../math/Quaternion';
8-
import { Vector2 } from './../math/Vector2';
9-
import { Vector3 } from './../math/Vector3';
3+
import { Box3 } from '../math/Box3';
4+
import { Sphere } from '../math/Sphere';
5+
import { Matrix4 } from '../math/Matrix4';
6+
import { Quaternion } from '../math/Quaternion';
7+
import { Vector2 } from '../math/Vector2';
8+
import { Vector3 } from '../math/Vector3';
109
import { EventDispatcher } from './EventDispatcher';
11-
import { BuiltinShaderAttributeName } from '../constants';
12-
import * as BufferGeometryUtils from '../../examples/jsm/utils/BufferGeometryUtils';
10+
import { GLBufferAttribute } from './GLBufferAttribute';
11+
12+
export type NormalBufferAttributes = Record<string, BufferAttribute | InterleavedBufferAttribute>;
13+
export type NormalOrGLBufferAttributes = Record<
14+
string,
15+
BufferAttribute | InterleavedBufferAttribute | GLBufferAttribute
16+
>;
1317

1418
/**
1519
* A representation of mesh, line, or point geometry
@@ -43,7 +47,9 @@ import * as BufferGeometryUtils from '../../examples/jsm/utils/BufferGeometryUti
4347
* @see {@link https://threejs.org/docs/index.html#api/en/core/BufferGeometry | Official Documentation}
4448
* @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/BufferGeometry.js | Source}
4549
*/
46-
export class BufferGeometry extends EventDispatcher {
50+
export class BufferGeometry<
51+
Attributes extends NormalOrGLBufferAttributes = NormalBufferAttributes,
52+
> extends EventDispatcher {
4753
/**
4854
* This creates a new {@link THREE.BufferGeometry | BufferGeometry} object.
4955
*/
@@ -87,9 +93,7 @@ export class BufferGeometry extends EventDispatcher {
8793
* use {@link setAttribute | .setAttribute} and {@link getAttribute | .getAttribute} to access attributes of this geometry.
8894
* @defaultValue `{}`
8995
*/
90-
attributes: {
91-
[name: string]: BufferAttribute | InterleavedBufferAttribute | GLBufferAttribute; // TODO Replace for 'Record<>'
92-
};
96+
attributes: Attributes;
9397

9498
/**
9599
* Hashmap of {@link THREE.BufferAttribute | BufferAttributes} holding details of the geometry's morph targets.
@@ -185,30 +189,25 @@ export class BufferGeometry extends EventDispatcher {
185189
* @param name
186190
* @param attribute
187191
*/
188-
setAttribute(
189-
name: BuiltinShaderAttributeName | (string & {}),
190-
attribute: BufferAttribute | InterleavedBufferAttribute | GLBufferAttribute,
191-
): this;
192+
setAttribute<K extends keyof Attributes>(name: K, attribute: Attributes[K]): this;
192193

193194
/**
194195
* Returns the {@link attributes | attribute} with the specified name.
195196
* @param name
196197
*/
197-
getAttribute(
198-
name: BuiltinShaderAttributeName | (string & {}),
199-
): BufferAttribute | InterleavedBufferAttribute | GLBufferAttribute;
198+
getAttribute<K extends keyof Attributes>(name: K): Attributes[K];
200199

201200
/**
202201
* Deletes the {@link attributes | attribute} with the specified name.
203202
* @param name
204203
*/
205-
deleteAttribute(name: BuiltinShaderAttributeName | (string & {})): BufferGeometry;
204+
deleteAttribute(name: keyof Attributes): this;
206205

207206
/**
208207
* Returns true if the {@link attributes | attribute} with the specified name exists.
209208
* @param name
210209
*/
211-
hasAttribute(name: BuiltinShaderAttributeName | (string & {})): boolean;
210+
hasAttribute(name: keyof Attributes): boolean;
212211

213212
/**
214213
* Adds a group to this geometry

types/three/src/objects/Points.d.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Material } from './../materials/Material';
2-
import { Raycaster } from './../core/Raycaster';
3-
import { Object3D } from './../core/Object3D';
4-
import { BufferGeometry } from '../core/BufferGeometry';
5-
import { Intersection } from '../core/Raycaster';
1+
import { Material } from '../materials/Material';
2+
import { Object3D } from '../core/Object3D';
3+
import { BufferGeometry, NormalOrGLBufferAttributes } from '../core/BufferGeometry';
4+
import { BufferAttribute } from '../core/BufferAttribute';
5+
import { InterleavedBufferAttribute } from '../core/InterleavedBufferAttribute';
6+
import { GLBufferAttribute } from '../core/GLBufferAttribute';
67

78
/**
89
* A class for displaying {@link Points}
@@ -12,7 +13,7 @@ import { Intersection } from '../core/Raycaster';
1213
* @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Points.js | Source}
1314
*/
1415
export class Points<
15-
TGeometry extends BufferGeometry = BufferGeometry,
16+
TGeometry extends BufferGeometry<NormalOrGLBufferAttributes> = BufferGeometry,
1617
TMaterial extends Material | Material[] = Material | Material[],
1718
> extends Object3D {
1819
/**

types/three/test/integration/three-examples/misc_controls_pointerlock.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,14 @@ function init() {
126126

127127
let position = floorGeometry.attributes.position;
128128

129-
if (!(position instanceof THREE.GLBufferAttribute)) {
130-
for (let i = 0, l = position.count; i < l; i++) {
131-
vertex.fromBufferAttribute(position, i);
129+
for (let i = 0, l = position.count; i < l; i++) {
130+
vertex.fromBufferAttribute(position, i);
132131

133-
vertex.x += Math.random() * 20 - 10;
134-
vertex.y += Math.random() * 2;
135-
vertex.z += Math.random() * 20 - 10;
132+
vertex.x += Math.random() * 20 - 10;
133+
vertex.y += Math.random() * 2;
134+
vertex.z += Math.random() * 20 - 10;
136135

137-
position.setXYZ(i, vertex.x, vertex.y, vertex.z);
138-
}
136+
position.setXYZ(i, vertex.x, vertex.y, vertex.z);
139137
}
140138

141139
const indexedGeometry = floorGeometry.toNonIndexed(); // ensure each face has unique vertices
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import * as THREE from 'three';
2+
3+
import * as Stats from 'three/examples/jsm/libs/stats.module.js';
4+
5+
let container: HTMLElement;
6+
let stats: Stats;
7+
8+
let camera: THREE.PerspectiveCamera;
9+
let scene: THREE.Scene;
10+
let renderer: THREE.WebGLRenderer;
11+
12+
let points: THREE.Points<THREE.BufferGeometry<THREE.NormalOrGLBufferAttributes>>;
13+
14+
const particles = 300000;
15+
let drawCount = 10000;
16+
17+
init();
18+
animate();
19+
20+
function init() {
21+
container = document.getElementById('container')!;
22+
23+
//
24+
25+
renderer = new THREE.WebGLRenderer({ antialias: false });
26+
renderer.setPixelRatio(window.devicePixelRatio);
27+
renderer.setSize(window.innerWidth, window.innerHeight);
28+
29+
container.appendChild(renderer.domElement);
30+
31+
//
32+
33+
camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 5, 3500);
34+
camera.position.z = 2750;
35+
36+
scene = new THREE.Scene();
37+
scene.background = new THREE.Color(0x050505);
38+
scene.fog = new THREE.Fog(0x050505, 2000, 3500);
39+
40+
//
41+
42+
const geometry = new THREE.BufferGeometry<THREE.NormalOrGLBufferAttributes>();
43+
44+
const positions = [];
45+
const positions2 = [];
46+
const colors = [];
47+
48+
const color = new THREE.Color();
49+
50+
const n = 1000;
51+
const n2 = n / 2; // particles spread in the cube
52+
53+
for (let i = 0; i < particles; i++) {
54+
// positions
55+
56+
const x = Math.random() * n - n2;
57+
const y = Math.random() * n - n2;
58+
const z = Math.random() * n - n2;
59+
60+
positions.push(x, y, z);
61+
positions2.push(z * 0.3, x * 0.3, y * 0.3);
62+
63+
// colors
64+
65+
const vx = x / n + 0.5;
66+
const vy = y / n + 0.5;
67+
const vz = z / n + 0.5;
68+
69+
color.setRGB(vx, vy, vz);
70+
71+
colors.push(color.r, color.g, color.b);
72+
}
73+
74+
const gl = renderer.getContext();
75+
76+
const pos = gl.createBuffer()!;
77+
gl.bindBuffer(gl.ARRAY_BUFFER, pos);
78+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
79+
80+
const pos2 = gl.createBuffer()!;
81+
gl.bindBuffer(gl.ARRAY_BUFFER, pos2);
82+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions2), gl.STATIC_DRAW);
83+
84+
const rgb = gl.createBuffer()!;
85+
gl.bindBuffer(gl.ARRAY_BUFFER, rgb);
86+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
87+
88+
const posAttr1 = new THREE.GLBufferAttribute(pos, gl.FLOAT, 3, 4, particles);
89+
const posAttr2 = new THREE.GLBufferAttribute(pos2, gl.FLOAT, 3, 4, particles);
90+
geometry.setAttribute('position', posAttr1);
91+
92+
setInterval(() => {
93+
const attr = geometry.getAttribute('position');
94+
95+
geometry.setAttribute('position', attr === posAttr1 ? posAttr2 : posAttr1);
96+
}, 2000);
97+
98+
geometry.setAttribute('color', new THREE.GLBufferAttribute(rgb, gl.FLOAT, 3, 4, particles));
99+
100+
//
101+
102+
const material = new THREE.PointsMaterial({ size: 15, vertexColors: true });
103+
104+
points = new THREE.Points(geometry, material);
105+
106+
// Choose one:
107+
// geometry.boundingSphere = ( new THREE.Sphere() ).set( new THREE.Vector3(), Infinity );
108+
points.frustumCulled = false;
109+
110+
scene.add(points);
111+
112+
//
113+
114+
stats = new Stats();
115+
container.appendChild(stats.dom);
116+
117+
//
118+
119+
window.addEventListener('resize', onWindowResize);
120+
}
121+
122+
function onWindowResize() {
123+
camera.aspect = window.innerWidth / window.innerHeight;
124+
camera.updateProjectionMatrix();
125+
126+
renderer.setSize(window.innerWidth, window.innerHeight);
127+
}
128+
129+
//
130+
131+
function animate() {
132+
requestAnimationFrame(animate);
133+
134+
render();
135+
stats.update();
136+
}
137+
138+
function render() {
139+
drawCount = (Math.max(5000, drawCount) + Math.floor(500 * Math.random())) % particles;
140+
points.geometry.setDrawRange(0, drawCount);
141+
142+
const time = Date.now() * 0.001;
143+
144+
points.rotation.x = time * 0.1;
145+
points.rotation.y = time * 0.2;
146+
147+
renderer.render(scene, camera);
148+
}

types/three/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"test/integration/three-examples/webgl2_multiple_rendertargets.ts",
3333
"test/integration/three-examples/webgl2_rendertarget_texture2darray.ts",
3434
"test/integration/three-examples/webgl2_volume_instancing.ts",
35+
"test/integration/three-examples/webgl_buffergeometry_glbufferattribute.ts",
3536
"test/integration/three-examples/webgl_buffergeometry_instancing_interleaved.ts",
3637
"test/integration/three-examples/webgl_camera.ts",
3738
"test/integration/three-examples/webgl_camera_array.ts",

0 commit comments

Comments
 (0)