nullEngine.ts 34 KB

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