@luma.gl/engine 8.6.0-alpha.5 → 9.0.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/animation/key-frames.d.ts +1 -0
- package/dist/animation/key-frames.d.ts.map +1 -1
- package/dist/animation/key-frames.js.map +1 -1
- package/dist/animation/timeline.d.ts +2 -2
- package/dist/animation/timeline.d.ts.map +1 -1
- package/dist/animation/timeline.js.map +1 -1
- package/dist/geometries/cone-geometry.d.ts.map +1 -1
- package/dist/geometries/cone-geometry.js +1 -1
- package/dist/geometries/cone-geometry.js.map +1 -1
- package/dist/geometries/cube-geometry.d.ts +4 -0
- package/dist/geometries/cube-geometry.d.ts.map +1 -1
- package/dist/geometries/cube-geometry.js +31 -7
- package/dist/geometries/cube-geometry.js.map +1 -1
- package/dist/geometries/cylinder-geometry.d.ts.map +1 -1
- package/dist/geometries/cylinder-geometry.js +1 -1
- package/dist/geometries/cylinder-geometry.js.map +1 -1
- package/dist/geometries/ico-sphere-geometry.d.ts.map +1 -1
- package/dist/geometries/ico-sphere-geometry.js +1 -1
- package/dist/geometries/ico-sphere-geometry.js.map +1 -1
- package/dist/geometries/plane-geometry.d.ts.map +1 -1
- package/dist/geometries/plane-geometry.js +1 -1
- package/dist/geometries/plane-geometry.js.map +1 -1
- package/dist/geometries/sphere-geometry.d.ts.map +1 -1
- package/dist/geometries/sphere-geometry.js +1 -1
- package/dist/geometries/sphere-geometry.js.map +1 -1
- package/dist/geometries/truncated-cone-geometry.d.ts +15 -3
- package/dist/geometries/truncated-cone-geometry.d.ts.map +1 -1
- package/dist/geometries/truncated-cone-geometry.js +29 -15
- package/dist/geometries/truncated-cone-geometry.js.map +1 -1
- package/dist/geometry/geometry-table.d.ts +15 -0
- package/dist/geometry/geometry-table.d.ts.map +1 -0
- package/dist/geometry/geometry-table.js +2 -0
- package/dist/{transform/transform-types.js.map → geometry/geometry-table.js.map} +1 -1
- package/dist/geometry/geometry-utils.js.map +1 -1
- package/dist/geometry/geometry.d.ts +32 -14
- package/dist/geometry/geometry.d.ts.map +1 -1
- package/dist/geometry/geometry.js +57 -34
- package/dist/geometry/geometry.js.map +1 -1
- package/dist/geometry/primitive-utils.d.ts +1 -0
- package/dist/geometry/primitive-utils.d.ts.map +1 -0
- package/dist/geometry/primitive-utils.js +2 -0
- package/dist/geometry/primitive-utils.js.map +1 -0
- package/dist/index.d.ts +6 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -6
- package/dist/index.js.map +1 -1
- package/dist/lib/animation-loop.d.ts +25 -87
- package/dist/lib/animation-loop.d.ts.map +1 -1
- package/dist/lib/animation-loop.js +111 -197
- package/dist/lib/animation-loop.js.map +1 -1
- package/dist/lib/animation-props.d.ts +24 -0
- package/dist/lib/animation-props.d.ts.map +1 -0
- package/dist/lib/animation-props.js +2 -0
- package/dist/lib/animation-props.js.map +1 -0
- package/dist/lib/clip-space.d.ts +1 -0
- package/dist/lib/clip-space.d.ts.map +1 -0
- package/dist/lib/clip-space.js +2 -0
- package/dist/lib/clip-space.js.map +1 -0
- package/dist/lib/model-utils.d.ts +4 -3
- package/dist/lib/model-utils.d.ts.map +1 -1
- package/dist/lib/model-utils.js +24 -70
- package/dist/lib/model-utils.js.map +1 -1
- package/dist/lib/model.d.ts +34 -104
- package/dist/lib/model.d.ts.map +1 -1
- package/dist/lib/model.js +111 -502
- package/dist/lib/model.js.map +1 -1
- package/dist/lib/pipeline-factory.d.ts +60 -0
- package/dist/lib/pipeline-factory.d.ts.map +1 -0
- package/dist/lib/pipeline-factory.js +219 -0
- package/dist/lib/pipeline-factory.js.map +1 -0
- package/dist/lib/render-loop.d.ts +10 -18
- package/dist/lib/render-loop.d.ts.map +1 -1
- package/dist/lib/render-loop.js +32 -35
- package/dist/lib/render-loop.js.map +1 -1
- package/package.json +6 -6
- package/src/animation/key-frames.ts +1 -0
- package/src/animation/timeline.ts +2 -2
- package/src/geometries/cone-geometry.ts +1 -1
- package/src/geometries/cube-geometry.ts +160 -8
- package/src/geometries/cylinder-geometry.ts +1 -1
- package/src/geometries/ico-sphere-geometry.ts +2 -2
- package/src/geometries/plane-geometry.ts +1 -1
- package/src/geometries/sphere-geometry.ts +2 -2
- package/src/geometries/truncated-cone-geometry.ts +30 -12
- package/src/geometry/geometry-table.ts +16 -0
- package/src/geometry/geometry-utils.ts +1 -1
- package/src/geometry/geometry.ts +97 -47
- package/src/geometry/primitive-utils.ts +30 -0
- package/src/index.ts +13 -12
- package/src/lib/animation-loop.ts +141 -319
- package/src/lib/animation-props.ts +31 -0
- package/src/{utils → lib}/clip-space.ts +5 -2
- package/src/lib/model-utils.ts +60 -32
- package/src/lib/model.ts +127 -639
- package/src/lib/pipeline-factory.ts +234 -0
- package/src/lib/render-loop.ts +44 -40
- package/dist/lib/program-manager.d.ts +0 -39
- package/dist/lib/program-manager.d.ts.map +0 -1
- package/dist/lib/program-manager.js +0 -175
- package/dist/lib/program-manager.js.map +0 -1
- package/dist/transform/buffer-transform.d.ts +0 -36
- package/dist/transform/buffer-transform.d.ts.map +0 -1
- package/dist/transform/buffer-transform.js +0 -255
- package/dist/transform/buffer-transform.js.map +0 -1
- package/dist/transform/texture-transform.d.ts +0 -57
- package/dist/transform/texture-transform.d.ts.map +0 -1
- package/dist/transform/texture-transform.js +0 -412
- package/dist/transform/texture-transform.js.map +0 -1
- package/dist/transform/transform-shader-utils.d.ts +0 -26
- package/dist/transform/transform-shader-utils.d.ts.map +0 -1
- package/dist/transform/transform-shader-utils.js +0 -149
- package/dist/transform/transform-shader-utils.js.map +0 -1
- package/dist/transform/transform-types.d.ts +0 -44
- package/dist/transform/transform-types.d.ts.map +0 -1
- package/dist/transform/transform-types.js +0 -2
- package/dist/transform/transform.d.ts +0 -47
- package/dist/transform/transform.d.ts.map +0 -1
- package/dist/transform/transform.js +0 -198
- package/dist/transform/transform.js.map +0 -1
- package/dist/utils/clip-space.d.ts +0 -5
- package/dist/utils/clip-space.d.ts.map +0 -1
- package/dist/utils/clip-space.js +0 -34
- package/dist/utils/clip-space.js.map +0 -1
- package/src/lib/program-manager.ts +0 -187
- package/src/transform/buffer-transform.ts +0 -214
- package/src/transform/texture-transform.ts +0 -344
- package/src/transform/transform-shader-utils.ts +0 -169
- package/src/transform/transform-types.ts +0 -42
- package/src/transform/transform.ts +0 -189
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import {assert} from '@luma.gl/api';
|
|
2
|
-
import {Buffer, TransformFeedback, isWebGL2} from '@luma.gl/webgl';
|
|
3
|
-
import type {TransformProps, TransformDrawOptions, TransformRunOptions, TransformBinding} from './transform-types';
|
|
4
|
-
|
|
5
|
-
// import {TransformDrawOptions, TransformModelProps} from './resource-transform';
|
|
6
|
-
// export interface BufferTransform2 {
|
|
7
|
-
// setupResources(opts: any): void;
|
|
8
|
-
// updateModelProps(props?: {}): TransformProps;
|
|
9
|
-
// getDrawOptions(opts?: {}): TransformDrawOptions;
|
|
10
|
-
// swap(): boolean;
|
|
11
|
-
// update(opts?: {}): void;
|
|
12
|
-
// getBuffer(varyingName: any): any;
|
|
13
|
-
// getData(options?: {varyingName: string}): any;
|
|
14
|
-
// delete(): void;
|
|
15
|
-
// }
|
|
16
|
-
|
|
17
|
-
export default class BufferTransform {
|
|
18
|
-
gl: WebGL2RenderingContext;
|
|
19
|
-
currentIndex = 0;
|
|
20
|
-
feedbackMap = {};
|
|
21
|
-
varyings: string[] | null = null; // varyings array
|
|
22
|
-
bindings: TransformBinding[] = [];
|
|
23
|
-
resources = {}; // resources to be deleted
|
|
24
|
-
|
|
25
|
-
constructor(gl: WebGL2RenderingContext, props: TransformProps = {}) {
|
|
26
|
-
this.gl = gl;
|
|
27
|
-
this._initialize(props);
|
|
28
|
-
Object.seal(this);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
setupResources(opts): void {
|
|
32
|
-
for (const binding of this.bindings) {
|
|
33
|
-
this._setupTransformFeedback(binding, opts);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
updateModelProps(props: TransformProps = {}): TransformProps {
|
|
38
|
-
const {varyings} = this;
|
|
39
|
-
if (varyings.length > 0) {
|
|
40
|
-
props = Object.assign({}, props, {varyings});
|
|
41
|
-
}
|
|
42
|
-
return props;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// @ts-expect-error
|
|
46
|
-
getDrawOptions(opts: TransformRunOptions = {}): TransformDrawOptions {
|
|
47
|
-
const binding = this.bindings[this.currentIndex];
|
|
48
|
-
const {sourceBuffers, transformFeedback} = binding;
|
|
49
|
-
// @ts-expect-error
|
|
50
|
-
const attributes = Object.assign({}, sourceBuffers, opts.attributes);
|
|
51
|
-
return {attributes, transformFeedback};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
swap(): boolean {
|
|
55
|
-
if (this.feedbackMap) {
|
|
56
|
-
this.currentIndex = this._getNextIndex();
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// update source and/or feedbackBuffers
|
|
63
|
-
update(opts = {}) {
|
|
64
|
-
this._setupBuffers(opts);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// returns current feedbackBuffer of given name
|
|
68
|
-
getBuffer(varyingName: string | null): Buffer | null {
|
|
69
|
-
const {feedbackBuffers} = this.bindings[this.currentIndex];
|
|
70
|
-
const bufferOrParams = varyingName ? feedbackBuffers[varyingName] : null;
|
|
71
|
-
if (!bufferOrParams) {
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
return bufferOrParams instanceof Buffer ? bufferOrParams : bufferOrParams.buffer;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
getData(options: {varyingName?: string} = {}) {
|
|
78
|
-
const {varyingName} = options;
|
|
79
|
-
const buffer = this.getBuffer(varyingName);
|
|
80
|
-
if (buffer) {
|
|
81
|
-
return buffer.getData();
|
|
82
|
-
}
|
|
83
|
-
return null;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// Delete owned resources.
|
|
87
|
-
delete(): void {
|
|
88
|
-
for (const name in this.resources) {
|
|
89
|
-
this.resources[name].delete();
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Private
|
|
94
|
-
|
|
95
|
-
_initialize(props: TransformProps = {}): void {
|
|
96
|
-
this._setupBuffers(props);
|
|
97
|
-
this.varyings = props.varyings || Object.keys(this.bindings[this.currentIndex].feedbackBuffers);
|
|
98
|
-
if (this.varyings.length > 0) {
|
|
99
|
-
// if writting to buffers make sure it is WebGL2
|
|
100
|
-
assert(isWebGL2(this.gl));
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// auto create feedback buffers if requested
|
|
105
|
-
_getFeedbackBuffers(props) {
|
|
106
|
-
const {sourceBuffers = {}} = props;
|
|
107
|
-
const feedbackBuffers = {};
|
|
108
|
-
if (this.bindings[this.currentIndex]) {
|
|
109
|
-
// this gurantees a partial feedback buffer set doesn't update
|
|
110
|
-
// previously set buffers during auto creation mode.
|
|
111
|
-
Object.assign(feedbackBuffers, this.bindings[this.currentIndex].feedbackBuffers);
|
|
112
|
-
}
|
|
113
|
-
if (this.feedbackMap) {
|
|
114
|
-
// feedbackMap is defined as sourceBuffer as key and feedbackBuffer name as object
|
|
115
|
-
for (const sourceName in this.feedbackMap) {
|
|
116
|
-
const feedbackName = this.feedbackMap[sourceName];
|
|
117
|
-
if (sourceName in sourceBuffers) {
|
|
118
|
-
feedbackBuffers[feedbackName] = sourceName;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
Object.assign(feedbackBuffers, props.feedbackBuffers);
|
|
123
|
-
for (const bufferName in feedbackBuffers) {
|
|
124
|
-
const bufferOrRef = feedbackBuffers[bufferName];
|
|
125
|
-
if (typeof bufferOrRef === 'string') {
|
|
126
|
-
// Create new buffer with same layout and settings as source buffer
|
|
127
|
-
const sourceBuffer = sourceBuffers[bufferOrRef];
|
|
128
|
-
const {byteLength, usage, accessor} = sourceBuffer;
|
|
129
|
-
feedbackBuffers[bufferName] = this._createNewBuffer(bufferName, {
|
|
130
|
-
byteLength,
|
|
131
|
-
usage,
|
|
132
|
-
accessor
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return feedbackBuffers;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
_setupBuffers(props = {}) {
|
|
141
|
-
// @ts-expect-error
|
|
142
|
-
const {sourceBuffers = null} = props;
|
|
143
|
-
// @ts-expect-error
|
|
144
|
-
Object.assign(this.feedbackMap, props.feedbackMap);
|
|
145
|
-
const feedbackBuffers = this._getFeedbackBuffers(props);
|
|
146
|
-
this._updateBindings({sourceBuffers, feedbackBuffers});
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
_setupTransformFeedback(binding, {model}): void {
|
|
150
|
-
const {program} = model;
|
|
151
|
-
binding.transformFeedback = new TransformFeedback(this.gl, {
|
|
152
|
-
program,
|
|
153
|
-
buffers: binding.feedbackBuffers
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
_updateBindings(opts): void {
|
|
158
|
-
this.bindings[this.currentIndex] = this._updateBinding(this.bindings[this.currentIndex], opts);
|
|
159
|
-
if (this.feedbackMap) {
|
|
160
|
-
const {sourceBuffers, feedbackBuffers} = this._swapBuffers(this.bindings[this.currentIndex]);
|
|
161
|
-
const nextIndex = this._getNextIndex();
|
|
162
|
-
this.bindings[nextIndex] = this._updateBinding(this.bindings[nextIndex], {
|
|
163
|
-
sourceBuffers,
|
|
164
|
-
feedbackBuffers
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
_updateBinding(binding, opts) {
|
|
170
|
-
if (!binding) {
|
|
171
|
-
return {
|
|
172
|
-
sourceBuffers: Object.assign({}, opts.sourceBuffers),
|
|
173
|
-
feedbackBuffers: Object.assign({}, opts.feedbackBuffers)
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
Object.assign(binding.sourceBuffers, opts.sourceBuffers);
|
|
177
|
-
Object.assign(binding.feedbackBuffers, opts.feedbackBuffers);
|
|
178
|
-
if (binding.transformFeedback) {
|
|
179
|
-
binding.transformFeedback.setBuffers(binding.feedbackBuffers);
|
|
180
|
-
}
|
|
181
|
-
return binding;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
_swapBuffers(opts): {sourceBuffers: any; feedbackBuffers: any} {
|
|
185
|
-
if (!this.feedbackMap) {
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
const sourceBuffers = Object.assign({}, opts.sourceBuffers);
|
|
189
|
-
const feedbackBuffers = Object.assign({}, opts.feedbackBuffers);
|
|
190
|
-
for (const srcName in this.feedbackMap) {
|
|
191
|
-
const dstName = this.feedbackMap[srcName];
|
|
192
|
-
sourceBuffers[srcName] = opts.feedbackBuffers[dstName];
|
|
193
|
-
feedbackBuffers[dstName] = opts.sourceBuffers[srcName];
|
|
194
|
-
|
|
195
|
-
// make sure the new destination buffer is a Buffer object
|
|
196
|
-
assert(feedbackBuffers[dstName] instanceof Buffer);
|
|
197
|
-
}
|
|
198
|
-
return {sourceBuffers, feedbackBuffers};
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// Create a buffer and add to list of buffers to be deleted.
|
|
202
|
-
_createNewBuffer(name, opts): Buffer {
|
|
203
|
-
const buffer = new Buffer(this.gl, opts);
|
|
204
|
-
if (this.resources[name]) {
|
|
205
|
-
this.resources[name].delete();
|
|
206
|
-
}
|
|
207
|
-
this.resources[name] = buffer;
|
|
208
|
-
return buffer;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
_getNextIndex(): number {
|
|
212
|
-
return (this.currentIndex + 1) % 2;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
import GL from '@luma.gl/constants';
|
|
2
|
-
|
|
3
|
-
import {cloneTextureFrom, readPixelsToArray, Buffer, Texture2D, Framebuffer} from '@luma.gl/webgl';
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
_transform as transformModule,
|
|
7
|
-
getShaderInfo,
|
|
8
|
-
getPassthroughFS,
|
|
9
|
-
typeToChannelCount,
|
|
10
|
-
combineInjects
|
|
11
|
-
} from '@luma.gl/shadertools';
|
|
12
|
-
|
|
13
|
-
import {updateForTextures, getSizeUniforms} from './transform-shader-utils';
|
|
14
|
-
|
|
15
|
-
import type {TransformProps, TransformDrawOptions} from './transform-types';
|
|
16
|
-
|
|
17
|
-
// TODO: move these constants to transform-shader-utils
|
|
18
|
-
// Texture parameters needed so sample can precisely pick pixel for given element id.
|
|
19
|
-
const SRC_TEX_PARAMETER_OVERRIDES = {
|
|
20
|
-
[GL.TEXTURE_MIN_FILTER]: GL.NEAREST,
|
|
21
|
-
[GL.TEXTURE_MAG_FILTER]: GL.NEAREST,
|
|
22
|
-
[GL.TEXTURE_WRAP_S]: GL.CLAMP_TO_EDGE,
|
|
23
|
-
[GL.TEXTURE_WRAP_T]: GL.CLAMP_TO_EDGE
|
|
24
|
-
};
|
|
25
|
-
const FS_OUTPUT_VARIABLE = 'transform_output';
|
|
26
|
-
|
|
27
|
-
export default class TextureTransform {
|
|
28
|
-
gl: WebGL2RenderingContext;
|
|
29
|
-
id = 0;
|
|
30
|
-
currentIndex = 0;
|
|
31
|
-
_swapTexture = null;
|
|
32
|
-
targetTextureVarying = null;
|
|
33
|
-
targetTextureType = null;
|
|
34
|
-
samplerTextureMap = null;
|
|
35
|
-
bindings = []; // each element is an object : {sourceTextures, targetTexture, framebuffer}
|
|
36
|
-
resources = {}; // resources to be deleted
|
|
37
|
-
|
|
38
|
-
hasTargetTexture: boolean = false;
|
|
39
|
-
hasSourceTextures: boolean = false;
|
|
40
|
-
ownTexture: Texture2D | null = null;
|
|
41
|
-
elementIDBuffer: Buffer | null = null;
|
|
42
|
-
_targetRefTexName: string;
|
|
43
|
-
elementCount: number;
|
|
44
|
-
|
|
45
|
-
constructor(gl: WebGL2RenderingContext, props: TransformProps = {}) {
|
|
46
|
-
this.gl = gl;
|
|
47
|
-
this.id = this.currentIndex = 0;
|
|
48
|
-
this._swapTexture = null;
|
|
49
|
-
this.targetTextureVarying = null;
|
|
50
|
-
this.targetTextureType = null;
|
|
51
|
-
this.samplerTextureMap = null;
|
|
52
|
-
this.bindings = []; // each element is an object : {sourceTextures, targetTexture, framebuffer}
|
|
53
|
-
|
|
54
|
-
this.resources = {}; // resources to be deleted
|
|
55
|
-
|
|
56
|
-
this._initialize(props);
|
|
57
|
-
Object.seal(this);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
updateModelProps(props: TransformProps = {}) {
|
|
61
|
-
const updatedModelProps = this._processVertexShader(props);
|
|
62
|
-
return Object.assign({}, props, updatedModelProps);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
getDrawOptions(opts: TransformProps = {}): TransformDrawOptions {
|
|
66
|
-
const {sourceBuffers, sourceTextures, framebuffer, targetTexture} =
|
|
67
|
-
this.bindings[this.currentIndex];
|
|
68
|
-
|
|
69
|
-
const attributes = Object.assign({}, sourceBuffers, opts.attributes);
|
|
70
|
-
const uniforms = Object.assign({}, opts.uniforms);
|
|
71
|
-
const parameters = Object.assign({}, opts.parameters);
|
|
72
|
-
let discard = opts.discard;
|
|
73
|
-
|
|
74
|
-
if (this.hasSourceTextures || this.hasTargetTexture) {
|
|
75
|
-
attributes.transform_elementID = this.elementIDBuffer;
|
|
76
|
-
|
|
77
|
-
for (const sampler in this.samplerTextureMap) {
|
|
78
|
-
const textureName = this.samplerTextureMap[sampler];
|
|
79
|
-
uniforms[sampler] = sourceTextures[textureName];
|
|
80
|
-
}
|
|
81
|
-
this._setSourceTextureParameters();
|
|
82
|
-
// get texture size uniforms
|
|
83
|
-
const sizeUniforms = getSizeUniforms({
|
|
84
|
-
sourceTextureMap: sourceTextures,
|
|
85
|
-
targetTextureVarying: this.targetTextureVarying,
|
|
86
|
-
targetTexture
|
|
87
|
-
});
|
|
88
|
-
Object.assign(uniforms, sizeUniforms);
|
|
89
|
-
}
|
|
90
|
-
if (this.hasTargetTexture) {
|
|
91
|
-
discard = false;
|
|
92
|
-
parameters.viewport = [0, 0, framebuffer.width, framebuffer.height];
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return {attributes, framebuffer, uniforms, discard, parameters};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
swap() {
|
|
99
|
-
if (this._swapTexture) {
|
|
100
|
-
this.currentIndex = this._getNextIndex();
|
|
101
|
-
return true;
|
|
102
|
-
}
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// update source and/or feedbackBuffers
|
|
107
|
-
update(opts = {}) {
|
|
108
|
-
this._setupTextures(opts);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// returns current target texture
|
|
112
|
-
getTargetTexture() {
|
|
113
|
-
const {targetTexture} = this.bindings[this.currentIndex];
|
|
114
|
-
return targetTexture;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
getData({packed = false} = {}) {
|
|
118
|
-
const {framebuffer} = this.bindings[this.currentIndex];
|
|
119
|
-
const pixels = readPixelsToArray(framebuffer);
|
|
120
|
-
|
|
121
|
-
if (!packed) {
|
|
122
|
-
return pixels;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// readPixels returns 4 elements for each pixel, pack the elements when requested
|
|
126
|
-
const ArrayType = pixels.constructor;
|
|
127
|
-
const channelCount = typeToChannelCount(this.targetTextureType);
|
|
128
|
-
// @ts-expect-error
|
|
129
|
-
const packedPixels = new ArrayType((pixels.length * channelCount) / 4);
|
|
130
|
-
let packCount = 0;
|
|
131
|
-
for (let i = 0; i < pixels.length; i += 4) {
|
|
132
|
-
for (let j = 0; j < channelCount; j++) {
|
|
133
|
-
packedPixels[packCount++] = pixels[i + j];
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
return packedPixels;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// returns current framebuffer object that is being used.
|
|
140
|
-
getFramebuffer() {
|
|
141
|
-
const currentResources = this.bindings[this.currentIndex];
|
|
142
|
-
return currentResources.framebuffer;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// Delete owned resources.
|
|
146
|
-
delete() {
|
|
147
|
-
if (this.ownTexture) {
|
|
148
|
-
this.ownTexture.delete();
|
|
149
|
-
}
|
|
150
|
-
if (this.elementIDBuffer) {
|
|
151
|
-
this.elementIDBuffer.delete();
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Private
|
|
156
|
-
|
|
157
|
-
_initialize(props: TransformProps = {}) {
|
|
158
|
-
const {_targetTextureVarying, _swapTexture} = props;
|
|
159
|
-
this._swapTexture = _swapTexture;
|
|
160
|
-
this.targetTextureVarying = _targetTextureVarying;
|
|
161
|
-
this.hasTargetTexture = Boolean(_targetTextureVarying);
|
|
162
|
-
this._setupTextures(props);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// auto create target texture if requested
|
|
166
|
-
_createTargetTexture(props) {
|
|
167
|
-
const {sourceTextures, textureOrReference} = props;
|
|
168
|
-
if (textureOrReference instanceof Texture2D) {
|
|
169
|
-
return textureOrReference;
|
|
170
|
-
}
|
|
171
|
-
// 'targetTexture' is a reference souce texture.
|
|
172
|
-
const refTexture = sourceTextures[textureOrReference];
|
|
173
|
-
if (!refTexture) {
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// save reference texture name, when corresponding source texture is updated
|
|
178
|
-
// we also update target texture.
|
|
179
|
-
this._targetRefTexName = textureOrReference;
|
|
180
|
-
|
|
181
|
-
return this._createNewTexture(refTexture);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
_setupTextures(props: TransformProps = {}) {
|
|
185
|
-
const {sourceBuffers, _sourceTextures = {}, _targetTexture} = props;
|
|
186
|
-
const targetTexture = this._createTargetTexture({
|
|
187
|
-
sourceTextures: _sourceTextures,
|
|
188
|
-
textureOrReference: _targetTexture
|
|
189
|
-
});
|
|
190
|
-
this.hasSourceTextures =
|
|
191
|
-
this.hasSourceTextures || (_sourceTextures && Object.keys(_sourceTextures).length > 0);
|
|
192
|
-
this._updateBindings({sourceBuffers, sourceTextures: _sourceTextures, targetTexture});
|
|
193
|
-
if ('elementCount' in props) {
|
|
194
|
-
this._updateElementIDBuffer(props.elementCount);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
_updateElementIDBuffer(elementCount: number): void {
|
|
199
|
-
if (typeof elementCount !== 'number' || this.elementCount >= elementCount) {
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
// NOTE: using float so this will work with GLSL 1.0 shaders.
|
|
203
|
-
const elementIds = new Float32Array(elementCount);
|
|
204
|
-
elementIds.forEach((_, index, array) => {
|
|
205
|
-
array[index] = index;
|
|
206
|
-
});
|
|
207
|
-
if (!this.elementIDBuffer) {
|
|
208
|
-
this.elementIDBuffer = new Buffer(this.gl, {
|
|
209
|
-
data: elementIds,
|
|
210
|
-
accessor: {size: 1}
|
|
211
|
-
});
|
|
212
|
-
} else {
|
|
213
|
-
this.elementIDBuffer.setData({data: elementIds});
|
|
214
|
-
}
|
|
215
|
-
this.elementCount = elementCount;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
_updateBindings(opts) {
|
|
219
|
-
this.bindings[this.currentIndex] = this._updateBinding(this.bindings[this.currentIndex], opts);
|
|
220
|
-
if (this._swapTexture) {
|
|
221
|
-
const {sourceTextures, targetTexture} = this._swapTextures(this.bindings[this.currentIndex]);
|
|
222
|
-
const nextIndex = this._getNextIndex();
|
|
223
|
-
this.bindings[nextIndex] = this._updateBinding(this.bindings[nextIndex], {
|
|
224
|
-
sourceTextures,
|
|
225
|
-
targetTexture
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
_updateBinding(binding, opts) {
|
|
231
|
-
const {sourceBuffers, sourceTextures, targetTexture} = opts;
|
|
232
|
-
if (!binding) {
|
|
233
|
-
binding = {
|
|
234
|
-
sourceBuffers: {},
|
|
235
|
-
sourceTextures: {},
|
|
236
|
-
targetTexture: null
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
Object.assign(binding.sourceTextures, sourceTextures);
|
|
240
|
-
Object.assign(binding.sourceBuffers, sourceBuffers);
|
|
241
|
-
if (targetTexture) {
|
|
242
|
-
binding.targetTexture = targetTexture;
|
|
243
|
-
|
|
244
|
-
const {width, height} = targetTexture;
|
|
245
|
-
const {framebuffer} = binding;
|
|
246
|
-
if (framebuffer) {
|
|
247
|
-
// First update texture without re-sizing attachments
|
|
248
|
-
framebuffer.update({
|
|
249
|
-
attachments: {[GL.COLOR_ATTACHMENT0]: targetTexture},
|
|
250
|
-
resizeAttachments: false
|
|
251
|
-
});
|
|
252
|
-
// Resize to new taget texture size
|
|
253
|
-
framebuffer.resize({width, height});
|
|
254
|
-
} else {
|
|
255
|
-
binding.framebuffer = new Framebuffer(this.gl, {
|
|
256
|
-
id: `transform-framebuffer`,
|
|
257
|
-
width,
|
|
258
|
-
height,
|
|
259
|
-
attachments: {
|
|
260
|
-
[GL.COLOR_ATTACHMENT0]: targetTexture
|
|
261
|
-
}
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
return binding;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// set texture filtering parameters on source textures.
|
|
269
|
-
_setSourceTextureParameters() {
|
|
270
|
-
const index = this.currentIndex;
|
|
271
|
-
const {sourceTextures} = this.bindings[index];
|
|
272
|
-
for (const name in sourceTextures) {
|
|
273
|
-
sourceTextures[name].setParameters(SRC_TEX_PARAMETER_OVERRIDES);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
_swapTextures(opts) {
|
|
278
|
-
if (!this._swapTexture) {
|
|
279
|
-
return null;
|
|
280
|
-
}
|
|
281
|
-
const sourceTextures = Object.assign({}, opts.sourceTextures);
|
|
282
|
-
sourceTextures[this._swapTexture] = opts.targetTexture;
|
|
283
|
-
|
|
284
|
-
const targetTexture = opts.sourceTextures[this._swapTexture];
|
|
285
|
-
|
|
286
|
-
return {sourceTextures, targetTexture};
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// Create a buffer and add to list of buffers to be deleted.
|
|
290
|
-
_createNewTexture(refTexture) {
|
|
291
|
-
const texture = cloneTextureFrom(refTexture, {
|
|
292
|
-
parameters: {
|
|
293
|
-
[GL.TEXTURE_MIN_FILTER]: GL.NEAREST,
|
|
294
|
-
[GL.TEXTURE_MAG_FILTER]: GL.NEAREST,
|
|
295
|
-
[GL.TEXTURE_WRAP_S]: GL.CLAMP_TO_EDGE,
|
|
296
|
-
[GL.TEXTURE_WRAP_T]: GL.CLAMP_TO_EDGE
|
|
297
|
-
},
|
|
298
|
-
pixelStore: {
|
|
299
|
-
[GL.UNPACK_FLIP_Y_WEBGL]: false
|
|
300
|
-
}
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
// thre can only be one target texture
|
|
304
|
-
if (this.ownTexture) {
|
|
305
|
-
this.ownTexture.delete();
|
|
306
|
-
}
|
|
307
|
-
this.ownTexture = texture;
|
|
308
|
-
|
|
309
|
-
return texture;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
_getNextIndex() {
|
|
313
|
-
return (this.currentIndex + 1) % 2;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
// build and return shader releated parameters
|
|
317
|
-
_processVertexShader(props: TransformProps = {}) {
|
|
318
|
-
const {sourceTextures, targetTexture} = this.bindings[this.currentIndex];
|
|
319
|
-
// @ts-expect-error TODO - uniforms is not present
|
|
320
|
-
const {vs, uniforms, targetTextureType, inject, samplerTextureMap} = updateForTextures({
|
|
321
|
-
vs: props.vs,
|
|
322
|
-
sourceTextureMap: sourceTextures,
|
|
323
|
-
targetTextureVarying: this.targetTextureVarying,
|
|
324
|
-
targetTexture
|
|
325
|
-
});
|
|
326
|
-
const combinedInject = combineInjects([props.inject || {}, inject]);
|
|
327
|
-
this.targetTextureType = targetTextureType;
|
|
328
|
-
this.samplerTextureMap = samplerTextureMap;
|
|
329
|
-
const fs =
|
|
330
|
-
props._fs ||
|
|
331
|
-
getPassthroughFS({
|
|
332
|
-
version: getShaderInfo(vs).version,
|
|
333
|
-
input: this.targetTextureVarying,
|
|
334
|
-
inputType: targetTextureType,
|
|
335
|
-
output: FS_OUTPUT_VARIABLE
|
|
336
|
-
});
|
|
337
|
-
const modules =
|
|
338
|
-
this.hasSourceTextures || this.targetTextureVarying
|
|
339
|
-
// @ts-expect-error
|
|
340
|
-
? [transformModule].concat(props.modules || [])
|
|
341
|
-
: props.modules;
|
|
342
|
-
return {vs, fs, modules, uniforms, inject: combinedInject};
|
|
343
|
-
}
|
|
344
|
-
}
|