babylon.nullEngine.ts 14 KB

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