-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathabstractEngine.functions.ts
200 lines (192 loc) · 8.43 KB
/
abstractEngine.functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { _WarnImport } from "core/Misc/devTools";
import { IsDocumentAvailable } from "core/Misc/domManagement";
import type { IFileRequest } from "core/Misc/fileRequest";
import type { LoadFileError } from "core/Misc/fileTools";
import type { IWebRequest } from "core/Misc/interfaces/iWebRequest";
import type { WebRequest } from "core/Misc/webRequest";
import type { IOfflineProvider } from "core/Offline/IOfflineProvider";
import type { Nullable } from "core/types";
import { Constants } from "./constants";
export const EngineFunctionContext: {
/**
* Loads a file from a url
* @param url url to load
* @param onSuccess callback called when the file successfully loads
* @param onProgress callback called while file is loading (if the server supports this mode)
* @param offlineProvider defines the offline provider for caching
* @param useArrayBuffer defines a boolean indicating that date must be returned as ArrayBuffer
* @param onError callback called when the file fails to load
* @returns a file request object
* @internal
*/
loadFile?: (
url: string,
onSuccess: (data: string | ArrayBuffer, responseURL?: string) => void,
onProgress?: (ev: ProgressEvent) => void,
offlineProvider?: IOfflineProvider,
useArrayBuffer?: boolean,
onError?: (request?: WebRequest, exception?: LoadFileError) => void
) => IFileRequest;
} = {};
/**
* @internal
*/
export function _ConcatenateShader(source: string, defines: Nullable<string>, shaderVersion: string = ""): string {
return shaderVersion + (defines ? defines + "\n" : "") + source;
}
/**
* @internal
*/
export function _loadFile(
url: string,
onSuccess: (data: string | ArrayBuffer, responseURL?: string) => void,
onProgress?: (data: any) => void,
offlineProvider?: IOfflineProvider,
useArrayBuffer?: boolean,
onError?: (request?: IWebRequest, exception?: any) => void,
injectedLoadFile?: (
url: string,
onSuccess: (data: string | ArrayBuffer, responseURL?: string | undefined) => void,
onProgress?: ((ev: ProgressEvent<EventTarget>) => void) | undefined,
offlineProvider?: IOfflineProvider | undefined,
useArrayBuffer?: boolean | undefined,
onError?: ((request?: WebRequest | undefined, exception?: LoadFileError | undefined) => void) | undefined
) => IFileRequest
): IFileRequest {
const loadFile = injectedLoadFile || EngineFunctionContext.loadFile;
if (loadFile) {
const request = loadFile(url, onSuccess, onProgress, offlineProvider, useArrayBuffer, onError);
return request;
}
throw _WarnImport("FileTools");
}
/**
* Gets host document
* @param renderingCanvas if provided, the canvas' owner document will be returned
* @returns the host document object
*/
export function getHostDocument(renderingCanvas: Nullable<HTMLCanvasElement> = null): Nullable<Document> {
if (renderingCanvas && renderingCanvas.ownerDocument) {
return renderingCanvas.ownerDocument;
}
return IsDocumentAvailable() ? document : null;
}
/** @internal */
export function _getGlobalDefines(
defines?: { [key: string]: string },
isNDCHalfZRange?: boolean,
useReverseDepthBuffer?: boolean,
useExactSrgbConversions?: boolean
): string | undefined {
if (defines) {
if (isNDCHalfZRange) {
defines["IS_NDC_HALF_ZRANGE"] = "";
} else {
delete defines["IS_NDC_HALF_ZRANGE"];
}
if (useReverseDepthBuffer) {
defines["USE_REVERSE_DEPTHBUFFER"] = "";
} else {
delete defines["USE_REVERSE_DEPTHBUFFER"];
}
if (useExactSrgbConversions) {
defines["USE_EXACT_SRGB_CONVERSIONS"] = "";
} else {
delete defines["USE_EXACT_SRGB_CONVERSIONS"];
}
return;
} else {
let s = "";
if (isNDCHalfZRange) {
s += "#define IS_NDC_HALF_ZRANGE";
}
if (useReverseDepthBuffer) {
if (s) {
s += "\n";
}
s += "#define USE_REVERSE_DEPTHBUFFER";
}
if (useExactSrgbConversions) {
if (s) {
s += "\n";
}
s += "#define USE_EXACT_SRGB_CONVERSIONS";
}
return s;
}
}
/**
* Allocate a typed array depending on a texture type. Optionally can copy existing data in the buffer.
* @param type type of the texture
* @param sizeOrDstBuffer size of the array OR an existing buffer that will be used as the destination of the copy (if copyBuffer is provided)
* @param sizeInBytes true if the size of the array is given in bytes, false if it is the number of elements of the array
* @param copyBuffer if provided, buffer to copy into the destination buffer (either a newly allocated buffer if sizeOrDstBuffer is a number or use sizeOrDstBuffer as the destination buffer otherwise)
* @returns the allocated buffer or sizeOrDstBuffer if the latter is an ArrayBuffer
*/
export function allocateAndCopyTypedBuffer(type: number, sizeOrDstBuffer: number | ArrayBuffer, sizeInBytes = false, copyBuffer?: ArrayBuffer): ArrayBufferView {
switch (type) {
case Constants.TEXTURETYPE_BYTE: {
const buffer = sizeOrDstBuffer instanceof ArrayBuffer ? new Int8Array(sizeOrDstBuffer) : new Int8Array(sizeOrDstBuffer);
if (copyBuffer) {
buffer.set(new Int8Array(copyBuffer));
}
return buffer;
}
case Constants.TEXTURETYPE_UNSIGNED_BYTE: {
const buffer = sizeOrDstBuffer instanceof ArrayBuffer ? new Uint8Array(sizeOrDstBuffer) : new Uint8Array(sizeOrDstBuffer);
if (copyBuffer) {
buffer.set(new Uint8Array(copyBuffer));
}
return buffer;
}
case Constants.TEXTURETYPE_SHORT: {
const buffer = sizeOrDstBuffer instanceof ArrayBuffer ? new Int16Array(sizeOrDstBuffer) : new Int16Array(sizeInBytes ? sizeOrDstBuffer / 2 : sizeOrDstBuffer);
if (copyBuffer) {
buffer.set(new Int16Array(copyBuffer));
}
return buffer;
}
case Constants.TEXTURETYPE_UNSIGNED_SHORT:
case Constants.TEXTURETYPE_UNSIGNED_SHORT_4_4_4_4:
case Constants.TEXTURETYPE_UNSIGNED_SHORT_5_5_5_1:
case Constants.TEXTURETYPE_UNSIGNED_SHORT_5_6_5:
case Constants.TEXTURETYPE_HALF_FLOAT: {
const buffer = sizeOrDstBuffer instanceof ArrayBuffer ? new Uint16Array(sizeOrDstBuffer) : new Uint16Array(sizeInBytes ? sizeOrDstBuffer / 2 : sizeOrDstBuffer);
if (copyBuffer) {
buffer.set(new Uint16Array(copyBuffer));
}
return buffer;
}
case Constants.TEXTURETYPE_INT: {
const buffer = sizeOrDstBuffer instanceof ArrayBuffer ? new Int32Array(sizeOrDstBuffer) : new Int32Array(sizeInBytes ? sizeOrDstBuffer / 4 : sizeOrDstBuffer);
if (copyBuffer) {
buffer.set(new Int32Array(copyBuffer));
}
return buffer;
}
case Constants.TEXTURETYPE_UNSIGNED_INTEGER:
case Constants.TEXTURETYPE_UNSIGNED_INT_2_10_10_10_REV:
case Constants.TEXTURETYPE_UNSIGNED_INT_24_8:
case Constants.TEXTURETYPE_UNSIGNED_INT_10F_11F_11F_REV:
case Constants.TEXTURETYPE_UNSIGNED_INT_5_9_9_9_REV:
case Constants.TEXTURETYPE_FLOAT_32_UNSIGNED_INT_24_8_REV: {
const buffer = sizeOrDstBuffer instanceof ArrayBuffer ? new Uint32Array(sizeOrDstBuffer) : new Uint32Array(sizeInBytes ? sizeOrDstBuffer / 4 : sizeOrDstBuffer);
if (copyBuffer) {
buffer.set(new Uint32Array(copyBuffer));
}
return buffer;
}
case Constants.TEXTURETYPE_FLOAT: {
const buffer = sizeOrDstBuffer instanceof ArrayBuffer ? new Float32Array(sizeOrDstBuffer) : new Float32Array(sizeInBytes ? sizeOrDstBuffer / 4 : sizeOrDstBuffer);
if (copyBuffer) {
buffer.set(new Float32Array(copyBuffer));
}
return buffer;
}
}
const buffer = sizeOrDstBuffer instanceof ArrayBuffer ? new Uint8Array(sizeOrDstBuffer) : new Uint8Array(sizeOrDstBuffer);
if (copyBuffer) {
buffer.set(new Uint8Array(copyBuffer));
}
return buffer;
}