babylon.nullEngine.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. module BABYLON {
  2. export class NullEngineOptions {
  3. public renderWidth = 512;
  4. public renderHeight = 256;
  5. public textureSize = 512;
  6. public deterministicLockstep = false;
  7. public lockstepMaxSteps = 4;
  8. }
  9. /**
  10. * The null engine class provides support for headless version of babylon.js.
  11. * This can be used in server side scenario or for testing purposes
  12. */
  13. export class NullEngine extends Engine {
  14. private _options: NullEngineOptions;
  15. public isDeterministicLockStep(): boolean {
  16. return this._options.deterministicLockstep;
  17. }
  18. public getLockstepMaxSteps(): number {
  19. return this._options.lockstepMaxSteps;
  20. }
  21. public getHardwareScalingLevel(): number {
  22. return 1.0;
  23. }
  24. public constructor(options: NullEngineOptions = new NullEngineOptions()) {
  25. super(null);
  26. if (options.deterministicLockstep === undefined) {
  27. options.deterministicLockstep = false;
  28. }
  29. if (options.lockstepMaxSteps === undefined) {
  30. options.lockstepMaxSteps = 4;
  31. }
  32. this._options = options;
  33. // Init caps
  34. // We consider we are on a webgl1 capable device
  35. this._caps = new EngineCapabilities();
  36. this._caps.maxTexturesImageUnits = 16;
  37. this._caps.maxVertexTextureImageUnits = 16;
  38. this._caps.maxTextureSize = 512;
  39. this._caps.maxCubemapTextureSize = 512;
  40. this._caps.maxRenderTextureSize = 512;
  41. this._caps.maxVertexAttribs = 16;
  42. this._caps.maxVaryingVectors = 16;
  43. this._caps.maxFragmentUniformVectors = 16;
  44. this._caps.maxVertexUniformVectors = 16;
  45. // Extensions
  46. this._caps.standardDerivatives = false;
  47. this._caps.astc = null;
  48. this._caps.s3tc = null;
  49. this._caps.pvrtc = null;
  50. this._caps.etc1 = null;
  51. this._caps.etc2 = null;
  52. this._caps.textureAnisotropicFilterExtension = null;
  53. this._caps.maxAnisotropy = 0;
  54. this._caps.uintIndices = false;
  55. this._caps.fragmentDepthSupported = false;
  56. this._caps.highPrecisionShaderSupported = true;
  57. this._caps.colorBufferFloat = false;
  58. this._caps.textureFloat = false;
  59. this._caps.textureFloatLinearFiltering = false;
  60. this._caps.textureFloatRender = false;
  61. this._caps.textureHalfFloat = false;
  62. this._caps.textureHalfFloatLinearFiltering = false;
  63. this._caps.textureHalfFloatRender = false;
  64. this._caps.textureLOD = false;
  65. this._caps.drawBuffersExtension = false;
  66. this._caps.depthTextureExtension = false;
  67. this._caps.vertexArrayObject = false;
  68. this._caps.instancedArrays = false;
  69. Tools.Log("Babylon.js null engine (v" + Engine.Version + ") launched");
  70. // Wrappers
  71. if (typeof URL === "undefined") {
  72. (<any>URL) = {
  73. createObjectURL: function () { },
  74. revokeObjectURL: function () { }
  75. }
  76. }
  77. if (typeof Blob === "undefined") {
  78. (<any>Blob) = function () { };
  79. }
  80. }
  81. public createVertexBuffer(vertices: FloatArray): WebGLBuffer {
  82. return {
  83. capacity: 0,
  84. references: 1,
  85. is32Bits: false
  86. };
  87. }
  88. public createIndexBuffer(indices: IndicesArray): WebGLBuffer {
  89. return {
  90. capacity: 0,
  91. references: 1,
  92. is32Bits: false
  93. };
  94. }
  95. public clear(color: Color4, backBuffer: boolean, depth: boolean, stencil: boolean = false): void {
  96. }
  97. public getRenderWidth(useScreen = false): number {
  98. if (!useScreen && this._currentRenderTarget) {
  99. return this._currentRenderTarget.width;
  100. }
  101. return this._options.renderWidth;
  102. }
  103. public getRenderHeight(useScreen = false): number {
  104. if (!useScreen && this._currentRenderTarget) {
  105. return this._currentRenderTarget.height;
  106. }
  107. return this._options.renderHeight;
  108. }
  109. public setViewport(viewport: Viewport, requiredWidth?: number, requiredHeight?: number): void {
  110. this._cachedViewport = viewport;
  111. }
  112. public createShaderProgram(vertexCode: string, fragmentCode: string, defines: string, context?: WebGLRenderingContext): WebGLProgram {
  113. return {
  114. transformFeedback: null,
  115. __SPECTOR_rebuildProgram: null
  116. };
  117. }
  118. public getUniforms(shaderProgram: WebGLProgram, uniformsNames: string[]): WebGLUniformLocation[] {
  119. return [];
  120. }
  121. public getAttributes(shaderProgram: WebGLProgram, attributesNames: string[]): number[] {
  122. return [];
  123. }
  124. public bindSamplers(effect: Effect): void {
  125. this._currentEffect = null;
  126. }
  127. public enableEffect(effect: Effect): void {
  128. this._currentEffect = effect;
  129. if (effect.onBind) {
  130. effect.onBind(effect);
  131. }
  132. effect.onBindObservable.notifyObservers(effect);
  133. }
  134. public setState(culling: boolean, zOffset: number = 0, force?: boolean, reverseSide = false): void {
  135. }
  136. public setIntArray(uniform: WebGLUniformLocation, array: Int32Array): void {
  137. }
  138. public setIntArray2(uniform: WebGLUniformLocation, array: Int32Array): void {
  139. }
  140. public setIntArray3(uniform: WebGLUniformLocation, array: Int32Array): void {
  141. }
  142. public setIntArray4(uniform: WebGLUniformLocation, array: Int32Array): void {
  143. }
  144. public setFloatArray(uniform: WebGLUniformLocation, array: Float32Array): void {
  145. }
  146. public setFloatArray2(uniform: WebGLUniformLocation, array: Float32Array): void {
  147. }
  148. public setFloatArray3(uniform: WebGLUniformLocation, array: Float32Array): void {
  149. }
  150. public setFloatArray4(uniform: WebGLUniformLocation, array: Float32Array): void {
  151. }
  152. public setArray(uniform: WebGLUniformLocation, array: number[]): void {
  153. }
  154. public setArray2(uniform: WebGLUniformLocation, array: number[]): void {
  155. }
  156. public setArray3(uniform: WebGLUniformLocation, array: number[]): void {
  157. }
  158. public setArray4(uniform: WebGLUniformLocation, array: number[]): void {
  159. }
  160. public setMatrices(uniform: WebGLUniformLocation, matrices: Float32Array): void {
  161. }
  162. public setMatrix(uniform: WebGLUniformLocation, matrix: Matrix): void {
  163. }
  164. public setMatrix3x3(uniform: WebGLUniformLocation, matrix: Float32Array): void {
  165. }
  166. public setMatrix2x2(uniform: WebGLUniformLocation, matrix: Float32Array): void {
  167. }
  168. public setFloat(uniform: WebGLUniformLocation, value: number): void {
  169. }
  170. public setFloat2(uniform: WebGLUniformLocation, x: number, y: number): void {
  171. }
  172. public setFloat3(uniform: WebGLUniformLocation, x: number, y: number, z: number): void {
  173. }
  174. public setBool(uniform: WebGLUniformLocation, bool: number): void {
  175. }
  176. public setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): void {
  177. }
  178. public setColor3(uniform: WebGLUniformLocation, color3: Color3): void {
  179. }
  180. public setColor4(uniform: WebGLUniformLocation, color3: Color3, alpha: number): void {
  181. }
  182. public setAlphaMode(mode: number, noDepthWriteChange: boolean = false): void {
  183. if (this._alphaMode === mode) {
  184. return;
  185. }
  186. this._alphaState.alphaBlend = (mode !== Engine.ALPHA_DISABLE);
  187. if (!noDepthWriteChange) {
  188. this.setDepthWrite(mode === Engine.ALPHA_DISABLE);
  189. }
  190. this._alphaMode = mode;
  191. }
  192. public bindBuffers(vertexBuffers: { [key: string]: VertexBuffer; }, indexBuffer: WebGLBuffer, effect: Effect): void {
  193. }
  194. public wipeCaches(bruteForce?: boolean): void {
  195. if (this.preventCacheWipeBetweenFrames) {
  196. return;
  197. }
  198. this.resetTextureCache();
  199. this._currentEffect = null;
  200. if (bruteForce) {
  201. this._currentProgram = null;
  202. this._stencilState.reset();
  203. this._depthCullingState.reset();
  204. this._alphaState.reset();
  205. }
  206. this._cachedVertexBuffers = null;
  207. this._cachedIndexBuffer = null;
  208. this._cachedEffectForVertexBuffers = null;
  209. }
  210. public draw(useTriangles: boolean, indexStart: number, indexCount: number, instancesCount?: number): void {
  211. }
  212. public drawElementsType(fillMode: number, indexStart: number, indexCount: number, instancesCount?: number): void {
  213. }
  214. public drawArraysType(fillMode: number, verticesStart: number, verticesCount: number, instancesCount?: number): void {
  215. }
  216. public _createTexture(): WebGLTexture {
  217. return {};
  218. }
  219. public _releaseTexture(texture: InternalTexture): void {
  220. }
  221. public createTexture(urlArg: string, noMipmap: boolean, invertY: boolean, scene: Scene, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: Nullable<() => void> = null, onError: Nullable<(message: string, exception: any) => void> = null, buffer: Nullable<ArrayBuffer | HTMLImageElement> = null, fallBack?: InternalTexture, format?: number): InternalTexture {
  222. var texture = new InternalTexture(this, InternalTexture.DATASOURCE_URL);
  223. var url = String(urlArg);
  224. texture.url = url;
  225. texture.generateMipMaps = !noMipmap;
  226. texture.samplingMode = samplingMode;
  227. texture.invertY = invertY;
  228. texture.baseWidth = this._options.textureSize;
  229. texture.baseHeight = this._options.textureSize;
  230. texture.width = this._options.textureSize;
  231. texture.height = this._options.textureSize;
  232. if (format) {
  233. texture.format = format;
  234. }
  235. texture.isReady = true;
  236. if (onLoad) {
  237. onLoad();
  238. }
  239. this._internalTexturesCache.push(texture);
  240. return texture;
  241. }
  242. public createRenderTargetTexture(size: any, options: boolean | RenderTargetCreationOptions): InternalTexture {
  243. let fullOptions = new RenderTargetCreationOptions();
  244. if (options !== undefined && typeof options === "object") {
  245. fullOptions.generateMipMaps = options.generateMipMaps;
  246. fullOptions.generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  247. fullOptions.generateStencilBuffer = fullOptions.generateDepthBuffer && options.generateStencilBuffer;
  248. fullOptions.type = options.type === undefined ? Engine.TEXTURETYPE_UNSIGNED_INT : options.type;
  249. fullOptions.samplingMode = options.samplingMode === undefined ? Texture.TRILINEAR_SAMPLINGMODE : options.samplingMode;
  250. } else {
  251. fullOptions.generateMipMaps = <boolean>options;
  252. fullOptions.generateDepthBuffer = true;
  253. fullOptions.generateStencilBuffer = false;
  254. fullOptions.type = Engine.TEXTURETYPE_UNSIGNED_INT;
  255. fullOptions.samplingMode = Texture.TRILINEAR_SAMPLINGMODE;
  256. }
  257. var texture = new InternalTexture(this, InternalTexture.DATASOURCE_RENDERTARGET);
  258. var width = size.width || size;
  259. var height = size.height || size;
  260. texture._depthStencilBuffer = {};
  261. texture._framebuffer = {};
  262. texture.baseWidth = width;
  263. texture.baseHeight = height;
  264. texture.width = width;
  265. texture.height = height;
  266. texture.isReady = true;
  267. texture.samples = 1;
  268. texture.generateMipMaps = fullOptions.generateMipMaps ? true : false;
  269. texture.samplingMode = fullOptions.samplingMode;
  270. texture.type = fullOptions.type;
  271. texture._generateDepthBuffer = fullOptions.generateDepthBuffer;
  272. texture._generateStencilBuffer = fullOptions.generateStencilBuffer ? true : false;
  273. this._internalTexturesCache.push(texture);
  274. return texture;
  275. }
  276. public updateTextureSamplingMode(samplingMode: number, texture: InternalTexture): void {
  277. texture.samplingMode = samplingMode;
  278. }
  279. public bindFramebuffer(texture: InternalTexture, faceIndex?: number, requiredWidth?: number, requiredHeight?: number, forceFullscreenViewport?: boolean): void {
  280. if (this._currentRenderTarget) {
  281. this.unBindFramebuffer(this._currentRenderTarget);
  282. }
  283. this._currentRenderTarget = texture;
  284. this._currentFramebuffer = texture._MSAAFramebuffer ? texture._MSAAFramebuffer : texture._framebuffer;
  285. if (this._cachedViewport && !forceFullscreenViewport) {
  286. this.setViewport(this._cachedViewport, requiredWidth, requiredHeight);
  287. }
  288. }
  289. public unBindFramebuffer(texture: InternalTexture, disableGenerateMipMaps = false, onBeforeUnbind?: () => void): void {
  290. this._currentRenderTarget = null;
  291. if (onBeforeUnbind) {
  292. if (texture._MSAAFramebuffer) {
  293. this._currentFramebuffer = texture._framebuffer;
  294. }
  295. onBeforeUnbind();
  296. }
  297. this._currentFramebuffer = null;
  298. }
  299. public createDynamicVertexBuffer(vertices: FloatArray): WebGLBuffer {
  300. var vbo = {
  301. capacity: 1,
  302. references: 1,
  303. is32Bits: false
  304. }
  305. return vbo;
  306. }
  307. public updateDynamicTexture(texture: Nullable<InternalTexture>, canvas: HTMLCanvasElement, invertY: boolean, premulAlpha: boolean = false, format?: number): void {
  308. }
  309. public updateDynamicIndexBuffer(indexBuffer: WebGLBuffer, indices: IndicesArray, offset: number = 0): void {
  310. }
  311. /**
  312. * Updates a dynamic vertex buffer.
  313. * @param vertexBuffer the vertex buffer to update
  314. * @param data the data used to update the vertex buffer
  315. * @param byteOffset the byte offset of the data (optional)
  316. * @param byteLength the byte length of the data (optional)
  317. */
  318. public updateDynamicVertexBuffer(vertexBuffer: WebGLBuffer, vertices: FloatArray, byteOffset?: number, byteLength?: number): void {
  319. }
  320. protected _bindTextureDirectly(target: number, texture: InternalTexture): boolean {
  321. if (this._boundTexturesCache[this._activeChannel] !== texture) {
  322. this._boundTexturesCache[this._activeChannel] = texture;
  323. return true;
  324. }
  325. return false;
  326. }
  327. public _bindTexture(channel: number, texture: InternalTexture): void {
  328. if (channel < 0) {
  329. return;
  330. }
  331. this._bindTextureDirectly(0, texture);
  332. }
  333. public _releaseBuffer(buffer: WebGLBuffer): boolean {
  334. buffer.references--;
  335. if (buffer.references === 0) {
  336. return true;
  337. }
  338. return false;
  339. }
  340. public releaseEffects() {
  341. }
  342. public displayLoadingUI(): void {
  343. }
  344. public hideLoadingUI(): void {
  345. }
  346. }
  347. }