@predy-js/render-interface 0.0.6

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 (62) hide show
  1. package/dist/index.js +5545 -0
  2. package/dist/index.js.map +1 -0
  3. package/dist/index.mjs +5524 -0
  4. package/dist/index.mjs.map +1 -0
  5. package/dist/src/common/console.d.ts +3 -0
  6. package/dist/src/common/env.d.ts +1 -0
  7. package/dist/src/common/raf.d.ts +2 -0
  8. package/dist/src/common/request.d.ts +10 -0
  9. package/dist/src/common/utils.d.ts +14 -0
  10. package/dist/src/index.d.ts +17 -0
  11. package/dist/src/render/MarsExtWrap.d.ts +15 -0
  12. package/dist/src/render/MarsGPUCapability.d.ts +2 -0
  13. package/dist/src/render/MarsGeometry.d.ts +64 -0
  14. package/dist/src/render/MarsMaterial.d.ts +29 -0
  15. package/dist/src/render/MarsMaterialDataBlock.d.ts +37 -0
  16. package/dist/src/render/MarsMesh.d.ts +24 -0
  17. package/dist/src/render/MarsRenderFrame.d.ts +22 -0
  18. package/dist/src/render/MarsRenderPass.d.ts +51 -0
  19. package/dist/src/render/MarsRenderPassColorAttachment.d.ts +27 -0
  20. package/dist/src/render/MarsRenderer.d.ts +20 -0
  21. package/dist/src/render/MarsSemanticMap.d.ts +10 -0
  22. package/dist/src/render/MarsSharedGeometry.d.ts +13 -0
  23. package/dist/src/render/MarsTexture.d.ts +29 -0
  24. package/dist/src/render/MarsTextureFactory.d.ts +20 -0
  25. package/dist/src/render/RenderFrameInternal.d.ts +8 -0
  26. package/dist/src/statistic/index.d.ts +20 -0
  27. package/dist/src/webgl/GLFrameBuffer.d.ts +45 -0
  28. package/dist/src/webgl/GLGPUBuffer.d.ts +27 -0
  29. package/dist/src/webgl/GLGPUCapability.d.ts +32 -0
  30. package/dist/src/webgl/GLGPURenderer.d.ts +57 -0
  31. package/dist/src/webgl/GLGeometry.d.ts +52 -0
  32. package/dist/src/webgl/GLMaterial.d.ts +15 -0
  33. package/dist/src/webgl/GLProgram.d.ts +42 -0
  34. package/dist/src/webgl/GLRenderBuffer.d.ts +22 -0
  35. package/dist/src/webgl/GLShaderLibrary.d.ts +33 -0
  36. package/dist/src/webgl/GLTexture.d.ts +35 -0
  37. package/dist/src/webgl/GLUniformUtils.d.ts +42 -0
  38. package/dist/src/webgl/GLVertexArrayObject.d.ts +11 -0
  39. package/dist/src/webgl/KhronosTextureContainer.d.ts +25 -0
  40. package/dist/src/webgl/WebGLState.d.ts +285 -0
  41. package/dist/src/webgl/constants.d.ts +4 -0
  42. package/dist/src/webgl/glType.d.ts +8 -0
  43. package/dist/statistic.js +406 -0
  44. package/dist/statistic.js.map +1 -0
  45. package/dist/types/Camera.d.ts +12 -0
  46. package/dist/types/Canvas.d.ts +12 -0
  47. package/dist/types/GPUBuffer.d.ts +54 -0
  48. package/dist/types/GPUCapability.d.ts +50 -0
  49. package/dist/types/Geometry.d.ts +142 -0
  50. package/dist/types/IGPURenderer.d.ts +14 -0
  51. package/dist/types/Material.d.ts +156 -0
  52. package/dist/types/Mesh.d.ts +50 -0
  53. package/dist/types/RenderFrame.d.ts +50 -0
  54. package/dist/types/RenderPass.d.ts +156 -0
  55. package/dist/types/Renderer.d.ts +52 -0
  56. package/dist/types/Scene.d.ts +48 -0
  57. package/dist/types/ShaderLibrary.d.ts +78 -0
  58. package/dist/types/Texture.d.ts +212 -0
  59. package/dist/types/constants.d.ts +6 -0
  60. package/dist/types/index.d.ts +26 -0
  61. package/dist/types/type.d.ts +37 -0
  62. package/package.json +54 -0
