ris-framework 0.1.0-dev.4

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.
Files changed (103) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +11 -0
  4. package/dist/assets/_ris-ktx2-node-module-stub-DwDS30PT.js +1 -0
  5. package/dist/assets/cat.jpg +0 -0
  6. package/dist/assets/index-BmeGit54.css +1 -0
  7. package/dist/assets/index-qyn4pzGw.js +151 -0
  8. package/dist/assets/libktx-Cjh8w7XG-Ci1Fm_MH.js +1 -0
  9. package/dist/assets/test.png +0 -0
  10. package/dist/index.html +16 -0
  11. package/dist/vite.svg +1 -0
  12. package/package.json +37 -0
  13. package/shaders/glsl/inspect_texture_mips_fs.glsl +18 -0
  14. package/shaders/glsl/inspect_texture_mips_vs.glsl +25 -0
  15. package/shaders/glsl/main_render_target_flip_y_fs.glsl +9 -0
  16. package/shaders/glsl/main_render_target_flip_y_vs.glsl +9 -0
  17. package/shaders/glsl/sprite_fs.glsl +14 -0
  18. package/shaders/glsl/sprite_vs.glsl +20 -0
  19. package/shaders/glsl/unlit_material_fs.glsl +19 -0
  20. package/shaders/glsl/unlit_material_vs.glsl +25 -0
  21. package/src/TextureFormat.ts +0 -0
  22. package/src/VertexFormat.ts +17 -0
  23. package/src/common/sampler-enums.ts +21 -0
  24. package/src/common/vertex-step-mode.ts +12 -0
  25. package/src/content/ShaderModuleContent.ts +47 -0
  26. package/src/core/Framework.ts +240 -0
  27. package/src/core/FrameworkConfig.ts +31 -0
  28. package/src/core/buffers/buffers-factory-interface.ts +0 -0
  29. package/src/core/buffers/vertex-buffer-interface.ts +0 -0
  30. package/src/core/content/ContentManager.ts +86 -0
  31. package/src/core/dependency-injection/register-services-interface.ts +15 -0
  32. package/src/core/framework-interface.ts +0 -0
  33. package/src/core/input/InputManager.ts +115 -0
  34. package/src/core/renderer/enums.ts +20 -0
  35. package/src/core/renderer/renderer-interface.ts +92 -0
  36. package/src/core/renderer/rendering-limits.ts +20 -0
  37. package/src/core/rendering/AGraphicsDevice.ts +119 -0
  38. package/src/core/rendering/ARenderer.ts +247 -0
  39. package/src/core/rendering/blending/blend-component-descriptor.ts +33 -0
  40. package/src/core/rendering/blending/blend-state-descriptor.ts +51 -0
  41. package/src/core/rendering/blending/enums.ts +50 -0
  42. package/src/core/rendering/enums.ts +22 -0
  43. package/src/core/rendering/primitive/PrimitiveStateDescriptor.ts +32 -0
  44. package/src/core/rendering/primitive/enums.ts +68 -0
  45. package/src/core/rendering/render-pass/render-pass-descriptor.ts +0 -0
  46. package/src/core/rendering/render-pass/render-pass-interface.ts +17 -0
  47. package/src/core/rendering/sampler/sampler-descriptor.ts +33 -0
  48. package/src/core/rendering/texture/texture.ts +146 -0
  49. package/src/core/rendering/vertex-attribute.ts +20 -0
  50. package/src/core/rendering/vertex-buffer-layout.ts +84 -0
  51. package/src/core/shader/shader-loader.ts +27 -0
  52. package/src/core/shader/shader-module-interface.ts +1 -0
  53. package/src/core/sprite-batch/SpriteBatch.ts +216 -0
  54. package/src/core/sprite-batch/SpriteBatchDrawable.ts +146 -0
  55. package/src/core/sprite-batch/SpriteBatchMesh.ts +243 -0
  56. package/src/core/window/window-manager.ts +76 -0
  57. package/src/counter.ts +9 -0
  58. package/src/geometry/BaseGeometry.ts +148 -0
  59. package/src/geometry/GeometryBuilder.ts +58 -0
  60. package/src/geometry/GeometryFormat.ts +37 -0
  61. package/src/main.ts +97 -0
  62. package/src/style.css +96 -0
  63. package/src/typescript.svg +1 -0
  64. package/src/webgl/WebGLGraphicsDeviceFeatures.ts +57 -0
  65. package/src/webgl/WebGlGpuInfo.ts +76 -0
  66. package/src/webgl/WebGlGraphicsDevice.ts +129 -0
  67. package/src/webgl/WebGlRenderer.ts +31 -0
  68. package/src/webgl/blending/webgl-blend-state.ts +59 -0
  69. package/src/webgl/buffers/WebGlBuffersFactory.ts +75 -0
  70. package/src/webgl/buffers/WebGlUniformBuffer.ts +87 -0
  71. package/src/webgl/buffers/WebGlVertexBuffer.ts +100 -0
  72. package/src/webgl/buffers/webgl-index-buffer.ts +110 -0
  73. package/src/webgl/cast/cast.ts +100 -0
  74. package/src/webgl/primitive/WebGlPrimitiveState.ts +60 -0
  75. package/src/webgl/render-pass/WebGlRenderPass.ts +186 -0
  76. package/src/webgl/render-pipelines/AWebGlRenderPipeline.ts +129 -0
  77. package/src/webgl/render-pipelines/WebGlMainRenderTargetRenderPipeline.ts +95 -0
  78. package/src/webgl/render-pipelines/WebGlRenderPipelineFactory.ts +75 -0
  79. package/src/webgl/render-pipelines/inspect/WebGlInspectTextureMipsRenderPipeline.ts +200 -0
  80. package/src/webgl/render-pipelines/material/WebGlUnlitRenderPipeline.ts +197 -0
  81. package/src/webgl/render-pipelines/sprite/WebGlSpriteRenderPipeline.ts +148 -0
  82. package/src/webgl/rendering/webgl-vertex-attribute.ts +16 -0
  83. package/src/webgl/rendering/webgl-vertex-buffer-layout.ts +64 -0
  84. package/src/webgl/sampler/webgl-sampler.ts +110 -0
  85. package/src/webgl/shader/WebGlShaderModule.ts +110 -0
  86. package/src/webgl/shader/WebGlShaderModuleLoader.ts +40 -0
  87. package/src/webgl/swap-chain/WebGlSwapChain.ts +56 -0
  88. package/src/webgl/texture/WebGlTexture2D.ts +133 -0
  89. package/src/webgl/texture/WebGlTextureFactory.ts +184 -0
  90. package/src/webgl/utilities/WebGlBufferUtilities.ts +124 -0
  91. package/src/webgl/utilities/WebGlConverter.ts +391 -0
  92. package/src/webgl/utilities/WebGlFrameBufferUtilities.ts +240 -0
  93. package/src/webgl/utilities/WebGlSamplerUtilities.ts +81 -0
  94. package/src/webgl/utilities/WebGlShaderUtilities.ts +48 -0
  95. package/src/webgl/utilities/WebGlTextureUtilities.ts +162 -0
  96. package/src/webgl/utilities/WebGlUtilities.ts +71 -0
  97. package/src/webgl/utilities/spector-js-utilities.ts +13 -0
  98. package/src/webgl/utilities/webgl-blending-utilities.ts +85 -0
  99. package/src/webgl/utilities/webgl-culling-utilities.ts +23 -0
  100. package/src/webgl/utilities/webgl-filter-anisotropic-utilities.ts +37 -0
  101. package/src/webgl/utilities/webgl-program-utilities.ts +29 -0
  102. package/src/webgl/utilities/webgl-render-buffer-utilities.ts +23 -0
  103. package/src/webgl/utilities/webgl-renderbuffer-utilities.ts +35 -0
