Skip to content

Commit eca7519

Browse files
committed
improve TexturedSprite
1 parent 1defed5 commit eca7519

File tree

7 files changed

+74
-33
lines changed

7 files changed

+74
-33
lines changed

packages/twopoint5d-demos/src/starfield/Starfield.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class Starfield {
5454
#minMaxSizeScale: [number, number] = [1, 1];
5555
#nearFar: Vector2 = new Vector2(0, 1);
5656
#cameraLineOfSightEscape = 2;
57-
#baseColors: [number, number, number, number][];
57+
#baseColors: Color[];
5858

5959
constructor(textureStore: TextureStore, stage: Stage2D, capacity: number, atlasName: string) {
6060
eventize(this);
@@ -84,13 +84,10 @@ export class Starfield {
8484
}
8585

8686
setBaseColors(colors: (Color | number | string)[]) {
87-
this.#baseColors = colors.map((color) => {
88-
const c = new Color(color);
89-
return [c.r, c.g, c.b, 1];
90-
});
87+
this.#baseColors = colors.map((color) => new Color(color));
9188
}
9289

93-
randomBaseColor(): [number, number, number, number] {
90+
randomBaseColor(): Color {
9491
return this.#baseColors[rand(this.#baseColors.length)];
9592
}
9693

packages/twopoint5d/src/sprites/TexturedSprites/TexturedSprite.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ export interface TexturedSprite extends VO {
2626
setQuadSize(quadSize: [width: number, height: number]): void;
2727
setTexCoords(texCoords: [s: number, t: number, u: number, v: number]): void;
2828
setInstancePosition(position: [x: number, y: number, z: number]): void;
29-
setColor(color: [r: number, g: number, b: number, b: number]): void;
29+
setColorValues(color: [r: number, g: number, b: number, b: number]): void;
3030
}
3131

3232
export class TexturedSprite {
3333
[voInitialize]() {
34-
this.setColor([1, 1, 1, 1]);
34+
this.setColorValues([1, 1, 1, 1]);
3535
}
3636

3737
setSize(width: number, height: number): void {
@@ -47,8 +47,8 @@ export class TexturedSprite {
4747
this.setTexCoords([coords.s, coords.t, coords.u, coords.v]);
4848
}
4949

50-
fromColor(color: Color, a = 1): void {
51-
this.setColor([color.r, color.g, color.b, a]);
50+
setColor(color: Color, a = 1): void {
51+
this.setColorValues([color.r, color.g, color.b, a]);
5252
}
5353

5454
getColor(target: Color = new Color()): Color {
@@ -65,7 +65,7 @@ export const TexturedSpriteDescriptor: VertexObjectDescription = {
6565
texCoords: {components: ['s', 't', 'u', 'v']},
6666
instancePosition: {components: ['x', 'y', 'z'], usage: 'dynamic'},
6767
rotation: {size: 1, usage: 'dynamic'},
68-
color: {components: ['r', 'g', 'b', 'a']},
68+
color: {components: ['r', 'g', 'b', 'a'], setter: 'setColorValues', getter: false},
6969
},
7070

7171
basePrototype: TexturedSprite.prototype,

packages/twopoint5d/src/vertex-objects/VertexAttributeDescriptor.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,32 @@ describe('VertexAttributeDescriptor', () => {
1717
expect(descriptor.normalizedData).toBe(false);
1818
expect(descriptor.usageType).toBe('dynamic');
1919
expect(descriptor.bufferName).toBe('dynamic_float32');
20+
expect(descriptor.getterName).toBe('getFoo');
21+
expect(descriptor.setterName).toBe('setFoo');
22+
});
23+
24+
test('construct with getter and setter', () => {
25+
const descriptor = new VertexAttributeDescriptor('foo', {
26+
size: 3,
27+
getter: 'getCoords',
28+
setter: 'setCoords',
29+
});
30+
expect(descriptor).toBeDefined();
31+
expect(descriptor.name).toBe('foo');
32+
expect(descriptor.getterName).toBe('getCoords');
33+
expect(descriptor.setterName).toBe('setCoords');
34+
});
35+
36+
test('construct without getter and setter', () => {
37+
const descriptor = new VertexAttributeDescriptor('foo', {
38+
components: ['x', 'y', 'z'],
39+
getter: undefined,
40+
setter: false,
41+
});
42+
expect(descriptor).toBeDefined();
43+
expect(descriptor.name).toBe('foo');
44+
expect(descriptor.getterName).toBe(undefined);
45+
expect(descriptor.setterName).toBe(undefined);
2046
});
2147

2248
test('construct with size', () => {

packages/twopoint5d/src/vertex-objects/VertexAttributeDescriptor.ts

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type {VertexAttributeDataType, VertexAttributeDescription, VertexAttributeUsageType} from './types.js';
22

3+
const toPascalCase = (str: string) => str.replace(/(^|_)([a-z])/g, (_match: string, _m0: string, m1: string) => m1.toUpperCase());
4+
35
export class VertexAttributeDescriptor {
46
private readonly description: VertexAttributeDescription;
57

@@ -44,4 +46,20 @@ export class VertexAttributeDescriptor {
4446
get bufferName(): string {
4547
return this.description.bufferName ?? `${this.usageType}_${this.dataType}${this.normalizedData ? 'N' : ''}`;
4648
}
49+
50+
get getterName(): string | undefined {
51+
if ('getter' in this.description && !this.description.getter) {
52+
return undefined;
53+
}
54+
if (typeof this.description.getter === 'string') return this.description.getter;
55+
return `get${toPascalCase(this.name)}`;
56+
}
57+
58+
get setterName(): string | undefined {
59+
if ('setter' in this.description && !this.description.setter) {
60+
return undefined;
61+
}
62+
if (typeof this.description.setter === 'string') return this.description.setter;
63+
return `set${toPascalCase(this.name)}`;
64+
}
4765
}

packages/twopoint5d/src/vertex-objects/VertexObjectPool.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class VertexObjectPool<VOType> {
1818
#index: Array<VOType & VO>;
1919
#usedCount = 0;
2020

21-
onCreateVO?: (vo: VOType & VO) => void;
21+
onCreateVO?: (vo: VOType & VO) => (VOType & VO) | void;
2222

2323
constructor(descriptor: VertexObjectDescriptor | VertexObjectDescription, capacityOrData: number | VertexObjectBuffersData) {
2424
this.descriptor = descriptor instanceof VertexObjectDescriptor ? descriptor : new VertexObjectDescriptor(descriptor);
@@ -119,7 +119,7 @@ export class VertexObjectPool<VOType> {
119119
#createVertexObject(idx: number) {
120120
const vo = createVertexObject(this.descriptor, this.buffer, idx);
121121
if (this.onCreateVO != null) {
122-
this.onCreateVO(vo);
122+
return this.onCreateVO(vo) ?? vo;
123123
}
124124
return vo;
125125
}

packages/twopoint5d/src/vertex-objects/createVertexObjectPrototype.ts

+16-19
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import {voBuffer, voIndex} from './constants.js';
33
import {createTypedArray} from './createTypedArray.js';
44
import type {VO} from './types.js';
55

6-
const toPascalCase = (str: string) => str.replace(/(^|_)([a-z])/g, (_match: string, _m0: string, m1: string) => m1.toUpperCase());
7-
86
const makeAttributeGetter = (bufferName: string, instanceOffset: number, attrOffset: number) => {
97
return function getAttribute(this: VO) {
108
const idx = this[voIndex] * instanceOffset + attrOffset;
@@ -73,9 +71,9 @@ export function createVertexObjectPrototype(voBuffer: VertexObjectBuffer): Objec
7371
const attr = descriptor.getAttribute(attrName)!;
7472
const bufAttr = voBuffer.bufferAttributes.get(attrName)!;
7573
const buf = voBuffer.buffers.get(bufAttr.bufferName)!;
76-
const AttrName = toPascalCase(attrName);
7774

7875
const methods: unknown[] = [];
76+
7977
if (descriptor.vertexCount === 1 && attr.size === 1) {
8078
methods.push([
8179
attrName,
@@ -86,23 +84,22 @@ export function createVertexObjectPrototype(voBuffer: VertexObjectBuffer): Objec
8684
},
8785
]);
8886
} else {
89-
methods.push(
90-
[
91-
`get${AttrName}`,
92-
{
93-
enumerable: true,
94-
value: makeAttributeValuesGetter(bufAttr.bufferName, buf.itemSize, descriptor.vertexCount, bufAttr.offset, attr.size),
95-
},
96-
],
97-
[
98-
`set${AttrName}`,
99-
{
100-
enumerable: true,
101-
value: makeAttributeValueSetter(bufAttr.bufferName, buf.itemSize, descriptor.vertexCount, bufAttr.offset, attr.size),
102-
},
103-
],
104-
);
87+
methods.push([
88+
attr.getterName,
89+
{
90+
enumerable: true,
91+
value: makeAttributeValuesGetter(bufAttr.bufferName, buf.itemSize, descriptor.vertexCount, bufAttr.offset, attr.size),
92+
},
93+
]);
94+
methods.push([
95+
attr.setterName,
96+
{
97+
enumerable: true,
98+
value: makeAttributeValueSetter(bufAttr.bufferName, buf.itemSize, descriptor.vertexCount, bufAttr.offset, attr.size),
99+
},
100+
]);
105101
}
102+
106103
if (attr.hasComponents) {
107104
attr.components.forEach((component, componentIndex) => {
108105
for (let vertexIndex = 0; vertexIndex < descriptor.vertexCount; vertexIndex++) {

packages/twopoint5d/src/vertex-objects/types.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ export interface VASizeDescription extends VADescription {
4545
size: number;
4646
}
4747

48-
export type VertexAttributeDescription = VAComponentsDescription | VASizeDescription;
48+
export type VertexAttributeDescription = (VAComponentsDescription | VASizeDescription) & {
49+
getter?: string | boolean;
50+
setter?: string | boolean;
51+
};
4952

5053
export type VertexAttributesType = Record<string, VertexAttributeDescription>;
5154

0 commit comments

Comments
 (0)