Skip to content

Commit

Permalink
fix: support rotation when applying billboard effect (#1456)
Browse files Browse the repository at this point in the history
* fix: support rotation when applying billboard effect #1445

* chore: commit changeset

* fix: polyline should support 3D points #1457

* chore: commit changeset

* fix: support size attenuation #1445

* fix: calc dash correctly in 3D

* chore: commit changeset
  • Loading branch information
xiaoiver committed Aug 4, 2023
1 parent 6f0a82e commit 3d4f5da
Show file tree
Hide file tree
Showing 50 changed files with 833 additions and 394 deletions.
7 changes: 7 additions & 0 deletions .changeset/big-roses-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@antv/g-plugin-device-renderer': patch
'@antv/g-shader-components': patch
'@antv/g-lite': patch
---

Support size attenuation.
7 changes: 7 additions & 0 deletions .changeset/lemon-coins-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@antv/g-plugin-device-renderer': patch
'@antv/g-shader-components': patch
'@antv/g-lite': patch
---

Polyline should support 3D points.
7 changes: 7 additions & 0 deletions .changeset/stupid-avocados-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@antv/g-plugin-device-renderer': patch
'@antv/g-shader-components': patch
'@antv/g-lite': patch
---

Support rotation when applying billboard effect.
4 changes: 2 additions & 2 deletions packages/g-lite/src/css/properties/CSSPropertyPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export class CSSPropertyPoints
Partial<
CSSProperty<
{
points: [number, number][];
points: [number, number, number?][];
totalLength: number;
segments: [number, number][];
},
{
points: [number, number][];
points: [number, number, number?][];
totalLength: number;
segments: [number, number][];
}
Expand Down
4 changes: 4 additions & 0 deletions packages/g-lite/src/display-objects/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface ImageStyleProps extends BaseStyleProps {
width?: number | string;
height?: number | string;
isBillboard?: boolean;
billboardRotation?: number;
isSizeAttenuation?: boolean;
}
export interface ParsedImageStyleProps extends ParsedBaseStyleProps {
x: number;
Expand All @@ -23,6 +25,8 @@ export interface ParsedImageStyleProps extends ParsedBaseStyleProps {
width?: number;
height?: number;
isBillboard?: boolean;
billboardRotation?: number;
isSizeAttenuation?: boolean;
}
export class Image extends DisplayObject<
ImageStyleProps,
Expand Down
4 changes: 2 additions & 2 deletions packages/g-lite/src/display-objects/Polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Shape } from '../types';
import { DisplayObject, isDisplayObject } from './DisplayObject';

export interface PolygonStyleProps extends BaseStyleProps {
points: [number, number][];
points: [number, number, number?][];
/**
* marker will be positioned at the first point
*/
Expand All @@ -31,7 +31,7 @@ export interface PolygonStyleProps extends BaseStyleProps {
}
export interface ParsedPolygonStyleProps extends ParsedBaseStyleProps {
points: {
points: [number, number][];
points: [number, number, number?][];
segments: [number, number][];
totalLength: number;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/g-lite/src/display-objects/Polyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { DisplayObject } from './DisplayObject';
import { Polygon } from './Polygon';

export interface PolylineStyleProps extends BaseStyleProps {
points: [number, number][];
points: [number, number, number?][];
/**
* marker will be positioned at the first point
*/
Expand All @@ -34,7 +34,7 @@ export interface PolylineStyleProps extends BaseStyleProps {
}
export interface ParsedPolylineStyleProps extends ParsedBaseStyleProps {
points: {
points: [number, number][];
points: [number, number, number?][];
segments: [number, number][];
totalLength: number;
};
Expand Down
14 changes: 8 additions & 6 deletions packages/g-lite/src/display-objects/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ export interface TextStyleProps extends BaseStyleProps {
*/
isBillboard?: boolean;

/**
* The rotation of the billboard in radians.
*/
billboardRotation?: number;

/**
* Whether the size of the sprite is attenuated by the camera depth. (Perspective camera only.)
*/
sizeAttenuation?: boolean;
isSizeAttenuation?: boolean;

text: number | string;

Expand Down Expand Up @@ -194,7 +199,8 @@ export interface ParsedTextStyleProps extends ParsedBaseStyleProps {
y: number;
z?: number;
isBillboard?: boolean;
sizeAttenuation?: boolean;
billboardRotation?: number;
isSizeAttenuation?: boolean;
text: string;
textAlign?: 'start' | 'center' | 'middle' | 'end' | 'left' | 'right';
textBaseline?:
Expand Down Expand Up @@ -268,8 +274,6 @@ export class Text extends DisplayObject<TextStyleProps, ParsedTextStyleProps> {
leading: 0,
dx: '',
dy: '',
isBillboard: false,
sizeAttenuation: true,
...style,
}
: {
Expand All @@ -295,8 +299,6 @@ export class Text extends DisplayObject<TextStyleProps, ParsedTextStyleProps> {
leading: 0,
dx: 0,
dy: 0,
isBillboard: false,
sizeAttenuation: true,
},
...rest,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/g-lite/src/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ function ellipseToCommands(
}

function polygonToCommands(
points: [number, number][],
points: [number, number, number?][],
closed: boolean,
): AbsoluteArray {
const result = points.map((point, i) => {
Expand Down
80 changes: 68 additions & 12 deletions packages/g-plugin-device-renderer/src/drawcalls/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import {
import { Format, VertexBufferFrequency } from '../platform';
import frag from '../shader/image.frag';
import vert from '../shader/image.vert';
import { enumToObject } from '../utils';

enum ImageVertexAttributeBufferIndex {
PACKED_STYLE = VertexAttributeBufferIndex.POSITION + 1,
}

enum ImageVertexAttributeLocation {
PACKED_STYLE3 = VertexAttributeLocation.MAX,
}

export class ImageDrawcall extends Instanced {
shouldMerge(object: DisplayObject, index: number) {
Expand All @@ -29,6 +38,7 @@ export class ImageDrawcall extends Instanced {

this.material.defines = {
...this.material.defines,
...enumToObject(ImageVertexAttributeLocation),
};

this.material.vertexShader = vert;
Expand All @@ -45,27 +55,54 @@ export class ImageDrawcall extends Instanced {
super.createGeometry(objects);

const instanced: number[] = [];
const packedStyle: number[] = [];
objects.forEach((object, i) => {
const image = object as ImageShape;
const { width, height, z, isBillboard } = image.parsedStyle;
instanced.push(width, height, z, isBillboard ? 1 : 0);
const {
width,
height,
isBillboard,
billboardRotation,
isSizeAttenuation,
} = image.parsedStyle;
instanced.push(width, height);
packedStyle.push(
isBillboard ? 1 : 0,
billboardRotation ?? 0,
isSizeAttenuation ? 1 : 0,
0,
);
});

this.geometry.setIndexBuffer(new Uint32Array([0, 2, 1, 0, 3, 2]));
this.geometry.vertexCount = 6;
this.geometry.setVertexBuffer({
bufferIndex: VertexAttributeBufferIndex.POSITION,
byteStride: 4 * 4,
byteStride: 4 * 2,
frequency: VertexBufferFrequency.PerInstance,
attributes: [
{
format: Format.F32_RGBA,
format: Format.F32_RG,
bufferByteOffset: 4 * 0,
location: VertexAttributeLocation.POSITION,
},
],
data: new Float32Array(instanced),
});
this.geometry.setVertexBuffer({
bufferIndex: ImageVertexAttributeBufferIndex.PACKED_STYLE,
byteStride: 4 * 4,
frequency: VertexBufferFrequency.PerInstance,
attributes: [
{
format: Format.F32_RGBA,
bufferByteOffset: 4 * 0,
location: ImageVertexAttributeLocation.PACKED_STYLE3,
divisor: 1,
},
],
data: new Float32Array(packedStyle),
});
this.geometry.setVertexBuffer({
bufferIndex: VertexAttributeBufferIndex.UV,
byteStride: 4 * 2,
Expand All @@ -91,17 +128,12 @@ export class ImageDrawcall extends Instanced {

this.updateBatchedAttribute(objects, startIndex, name, value);

if (
name === 'width' ||
name === 'height' ||
name === 'z' ||
name === 'isBillboard'
) {
if (name === 'width' || name === 'height' || name === 'z') {
const packed: number[] = [];
objects.forEach((object) => {
const image = object as ImageShape;
const { width, height, z, isBillboard } = image.parsedStyle;
packed.push(width, height, z, isBillboard ? 1 : 0);
const { width, height } = image.parsedStyle;
packed.push(width, height);
});

this.geometry.updateVertexBuffer(
Expand All @@ -110,6 +142,30 @@ export class ImageDrawcall extends Instanced {
startIndex,
new Uint8Array(new Float32Array(packed).buffer),
);
} else if (
name === 'isBillboard' ||
name === 'billboardRotation' ||
name === 'isSizeAttenuation'
) {
const packed: number[] = [];
objects.forEach((object) => {
const image = object as ImageShape;
const { isBillboard, billboardRotation, isSizeAttenuation } =
image.parsedStyle;
packed.push(
isBillboard ? 1 : 0,
billboardRotation ?? 0,
isSizeAttenuation ? 1 : 0,
0,
);
});

this.geometry.updateVertexBuffer(
ImageVertexAttributeBufferIndex.PACKED_STYLE,
ImageVertexAttributeLocation.PACKED_STYLE3,
startIndex,
new Uint8Array(new Float32Array(packed).buffer),
);
} else if (name === 'img') {
const map = this.texturePool.getOrCreateTexture(
this.context.device,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ export class InstancedFillDrawcall extends Instanced {

this.geometry.setVertexBuffer({
bufferIndex: VertexAttributeBufferIndex.POSITION,
byteStride: 4 * 2,
byteStride: 4 * 3,
frequency: VertexBufferFrequency.PerVertex,
attributes: [
{
format: Format.F32_RG,
format: Format.F32_RGB,
bufferByteOffset: 4 * 0,
location: VertexAttributeLocation.POSITION,
},
Expand Down
Loading

0 comments on commit 3d4f5da

Please sign in to comment.