nullEngine.ts 32 KB

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