@@ -0,0 +1,78 @@
1
+ export type Shader = ShaderWithSource | ShaderUseCacheId;
2
+ export type ShaderWithSource = InstancedShaderWithSource | SharedShaderWithSource;
3
+ export interface InstancedShaderWithSource {
4
+ fragment: string;
5
+ vertex: string;
6
+ name?: string;
7
+ marcos?: ShaderMarcos;
8
+ shared?: false;
9
+ downgrade?: boolean;
10
+ }
11
+ export interface SharedShaderWithSource {
12
+ fragment: string;
13
+ vertex: string;
14
+ name?: string;
15
+ downgrade?: boolean;
16
+ marcos?: ShaderMarcos;
17
+ cacheId?: string;
18
+ shared?: true;
19
+ }
20
+ export interface ShaderUseCacheId {
21
+ cacheId: string;
22
+ }
23
+ export type ShaderMarcos = [key: string, value: string | number | boolean][];
24
+ export declare enum ShaderCompileResultStatus {
25
+ noShader = 0,
26
+ success = 1,
27
+ fail = 2,
28
+ compiling = 3
29
+ }
30
+ export interface ShaderCompileResult {
31
+ status: ShaderCompileResultStatus;
32
+ cacheId?: string;
33
+ error?: string | null;
34
+ shared?: boolean;
35
+ compileTime?: number;
36
+ }
37
+ /**
38
+ * @example
39
+ * const lib:ShaderLibrary = ...;
40
+ * const shaderCacheId = lib.addShader({fragment:'...',vertex:'...'});
41
+ * lib.compileShader(shaderCacheId);
42
+ * //pre-compiled program
43
+ * const mtl = new Material({
44
+ * shader:{ shaderCacheId }
45
+ * })
46
+ */
47
+ export interface ShaderLibrary {
48
+ readonly shaderResults: {
49
+ [cacheId: string]: ShaderCompileResult;
50
+ };
51
+ addShader(shader: Shader): string;
52
+ /**
53
+ * @param cacheId
54
+ */
55
+ deleteShader(cacheId: string): void;
56
+ /**
57
+ * 编译shader
58
+ * @param shaderCacheId
59
+ * @param asyncCallback 如果传入,则会启用异步编译,当所有编译完成后被回调
60
+ */
61
+ compileShader(shaderCacheId: string, asyncCallback?: (result: ShaderCompileResult) => void): ShaderCompileResult;
62
+ /***
63
+ * 编译现有的所有shader
64
+ * @param asyncCallback 如果传入,则会启用异步编译,当所有编译完成后被回调
65
+ */
66
+ compileAllShaders(asyncCallback?: (results: ShaderCompileResult[]) => void): void;
67
+ destroy(): void;
68
+ }
69
+ export declare class ShaderLibraryEmpty implements ShaderLibrary {
70
+ readonly shaderResults: {
71
+ [cacheId: string]: ShaderCompileResult;
72
+ };
73
+ addShader(shader: Shader, marcos?: ShaderMarcos): string;
74
+ compileAllShaders(asyncCallback?: (results: ShaderCompileResult[]) => void): never[];
75
+ deleteShader(cacheId: string): void;
76
+ compileShader(shaderCacheId: string): ShaderCompileResult;
77
+ destroy(): void;
78
+ }
@@ -0,0 +1,212 @@
1
+ import type { IGPURenderer, IGPUResource } from './IGPURenderer';
2
+ import type { Immutable, TypedArray } from './type';
3
+ import type { GPURenderer } from './Renderer';
4
+ export declare enum TextureSourceType {
5
+ none = 0,
6
+ data = 1,
7
+ image = 2,
8
+ compressed = 3,
9
+ video = 4,
10
+ canvas = 5,
11
+ framebuffer = 6,
12
+ mipmaps = 7
13
+ }
14
+ export interface TextureFactorySource2DFrom {
15
+ type: TextureSourceType.image | TextureSourceType.video;
16
+ url: string;
17
+ target?: WebGLRenderingContext['TEXTURE_2D'];
18
+ }
19
+ export interface TextureFactorySourceFromCompressed {
20
+ type: TextureSourceType.compressed;
21
+ url: string;
22
+ }
23
+ export type TextureFactorySourceCubeFrom = {
24
+ type: TextureSourceType.image;
25
+ map: TextureCubeSourceURLMap | string[];
26
+ target: WebGLRenderingContext['TEXTURE_CUBE_MAP'];
27
+ };
28
+ type DataRange = [start: number, length: number];
29
+ export type TextureFactorySourceCubeBinaryMipmapsFrom = {
30
+ target: WebGLRenderingContext['TEXTURE_CUBE_MAP'];
31
+ type: TextureSourceType.mipmaps;
32
+ bin: string;
33
+ mipmaps: Array<[
34
+ TEXTURE_CUBE_MAP_POSITIVE_X: DataRange,
35
+ TEXTURE_CUBE_MAP_NEGATIVE_X: DataRange,
36
+ TEXTURE_CUBE_MAP_POSITIVE_Y: DataRange,
37
+ TEXTURE_CUBE_MAP_NEGATIVE_Y: DataRange,
38
+ TEXTURE_CUBE_MAP_POSITIVE_Z: DataRange,
39
+ TEXTURE_CUBE_MAP_NEGATIVE_Z: DataRange
40
+ ]>;
41
+ };
42
+ export type TextureFactorySource2DBinaryMipmapsFrom = {
43
+ target?: WebGLRenderingContext['TEXTURE_2D'];
44
+ type: TextureSourceType.mipmaps;
45
+ bin: string;
46
+ mipmaps: Array<DataRange>;
47
+ };
48
+ export interface TextureFactorySourceCubeMipmapsFrom {
49
+ type: TextureSourceType.mipmaps;
50
+ target: WebGLRenderingContext['TEXTURE_CUBE_MAP'];
51
+ maps: TextureCubeSourceURLMap[] | string[][];
52
+ }
53
+ export interface TextureFactorySource2DMipmapsFrom {
54
+ type: TextureSourceType.mipmaps;
55
+ target?: WebGLRenderingContext['TEXTURE_2D'];
56
+ urls: string[];
57
+ }
58
+ export type TextureFactorySourceFrom = TextureFactorySource2DBinaryMipmapsFrom | TextureFactorySourceCubeBinaryMipmapsFrom | TextureFactorySource2DFrom | TextureFactorySourceCubeFrom | TextureFactorySourceCubeMipmapsFrom | TextureFactorySource2DMipmapsFrom | TextureFactorySourceFromCompressed;
59
+ export interface TextureFactory {
60
+ loadSourceAsync(options: TextureFactorySourceFrom, config?: TextureConfigOptions): Promise<TextureOptions>;
61
+ canOffloadTexture(texture: Texture): boolean;
62
+ reloadTextureAsync(texture: Texture): Promise<Texture>;
63
+ loadCompressedTextureFromArrayBuffer(arrayBuffer: ArrayBuffer, options?: TextureOptions): TextureOptions;
64
+ }
65
+ export declare class Texture implements IGPUResource {
66
+ readonly width: number;
67
+ readonly height: number;
68
+ readonly id: string;
69
+ readonly options: Immutable<TextureOptions>;
70
+ readonly renderer?: IGPURenderer;
71
+ readonly sourceType: TextureSourceType;
72
+ readonly name: string;
73
+ readonly isDestroyed: boolean;
74
+ constructor(options: TextureOptions, renderer?: IGPURenderer);
75
+ destroy(): void;
76
+ updateSource(options: TextureSourceOptions): void;
77
+ uploadCurrentVideoFrame(): boolean;
78
+ offloadData(): void;
79
+ reloadDataAsync(): Promise<Texture>;
80
+ assignRenderer(renderer: GPURenderer): Texture;
81
+ }
82
+ type ImageSource = ImageBitmap | HTMLImageElement | HTMLCanvasElement;
83
+ export type TextureSourceCubeData = [
84
+ TEXTURE_CUBE_MAP_POSITIVE_X: ImageSource | {
85
+ data: TypedArray;
86
+ width: number;
87
+ height: number;
88
+ },
89
+ TEXTURE_CUBE_MAP_NEGATIVE_X: ImageSource | {
90
+ data: TypedArray;
91
+ width: number;
92
+ height: number;
93
+ },
94
+ TEXTURE_CUBE_MAP_POSITIVE_Y: ImageSource | {
95
+ data: TypedArray;
96
+ width: number;
97
+ height: number;
98
+ },
99
+ TEXTURE_CUBE_MAP_NEGATIVE_Y: ImageSource | {
100
+ data: TypedArray;
101
+ width: number;
102
+ height: number;
103
+ },
104
+ TEXTURE_CUBE_MAP_POSITIVE_Z: ImageSource | {
105
+ data: TypedArray;
106
+ width: number;
107
+ height: number;
108
+ },
109
+ TEXTURE_CUBE_MAP_NEGATIVE_Z: ImageSource | {
110
+ data: TypedArray;
111
+ width: number;
112
+ height: number;
113
+ }
114
+ ];
115
+ export type TextureSourceOptions = Texture2DSourceOptions | TextureCubeSourceOptions;
116
+ export interface Texture2DSourceOptionsNone extends TextureOptionsBase {
117
+ sourceType?: TextureSourceType.none;
118
+ target?: GLenum;
119
+ }
120
+ export interface Texture2DSourceOptionsImage extends TextureOptionsBase {
121
+ sourceType?: TextureSourceType.image;
122
+ image: ImageSource;
123
+ target?: WebGLRenderingContext['TEXTURE_2D'];
124
+ generateMipmap?: boolean;
125
+ }
126
+ export interface Texture2DSourceOptionsFrameBuffer extends TextureOptionsBase {
127
+ sourceType: TextureSourceType.framebuffer;
128
+ data?: {
129
+ width: number;
130
+ height: number;
131
+ };
132
+ target?: WebGLRenderingContext['TEXTURE_2D'];
133
+ }
134
+ export interface Texture2DSourceOptionsData extends TextureOptionsBase {
135
+ sourceType?: TextureSourceType.data;
136
+ data: {
137
+ data: TypedArray;
138
+ width: number;
139
+ height: number;
140
+ };
141
+ target?: WebGLRenderingContext['TEXTURE_2D'];
142
+ }
143
+ export interface Texture2DSourceOptionsVideo extends TextureOptionsBase {
144
+ sourceType?: TextureSourceType.video;
145
+ video: HTMLVideoElement;
146
+ target?: WebGLRenderingContext['TEXTURE_2D'];
147
+ generateMipmap?: boolean;
148
+ }
149
+ export interface Texture2DSourceOptionsImageMipmaps extends TextureOptionsBase {
150
+ sourceType?: TextureSourceType.mipmaps;
151
+ mipmaps: Array<ImageSource | {
152
+ data: TypedArray;
153
+ width: number;
154
+ height: number;
155
+ }>;
156
+ target?: WebGLRenderingContext['TEXTURE_2D'];
157
+ }
158
+ export interface Texture2DSourceOptionsCompressed extends TextureOptionsBase {
159
+ sourceType?: TextureSourceType.compressed;
160
+ mipmaps: Array<{
161
+ data: TypedArray;
162
+ width: number;
163
+ height: number;
164
+ }>;
165
+ target?: WebGLRenderingContext['TEXTURE_2D'];
166
+ }
167
+ export interface TextureCubeSourceOptionsImage extends TextureOptionsBase {
168
+ sourceType: TextureSourceType.image | TextureSourceType.data;
169
+ target: WebGLRenderingContext['TEXTURE_CUBE_MAP'];
170
+ cube: TextureSourceCubeData;
171
+ generateMipmap?: boolean;
172
+ }
173
+ export interface TextureCubeSourceOptionsImageMipmaps extends TextureOptionsBase {
174
+ sourceType?: TextureSourceType.mipmaps;
175
+ mipmaps: Array<TextureSourceCubeData>;
176
+ target: WebGLRenderingContext['TEXTURE_CUBE_MAP'];
177
+ }
178
+ export type TextureCubeSourceURLMap = [
179
+ TEXTURE_CUBE_MAP_POSITIVE_X: string,
180
+ TEXTURE_CUBE_MAP_NEGATIVE_X: string,
181
+ TEXTURE_CUBE_MAP_POSITIVE_Y: string,
182
+ TEXTURE_CUBE_MAP_NEGATIVE_Y: string,
183
+ TEXTURE_CUBE_MAP_POSITIVE_Z: string,
184
+ TEXTURE_CUBE_MAP_NEGATIVE_Z: string
185
+ ];
186
+ export interface TextureConfigOptionsBase {
187
+ name?: string;
188
+ wrapS?: GLenum;
189
+ wrapT?: GLenum;
190
+ magFilter?: GLenum;
191
+ minFilter?: GLenum;
192
+ anisotropic?: number;
193
+ flipY?: boolean;
194
+ premultiplyAlpha?: boolean;
195
+ keepImageSource?: boolean;
196
+ }
197
+ export interface TextureConfigOptions extends TextureConfigOptionsBase {
198
+ sourceFrom?: TextureFactorySourceFrom;
199
+ }
200
+ export interface TextureFormatOptions {
201
+ format?: GLenum;
202
+ internalFormat?: GLenum;
203
+ type?: GLenum;
204
+ }
205
+ export interface TextureOptionsBase extends TextureConfigOptions, TextureFormatOptions {
206
+ }
207
+ export type TextureCubeSourceOptions = TextureCubeSourceOptionsImage | TextureCubeSourceOptionsImageMipmaps;
208
+ export type Texture2DSourceOptions = Texture2DSourceOptionsImage | Texture2DSourceOptionsData | Texture2DSourceOptionsVideo | Texture2DSourceOptionsImageMipmaps | Texture2DSourceOptionsCompressed | Texture2DSourceOptionsFrameBuffer;
209
+ export type Texture2DOptions = Texture2DSourceOptions;
210
+ export type TextureCubeOptions = TextureCubeSourceOptions;
211
+ export type TextureOptions = Texture2DOptions | TextureCubeOptions;
212
+ export {};
@@ -0,0 +1,6 @@
1
+ export declare const constants: WebGL2RenderingContext;
2
+ export declare enum DestroyOptions {
3
+ destroy = 0,
4
+ keep = 1,
5
+ force = 0
6
+ }
@@ -0,0 +1,26 @@
1
+ import type { TextureFactory, Texture, TextureConfigOptions, TextureOptions, TextureFactorySourceFrom } from './Texture';
2
+ import type { GPUCapability } from './GPUCapability';
3
+ import type { TypedArray } from './type';
4
+ export * from './constants';
5
+ export * from './GPUCapability';
6
+ export * from './Renderer';
7
+ export * from './RenderFrame';
8
+ export * from './Texture';
9
+ export * from './Material';
10
+ export * from './GPUBuffer';
11
+ export * from './RenderPass';
12
+ export * from './Mesh';
13
+ export * from './Geometry';
14
+ export * from './ShaderLibrary';
15
+ export { GPURenderer as Renderer } from './Renderer';
16
+ export * from './type';
17
+ export declare function getDefaultTextureFactory(): TextureFactory;
18
+ export declare function setDefaultTextureFactory(factory: TextureFactory): void;
19
+ export declare function getDefaultGPUCapability(): GPUCapability;
20
+ export declare class MarsTextureFactory implements TextureFactory {
21
+ canOffloadTexture(texture: Texture): boolean;
22
+ loadSourceAsync(options: TextureFactorySourceFrom, config?: TextureConfigOptions): Promise<TextureOptions>;
23
+ reloadTextureAsync(texture: Texture): Promise<Texture>;
24
+ loadCompressedTextureFromArrayBuffer(arrayBuffer: ArrayBuffer, options?: TextureOptions): TextureOptions;
25
+ loadImageBinaryAsync(binary: ArrayBuffer | TypedArray | Blob, mime?: string): Promise<HTMLImageElement | ImageBitmap>;
26
+ }
@@ -0,0 +1,37 @@
1
+ export type Immutable<O> = O extends Record<any, any> ? {
2
+ readonly [key in keyof O]: Immutable<O[key]>;
3
+ } : O extends Array<infer X> ? ReadonlyArray<X> : O;
4
+ export type vec4 = [x: number, y: number, z: number, w: number];
5
+ export type vec3 = [x: number, y: number, z: number];
6
+ export type vec2 = [x: number, y: number];
7
+ export type mat2 = [m11: number, m12: number, m21: number, m22: number];
8
+ export type mat3 = [
9
+ m11: number,
10
+ m12: number,
11
+ m13: number,
12
+ m21: number,
13
+ m22: number,
14
+ m23: number,
15
+ m31: number,
16
+ m32: number,
17
+ m33: number
18
+ ];
19
+ export type mat4 = [
20
+ m11: number,
21
+ m12: number,
22
+ m13: number,
23
+ m14: number,
24
+ m21: number,
25
+ m22: number,
26
+ m23: number,
27
+ m24: number,
28
+ m31: number,
29
+ m32: number,
30
+ m33: number,
31
+ m34: number,
32
+ m41: number,
33
+ m42: number,
34
+ m43: number,
35
+ m44: number
36
+ ];
37
+ export type TypedArray = Float32Array | Float64Array | Uint8Array | Uint32Array | Uint16Array | Int8Array | Int16Array | Int32Array;
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@predy-js/render-interface",
3
+ "version": "0.0.6",
4
+ "license": "MIT",
5
+ "module": "./dist/index.mjs",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/types/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.js",
15
+ "types": "./dist/types/index.d.ts"
16
+ },
17
+ "./statistic": "./dist/statistic.js"
18
+ },
19
+ "scripts": {
20
+ "dev": "rollup -c --watch",
21
+ "prebuild": "npm run clean:all",
22
+ "build": "npm run build:dist",
23
+ "build:dist": "rollup -c",
24
+ "lint": "eslint {src,types,test,demo} --ext .ts",
25
+ "lint:fix": "eslint {src,types,test,demo} --fix --quiet --ext .ts",
26
+ "check:ts": "tsc -b ./tsconfig.check.json",
27
+ "clean:all": "npm run clean:dist",
28
+ "clean:dist": "rm -rf dist/**",
29
+ "test": "rollup -c --watch --test",
30
+ "prepublishOnly": "npm run build"
31
+ },
32
+ "devDependencies": {
33
+ "@commitlint/cli": "^13.2.1",
34
+ "@commitlint/config-conventional": "^13.2.0",
35
+ "@rollup/plugin-commonjs": "^21.0.3",
36
+ "@rollup/plugin-node-resolve": "^13.1.3",
37
+ "@rollup/plugin-replace": "^5.0.0",
38
+ "@rollup/plugin-typescript": "^8.3.1",
39
+ "@types/chai": "^4.3.0",
40
+ "@types/chai-spies": "^1.0.3",
41
+ "@types/mocha": "^9.0.0",
42
+ "@types/webgl2": "0.0.6",
43
+ "@typescript-eslint/eslint-plugin": "^5.26.0",
44
+ "@typescript-eslint/parser": "^5.26.0",
45
+ "assert": "^2.0.0",
46
+ "chai": "^4.3.6",
47
+ "chai-spies": "^1.0.0",
48
+ "eslint": "^8.13.0",
49
+ "eslint-plugin-promise": "^5.2.0",
50
+ "husky": "^7.0.4",
51
+ "lint-staged": "^11.2.6",
52
+ "meshoptimizer": "^0.18.1"
53
+ }
54
+ }