nullEngine.ts 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. canUseGLInstanceID: false,
  127. };
  128. this._features = {
  129. forceBitmapOverHTMLImageElement: false,
  130. supportRenderAndCopyToLodForFloatTextures: false,
  131. supportDepthStencilTexture: false,
  132. supportShadowSamplers: false,
  133. uniformBufferHardCheckMatrix: false,
  134. allowTexturePrefiltering: false,
  135. trackUbosInFrame: false,
  136. supportCSM: false,
  137. basisNeedsPOT: false,
  138. support3DTextures: false,
  139. needTypeSuffixInShaderConstants: false,
  140. supportMSAA: false,
  141. supportSSAO2: false,
  142. supportExtendedTextureFormats: false,
  143. supportSwitchCaseInShader: false,
  144. supportSyncTextureRead: false,
  145. _collectUbosUpdatedInFrame: false,
  146. };
  147. Logger.Log(`Babylon.js v${Engine.Version} - Null engine`);
  148. // Wrappers
  149. const theCurrentGlobal = (typeof self !== "undefined" ? self : typeof global !== "undefined" ? global : window);
  150. if (typeof URL === "undefined") {
  151. theCurrentGlobal.URL = {
  152. createObjectURL: function() { },
  153. revokeObjectURL: function() { }
  154. };
  155. }
  156. if (typeof Blob === "undefined") {
  157. theCurrentGlobal.Blob = function() { };
  158. }
  159. }
  160. /**
  161. * Creates a vertex buffer
  162. * @param vertices the data for the vertex buffer
  163. * @returns the new WebGL static buffer
  164. */
  165. public createVertexBuffer(vertices: FloatArray): DataBuffer {
  166. let buffer = new DataBuffer();
  167. buffer.references = 1;
  168. return buffer;
  169. }
  170. /**
  171. * Creates a new index buffer
  172. * @param indices defines the content of the index buffer
  173. * @param updatable defines if the index buffer must be updatable
  174. * @returns a new webGL buffer
  175. */
  176. public createIndexBuffer(indices: IndicesArray): DataBuffer {
  177. let buffer = new DataBuffer();
  178. buffer.references = 1;
  179. return buffer;
  180. }
  181. /**
  182. * Clear the current render buffer or the current render target (if any is set up)
  183. * @param color defines the color to use
  184. * @param backBuffer defines if the back buffer must be cleared
  185. * @param depth defines if the depth buffer must be cleared
  186. * @param stencil defines if the stencil buffer must be cleared
  187. */
  188. public clear(color: IColor4Like, backBuffer: boolean, depth: boolean, stencil: boolean = false): void {
  189. }
  190. /**
  191. * Gets the current render width
  192. * @param useScreen defines if screen size must be used (or the current render target if any)
  193. * @returns a number defining the current render width
  194. */
  195. public getRenderWidth(useScreen = false): number {
  196. if (!useScreen && this._currentRenderTarget) {
  197. return this._currentRenderTarget.width;
  198. }
  199. return this._options.renderWidth;
  200. }
  201. /**
  202. * Gets the current render height
  203. * @param useScreen defines if screen size must be used (or the current render target if any)
  204. * @returns a number defining the current render height
  205. */
  206. public getRenderHeight(useScreen = false): number {
  207. if (!useScreen && this._currentRenderTarget) {
  208. return this._currentRenderTarget.height;
  209. }
  210. return this._options.renderHeight;
  211. }
  212. /**
  213. * Set the WebGL's viewport
  214. * @param viewport defines the viewport element to be used
  215. * @param requiredWidth defines the width required for rendering. If not provided the rendering canvas' width is used
  216. * @param requiredHeight defines the height required for rendering. If not provided the rendering canvas' height is used
  217. */
  218. public setViewport(viewport: IViewportLike, requiredWidth?: number, requiredHeight?: number): void {
  219. this._cachedViewport = viewport;
  220. }
  221. public createShaderProgram(pipelineContext: IPipelineContext, vertexCode: string, fragmentCode: string, defines: string, context?: WebGLRenderingContext): WebGLProgram {
  222. return {
  223. __SPECTOR_rebuildProgram: null,
  224. };
  225. }
  226. /**
  227. * Gets the list of webGL uniform locations associated with a specific program based on a list of uniform names
  228. * @param pipelineContext defines the pipeline context to use
  229. * @param uniformsNames defines the list of uniform names
  230. * @returns an array of webGL uniform locations
  231. */
  232. public getUniforms(pipelineContext: IPipelineContext, uniformsNames: string[]): Nullable<WebGLUniformLocation>[] {
  233. return [];
  234. }
  235. /**
  236. * Gets the lsit of active attributes for a given webGL program
  237. * @param pipelineContext defines the pipeline context to use
  238. * @param attributesNames defines the list of attribute names to get
  239. * @returns an array of indices indicating the offset of each attribute
  240. */
  241. public getAttributes(pipelineContext: IPipelineContext, attributesNames: string[]): number[] {
  242. return [];
  243. }
  244. /**
  245. * Binds an effect to the webGL context
  246. * @param effect defines the effect to bind
  247. */
  248. public bindSamplers(effect: Effect): void {
  249. this._currentEffect = null;
  250. }
  251. /**
  252. * Activates an effect, mkaing it the current one (ie. the one used for rendering)
  253. * @param effect defines the effect to activate
  254. */
  255. public enableEffect(effect: Effect): void {
  256. this._currentEffect = effect;
  257. if (effect.onBind) {
  258. effect.onBind(effect);
  259. }
  260. if (effect._onBindObservable) {
  261. effect._onBindObservable.notifyObservers(effect);
  262. }
  263. }
  264. /**
  265. * Set various states to the webGL context
  266. * @param culling defines backface culling state
  267. * @param zOffset defines the value to apply to zOffset (0 by default)
  268. * @param force defines if states must be applied even if cache is up to date
  269. * @param reverseSide defines if culling must be reversed (CCW instead of CW and CW instead of CCW)
  270. */
  271. public setState(culling: boolean, zOffset: number = 0, force?: boolean, reverseSide = false): void {
  272. }
  273. /**
  274. * Set the value of an uniform to an array of int32
  275. * @param uniform defines the webGL uniform location where to store the value
  276. * @param array defines the array of int32 to store
  277. * @returns true if value was set
  278. */
  279. public setIntArray(uniform: WebGLUniformLocation, array: Int32Array): boolean {
  280. return true;
  281. }
  282. /**
  283. * Set the value of an uniform to an array of int32 (stored as vec2)
  284. * @param uniform defines the webGL uniform location where to store the value
  285. * @param array defines the array of int32 to store
  286. * @returns true if value was set
  287. */
  288. public setIntArray2(uniform: WebGLUniformLocation, array: Int32Array): boolean {
  289. return true;
  290. }
  291. /**
  292. * Set the value of an uniform to an array of int32 (stored as vec3)
  293. * @param uniform defines the webGL uniform location where to store the value
  294. * @param array defines the array of int32 to store
  295. * @returns true if value was set
  296. */
  297. public setIntArray3(uniform: WebGLUniformLocation, array: Int32Array): boolean {
  298. return true;
  299. }
  300. /**
  301. * Set the value of an uniform to an array of int32 (stored as vec4)
  302. * @param uniform defines the webGL uniform location where to store the value
  303. * @param array defines the array of int32 to store
  304. * @returns true if value was set
  305. */
  306. public setIntArray4(uniform: WebGLUniformLocation, array: Int32Array): boolean {
  307. return true;
  308. }
  309. /**
  310. * Set the value of an uniform to an array of float32
  311. * @param uniform defines the webGL uniform location where to store the value
  312. * @param array defines the array of float32 to store
  313. * @returns true if value was set
  314. */
  315. public setFloatArray(uniform: WebGLUniformLocation, array: Float32Array): boolean {
  316. return true;
  317. }
  318. /**
  319. * Set the value of an uniform to an array of float32 (stored as vec2)
  320. * @param uniform defines the webGL uniform location where to store the value
  321. * @param array defines the array of float32 to store
  322. * @returns true if value was set
  323. */
  324. public setFloatArray2(uniform: WebGLUniformLocation, array: Float32Array): boolean {
  325. return true;
  326. }
  327. /**
  328. * Set the value of an uniform to an array of float32 (stored as vec3)
  329. * @param uniform defines the webGL uniform location where to store the value
  330. * @param array defines the array of float32 to store
  331. * @returns true if value was set
  332. */
  333. public setFloatArray3(uniform: WebGLUniformLocation, array: Float32Array): boolean {
  334. return true;
  335. }
  336. /**
  337. * Set the value of an uniform to an array of float32 (stored as vec4)
  338. * @param uniform defines the webGL uniform location where to store the value
  339. * @param array defines the array of float32 to store
  340. * @returns true if value was set
  341. */
  342. public setFloatArray4(uniform: WebGLUniformLocation, array: Float32Array): boolean {
  343. return true;
  344. }
  345. /**
  346. * Set the value of an uniform to an array of number
  347. * @param uniform defines the webGL uniform location where to store the value
  348. * @param array defines the array of number to store
  349. * @returns true if value was set
  350. */
  351. public setArray(uniform: WebGLUniformLocation, array: number[]): boolean {
  352. return true;
  353. }
  354. /**
  355. * Set the value of an uniform to an array of number (stored as vec2)
  356. * @param uniform defines the webGL uniform location where to store the value
  357. * @param array defines the array of number to store
  358. * @returns true if value was set
  359. */
  360. public setArray2(uniform: WebGLUniformLocation, array: number[]): boolean {
  361. return true;
  362. }
  363. /**
  364. * Set the value of an uniform to an array of number (stored as vec3)
  365. * @param uniform defines the webGL uniform location where to store the value
  366. * @param array defines the array of number to store
  367. * @returns true if value was set
  368. */
  369. public setArray3(uniform: WebGLUniformLocation, array: number[]): boolean {
  370. return true;
  371. }
  372. /**
  373. * Set the value of an uniform to an array of number (stored as vec4)
  374. * @param uniform defines the webGL uniform location where to store the value
  375. * @param array defines the array of number to store
  376. * @returns true if value was set
  377. */
  378. public setArray4(uniform: WebGLUniformLocation, array: number[]): boolean {
  379. return true;
  380. }
  381. /**
  382. * Set the value of an uniform to an array of float32 (stored as matrices)
  383. * @param uniform defines the webGL uniform location where to store the value
  384. * @param matrices defines the array of float32 to store
  385. * @returns true if value was set
  386. */
  387. public setMatrices(uniform: WebGLUniformLocation, matrices: Float32Array): boolean {
  388. return true;
  389. }
  390. /**
  391. * Set the value of an uniform to a matrix (3x3)
  392. * @param uniform defines the webGL uniform location where to store the value
  393. * @param matrix defines the Float32Array representing the 3x3 matrix to store
  394. * @returns true if value was set
  395. */
  396. public setMatrix3x3(uniform: WebGLUniformLocation, matrix: Float32Array): boolean {
  397. return true;
  398. }
  399. /**
  400. * Set the value of an uniform to a matrix (2x2)
  401. * @param uniform defines the webGL uniform location where to store the value
  402. * @param matrix defines the Float32Array representing the 2x2 matrix to store
  403. * @returns true if value was set
  404. */
  405. public setMatrix2x2(uniform: WebGLUniformLocation, matrix: Float32Array): boolean {
  406. return true;
  407. }
  408. /**
  409. * Set the value of an uniform to a number (float)
  410. * @param uniform defines the webGL uniform location where to store the value
  411. * @param value defines the float number to store
  412. * @returns true if value was set
  413. */
  414. public setFloat(uniform: WebGLUniformLocation, value: number): boolean {
  415. return true;
  416. }
  417. /**
  418. * Set the value of an uniform to a vec2
  419. * @param uniform defines the webGL uniform location where to store the value
  420. * @param x defines the 1st component of the value
  421. * @param y defines the 2nd component of the value
  422. * @returns true if value was set
  423. */
  424. public setFloat2(uniform: WebGLUniformLocation, x: number, y: number): boolean {
  425. return true;
  426. }
  427. /**
  428. * Set the value of an uniform to a vec3
  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. * @returns true if value was set
  434. */
  435. public setFloat3(uniform: WebGLUniformLocation, x: number, y: number, z: number): boolean {
  436. return true;
  437. }
  438. /**
  439. * Set the value of an uniform to a boolean
  440. * @param uniform defines the webGL uniform location where to store the value
  441. * @param bool defines the boolean to store
  442. * @returns true if value was set
  443. */
  444. public setBool(uniform: WebGLUniformLocation, bool: number): boolean {
  445. return true;
  446. }
  447. /**
  448. * Set the value of an uniform to a vec4
  449. * @param uniform defines the webGL uniform location where to store the value
  450. * @param x defines the 1st component of the value
  451. * @param y defines the 2nd component of the value
  452. * @param z defines the 3rd component of the value
  453. * @param w defines the 4th component of the value
  454. * @returns true if value was set
  455. */
  456. public setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): boolean {
  457. return true;
  458. }
  459. /**
  460. * Sets the current alpha mode
  461. * @param mode defines the mode to use (one of the Engine.ALPHA_XXX)
  462. * @param noDepthWriteChange defines if depth writing state should remains unchanged (false by default)
  463. * @see https://doc.babylonjs.com/resources/transparency_and_how_meshes_are_rendered
  464. */
  465. public setAlphaMode(mode: number, noDepthWriteChange: boolean = false): void {
  466. if (this._alphaMode === mode) {
  467. return;
  468. }
  469. this.alphaState.alphaBlend = (mode !== Constants.ALPHA_DISABLE);
  470. if (!noDepthWriteChange) {
  471. this.setDepthWrite(mode === Constants.ALPHA_DISABLE);
  472. }
  473. this._alphaMode = mode;
  474. }
  475. /**
  476. * Bind webGl buffers directly to the webGL context
  477. * @param vertexBuffers defines the vertex buffer to bind
  478. * @param indexBuffer defines the index buffer to bind
  479. * @param vertexDeclaration defines the vertex declaration to use with the vertex buffer
  480. * @param vertexStrideSize defines the vertex stride of the vertex buffer
  481. * @param effect defines the effect associated with the vertex buffer
  482. */
  483. public bindBuffers(vertexBuffers: { [key: string]: VertexBuffer; }, indexBuffer: DataBuffer, effect: Effect): void {
  484. }
  485. /**
  486. * Force the entire cache to be cleared
  487. * You should not have to use this function unless your engine needs to share the webGL context with another engine
  488. * @param bruteForce defines a boolean to force clearing ALL caches (including stencil, detoh and alpha states)
  489. */
  490. public wipeCaches(bruteForce?: boolean): void {
  491. if (this.preventCacheWipeBetweenFrames) {
  492. return;
  493. }
  494. this.resetTextureCache();
  495. this._currentEffect = null;
  496. if (bruteForce) {
  497. this._currentProgram = null;
  498. this.stencilState.reset();
  499. this.depthCullingState.reset();
  500. this.alphaState.reset();
  501. }
  502. this._cachedVertexBuffers = null;
  503. this._cachedIndexBuffer = null;
  504. this._cachedEffectForVertexBuffers = null;
  505. }
  506. /**
  507. * Send a draw order
  508. * @param useTriangles defines if triangles must be used to draw (else wireframe will be used)
  509. * @param indexStart defines the starting index
  510. * @param indexCount defines the number of index to draw
  511. * @param instancesCount defines the number of instances to draw (if instanciation is enabled)
  512. */
  513. public draw(useTriangles: boolean, indexStart: number, indexCount: number, instancesCount?: number): void {
  514. }
  515. /**
  516. * Draw a list of indexed primitives
  517. * @param fillMode defines the primitive to use
  518. * @param indexStart defines the starting index
  519. * @param indexCount defines the number of index to draw
  520. * @param instancesCount defines the number of instances to draw (if instanciation is enabled)
  521. */
  522. public drawElementsType(fillMode: number, indexStart: number, indexCount: number, instancesCount?: number): void {
  523. }
  524. /**
  525. * Draw a list of unindexed primitives
  526. * @param fillMode defines the primitive to use
  527. * @param verticesStart defines the index of first vertex to draw
  528. * @param verticesCount defines the count of vertices to draw
  529. * @param instancesCount defines the number of instances to draw (if instanciation is enabled)
  530. */
  531. public drawArraysType(fillMode: number, verticesStart: number, verticesCount: number, instancesCount?: number): void {
  532. }
  533. /** @hidden */
  534. protected _createTexture(): WebGLTexture {
  535. return {};
  536. }
  537. /** @hidden */
  538. public _releaseTexture(texture: InternalTexture): void {
  539. }
  540. /**
  541. * Usually called from Texture.ts.
  542. * Passed information to create a WebGLTexture
  543. * @param urlArg defines a value which contains one of the following:
  544. * * A conventional http URL, e.g. 'http://...' or 'file://...'
  545. * * A base64 string of in-line texture data, e.g. 'data:image/jpg;base64,/...'
  546. * * An indicator that data being passed using the buffer parameter, e.g. 'data:mytexture.jpg'
  547. * @param noMipmap defines a boolean indicating that no mipmaps shall be generated. Ignored for compressed textures. They must be in the file
  548. * @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)
  549. * @param scene needed for loading to the correct scene
  550. * @param samplingMode mode with should be used sample / access the texture (Default: Texture.TRILINEAR_SAMPLINGMODE)
  551. * @param onLoad optional callback to be called upon successful completion
  552. * @param onError optional callback to be called upon failure
  553. * @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
  554. * @param fallback an internal argument in case the function must be called again, due to etc1 not having alpha capabilities
  555. * @param format internal format. Default: RGB when extension is '.jpg' else RGBA. Ignored for compressed textures
  556. * @param forcedExtension defines the extension to use to pick the right loader
  557. * @param mimeType defines an optional mime type
  558. * @returns a InternalTexture for assignment back into BABYLON.Texture
  559. */
  560. public createTexture(urlArg: Nullable<string>, noMipmap: boolean, invertY: boolean, scene: Nullable<ISceneLike>, samplingMode: number = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE,
  561. onLoad: Nullable<() => void> = null, onError: Nullable<(message: string, exception: any) => void> = null,
  562. buffer: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob | ImageBitmap> = null, fallback: Nullable<InternalTexture> = null, format: Nullable<number> = null,
  563. forcedExtension: Nullable<string> = null, mimeType?: string): InternalTexture {
  564. var texture = new InternalTexture(this, InternalTextureSource.Url);
  565. var url = String(urlArg);
  566. texture.url = url;
  567. texture.generateMipMaps = !noMipmap;
  568. texture.samplingMode = samplingMode;
  569. texture.invertY = invertY;
  570. texture.baseWidth = this._options.textureSize;
  571. texture.baseHeight = this._options.textureSize;
  572. texture.width = this._options.textureSize;
  573. texture.height = this._options.textureSize;
  574. if (format) {
  575. texture.format = format;
  576. }
  577. texture.isReady = true;
  578. if (onLoad) {
  579. onLoad();
  580. }
  581. this._internalTexturesCache.push(texture);
  582. return texture;
  583. }
  584. /**
  585. * Creates a new render target texture
  586. * @param size defines the size of the texture
  587. * @param options defines the options used to create the texture
  588. * @returns a new render target texture stored in an InternalTexture
  589. */
  590. public createRenderTargetTexture(size: any, options: boolean | RenderTargetCreationOptions): InternalTexture {
  591. let fullOptions = new RenderTargetCreationOptions();
  592. if (options !== undefined && typeof options === "object") {
  593. fullOptions.generateMipMaps = options.generateMipMaps;
  594. fullOptions.generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  595. fullOptions.generateStencilBuffer = fullOptions.generateDepthBuffer && options.generateStencilBuffer;
  596. fullOptions.type = options.type === undefined ? Constants.TEXTURETYPE_UNSIGNED_INT : options.type;
  597. fullOptions.samplingMode = options.samplingMode === undefined ? Constants.TEXTURE_TRILINEAR_SAMPLINGMODE : options.samplingMode;
  598. } else {
  599. fullOptions.generateMipMaps = <boolean>options;
  600. fullOptions.generateDepthBuffer = true;
  601. fullOptions.generateStencilBuffer = false;
  602. fullOptions.type = Constants.TEXTURETYPE_UNSIGNED_INT;
  603. fullOptions.samplingMode = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE;
  604. }
  605. var texture = new InternalTexture(this, InternalTextureSource.RenderTarget);
  606. var width = size.width || size;
  607. var height = size.height || size;
  608. texture._depthStencilBuffer = {};
  609. texture._framebuffer = {};
  610. texture.baseWidth = width;
  611. texture.baseHeight = height;
  612. texture.width = width;
  613. texture.height = height;
  614. texture.isReady = true;
  615. texture.samples = 1;
  616. texture.generateMipMaps = fullOptions.generateMipMaps ? true : false;
  617. texture.samplingMode = fullOptions.samplingMode;
  618. texture.type = fullOptions.type;
  619. texture._generateDepthBuffer = fullOptions.generateDepthBuffer;
  620. texture._generateStencilBuffer = fullOptions.generateStencilBuffer ? true : false;
  621. this._internalTexturesCache.push(texture);
  622. return texture;
  623. }
  624. /**
  625. * Update the sampling mode of a given texture
  626. * @param samplingMode defines the required sampling mode
  627. * @param texture defines the texture to update
  628. */
  629. public updateTextureSamplingMode(samplingMode: number, texture: InternalTexture): void {
  630. texture.samplingMode = samplingMode;
  631. }
  632. /**
  633. * Binds the frame buffer to the specified texture.
  634. * @param texture The texture to render to or null for the default canvas
  635. * @param faceIndex The face of the texture to render to in case of cube texture
  636. * @param requiredWidth The width of the target to render to
  637. * @param requiredHeight The height of the target to render to
  638. * @param forceFullscreenViewport Forces the viewport to be the entire texture/screen if true
  639. * @param lodLevel defines le lod level to bind to the frame buffer
  640. */
  641. public bindFramebuffer(texture: InternalTexture, faceIndex?: number, requiredWidth?: number, requiredHeight?: number, forceFullscreenViewport?: boolean): void {
  642. if (this._currentRenderTarget) {
  643. this.unBindFramebuffer(this._currentRenderTarget);
  644. }
  645. this._currentRenderTarget = texture;
  646. this._currentFramebuffer = texture._MSAAFramebuffer ? texture._MSAAFramebuffer : texture._framebuffer;
  647. if (this._cachedViewport && !forceFullscreenViewport) {
  648. this.setViewport(this._cachedViewport, requiredWidth, requiredHeight);
  649. }
  650. }
  651. /**
  652. * Unbind the current render target texture from the webGL context
  653. * @param texture defines the render target texture to unbind
  654. * @param disableGenerateMipMaps defines a boolean indicating that mipmaps must not be generated
  655. * @param onBeforeUnbind defines a function which will be called before the effective unbind
  656. */
  657. public unBindFramebuffer(texture: InternalTexture, disableGenerateMipMaps = false, onBeforeUnbind?: () => void): void {
  658. this._currentRenderTarget = null;
  659. if (onBeforeUnbind) {
  660. if (texture._MSAAFramebuffer) {
  661. this._currentFramebuffer = texture._framebuffer;
  662. }
  663. onBeforeUnbind();
  664. }
  665. this._currentFramebuffer = null;
  666. }
  667. /**
  668. * Creates a dynamic vertex buffer
  669. * @param vertices the data for the dynamic vertex buffer
  670. * @returns the new WebGL dynamic buffer
  671. */
  672. public createDynamicVertexBuffer(vertices: FloatArray): DataBuffer {
  673. let buffer = new DataBuffer();
  674. buffer.references = 1;
  675. buffer.capacity = 1;
  676. return buffer;
  677. }
  678. /**
  679. * Update the content of a dynamic texture
  680. * @param texture defines the texture to update
  681. * @param canvas defines the canvas containing the source
  682. * @param invertY defines if data must be stored with Y axis inverted
  683. * @param premulAlpha defines if alpha is stored as premultiplied
  684. * @param format defines the format of the data
  685. * @param forceBindTexture if the texture should be forced to be bound eg. after a graphics context loss (Default: false)
  686. */
  687. public updateDynamicTexture(texture: Nullable<InternalTexture>, canvas: HTMLCanvasElement, invertY: boolean, premulAlpha: boolean = false, format?: number): void {
  688. }
  689. /**
  690. * Gets a boolean indicating if all created effects are ready
  691. * @returns true if all effects are ready
  692. */
  693. public areAllEffectsReady(): boolean {
  694. return true;
  695. }
  696. /**
  697. * @hidden
  698. * Get the current error code of the webGL context
  699. * @returns the error code
  700. * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/getError
  701. */
  702. public getError(): number {
  703. return 0;
  704. }
  705. /** @hidden */
  706. public _getUnpackAlignement(): number {
  707. return 1;
  708. }
  709. /** @hidden */
  710. public _unpackFlipY(value: boolean) {
  711. }
  712. /**
  713. * Update a dynamic index buffer
  714. * @param indexBuffer defines the target index buffer
  715. * @param indices defines the data to update
  716. * @param offset defines the offset in the target index buffer where update should start
  717. */
  718. public updateDynamicIndexBuffer(indexBuffer: WebGLBuffer, indices: IndicesArray, offset: number = 0): void {
  719. }
  720. /**
  721. * Updates a dynamic vertex buffer.
  722. * @param vertexBuffer the vertex buffer to update
  723. * @param vertices the data used to update the vertex buffer
  724. * @param byteOffset the byte offset of the data (optional)
  725. * @param byteLength the byte length of the data (optional)
  726. */
  727. public updateDynamicVertexBuffer(vertexBuffer: WebGLBuffer, vertices: FloatArray, byteOffset?: number, byteLength?: number): void {
  728. }
  729. /** @hidden */
  730. public _bindTextureDirectly(target: number, texture: InternalTexture): boolean {
  731. if (this._boundTexturesCache[this._activeChannel] !== texture) {
  732. this._boundTexturesCache[this._activeChannel] = texture;
  733. return true;
  734. }
  735. return false;
  736. }
  737. /** @hidden */
  738. public _bindTexture(channel: number, texture: InternalTexture): void {
  739. if (channel < 0) {
  740. return;
  741. }
  742. this._bindTextureDirectly(0, texture);
  743. }
  744. protected _deleteBuffer(buffer: WebGLBuffer): void {
  745. }
  746. /**
  747. * 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
  748. */
  749. public releaseEffects() {
  750. }
  751. public displayLoadingUI(): void {
  752. }
  753. public hideLoadingUI(): void {
  754. }
  755. /** @hidden */
  756. public _uploadCompressedDataToTextureDirectly(texture: InternalTexture, internalFormat: number, width: number, height: number, data: ArrayBufferView, faceIndex: number = 0, lod: number = 0) {
  757. }
  758. /** @hidden */
  759. public _uploadDataToTextureDirectly(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
  760. }
  761. /** @hidden */
  762. public _uploadArrayBufferViewToTexture(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
  763. }
  764. /** @hidden */
  765. public _uploadImageToTexture(texture: InternalTexture, image: HTMLImageElement, faceIndex: number = 0, lod: number = 0) {
  766. }
  767. }