babylon.nullEngine.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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: number[] | Float32Array): 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. __SPECTOR_rebuildProgram: undefined,
  98. };
  99. }
  100. public getUniforms(shaderProgram: WebGLProgram, uniformsNames: string[]): WebGLUniformLocation[] {
  101. return [];
  102. }
  103. public getAttributes(shaderProgram: WebGLProgram, attributesNames: string[]): number[] {
  104. return [];
  105. }
  106. public bindSamplers(effect: Effect): void {
  107. this._currentEffect = null;
  108. }
  109. public enableEffect(effect: Effect): void {
  110. this._currentEffect = effect;
  111. if (effect.onBind) {
  112. effect.onBind(effect);
  113. }
  114. effect.onBindObservable.notifyObservers(effect);
  115. }
  116. public setState(culling: boolean, zOffset: number = 0, force?: boolean, reverseSide = false): void {
  117. }
  118. public setIntArray(uniform: WebGLUniformLocation, array: Int32Array): void {
  119. }
  120. public setIntArray2(uniform: WebGLUniformLocation, array: Int32Array): void {
  121. }
  122. public setIntArray3(uniform: WebGLUniformLocation, array: Int32Array): void {
  123. }
  124. public setIntArray4(uniform: WebGLUniformLocation, array: Int32Array): void {
  125. }
  126. public setFloatArray(uniform: WebGLUniformLocation, array: Float32Array): void {
  127. }
  128. public setFloatArray2(uniform: WebGLUniformLocation, array: Float32Array): void {
  129. }
  130. public setFloatArray3(uniform: WebGLUniformLocation, array: Float32Array): void {
  131. }
  132. public setFloatArray4(uniform: WebGLUniformLocation, array: Float32Array): void {
  133. }
  134. public setArray(uniform: WebGLUniformLocation, array: number[]): void {
  135. }
  136. public setArray2(uniform: WebGLUniformLocation, array: number[]): void {
  137. }
  138. public setArray3(uniform: WebGLUniformLocation, array: number[]): void {
  139. }
  140. public setArray4(uniform: WebGLUniformLocation, array: number[]): void {
  141. }
  142. public setMatrices(uniform: WebGLUniformLocation, matrices: Float32Array): void {
  143. }
  144. public setMatrix(uniform: WebGLUniformLocation, matrix: Matrix): void {
  145. }
  146. public setMatrix3x3(uniform: WebGLUniformLocation, matrix: Float32Array): void {
  147. }
  148. public setMatrix2x2(uniform: WebGLUniformLocation, matrix: Float32Array): void {
  149. }
  150. public setFloat(uniform: WebGLUniformLocation, value: number): void {
  151. }
  152. public setFloat2(uniform: WebGLUniformLocation, x: number, y: number): void {
  153. }
  154. public setFloat3(uniform: WebGLUniformLocation, x: number, y: number, z: number): void {
  155. }
  156. public setBool(uniform: WebGLUniformLocation, bool: number): void {
  157. }
  158. public setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): void {
  159. }
  160. public setColor3(uniform: WebGLUniformLocation, color3: Color3): void {
  161. }
  162. public setColor4(uniform: WebGLUniformLocation, color3: Color3, alpha: number): void {
  163. }
  164. public setAlphaMode(mode: number, noDepthWriteChange: boolean = false): void {
  165. if (this._alphaMode === mode) {
  166. return;
  167. }
  168. this._alphaState.alphaBlend = (mode !== Engine.ALPHA_DISABLE);
  169. if (!noDepthWriteChange) {
  170. this.setDepthWrite(mode === Engine.ALPHA_DISABLE);
  171. }
  172. this._alphaMode = mode;
  173. }
  174. public bindBuffers(vertexBuffers: { [key: string]: VertexBuffer; }, indexBuffer: WebGLBuffer, effect: Effect): void {
  175. }
  176. public wipeCaches(bruteForce?: boolean): void {
  177. if (this.preventCacheWipeBetweenFrames) {
  178. return;
  179. }
  180. this.resetTextureCache();
  181. this._currentEffect = null;
  182. if (bruteForce) {
  183. this._currentProgram = null;
  184. this._stencilState.reset();
  185. this._depthCullingState.reset();
  186. this.setDepthFunctionToLessOrEqual();
  187. this._alphaState.reset();
  188. }
  189. this._cachedVertexBuffers = null;
  190. this._cachedIndexBuffer = null;
  191. this._cachedEffectForVertexBuffers = null;
  192. }
  193. public draw(useTriangles: boolean, indexStart: number, indexCount: number, instancesCount?: number): void {
  194. }
  195. public _createTexture(): WebGLTexture {
  196. return {};
  197. }
  198. public createTexture(urlArg: string, noMipmap: boolean, invertY: boolean, scene: Scene, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: () => void = null, onError: () => void = null, buffer: ArrayBuffer | HTMLImageElement = null, fallBack?: InternalTexture, format?: number): InternalTexture {
  199. var texture = new InternalTexture(this, InternalTexture.DATASOURCE_URL);
  200. var url = String(urlArg);
  201. texture.url = url;
  202. texture.generateMipMaps = !noMipmap;
  203. texture.samplingMode = samplingMode;
  204. texture.invertY = invertY;
  205. texture.baseWidth = this._options.textureSize;
  206. texture.baseHeight = this._options.textureSize;
  207. texture.width = this._options.textureSize;
  208. texture.height = this._options.textureSize;
  209. texture.format = format;
  210. texture.isReady = true;
  211. if (onLoad) {
  212. onLoad();
  213. }
  214. return texture;
  215. }
  216. public createRenderTargetTexture(size: any, options: boolean | RenderTargetCreationOptions): InternalTexture {
  217. let fullOptions = new RenderTargetCreationOptions();
  218. if (options !== undefined && typeof options === "object") {
  219. fullOptions.generateMipMaps = options.generateMipMaps;
  220. fullOptions.generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  221. fullOptions.generateStencilBuffer = fullOptions.generateDepthBuffer && options.generateStencilBuffer;
  222. fullOptions.type = options.type === undefined ? Engine.TEXTURETYPE_UNSIGNED_INT : options.type;
  223. fullOptions.samplingMode = options.samplingMode === undefined ? Texture.TRILINEAR_SAMPLINGMODE : options.samplingMode;
  224. } else {
  225. fullOptions.generateMipMaps = <boolean>options;
  226. fullOptions.generateDepthBuffer = true;
  227. fullOptions.generateStencilBuffer = false;
  228. fullOptions.type = Engine.TEXTURETYPE_UNSIGNED_INT;
  229. fullOptions.samplingMode = Texture.TRILINEAR_SAMPLINGMODE;
  230. }
  231. var texture = new InternalTexture(this, InternalTexture.DATASOURCE_RENDERTARGET);
  232. var width = size.width || size;
  233. var height = size.height || size;
  234. texture._depthStencilBuffer = {};
  235. texture._framebuffer = {};
  236. texture.baseWidth = width;
  237. texture.baseHeight = height;
  238. texture.width = width;
  239. texture.height = height;
  240. texture.isReady = true;
  241. texture.samples = 1;
  242. texture.generateMipMaps = fullOptions.generateMipMaps;
  243. texture.samplingMode = fullOptions.samplingMode;
  244. texture.type = fullOptions.type;
  245. texture._generateDepthBuffer = fullOptions.generateDepthBuffer;
  246. texture._generateStencilBuffer = fullOptions.generateStencilBuffer;
  247. return texture;
  248. }
  249. public updateTextureSamplingMode(samplingMode: number, texture: InternalTexture): void {
  250. texture.samplingMode = samplingMode;
  251. }
  252. public bindFramebuffer(texture: InternalTexture, faceIndex?: number, requiredWidth?: number, requiredHeight?: number, forceFullscreenViewport?: boolean): void {
  253. if (this._currentRenderTarget) {
  254. this.unBindFramebuffer(this._currentRenderTarget);
  255. }
  256. this._currentRenderTarget = texture;
  257. this._currentFramebuffer = texture._MSAAFramebuffer ? texture._MSAAFramebuffer : texture._framebuffer;
  258. if (this._cachedViewport && !forceFullscreenViewport) {
  259. this.setViewport(this._cachedViewport, requiredWidth, requiredHeight);
  260. }
  261. }
  262. public unBindFramebuffer(texture: InternalTexture, disableGenerateMipMaps = false, onBeforeUnbind?: () => void): void {
  263. this._currentRenderTarget = null;
  264. if (onBeforeUnbind) {
  265. if (texture._MSAAFramebuffer) {
  266. this._currentFramebuffer = texture._framebuffer;
  267. }
  268. onBeforeUnbind();
  269. }
  270. this._currentFramebuffer = null;
  271. }
  272. public createDynamicVertexBuffer(vertices: number[] | Float32Array): WebGLBuffer {
  273. var vbo = {
  274. capacity: 1,
  275. references: 1,
  276. is32Bits: false
  277. }
  278. return vbo;
  279. }
  280. public updateDynamicIndexBuffer(indexBuffer: WebGLBuffer, indices: IndicesArray, offset: number = 0): void {
  281. }
  282. public updateDynamicVertexBuffer(vertexBuffer: WebGLBuffer, vertices: number[] | Float32Array, offset?: number, count?: number): void {
  283. }
  284. public _bindTextureDirectly(target: number, texture: InternalTexture): void {
  285. if (this._activeTexturesCache[this._activeTexture] !== texture) {
  286. this._activeTexturesCache[this._activeTexture] = texture;
  287. }
  288. }
  289. public _bindTexture(channel: number, texture: InternalTexture): void {
  290. if (channel < 0) {
  291. return;
  292. }
  293. this._bindTextureDirectly(0, texture);
  294. }
  295. }
  296. }