nullEngine.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. import { Logger } from "../Misc/logger";
  2. import { Nullable, FloatArray, IndicesArray } from "../types";
  3. import { Engine } from "../Engines/engine";
  4. import { RenderTargetCreationOptions } from "../Materials/Textures/renderTargetCreationOptions";
  5. import { VertexBuffer } from "../Meshes/buffer";
  6. import { InternalTexture, InternalTextureSource } from "../Materials/Textures/internalTexture";
  7. import { Effect } from "../Materials/effect";
  8. import { Constants } from "./constants";
  9. import { IPipelineContext } from './IPipelineContext';
  10. import { DataBuffer } from '../Meshes/dataBuffer';
  11. import { IColor4Like, IViewportLike } from '../Maths/math.like';
  12. import { ISceneLike } from './thinEngine';
  13. import { PerformanceConfigurator } from './performanceConfigurator';
  14. declare const global: any;
  15. /**
  16. * Options to create the null engine
  17. */
  18. export class NullEngineOptions {
  19. /**
  20. * Render width (Default: 512)
  21. */
  22. public renderWidth = 512;
  23. /**
  24. * Render height (Default: 256)
  25. */
  26. public renderHeight = 256;
  27. /**
  28. * Texture size (Default: 512)
  29. */
  30. public textureSize = 512;
  31. /**
  32. * If delta time between frames should be constant
  33. * @see https://doc.babylonjs.com/babylon101/animations#deterministic-lockstep
  34. */
  35. public deterministicLockstep = false;
  36. /**
  37. * Maximum about of steps between frames (Default: 4)
  38. * @see https://doc.babylonjs.com/babylon101/animations#deterministic-lockstep
  39. */
  40. public lockstepMaxSteps = 4;
  41. /**
  42. * Make the matrix computations to be performed in 64 bits instead of 32 bits. False by default
  43. */
  44. useHighPrecisionMatrix?: boolean;
  45. }
  46. /**
  47. * The null engine class provides support for headless version of babylon.js.
  48. * This can be used in server side scenario or for testing purposes
  49. */
  50. export class NullEngine extends Engine {
  51. private _options: NullEngineOptions;
  52. /**
  53. * Gets a boolean indicating that the engine is running in deterministic lock step mode
  54. * @see https://doc.babylonjs.com/babylon101/animations#deterministic-lockstep
  55. * @returns true if engine is in deterministic lock step mode
  56. */
  57. public isDeterministicLockStep(): boolean {
  58. return this._options.deterministicLockstep;
  59. }
  60. /**
  61. * Gets the max steps when engine is running in deterministic lock step
  62. * @see https://doc.babylonjs.com/babylon101/animations#deterministic-lockstep
  63. * @returns the max steps
  64. */
  65. public getLockstepMaxSteps(): number {
  66. return this._options.lockstepMaxSteps;
  67. }
  68. /**
  69. * Gets the current hardware scaling level.
  70. * By default the hardware scaling level is computed from the window device ratio.
  71. * if level = 1 then the engine will render at the exact resolution of the canvas. If level = 0.5 then the engine will render at twice the size of the canvas.
  72. * @returns a number indicating the current hardware scaling level
  73. */
  74. public getHardwareScalingLevel(): number {
  75. return 1.0;
  76. }
  77. public constructor(options: NullEngineOptions = new NullEngineOptions()) {
  78. super(null);
  79. Engine.Instances.push(this);
  80. if (options.deterministicLockstep === undefined) {
  81. options.deterministicLockstep = false;
  82. }
  83. if (options.lockstepMaxSteps === undefined) {
  84. options.lockstepMaxSteps = 4;
  85. }
  86. this._options = options;
  87. PerformanceConfigurator.SetMatrixPrecision(!!options.useHighPrecisionMatrix);
  88. // Init caps
  89. // We consider we are on a webgl1 capable device
  90. this._caps = {
  91. maxTexturesImageUnits: 16,
  92. maxVertexTextureImageUnits: 16,
  93. maxCombinedTexturesImageUnits: 32,
  94. maxTextureSize: 512,
  95. maxCubemapTextureSize: 512,
  96. maxRenderTextureSize: 512,
  97. maxVertexAttribs: 16,
  98. maxVaryingVectors: 16,
  99. maxFragmentUniformVectors: 16,
  100. maxVertexUniformVectors: 16,
  101. standardDerivatives: false,
  102. astc: null,
  103. pvrtc: null,
  104. etc1: null,
  105. etc2: null,
  106. bptc: null,
  107. maxAnisotropy: 0,
  108. uintIndices: false,
  109. fragmentDepthSupported: false,
  110. highPrecisionShaderSupported: true,
  111. colorBufferFloat: false,
  112. textureFloat: false,
  113. textureFloatLinearFiltering: false,
  114. textureFloatRender: false,
  115. textureHalfFloat: false,
  116. textureHalfFloatLinearFiltering: false,
  117. textureHalfFloatRender: false,
  118. textureLOD: false,
  119. drawBuffersExtension: false,
  120. depthTextureExtension: false,
  121. vertexArrayObject: false,
  122. instancedArrays: false,
  123. canUseTimestampForTimerQuery: false,
  124. maxMSAASamples: 1,
  125. blendMinMax: false
  126. };
  127. Logger.Log(`Babylon.js v${Engine.Version} - Null engine`);
  128. // Wrappers
  129. const theCurrentGlobal = (typeof self !== "undefined" ? self : typeof global !== "undefined" ? global : window);
  130. if (typeof URL === "undefined") {
  131. theCurrentGlobal.URL = {
  132. createObjectURL: function() { },
  133. revokeObjectURL: function() { }
  134. };
  135. }
  136. if (typeof Blob === "undefined") {
  137. theCurrentGlobal.Blob = function() { };
  138. }
  139. }
  140. /**
  141. * Creates a vertex buffer
  142. * @param vertices the data for the vertex buffer
  143. * @returns the new WebGL static buffer
  144. */
  145. public createVertexBuffer(vertices: FloatArray): DataBuffer {
  146. let buffer = new DataBuffer();
  147. buffer.references = 1;
  148. return buffer;
  149. }
  150. /**
  151. * Creates a new index buffer
  152. * @param indices defines the content of the index buffer
  153. * @param updatable defines if the index buffer must be updatable
  154. * @returns a new webGL buffer
  155. */
  156. public createIndexBuffer(indices: IndicesArray): DataBuffer {
  157. let buffer = new DataBuffer();
  158. buffer.references = 1;
  159. return buffer;
  160. }
  161. /**
  162. * Clear the current render buffer or the current render target (if any is set up)
  163. * @param color defines the color to use
  164. * @param backBuffer defines if the back buffer must be cleared
  165. * @param depth defines if the depth buffer must be cleared
  166. * @param stencil defines if the stencil buffer must be cleared
  167. */
  168. public clear(color: IColor4Like, backBuffer: boolean, depth: boolean, stencil: boolean = false): void {
  169. }
  170. /**
  171. * Gets the current render width
  172. * @param useScreen defines if screen size must be used (or the current render target if any)
  173. * @returns a number defining the current render width
  174. */
  175. public getRenderWidth(useScreen = false): number {
  176. if (!useScreen && this._currentRenderTarget) {
  177. return this._currentRenderTarget.width;
  178. }
  179. return this._options.renderWidth;
  180. }
  181. /**
  182. * Gets the current render height
  183. * @param useScreen defines if screen size must be used (or the current render target if any)
  184. * @returns a number defining the current render height
  185. */
  186. public getRenderHeight(useScreen = false): number {
  187. if (!useScreen && this._currentRenderTarget) {
  188. return this._currentRenderTarget.height;
  189. }
  190. return this._options.renderHeight;
  191. }
  192. /**
  193. * Set the WebGL's viewport
  194. * @param viewport defines the viewport element to be used
  195. * @param requiredWidth defines the width required for rendering. If not provided the rendering canvas' width is used
  196. * @param requiredHeight defines the height required for rendering. If not provided the rendering canvas' height is used
  197. */
  198. public setViewport(viewport: IViewportLike, requiredWidth?: number, requiredHeight?: number): void {
  199. this._cachedViewport = viewport;
  200. }
  201. public createShaderProgram(pipelineContext: IPipelineContext, vertexCode: string, fragmentCode: string, defines: string, context?: WebGLRenderingContext): WebGLProgram {
  202. return {
  203. __SPECTOR_rebuildProgram: null,
  204. };
  205. }
  206. /**
  207. * Gets the list of webGL uniform locations associated with a specific program based on a list of uniform names
  208. * @param pipelineContext defines the pipeline context to use
  209. * @param uniformsNames defines the list of uniform names
  210. * @returns an array of webGL uniform locations
  211. */
  212. public getUniforms(pipelineContext: IPipelineContext, uniformsNames: string[]): Nullable<WebGLUniformLocation>[] {
  213. return [];
  214. }
  215. /**
  216. * Gets the lsit of active attributes for a given webGL program
  217. * @param pipelineContext defines the pipeline context to use
  218. * @param attributesNames defines the list of attribute names to get
  219. * @returns an array of indices indicating the offset of each attribute
  220. */
  221. public getAttributes(pipelineContext: IPipelineContext, attributesNames: string[]): number[] {
  222. return [];
  223. }
  224. /**
  225. * Binds an effect to the webGL context
  226. * @param effect defines the effect to bind
  227. */
  228. public bindSamplers(effect: Effect): void {
  229. this._currentEffect = null;
  230. }
  231. /**
  232. * Activates an effect, mkaing it the current one (ie. the one used for rendering)
  233. * @param effect defines the effect to activate
  234. */
  235. public enableEffect(effect: Effect): void {
  236. this._currentEffect = effect;
  237. if (effect.onBind) {
  238. effect.onBind(effect);
  239. }
  240. if (effect._onBindObservable) {
  241. effect._onBindObservable.notifyObservers(effect);
  242. }
  243. }
  244. /**
  245. * Set various states to the webGL context
  246. * @param culling defines backface culling state
  247. * @param zOffset defines the value to apply to zOffset (0 by default)
  248. * @param force defines if states must be applied even if cache is up to date
  249. * @param reverseSide defines if culling must be reversed (CCW instead of CW and CW instead of CCW)
  250. */
  251. public setState(culling: boolean, zOffset: number = 0, force?: boolean, reverseSide = false): void {
  252. }
  253. /**
  254. * Set the value of an uniform to an array of int32
  255. * @param uniform defines the webGL uniform location where to store the value
  256. * @param array defines the array of int32 to store
  257. * @returns true if value was set
  258. */
  259. public setIntArray(uniform: WebGLUniformLocation, array: Int32Array): boolean {
  260. return true;
  261. }
  262. /**
  263. * Set the value of an uniform to an array of int32 (stored as vec2)
  264. * @param uniform defines the webGL uniform location where to store the value
  265. * @param array defines the array of int32 to store
  266. * @returns true if value was set
  267. */
  268. public setIntArray2(uniform: WebGLUniformLocation, array: Int32Array): boolean {
  269. return true;
  270. }
  271. /**
  272. * Set the value of an uniform to an array of int32 (stored as vec3)
  273. * @param uniform defines the webGL uniform location where to store the value
  274. * @param array defines the array of int32 to store
  275. * @returns true if value was set
  276. */
  277. public setIntArray3(uniform: WebGLUniformLocation, array: Int32Array): boolean {
  278. return true;
  279. }
  280. /**
  281. * Set the value of an uniform to an array of int32 (stored as vec4)
  282. * @param uniform defines the webGL uniform location where to store the value
  283. * @param array defines the array of int32 to store
  284. * @returns true if value was set
  285. */
  286. public setIntArray4(uniform: WebGLUniformLocation, array: Int32Array): boolean {
  287. return true;
  288. }
  289. /**
  290. * Set the value of an uniform to an array of float32
  291. * @param uniform defines the webGL uniform location where to store the value
  292. * @param array defines the array of float32 to store
  293. * @returns true if value was set
  294. */
  295. public setFloatArray(uniform: WebGLUniformLocation, array: Float32Array): boolean {
  296. return true;
  297. }
  298. /**
  299. * Set the value of an uniform to an array of float32 (stored as vec2)
  300. * @param uniform defines the webGL uniform location where to store the value
  301. * @param array defines the array of float32 to store
  302. * @returns true if value was set
  303. */
  304. public setFloatArray2(uniform: WebGLUniformLocation, array: Float32Array): boolean {
  305. return true;
  306. }
  307. /**
  308. * Set the value of an uniform to an array of float32 (stored as vec3)
  309. * @param uniform defines the webGL uniform location where to store the value
  310. * @param array defines the array of float32 to store
  311. * @returns true if value was set
  312. */
  313. public setFloatArray3(uniform: WebGLUniformLocation, array: Float32Array): boolean {
  314. return true;
  315. }
  316. /**
  317. * Set the value of an uniform to an array of float32 (stored as vec4)
  318. * @param uniform defines the webGL uniform location where to store the value
  319. * @param array defines the array of float32 to store
  320. * @returns true if value was set
  321. */
  322. public setFloatArray4(uniform: WebGLUniformLocation, array: Float32Array): boolean {
  323. return true;
  324. }
  325. /**
  326. * Set the value of an uniform to an array of number
  327. * @param uniform defines the webGL uniform location where to store the value
  328. * @param array defines the array of number to store
  329. * @returns true if value was set
  330. */
  331. public setArray(uniform: WebGLUniformLocation, array: number[]): boolean {
  332. return true;
  333. }
  334. /**
  335. * Set the value of an uniform to an array of number (stored as vec2)
  336. * @param uniform defines the webGL uniform location where to store the value
  337. * @param array defines the array of number to store
  338. * @returns true if value was set
  339. */
  340. public setArray2(uniform: WebGLUniformLocation, array: number[]): boolean {
  341. return true;
  342. }
  343. /**
  344. * Set the value of an uniform to an array of number (stored as vec3)
  345. * @param uniform defines the webGL uniform location where to store the value
  346. * @param array defines the array of number to store
  347. * @returns true if value was set
  348. */
  349. public setArray3(uniform: WebGLUniformLocation, array: number[]): boolean {
  350. return true;
  351. }
  352. /**
  353. * Set the value of an uniform to an array of number (stored as vec4)
  354. * @param uniform defines the webGL uniform location where to store the value
  355. * @param array defines the array of number to store
  356. * @returns true if value was set
  357. */
  358. public setArray4(uniform: WebGLUniformLocation, array: number[]): boolean {
  359. return true;
  360. }
  361. /**
  362. * Set the value of an uniform to an array of float32 (stored as matrices)
  363. * @param uniform defines the webGL uniform location where to store the value
  364. * @param matrices defines the array of float32 to store
  365. * @returns true if value was set
  366. */
  367. public setMatrices(uniform: WebGLUniformLocation, matrices: Float32Array): boolean {
  368. return true;
  369. }
  370. /**
  371. * Set the value of an uniform to a matrix (3x3)
  372. * @param uniform defines the webGL uniform location where to store the value
  373. * @param matrix defines the Float32Array representing the 3x3 matrix to store
  374. * @returns true if value was set
  375. */
  376. public setMatrix3x3(uniform: WebGLUniformLocation, matrix: Float32Array): boolean {
  377. return true;
  378. }
  379. /**
  380. * Set the value of an uniform to a matrix (2x2)
  381. * @param uniform defines the webGL uniform location where to store the value
  382. * @param matrix defines the Float32Array representing the 2x2 matrix to store
  383. * @returns true if value was set
  384. */
  385. public setMatrix2x2(uniform: WebGLUniformLocation, matrix: Float32Array): boolean {
  386. return true;
  387. }
  388. /**
  389. * Set the value of an uniform to a number (float)
  390. * @param uniform defines the webGL uniform location where to store the value
  391. * @param value defines the float number to store
  392. * @returns true if value was set
  393. */
  394. public setFloat(uniform: WebGLUniformLocation, value: number): boolean {
  395. return true;
  396. }
  397. /**
  398. * Set the value of an uniform to a vec2
  399. * @param uniform defines the webGL uniform location where to store the value
  400. * @param x defines the 1st component of the value
  401. * @param y defines the 2nd component of the value
  402. * @returns true if value was set
  403. */
  404. public setFloat2(uniform: WebGLUniformLocation, x: number, y: number): boolean {
  405. return true;
  406. }
  407. /**
  408. * Set the value of an uniform to a vec3
  409. * @param uniform defines the webGL uniform location where to store the value
  410. * @param x defines the 1st component of the value
  411. * @param y defines the 2nd component of the value
  412. * @param z defines the 3rd component of the value
  413. * @returns true if value was set
  414. */
  415. public setFloat3(uniform: WebGLUniformLocation, x: number, y: number, z: number): boolean {
  416. return true;
  417. }
  418. /**
  419. * Set the value of an uniform to a boolean
  420. * @param uniform defines the webGL uniform location where to store the value
  421. * @param bool defines the boolean to store
  422. * @returns true if value was set
  423. */
  424. public setBool(uniform: WebGLUniformLocation, bool: number): boolean {
  425. return true;
  426. }
  427. /**
  428. * Set the value of an uniform to a vec4
  429. * @param uniform defines the webGL uniform location where to store the value
  430. * @param x defines the 1st component of the value
  431. * @param y defines the 2nd component of the value
  432. * @param z defines the 3rd component of the value
  433. * @param w defines the 4th component of the value
  434. * @returns true if value was set
  435. */
  436. public setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): boolean {
  437. return true;
  438. }
  439. /**
  440. * Sets the current alpha mode
  441. * @param mode defines the mode to use (one of the Engine.ALPHA_XXX)
  442. * @param noDepthWriteChange defines if depth writing state should remains unchanged (false by default)
  443. * @see https://doc.babylonjs.com/resources/transparency_and_how_meshes_are_rendered
  444. */
  445. public setAlphaMode(mode: number, noDepthWriteChange: boolean = false): void {
  446. if (this._alphaMode === mode) {
  447. return;
  448. }
  449. this.alphaState.alphaBlend = (mode !== Constants.ALPHA_DISABLE);
  450. if (!noDepthWriteChange) {
  451. this.setDepthWrite(mode === Constants.ALPHA_DISABLE);
  452. }
  453. this._alphaMode = mode;
  454. }
  455. /**
  456. * Bind webGl buffers directly to the webGL context
  457. * @param vertexBuffers defines the vertex buffer to bind
  458. * @param indexBuffer defines the index buffer to bind
  459. * @param vertexDeclaration defines the vertex declaration to use with the vertex buffer
  460. * @param vertexStrideSize defines the vertex stride of the vertex buffer
  461. * @param effect defines the effect associated with the vertex buffer
  462. */
  463. public bindBuffers(vertexBuffers: { [key: string]: VertexBuffer; }, indexBuffer: DataBuffer, effect: Effect): void {
  464. }
  465. /**
  466. * Force the entire cache to be cleared
  467. * You should not have to use this function unless your engine needs to share the webGL context with another engine
  468. * @param bruteForce defines a boolean to force clearing ALL caches (including stencil, detoh and alpha states)
  469. */
  470. public wipeCaches(bruteForce?: boolean): void {
  471. if (this.preventCacheWipeBetweenFrames) {
  472. return;
  473. }
  474. this.resetTextureCache();
  475. this._currentEffect = null;
  476. if (bruteForce) {
  477. this._currentProgram = null;
  478. this.stencilState.reset();
  479. this.depthCullingState.reset();
  480. this.alphaState.reset();
  481. }
  482. this._cachedVertexBuffers = null;
  483. this._cachedIndexBuffer = null;
  484. this._cachedEffectForVertexBuffers = null;
  485. }
  486. /**
  487. * Send a draw order
  488. * @param useTriangles defines if triangles must be used to draw (else wireframe will be used)
  489. * @param indexStart defines the starting index
  490. * @param indexCount defines the number of index to draw
  491. * @param instancesCount defines the number of instances to draw (if instanciation is enabled)
  492. */
  493. public draw(useTriangles: boolean, indexStart: number, indexCount: number, instancesCount?: number): void {
  494. }
  495. /**
  496. * Draw a list of indexed primitives
  497. * @param fillMode defines the primitive to use
  498. * @param indexStart defines the starting index
  499. * @param indexCount defines the number of index to draw
  500. * @param instancesCount defines the number of instances to draw (if instanciation is enabled)
  501. */
  502. public drawElementsType(fillMode: number, indexStart: number, indexCount: number, instancesCount?: number): void {
  503. }
  504. /**
  505. * Draw a list of unindexed primitives
  506. * @param fillMode defines the primitive to use
  507. * @param verticesStart defines the index of first vertex to draw
  508. * @param verticesCount defines the count of vertices to draw
  509. * @param instancesCount defines the number of instances to draw (if instanciation is enabled)
  510. */
  511. public drawArraysType(fillMode: number, verticesStart: number, verticesCount: number, instancesCount?: number): void {
  512. }
  513. /** @hidden */
  514. public _createTexture(): WebGLTexture {
  515. return {};
  516. }
  517. /** @hidden */
  518. public _releaseTexture(texture: InternalTexture): void {
  519. }
  520. /**
  521. * Usually called from Texture.ts.
  522. * Passed information to create a WebGLTexture
  523. * @param urlArg defines a value which contains one of the following:
  524. * * A conventional http URL, e.g. 'http://...' or 'file://...'
  525. * * A base64 string of in-line texture data, e.g. 'data:image/jpg;base64,/...'
  526. * * An indicator that data being passed using the buffer parameter, e.g. 'data:mytexture.jpg'
  527. * @param noMipmap defines a boolean indicating that no mipmaps shall be generated. Ignored for compressed textures. They must be in the file
  528. * @param invertY when true, image is flipped when loaded. You probably want true. Certain compressed textures may invert this if their default is inverted (eg. ktx)
  529. * @param scene needed for loading to the correct scene
  530. * @param samplingMode mode with should be used sample / access the texture (Default: Texture.TRILINEAR_SAMPLINGMODE)
  531. * @param onLoad optional callback to be called upon successful completion
  532. * @param onError optional callback to be called upon failure
  533. * @param buffer a source of a file previously fetched as either a base64 string, an ArrayBuffer (compressed or image format), HTMLImageElement (image format), or a Blob
  534. * @param fallback an internal argument in case the function must be called again, due to etc1 not having alpha capabilities
  535. * @param format internal format. Default: RGB when extension is '.jpg' else RGBA. Ignored for compressed textures
  536. * @param forcedExtension defines the extension to use to pick the right loader
  537. * @param mimeType defines an optional mime type
  538. * @returns a InternalTexture for assignment back into BABYLON.Texture
  539. */
  540. public createTexture(urlArg: Nullable<string>, noMipmap: boolean, invertY: boolean, scene: Nullable<ISceneLike>, samplingMode: number = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE,
  541. onLoad: Nullable<() => void> = null, onError: Nullable<(message: string, exception: any) => void> = null,
  542. buffer: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob | ImageBitmap> = null, fallback: Nullable<InternalTexture> = null, format: Nullable<number> = null,
  543. forcedExtension: Nullable<string> = null, mimeType?: string): InternalTexture {
  544. var texture = new InternalTexture(this, InternalTextureSource.Url);
  545. var url = String(urlArg);
  546. texture.url = url;
  547. texture.generateMipMaps = !noMipmap;
  548. texture.samplingMode = samplingMode;
  549. texture.invertY = invertY;
  550. texture.baseWidth = this._options.textureSize;
  551. texture.baseHeight = this._options.textureSize;
  552. texture.width = this._options.textureSize;
  553. texture.height = this._options.textureSize;
  554. if (format) {
  555. texture.format = format;
  556. }
  557. texture.isReady = true;
  558. if (onLoad) {
  559. onLoad();
  560. }
  561. this._internalTexturesCache.push(texture);
  562. return texture;
  563. }
  564. /**
  565. * Creates a new render target texture
  566. * @param size defines the size of the texture
  567. * @param options defines the options used to create the texture
  568. * @returns a new render target texture stored in an InternalTexture
  569. */
  570. public createRenderTargetTexture(size: any, options: boolean | RenderTargetCreationOptions): InternalTexture {
  571. let fullOptions = new RenderTargetCreationOptions();
  572. if (options !== undefined && typeof options === "object") {
  573. fullOptions.generateMipMaps = options.generateMipMaps;
  574. fullOptions.generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  575. fullOptions.generateStencilBuffer = fullOptions.generateDepthBuffer && options.generateStencilBuffer;
  576. fullOptions.type = options.type === undefined ? Constants.TEXTURETYPE_UNSIGNED_INT : options.type;
  577. fullOptions.samplingMode = options.samplingMode === undefined ? Constants.TEXTURE_TRILINEAR_SAMPLINGMODE : options.samplingMode;
  578. } else {
  579. fullOptions.generateMipMaps = <boolean>options;
  580. fullOptions.generateDepthBuffer = true;
  581. fullOptions.generateStencilBuffer = false;
  582. fullOptions.type = Constants.TEXTURETYPE_UNSIGNED_INT;
  583. fullOptions.samplingMode = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE;
  584. }
  585. var texture = new InternalTexture(this, InternalTextureSource.RenderTarget);
  586. var width = size.width || size;
  587. var height = size.height || size;
  588. texture._depthStencilBuffer = {};
  589. texture._framebuffer = {};
  590. texture.baseWidth = width;
  591. texture.baseHeight = height;
  592. texture.width = width;
  593. texture.height = height;
  594. texture.isReady = true;
  595. texture.samples = 1;
  596. texture.generateMipMaps = fullOptions.generateMipMaps ? true : false;
  597. texture.samplingMode = fullOptions.samplingMode;
  598. texture.type = fullOptions.type;
  599. texture._generateDepthBuffer = fullOptions.generateDepthBuffer;
  600. texture._generateStencilBuffer = fullOptions.generateStencilBuffer ? true : false;
  601. this._internalTexturesCache.push(texture);
  602. return texture;
  603. }
  604. /**
  605. * Update the sampling mode of a given texture
  606. * @param samplingMode defines the required sampling mode
  607. * @param texture defines the texture to update
  608. */
  609. public updateTextureSamplingMode(samplingMode: number, texture: InternalTexture): void {
  610. texture.samplingMode = samplingMode;
  611. }
  612. /**
  613. * Binds the frame buffer to the specified texture.
  614. * @param texture The texture to render to or null for the default canvas
  615. * @param faceIndex The face of the texture to render to in case of cube texture
  616. * @param requiredWidth The width of the target to render to
  617. * @param requiredHeight The height of the target to render to
  618. * @param forceFullscreenViewport Forces the viewport to be the entire texture/screen if true
  619. * @param lodLevel defines le lod level to bind to the frame buffer
  620. */
  621. public bindFramebuffer(texture: InternalTexture, faceIndex?: number, requiredWidth?: number, requiredHeight?: number, forceFullscreenViewport?: boolean): void {
  622. if (this._currentRenderTarget) {
  623. this.unBindFramebuffer(this._currentRenderTarget);
  624. }
  625. this._currentRenderTarget = texture;
  626. this._currentFramebuffer = texture._MSAAFramebuffer ? texture._MSAAFramebuffer : texture._framebuffer;
  627. if (this._cachedViewport && !forceFullscreenViewport) {
  628. this.setViewport(this._cachedViewport, requiredWidth, requiredHeight);
  629. }
  630. }
  631. /**
  632. * Unbind the current render target texture from the webGL context
  633. * @param texture defines the render target texture to unbind
  634. * @param disableGenerateMipMaps defines a boolean indicating that mipmaps must not be generated
  635. * @param onBeforeUnbind defines a function which will be called before the effective unbind
  636. */
  637. public unBindFramebuffer(texture: InternalTexture, disableGenerateMipMaps = false, onBeforeUnbind?: () => void): void {
  638. this._currentRenderTarget = null;
  639. if (onBeforeUnbind) {
  640. if (texture._MSAAFramebuffer) {
  641. this._currentFramebuffer = texture._framebuffer;
  642. }
  643. onBeforeUnbind();
  644. }
  645. this._currentFramebuffer = null;
  646. }
  647. /**
  648. * Creates a dynamic vertex buffer
  649. * @param vertices the data for the dynamic vertex buffer
  650. * @returns the new WebGL dynamic buffer
  651. */
  652. public createDynamicVertexBuffer(vertices: FloatArray): DataBuffer {
  653. let buffer = new DataBuffer();
  654. buffer.references = 1;
  655. buffer.capacity = 1;
  656. return buffer;
  657. }
  658. /**
  659. * Update the content of a dynamic texture
  660. * @param texture defines the texture to update
  661. * @param canvas defines the canvas containing the source
  662. * @param invertY defines if data must be stored with Y axis inverted
  663. * @param premulAlpha defines if alpha is stored as premultiplied
  664. * @param format defines the format of the data
  665. * @param forceBindTexture if the texture should be forced to be bound eg. after a graphics context loss (Default: false)
  666. */
  667. public updateDynamicTexture(texture: Nullable<InternalTexture>, canvas: HTMLCanvasElement, invertY: boolean, premulAlpha: boolean = false, format?: number): void {
  668. }
  669. /**
  670. * Gets a boolean indicating if all created effects are ready
  671. * @returns true if all effects are ready
  672. */
  673. public areAllEffectsReady(): boolean {
  674. return true;
  675. }
  676. /**
  677. * @hidden
  678. * Get the current error code of the webGL context
  679. * @returns the error code
  680. * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/getError
  681. */
  682. public getError(): number {
  683. return 0;
  684. }
  685. /** @hidden */
  686. public _getUnpackAlignement(): number {
  687. return 1;
  688. }
  689. /** @hidden */
  690. public _unpackFlipY(value: boolean) {
  691. }
  692. /**
  693. * Update a dynamic index buffer
  694. * @param indexBuffer defines the target index buffer
  695. * @param indices defines the data to update
  696. * @param offset defines the offset in the target index buffer where update should start
  697. */
  698. public updateDynamicIndexBuffer(indexBuffer: WebGLBuffer, indices: IndicesArray, offset: number = 0): void {
  699. }
  700. /**
  701. * Updates a dynamic vertex buffer.
  702. * @param vertexBuffer the vertex buffer to update
  703. * @param vertices the data used to update the vertex buffer
  704. * @param byteOffset the byte offset of the data (optional)
  705. * @param byteLength the byte length of the data (optional)
  706. */
  707. public updateDynamicVertexBuffer(vertexBuffer: WebGLBuffer, vertices: FloatArray, byteOffset?: number, byteLength?: number): void {
  708. }
  709. /** @hidden */
  710. public _bindTextureDirectly(target: number, texture: InternalTexture): boolean {
  711. if (this._boundTexturesCache[this._activeChannel] !== texture) {
  712. this._boundTexturesCache[this._activeChannel] = texture;
  713. return true;
  714. }
  715. return false;
  716. }
  717. /** @hidden */
  718. public _bindTexture(channel: number, texture: InternalTexture): void {
  719. if (channel < 0) {
  720. return;
  721. }
  722. this._bindTextureDirectly(0, texture);
  723. }
  724. protected _deleteBuffer(buffer: WebGLBuffer): void {
  725. }
  726. /**
  727. * Force the engine to release all cached effects. This means that next effect compilation will have to be done completely even if a similar effect was already compiled
  728. */
  729. public releaseEffects() {
  730. }
  731. public displayLoadingUI(): void {
  732. }
  733. public hideLoadingUI(): void {
  734. }
  735. /** @hidden */
  736. public _uploadCompressedDataToTextureDirectly(texture: InternalTexture, internalFormat: number, width: number, height: number, data: ArrayBufferView, faceIndex: number = 0, lod: number = 0) {
  737. }
  738. /** @hidden */
  739. public _uploadDataToTextureDirectly(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
  740. }
  741. /** @hidden */
  742. public _uploadArrayBufferViewToTexture(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
  743. }
  744. /** @hidden */
  745. public _uploadImageToTexture(texture: InternalTexture, image: HTMLImageElement, faceIndex: number = 0, lod: number = 0) {
  746. }
  747. }