@@ -0,0 +1,68 @@
1
+ /**
2
+ * The culling mode.
3
+ */
4
+ export enum CullMode
5
+ {
6
+ /**
7
+ * The culling is disabled. Both front and back faces will be rendered.
8
+ */
9
+ NONE,
10
+
11
+ /**
12
+ * The back faces will be culled. Only front faces will be rendered.
13
+ */
14
+ BACK,
15
+
16
+ /**
17
+ * The front faces will be culled. Only back faces will be rendered.
18
+ */
19
+ FRONT
20
+ }
21
+
22
+ /**
23
+ * The front face winding order.
24
+ */
25
+ export enum FrontFace
26
+ {
27
+ /**
28
+ * The clockwise winding order.
29
+ */
30
+ CW,
31
+
32
+ /**
33
+ * The counter-clockwise winding order.
34
+ */
35
+ CCW,
36
+ }
37
+
38
+
39
+ /**
40
+ * The primitive topology.
41
+ */
42
+ export enum PrimitiveTopology
43
+ {
44
+ /**
45
+ * Each consecutive pair of two vertices defines a line primitive.
46
+ */
47
+ LINE_LIST,
48
+
49
+ /**
50
+ * Each vertex after the first defines a line primitive between it and the previous vertex.
51
+ */
52
+ LINE_STRIP,
53
+
54
+ /**
55
+ * Each vertex defines a point primitive.
56
+ */
57
+ POINT_LIST,
58
+
59
+ /**
60
+ * Each consecutive triplet of three vertices defines a triangle primitive.
61
+ */
62
+ TRIANGLE_LIST,
63
+
64
+ /**
65
+ * Each vertex after the first two defines a triangle primitive between it and the previous two vertices.
66
+ */
67
+ TRIANGLE_STRIP,
68
+ }
@@ -0,0 +1,17 @@
1
+ import type {IDisposable} from "ris-framework-api";
2
+
3
+ /**
4
+ * Defines the interface for a render pass, which encapsulates the operations and state management required to execute a rendering sequence.
5
+ */
6
+ export interface IRenderPass extends IDisposable {
7
+
8
+ /**
9
+ * Begins the render pass, setting up necessary state and resources for rendering.
10
+ */
11
+ beginPass(): void;
12
+
13
+ /**
14
+ * Ends the render pass, finalizing any rendering operations and cleaning up resources as needed.
15
+ */
16
+ endPass(): void;
17
+ }
@@ -0,0 +1,33 @@
1
+ import {MipMapSamplerFilter, SamplerAddressMode, SamplerFilter} from "ris-framework-api";
2
+
3
+ export class SamplerDescriptor {
4
+ /**
5
+ * The minification filter.
6
+ */
7
+ public minFilter: SamplerFilter = SamplerFilter.NEAREST;
8
+
9
+ /**
10
+ * The magnification filter.
11
+ */
12
+ public magFilter: SamplerFilter = SamplerFilter.NEAREST;
13
+
14
+ /**
15
+ * The mipmap filter.
16
+ */
17
+ public mipMapFilter: MipMapSamplerFilter = MipMapSamplerFilter.NONE;
18
+
19
+ /**
20
+ * The address mode for the U coordinate.
21
+ */
22
+ public addressModeU: SamplerAddressMode = SamplerAddressMode.CLAMP_TO_EDGE;
23
+
24
+ /**
25
+ * The address mode for the V coordinate.
26
+ */
27
+ public addressModeV: SamplerAddressMode = SamplerAddressMode.CLAMP_TO_EDGE;
28
+
29
+ /**
30
+ * The address mode for the W coordinate.
31
+ */
32
+ public addressModeW: SamplerAddressMode = SamplerAddressMode.CLAMP_TO_EDGE;
33
+ }
@@ -0,0 +1,146 @@
1
+ import {
2
+ type ITexture2D, type ITextureView,
3
+ State,
4
+ type TextureDescriptor,
5
+ type TextureFormat,
6
+ TextureUsage,
7
+ TextureViewDescriptor
8
+ } from "ris-framework-api";
9
+ import {vec2} from "gl-matrix";
10
+
11
+ /**
12
+ * The ATexture2D class is an abstract base class that implements the ITexture2D interface. It provides common properties and a constructor for initializing a 2D texture, but it does not implement the actual texture creation and management logic, which must be provided by subclasses that extend this base class.
13
+ */
14
+ export abstract class ATexture2D implements ITexture2D {
15
+
16
+ private static _nextId: number = 1;
17
+
18
+ protected _handle: any;
19
+ protected _state: State = State.CREATED;
20
+ protected _size = 0;
21
+ protected _generateMipMaps: boolean = false;
22
+ protected _mipLevels: number;
23
+ protected _data?: Uint8Array[];
24
+ protected _blockSize: vec2;
25
+
26
+ protected _disposedListeners: ((tex: ITexture2D) => void)[] = [];
27
+
28
+ /**
29
+ * The constructor for the ATexture2D class.
30
+ * @param descriptor - The texture descriptor.
31
+ */
32
+ protected constructor(
33
+ descriptor: TextureDescriptor,
34
+ ) {
35
+
36
+ if (!descriptor.textureFormat) {
37
+ throw new Error("Texture format is required.");
38
+ }
39
+
40
+ this.id = ATexture2D.generateId();
41
+ this.textureFormat = descriptor.textureFormat;
42
+ this.textureUsage = descriptor.textureUsage;
43
+ this.textureViewFormat = descriptor.textureFormat;
44
+ this.width = descriptor.width;
45
+ this.height = descriptor.height;
46
+ this._data = descriptor.data;
47
+ this._mipLevels = this._data?.length ?? 1;
48
+ this._generateMipMaps = descriptor.generateMipmaps && this._mipLevels <= 1;
49
+ this.label = descriptor.label;
50
+ this._blockSize = descriptor.blockSize ?? vec2.fromValues(1, 1);
51
+
52
+ if (this.width == 0 || this.height == 0) {
53
+ throw new Error("Width or height cannot be zero.");
54
+ }
55
+
56
+ // @ts-ignore
57
+ this.defaultTextureView = null;
58
+
59
+ }
60
+
61
+ /** @inheritDoc */
62
+ public addOnDisposedListener(event: (sender: ITexture2D) => void): void {
63
+ this._disposedListeners.push(event);
64
+ }
65
+
66
+ /** @inheritDoc */
67
+ public removeOnDisposedListener(event: (sender: ITexture2D) => void): void {
68
+
69
+ let index = this._disposedListeners.indexOf(event);
70
+ if (index > -1) {
71
+ this._disposedListeners = this._disposedListeners.splice(index, 1);
72
+ }
73
+ }
74
+
75
+ /** @inheritDoc */
76
+ public readonly id: number;
77
+
78
+ /** @inheritDoc */
79
+ public readonly defaultTextureView: ITextureView;
80
+
81
+ /** @inheritDoc */
82
+ public readonly textureUsage: TextureUsage;
83
+
84
+ /** @inheritDoc */
85
+ public readonly textureViewFormat: TextureFormat;
86
+
87
+ /** @inheritDoc */
88
+ public readonly textureFormat: TextureFormat;
89
+
90
+ /** @inheritDoc */
91
+ public get textureState(): State {
92
+ return this._state;
93
+ }
94
+
95
+ /** @inheritDoc */
96
+ public get handle(): any {
97
+ return this._handle;
98
+ }
99
+
100
+ /**
101
+ * Generates a unique ID for each texture instance.
102
+ * @returns A unique ID for the texture instance.
103
+ */
104
+ private static generateId(): number {
105
+ return ATexture2D._nextId++;
106
+ }
107
+
108
+ /**
109
+ * Initializes the texture.
110
+ */
111
+ abstract initialize(): void;
112
+
113
+ /** @inheritdoc */
114
+ public get state(): State {
115
+ return this._state;
116
+ }
117
+
118
+ /** @inheritdoc */
119
+ public readonly width: number;
120
+
121
+ /** @inheritdoc */
122
+ public readonly height: number;
123
+
124
+ /** @inheritDoc */
125
+ public get size(): number {
126
+ return this._size;
127
+ }
128
+
129
+ /** @inheritDoc */
130
+ public get mipLevels(): number {
131
+ return this._mipLevels;
132
+ }
133
+
134
+ /** @inheritdoc */
135
+ public readonly label: string | null;
136
+
137
+ /**
138
+ * Creates a texture view for the texture.
139
+ * @param descriptor The descriptor for the texture view.
140
+ * @returns The created texture view, which can be used for sampling the texture in shaders.
141
+ */
142
+ public abstract createView(descriptor?: TextureViewDescriptor): ITextureView;
143
+
144
+ /** @inheritDoc */
145
+ public abstract dispose(): void;
146
+ }
@@ -0,0 +1,20 @@
1
+ import {VertexFormat} from "../../VertexFormat.ts";
2
+
3
+ export class VertexAttribute {
4
+
5
+ /**
6
+ * The format of the vertex attribute.
7
+ */
8
+ format: VertexFormat = VertexFormat.FLOAT_32;
9
+
10
+ /**
11
+ * The offset, in bytes, of the vertex attribute from the start of the vertex.
12
+ */
13
+ offset: number = 0;
14
+
15
+ /**
16
+ * The shader location of the vertex attribute.
17
+ * This is used to bind the vertex attribute to the correct location in the shader.
18
+ */
19
+ shaderLocation: number = 0;
20
+ }
@@ -0,0 +1,84 @@
1
+ import { VertexStepMode } from "ris-framework-api";
2
+ import type { VertexAttribute } from "./vertex-attribute";
3
+ import {VertexFormat} from "../../VertexFormat.ts";
4
+
5
+ /**
6
+ * Describes the layout of a vertex buffer, including the stride, step mode, and attributes.
7
+ */
8
+ export class VertexBufferLayout {
9
+ /**
10
+ * Stride in bytes (size of one vertex)
11
+ */
12
+ arrayStride: number;
13
+
14
+ /**
15
+ * How to step through the buffer
16
+ */
17
+ stepMode: VertexStepMode;
18
+
19
+ /**
20
+ * Vertex attributes
21
+ */
22
+ attributes: VertexAttribute[] = [];
23
+
24
+ constructor(
25
+ arrayStride: number,
26
+ stepMode: VertexStepMode,
27
+ attributes: VertexAttribute[]
28
+ ) {
29
+ this.arrayStride = arrayStride;
30
+ this.stepMode = stepMode;
31
+ this.attributes = attributes;
32
+ }
33
+
34
+ // =========================
35
+ // Static helpers
36
+ // =========================
37
+
38
+ static createFloat3Float2Layout(): VertexBufferLayout {
39
+ const floatSize = 4;
40
+
41
+ return new VertexBufferLayout(
42
+ (3 + 2) * floatSize,
43
+ VertexStepMode.VERTEX,
44
+ [
45
+ {
46
+ format: VertexFormat.FLOAT_32X3,
47
+ offset: 0,
48
+ shaderLocation: 0,
49
+ },
50
+ {
51
+ format: VertexFormat.FLOAT_32X2,
52
+ offset: 3 * floatSize,
53
+ shaderLocation: 1,
54
+ },
55
+ ]
56
+ );
57
+ }
58
+
59
+ static createFloat3Float4Float2Layout(): VertexBufferLayout {
60
+ const floatSize = 4;
61
+
62
+ return new VertexBufferLayout(
63
+ (3 + 4 + 2) * floatSize,
64
+ VertexStepMode.VERTEX,
65
+ [
66
+ {
67
+ format: VertexFormat.FLOAT_32X3,
68
+ offset: 0,
69
+ shaderLocation: 0,
70
+ },
71
+ {
72
+ format: VertexFormat.FLOAT_32X4,
73
+ offset: 3 * floatSize,
74
+ shaderLocation: 1,
75
+ },
76
+ {
77
+ format: VertexFormat.FLOAT_32X2,
78
+ offset: (3 + 4) * floatSize,
79
+ shaderLocation: 2,
80
+ },
81
+ ]
82
+ );
83
+ }
84
+ }
@@ -0,0 +1,27 @@
1
+ import {RenderingBackend, type ShaderStage} from "ris-framework-api";
2
+
3
+ export class ShaderLoader {
4
+
5
+ /**
6
+ * Loads the shader source code from the given shader file path for the specified rendering backend and shader stages.
7
+ * @param shaderFilePath The shader file path.
8
+ * @param renderingBackend The rendering backend.
9
+ * @param stages The shader stages. For WebGL, only one shader stage is allowed. For WebGPU, multiple shader stages are allowed.
10
+ * @returns The shader source code.
11
+ */
12
+ public async load(shaderFilePath: string, renderingBackend: RenderingBackend, stages: ShaderStage[]): Promise<string> {
13
+
14
+ if(stages.length === 0) {
15
+ throw new Error("At least one shader stage must be specified.");
16
+ }
17
+
18
+ if(renderingBackend === RenderingBackend.WEB_GL) {
19
+ if(stages.length > 1) {
20
+ throw new Error("WebGL does not support multiple shader stages in a single file.");
21
+ }
22
+ }
23
+
24
+ const shaderSource = await fetch(shaderFilePath);
25
+ return await shaderSource.text();
26
+ }
27
+ }
@@ -0,0 +1,216 @@
1
+ import {type mat4, vec2, vec3} from "gl-matrix";
2
+ import {SpriteBatchDrawable} from "./SpriteBatchDrawable";
3
+ import {
4
+ type IFramework,
5
+ type ITexture2D,
6
+ type ISpriteBatch,
7
+ type IOrthographicCamera,
8
+ type IUniformBuffer,
9
+ Color, Rect, type ISampler
10
+ } from "ris-framework-api";
11
+
12
+ /**
13
+ * The implementation of sprite batch.
14
+ */
15
+ export class SpriteBatch implements ISpriteBatch {
16
+ static MAX_BATCH_SIZE = 1000;
17
+ static ZERO_VECTOR = vec2.fromValues(0, 0);
18
+ static CENTER_VECTOR = vec2.fromValues(0.5, 0.5);
19
+
20
+ private readonly _tempPosition = vec3.create();
21
+ private readonly _tempSize = vec2.create();
22
+ private readonly _tempSourceRect = new Rect(0, 0, 0, 0);
23
+
24
+ /**
25
+ * The drawables where first key is texture id, second key is sampler id and value is drawable.
26
+ */
27
+ private readonly _spriteBatchDrawables: Map<number, Map<number, SpriteBatchDrawable>> = new Map();
28
+ private _currentTexture: ITexture2D | null = null;
29
+ private _currentSampler: ISampler | null = null;
30
+ private _currentSpriteBatchDrawable: SpriteBatchDrawable = null!;
31
+ private _defaultCamera: IOrthographicCamera = null!;
32
+
33
+ // It is set on Begin and reset back to the default camera on End.
34
+ private _currentProjectionViewBuffer: IUniformBuffer = null!;
35
+
36
+ /**
37
+ * The default texture to use for "texture less" draw calls.
38
+ * It is white so that color multiplication works correctly.
39
+ */
40
+ private _defaultWhiteTexture: ITexture2D = null!;
41
+
42
+ /**
43
+ * The constructor.
44
+ * @param _framework The framework.
45
+ */
46
+ public constructor(private readonly _framework: IFramework) {
47
+ }
48
+
49
+ private _tryGetDrawable(texture: ITexture2D, sampler: ISampler) {
50
+ const samplerKeyValue = this._spriteBatchDrawables.get(texture.id);
51
+ if (!samplerKeyValue) {
52
+ return null;
53
+ }
54
+
55
+ return samplerKeyValue.get(sampler.id);
56
+ }
57
+
58
+ private _setDrawable(texture: ITexture2D, sampler: ISampler, drawable: SpriteBatchDrawable): void {
59
+ let samplerKeyValue = this._spriteBatchDrawables.get(texture.id);
60
+ if (!samplerKeyValue) {
61
+ samplerKeyValue = new Map();
62
+ samplerKeyValue.set(sampler.id, drawable);
63
+ }
64
+
65
+ this._spriteBatchDrawables.set(texture.id, samplerKeyValue);
66
+ }
67
+
68
+ private _validateTexture(texture?: ITexture2D) {
69
+ if (!texture) {
70
+ throw new Error(`Texture cannot be empty.`);
71
+ }
72
+ }
73
+
74
+ private _checkIfNewDrawableShouldBeCreated(texture: ITexture2D, sampler: ISampler): void {
75
+ // If there was a texture change, we need to end the current sprite batch drawable and start a new one.
76
+ if (texture != this._currentTexture || sampler != this._currentSampler) {
77
+ // End will draw. This draws with a previously set sprite batch drawable.
78
+ this.end();
79
+
80
+ this._currentTexture = texture;
81
+
82
+ // Create a new sprite batch drawable if needed.
83
+ let spriteBatchDrawable = this._tryGetDrawable(texture, sampler);
84
+
85
+ if (!spriteBatchDrawable) {
86
+ spriteBatchDrawable = new SpriteBatchDrawable(
87
+ this._framework, texture, sampler,
88
+ this._currentProjectionViewBuffer,
89
+ SpriteBatch.MAX_BATCH_SIZE);
90
+ spriteBatchDrawable.initialize();
91
+
92
+ this._setDrawable(texture, sampler, spriteBatchDrawable);
93
+
94
+ // When texture is disposed destroy this drawable.
95
+ const disposeListener = () => {
96
+ spriteBatchDrawable?.dispose();
97
+ texture.removeOnDisposedListener(disposeListener);
98
+ };
99
+ texture.addOnDisposedListener(disposeListener);
100
+ }
101
+
102
+ // Assign current.
103
+ this._currentSpriteBatchDrawable = spriteBatchDrawable;
104
+ }
105
+ }
106
+
107
+ /** @inheritDoc */
108
+ public initialize() {
109
+ this._defaultCamera = this._framework.cameraFactory.createDefaultOrthographicCamera();
110
+ this._currentProjectionViewBuffer = this._defaultCamera.projectionViewBuffer;
111
+ this._defaultWhiteTexture = this._framework.textureFactory.createEmpty(1, 1, Color.white());
112
+
113
+ this._framework.renderer.addOnResizedListener((_, size) => {
114
+ this._defaultCamera.right = size[0]
115
+ this._defaultCamera.bottom = size[1];
116
+ this._defaultCamera.updateBuffers();
117
+ });
118
+ }
119
+
120
+ /** @inheritDoc */
121
+ public begin(projectionViewMatrix: mat4 | undefined = undefined,
122
+ sampler: ISampler | undefined = undefined) {
123
+ this.end();
124
+
125
+ this._currentSampler = sampler ?? this._framework.graphicsDevice.defaultTextureSampler;
126
+
127
+ if (projectionViewMatrix) {
128
+ this._currentProjectionViewBuffer.update(projectionViewMatrix);
129
+ }
130
+
131
+ for (const texSamplerDrawableKvp of this._spriteBatchDrawables) {
132
+ for (const samplerDrawableKvp of texSamplerDrawableKvp[1]) {
133
+ samplerDrawableKvp[1].reset();
134
+ }
135
+ }
136
+
137
+ // Clear current texture.
138
+ this._currentTexture = null;
139
+ }
140
+
141
+
142
+ /** @inheritDoc */
143
+
144
+ public drawRect(drawRect: Rect, color: Color, __: number = 0, _: vec2 | undefined = undefined) {
145
+ this._checkIfNewDrawableShouldBeCreated(this._defaultWhiteTexture, this._currentSampler!);
146
+
147
+ this._tempPosition[0] = drawRect.x;
148
+ this._tempPosition[1] = drawRect.y;
149
+ this._tempPosition[2] = 0;
150
+ this._tempSize[0] = drawRect.width;
151
+ this._tempSize[1] = drawRect.height;
152
+
153
+ this._currentSpriteBatchDrawable.writeSprite(
154
+ this._tempPosition, this._tempSize, color
155
+ );
156
+ }
157
+
158
+ /** @inheritDoc */
159
+ public draw(texture: ITexture2D,
160
+ drawRect: Rect,
161
+ color: Color,
162
+ sourceRect?: Rect,
163
+ // @ts-ignore
164
+ rotation: number = 0,
165
+ // @ts-ignore
166
+ rotationOrigin?: vec2,
167
+ layerDepth: number = 0): void {
168
+
169
+ this._validateTexture(texture);
170
+ this._checkIfNewDrawableShouldBeCreated(texture, this._currentSampler!);
171
+
172
+ // Safe to assign current texture.
173
+ this._currentTexture = texture;
174
+
175
+ if (!sourceRect) {
176
+ sourceRect = this._tempSourceRect;
177
+ sourceRect.x = 0;
178
+ sourceRect.y = 0;
179
+ sourceRect.width = texture.width;
180
+ sourceRect.height = texture.height;
181
+ }
182
+
183
+ const u0 = sourceRect.x / texture.width;
184
+ const v0 = sourceRect.y / texture.height;
185
+ const u1 = (sourceRect.x + sourceRect.width) / texture.width;
186
+ const v1 = (sourceRect.y + sourceRect.height) / texture.height;
187
+
188
+ this._tempPosition[0] = drawRect.x;
189
+ this._tempPosition[1] = drawRect.y;
190
+ this._tempPosition[2] = layerDepth;
191
+ this._tempSize[0] = drawRect.width;
192
+ this._tempSize[1] = drawRect.height;
193
+
194
+ this._currentSpriteBatchDrawable.writeSprite(
195
+ this._tempPosition, this._tempSize,
196
+ color,
197
+ u0, v0, u1, v1
198
+ );
199
+ }
200
+
201
+ /** @inheritDoc */
202
+ public end(): void {
203
+ if (this._currentTexture != null) {
204
+ this._currentSpriteBatchDrawable.draw();
205
+ }
206
+ }
207
+
208
+ /** @inheritDoc */
209
+ public frameEnd(): void {
210
+ for (const texSamplerDrawableKvp of this._spriteBatchDrawables) {
211
+ for (const samplerDrawableKvp of texSamplerDrawableKvp[1]) {
212
+ samplerDrawableKvp[1].frameEnd();
213
+ }
214
+ }
215
+ }
216
+ }