123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022 |
- import { Nullable, IndicesArray, DataArray } from "../types";
- import { Engine } from "../Engines/engine";
- import { VertexBuffer } from "../Meshes/buffer";
- import { InternalTexture, InternalTextureSource } from "../Materials/Textures/internalTexture";
- import { IInternalTextureLoader } from "../Materials/Textures/internalTextureLoader";
- import { Texture } from "../Materials/Textures/texture";
- import { BaseTexture } from "../Materials/Textures/baseTexture";
- import { VideoTexture } from "../Materials/Textures/videoTexture";
- import { RenderTargetTexture } from "../Materials/Textures/renderTargetTexture";
- import { Effect } from "../Materials/effect";
- import { DataBuffer } from '../Meshes/dataBuffer';
- import { Tools } from "../Misc/tools";
- import { Observer } from "../Misc/observable";
- import { EnvironmentTextureTools, EnvironmentTextureSpecularInfoV1 } from "../Misc/environmentTextureTools";
- import { Scene } from "../scene";
- import { RenderTargetCreationOptions } from "../Materials/Textures/renderTargetCreationOptions";
- import { IPipelineContext } from './IPipelineContext';
- import { IMatrixLike, IVector2Like, IVector3Like, IVector4Like, IColor3Like, IColor4Like, IViewportLike } from '../Maths/math.like';
- import { Logger } from "../Misc/logger";
- import { Constants } from './constants';
- import { ThinEngine, ISceneLike } from './thinEngine';
- import { IWebRequest } from '../Misc/interfaces/iWebRequest';
- import { EngineStore } from './engineStore';
- import { ShaderCodeInliner } from "./Processors/shaderCodeInliner";
- import { WebGL2ShaderProcessor } from '../Engines/WebGL/webGL2ShaderProcessors';
- interface INativeEngine {
- dispose(): void;
- requestAnimationFrame(callback: () => void): void;
- createVertexArray(): any;
- deleteVertexArray(vertexArray: any): void;
- bindVertexArray(vertexArray: any): void;
- createIndexBuffer(data: ArrayBufferView, dynamic: boolean): any;
- deleteIndexBuffer(buffer: any): void;
- recordIndexBuffer(vertexArray: any, buffer: any): void;
- updateDynamicIndexBuffer(buffer: any, data: ArrayBufferView, startingIndex: number): void;
- createVertexBuffer(data: ArrayBufferView, dynamic: boolean): any;
- deleteVertexBuffer(buffer: any): void;
- recordVertexBuffer(vertexArray: any, buffer: any, location: number, byteOffset: number, byteStride: number, numElements: number, type: number, normalized: boolean): void;
- updateDynamicVertexBuffer(buffer: any, data: ArrayBufferView, byteOffset: number, byteLength: number): void;
- createProgram(vertexShader: string, fragmentShader: string): any;
- getUniforms(shaderProgram: any, uniformsNames: string[]): WebGLUniformLocation[];
- getAttributes(shaderProgram: any, attributeNames: string[]): number[];
- setProgram(program: any): void;
- setState(culling: boolean, zOffset: number, reverseSide: boolean): void;
- setZOffset(zOffset: number): void;
- getZOffset(): number;
- setDepthTest(enable: number): void;
- getDepthWrite(): boolean;
- setDepthWrite(enable: boolean): void;
- setColorWrite(enable: boolean): void;
- setBlendMode(blendMode: number): void;
- setMatrix(uniform: WebGLUniformLocation, matrix: Float32Array): void;
- setInt(uniform: WebGLUniformLocation, int: number): void;
- setIntArray(uniform: WebGLUniformLocation, array: Int32Array): void;
- setIntArray2(uniform: WebGLUniformLocation, array: Int32Array): void;
- setIntArray3(uniform: WebGLUniformLocation, array: Int32Array): void;
- setIntArray4(uniform: WebGLUniformLocation, array: Int32Array): void;
- setFloatArray(uniform: WebGLUniformLocation, array: Float32Array | number[]): void;
- setFloatArray2(uniform: WebGLUniformLocation, array: Float32Array | number[]): void;
- setFloatArray3(uniform: WebGLUniformLocation, array: Float32Array | number[]): void;
- setFloatArray4(uniform: WebGLUniformLocation, array: Float32Array | number[]): void;
- setMatrices(uniform: WebGLUniformLocation, matrices: Float32Array): void;
- setMatrix3x3(uniform: WebGLUniformLocation, matrix: Float32Array): void;
- setMatrix2x2(uniform: WebGLUniformLocation, matrix: Float32Array): void;
- setFloat(uniform: WebGLUniformLocation, value: number): void;
- setFloat2(uniform: WebGLUniformLocation, x: number, y: number): void;
- setFloat3(uniform: WebGLUniformLocation, x: number, y: number, z: number): void;
- setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): void;
- createTexture(): WebGLTexture;
- loadTexture(texture: WebGLTexture, data: ArrayBufferView, generateMips: boolean, invertY: boolean, onSuccess: () => void, onError: () => void): void;
- loadCubeTexture(texture: WebGLTexture, data: Array<ArrayBufferView>, generateMips: boolean, onSuccess: () => void, onError: () => void): void;
- loadCubeTextureWithMips(texture: WebGLTexture, data: Array<Array<ArrayBufferView>>, onSuccess: () => void, onError: () => void): void;
- getTextureWidth(texture: WebGLTexture): number;
- getTextureHeight(texture: WebGLTexture): number;
- setTextureSampling(texture: WebGLTexture, filter: number): void; // filter is a NativeFilter.XXXX value.
- setTextureWrapMode(texture: WebGLTexture, addressModeU: number, addressModeV: number, addressModeW: number): void; // addressModes are NativeAddressMode.XXXX values.
- setTextureAnisotropicLevel(texture: WebGLTexture, value: number): void;
- setTexture(uniform: WebGLUniformLocation, texture: Nullable<WebGLTexture>): void;
- deleteTexture(texture: Nullable<WebGLTexture>): void;
- createFramebuffer(texture: WebGLTexture, width: number, height: number, format: number, samplingMode: number, generateStencilBuffer: boolean, generateDepthBuffer: boolean, generateMips: boolean): WebGLFramebuffer;
- deleteFramebuffer(framebuffer: WebGLFramebuffer): void;
- bindFramebuffer(framebuffer: WebGLFramebuffer): void;
- unbindFramebuffer(framebuffer: WebGLFramebuffer): void;
- drawIndexed(fillMode: number, indexStart: number, indexCount: number): void;
- draw(fillMode: number, vertexStart: number, vertexCount: number): void;
- clear(flags: number): void;
- clearColor(r: number, g: number, b: number, a: number): void;
- clearDepth(depth: number): void;
- clearStencil(stencil: number): void;
- getRenderWidth(): number;
- getRenderHeight(): number;
- setViewPort(x: number, y: number, width: number, height: number): void;
- }
- class NativePipelineContext implements IPipelineContext {
- // TODO: async should be true?
- public isAsync = false;
- public isReady = false;
- public _getVertexShaderCode(): string | null {
- return null;
- }
- public _getFragmentShaderCode(): string | null {
- return null;
- }
- // TODO: what should this do?
- public _handlesSpectorRebuildCallback(onCompiled: (compiledObject: any) => void): void {
- throw new Error("Not implemented");
- }
- public nativeProgram: any;
- private _valueCache: { [key: string]: any } = {};
- private _uniforms: { [key: string]: Nullable<WebGLUniformLocation> };
- public engine: NativeEngine;
- public context?: WebGLRenderingContext;
- public vertexShader?: WebGLShader;
- public fragmentShader?: WebGLShader;
- public isParallelCompiled: boolean;
- public onCompiled?: () => void;
- public transformFeedback?: WebGLTransformFeedback | null;
- public _fillEffectInformation(effect: Effect, uniformBuffersNames: { [key: string]: number }, uniformsNames: string[], uniforms: { [key: string]: Nullable<WebGLUniformLocation> }, samplerList: string[], samplers: { [key: string]: number }, attributesNames: string[], attributes: number[]) {
- const engine = this.engine;
- if (engine.supportsUniformBuffers) {
- for (var name in uniformBuffersNames) {
- effect.bindUniformBlock(name, uniformBuffersNames[name]);
- }
- }
- const effectAvailableUniforms = this.engine.getUniforms(this, uniformsNames);
- effectAvailableUniforms.forEach((uniform, index) => {
- uniforms[uniformsNames[index]] = uniform;
- });
- this._uniforms = uniforms;
- let index: number;
- for (index = 0; index < samplerList.length; index++) {
- const sampler = effect.getUniform(samplerList[index]);
- if (sampler == null) {
- samplerList.splice(index, 1);
- index--;
- }
- }
- samplerList.forEach((name, index) => {
- samplers[name] = index;
- });
- attributes.push(...engine.getAttributes(this, attributesNames));
- }
- /**
- * Release all associated resources.
- **/
- public dispose() {
- this._uniforms = { };
- }
- /** @hidden */
- public _cacheMatrix(uniformName: string, matrix: IMatrixLike): boolean {
- var cache = this._valueCache[uniformName];
- var flag = matrix.updateFlag;
- if (cache !== undefined && cache === flag) {
- return false;
- }
- this._valueCache[uniformName] = flag;
- return true;
- }
- /** @hidden */
- public _cacheFloat2(uniformName: string, x: number, y: number): boolean {
- var cache = this._valueCache[uniformName];
- if (!cache) {
- cache = [x, y];
- this._valueCache[uniformName] = cache;
- return true;
- }
- var changed = false;
- if (cache[0] !== x) {
- cache[0] = x;
- changed = true;
- }
- if (cache[1] !== y) {
- cache[1] = y;
- changed = true;
- }
- return changed;
- }
- /** @hidden */
- public _cacheFloat3(uniformName: string, x: number, y: number, z: number): boolean {
- var cache = this._valueCache[uniformName];
- if (!cache) {
- cache = [x, y, z];
- this._valueCache[uniformName] = cache;
- return true;
- }
- var changed = false;
- if (cache[0] !== x) {
- cache[0] = x;
- changed = true;
- }
- if (cache[1] !== y) {
- cache[1] = y;
- changed = true;
- }
- if (cache[2] !== z) {
- cache[2] = z;
- changed = true;
- }
- return changed;
- }
- /** @hidden */
- public _cacheFloat4(uniformName: string, x: number, y: number, z: number, w: number): boolean {
- var cache = this._valueCache[uniformName];
- if (!cache) {
- cache = [x, y, z, w];
- this._valueCache[uniformName] = cache;
- return true;
- }
- var changed = false;
- if (cache[0] !== x) {
- cache[0] = x;
- changed = true;
- }
- if (cache[1] !== y) {
- cache[1] = y;
- changed = true;
- }
- if (cache[2] !== z) {
- cache[2] = z;
- changed = true;
- }
- if (cache[3] !== w) {
- cache[3] = w;
- changed = true;
- }
- return changed;
- }
- /**
- * Sets an interger value on a uniform variable.
- * @param uniformName Name of the variable.
- * @param value Value to be set.
- */
- public setInt(uniformName: string, value: number): void {
- var cache = this._valueCache[uniformName];
- if (cache !== undefined && cache === value) {
- return;
- }
- if (this.engine.setInt(this._uniforms[uniformName]!, value)) {
- this._valueCache[uniformName] = value;
- }
- }
- /**
- * Sets an int array on a uniform variable.
- * @param uniformName Name of the variable.
- * @param array array to be set.
- */
- public setIntArray(uniformName: string, array: Int32Array): void {
- this._valueCache[uniformName] = null;
- this.engine.setIntArray(this._uniforms[uniformName]!, array);
- }
- /**
- * Sets an int array 2 on a uniform variable. (Array is specified as single array eg. [1,2,3,4] will result in [[1,2],[3,4]] in the shader)
- * @param uniformName Name of the variable.
- * @param array array to be set.
- */
- public setIntArray2(uniformName: string, array: Int32Array): void {
- this._valueCache[uniformName] = null;
- this.engine.setIntArray2(this._uniforms[uniformName]!, array);
- }
- /**
- * Sets an int array 3 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6] will result in [[1,2,3],[4,5,6]] in the shader)
- * @param uniformName Name of the variable.
- * @param array array to be set.
- */
- public setIntArray3(uniformName: string, array: Int32Array): void {
- this._valueCache[uniformName] = null;
- this.engine.setIntArray3(this._uniforms[uniformName]!, array);
- }
- /**
- * Sets an int array 4 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6,7,8] will result in [[1,2,3,4],[5,6,7,8]] in the shader)
- * @param uniformName Name of the variable.
- * @param array array to be set.
- */
- public setIntArray4(uniformName: string, array: Int32Array): void {
- this._valueCache[uniformName] = null;
- this.engine.setIntArray4(this._uniforms[uniformName]!, array);
- }
- /**
- * Sets an float array on a uniform variable.
- * @param uniformName Name of the variable.
- * @param array array to be set.
- */
- public setFloatArray(uniformName: string, array: Float32Array): void {
- this._valueCache[uniformName] = null;
- this.engine.setFloatArray(this._uniforms[uniformName]!, array);
- }
- /**
- * Sets an float array 2 on a uniform variable. (Array is specified as single array eg. [1,2,3,4] will result in [[1,2],[3,4]] in the shader)
- * @param uniformName Name of the variable.
- * @param array array to be set.
- */
- public setFloatArray2(uniformName: string, array: Float32Array): void {
- this._valueCache[uniformName] = null;
- this.engine.setFloatArray2(this._uniforms[uniformName]!, array);
- }
- /**
- * Sets an float array 3 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6] will result in [[1,2,3],[4,5,6]] in the shader)
- * @param uniformName Name of the variable.
- * @param array array to be set.
- */
- public setFloatArray3(uniformName: string, array: Float32Array): void {
- this._valueCache[uniformName] = null;
- this.engine.setFloatArray3(this._uniforms[uniformName]!, array);
- }
- /**
- * Sets an float array 4 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6,7,8] will result in [[1,2,3,4],[5,6,7,8]] in the shader)
- * @param uniformName Name of the variable.
- * @param array array to be set.
- */
- public setFloatArray4(uniformName: string, array: Float32Array): void {
- this._valueCache[uniformName] = null;
- this.engine.setFloatArray4(this._uniforms[uniformName]!, array);
- }
- /**
- * Sets an array on a uniform variable.
- * @param uniformName Name of the variable.
- * @param array array to be set.
- */
- public setArray(uniformName: string, array: number[]): void {
- this._valueCache[uniformName] = null;
- this.engine.setArray(this._uniforms[uniformName]!, array);
- }
- /**
- * Sets an array 2 on a uniform variable. (Array is specified as single array eg. [1,2,3,4] will result in [[1,2],[3,4]] in the shader)
- * @param uniformName Name of the variable.
- * @param array array to be set.
- */
- public setArray2(uniformName: string, array: number[]): void {
- this._valueCache[uniformName] = null;
- this.engine.setArray2(this._uniforms[uniformName]!, array);
- }
- /**
- * Sets an array 3 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6] will result in [[1,2,3],[4,5,6]] in the shader)
- * @param uniformName Name of the variable.
- * @param array array to be set.
- * @returns this effect.
- */
- public setArray3(uniformName: string, array: number[]): void {
- this._valueCache[uniformName] = null;
- this.engine.setArray3(this._uniforms[uniformName]!, array);
- }
- /**
- * Sets an array 4 on a uniform variable. (Array is specified as single array eg. [1,2,3,4,5,6,7,8] will result in [[1,2,3,4],[5,6,7,8]] in the shader)
- * @param uniformName Name of the variable.
- * @param array array to be set.
- */
- public setArray4(uniformName: string, array: number[]): void {
- this._valueCache[uniformName] = null;
- this.engine.setArray4(this._uniforms[uniformName]!, array);
- }
- /**
- * Sets matrices on a uniform variable.
- * @param uniformName Name of the variable.
- * @param matrices matrices to be set.
- */
- public setMatrices(uniformName: string, matrices: Float32Array): void {
- if (!matrices) {
- return;
- }
- this._valueCache[uniformName] = null;
- this.engine.setMatrices(this._uniforms[uniformName]!, matrices);
- }
- /**
- * Sets matrix on a uniform variable.
- * @param uniformName Name of the variable.
- * @param matrix matrix to be set.
- */
- public setMatrix(uniformName: string, matrix: IMatrixLike): void {
- if (this._cacheMatrix(uniformName, matrix)) {
- if (!this.engine.setMatrices(this._uniforms[uniformName]!, matrix.toArray() as Float32Array)) {
- this._valueCache[uniformName] = null;
- }
- }
- }
- /**
- * Sets a 3x3 matrix on a uniform variable. (Speicified as [1,2,3,4,5,6,7,8,9] will result in [1,2,3][4,5,6][7,8,9] matrix)
- * @param uniformName Name of the variable.
- * @param matrix matrix to be set.
- */
- public setMatrix3x3(uniformName: string, matrix: Float32Array): void {
- this._valueCache[uniformName] = null;
- this.engine.setMatrix3x3(this._uniforms[uniformName]!, matrix);
- }
- /**
- * Sets a 2x2 matrix on a uniform variable. (Speicified as [1,2,3,4] will result in [1,2][3,4] matrix)
- * @param uniformName Name of the variable.
- * @param matrix matrix to be set.
- */
- public setMatrix2x2(uniformName: string, matrix: Float32Array): void {
- this._valueCache[uniformName] = null;
- this.engine.setMatrix2x2(this._uniforms[uniformName]!, matrix);
- }
- /**
- * Sets a float on a uniform variable.
- * @param uniformName Name of the variable.
- * @param value value to be set.
- * @returns this effect.
- */
- public setFloat(uniformName: string, value: number): void {
- var cache = this._valueCache[uniformName];
- if (cache !== undefined && cache === value) {
- return;
- }
- if (this.engine.setFloat(this._uniforms[uniformName]!, value)) {
- this._valueCache[uniformName] = value;
- }
- }
- /**
- * Sets a boolean on a uniform variable.
- * @param uniformName Name of the variable.
- * @param bool value to be set.
- */
- public setBool(uniformName: string, bool: boolean): void {
- var cache = this._valueCache[uniformName];
- if (cache !== undefined && cache === bool) {
- return;
- }
- this._valueCache[uniformName] = bool;
- if (this.engine.setInt(this._uniforms[uniformName]!, bool ? 1 : 0)) {
- this._valueCache[uniformName] = bool ? 1 : 0;
- }
- }
- /**
- * Sets a Vector2 on a uniform variable.
- * @param uniformName Name of the variable.
- * @param vector2 vector2 to be set.
- */
- public setVector2(uniformName: string, vector2: IVector2Like): void {
- if (this._cacheFloat2(uniformName, vector2.x, vector2.y)) {
- if (!this.engine.setFloat2(this._uniforms[uniformName]!, vector2.x, vector2.y)) {
- this._valueCache[uniformName] = null;
- }
- }
- }
- /**
- * Sets a float2 on a uniform variable.
- * @param uniformName Name of the variable.
- * @param x First float in float2.
- * @param y Second float in float2.
- */
- public setFloat2(uniformName: string, x: number, y: number): void {
- if (this._cacheFloat2(uniformName, x, y)) {
- if (!this.engine.setFloat2(this._uniforms[uniformName]!, x, y)) {
- this._valueCache[uniformName] = null;
- }
- }
- }
- /**
- * Sets a Vector3 on a uniform variable.
- * @param uniformName Name of the variable.
- * @param vector3 Value to be set.
- */
- public setVector3(uniformName: string, vector3: IVector3Like): void {
- if (this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z)) {
- if (!this.engine.setFloat3(this._uniforms[uniformName]!, vector3.x, vector3.y, vector3.z)) {
- this._valueCache[uniformName] = null;
- }
- }
- }
- /**
- * Sets a float3 on a uniform variable.
- * @param uniformName Name of the variable.
- * @param x First float in float3.
- * @param y Second float in float3.
- * @param z Third float in float3.
- */
- public setFloat3(uniformName: string, x: number, y: number, z: number): void {
- if (this._cacheFloat3(uniformName, x, y, z)) {
- if (!this.engine.setFloat3(this._uniforms[uniformName]!, x, y, z)) {
- this._valueCache[uniformName] = null;
- }
- }
- }
- /**
- * Sets a Vector4 on a uniform variable.
- * @param uniformName Name of the variable.
- * @param vector4 Value to be set.
- */
- public setVector4(uniformName: string, vector4: IVector4Like): void {
- if (this._cacheFloat4(uniformName, vector4.x, vector4.y, vector4.z, vector4.w)) {
- if (!this.engine.setFloat4(this._uniforms[uniformName]!, vector4.x, vector4.y, vector4.z, vector4.w)) {
- this._valueCache[uniformName] = null;
- }
- }
- }
- /**
- * Sets a float4 on a uniform variable.
- * @param uniformName Name of the variable.
- * @param x First float in float4.
- * @param y Second float in float4.
- * @param z Third float in float4.
- * @param w Fourth float in float4.
- * @returns this effect.
- */
- public setFloat4(uniformName: string, x: number, y: number, z: number, w: number): void {
- if (this._cacheFloat4(uniformName, x, y, z, w)) {
- if (!this.engine.setFloat4(this._uniforms[uniformName]!, x, y, z, w)) {
- this._valueCache[uniformName] = null;
- }
- }
- }
- /**
- * Sets a Color3 on a uniform variable.
- * @param uniformName Name of the variable.
- * @param color3 Value to be set.
- */
- public setColor3(uniformName: string, color3: IColor3Like): void {
- if (this._cacheFloat3(uniformName, color3.r, color3.g, color3.b)) {
- if (!this.engine.setFloat3(this._uniforms[uniformName]!, color3.r, color3.g, color3.b)) {
- this._valueCache[uniformName] = null;
- }
- }
- }
- /**
- * Sets a Color4 on a uniform variable.
- * @param uniformName Name of the variable.
- * @param color3 Value to be set.
- * @param alpha Alpha value to be set.
- */
- public setColor4(uniformName: string, color3: IColor3Like, alpha: number): void {
- if (this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha)) {
- if (!this.engine.setFloat4(this._uniforms[uniformName]!, color3.r, color3.g, color3.b, alpha)) {
- this._valueCache[uniformName] = null;
- }
- }
- }
- /**
- * Sets a Color4 on a uniform variable
- * @param uniformName defines the name of the variable
- * @param color4 defines the value to be set
- */
- public setDirectColor4(uniformName: string, color4: IColor4Like): void {
- if (this._cacheFloat4(uniformName, color4.r, color4.g, color4.b, color4.a)) {
- if (!this.engine.setFloat4(this._uniforms[uniformName]!, color4.r, color4.g, color4.b, color4.a)) {
- this._valueCache[uniformName] = null;
- }
- }
- }
- }
- /**
- * Container for accessors for natively-stored mesh data buffers.
- */
- class NativeDataBuffer extends DataBuffer {
- /**
- * Accessor value used to identify/retrieve a natively-stored index buffer.
- */
- public nativeIndexBuffer?: any;
- /**
- * Accessor value used to identify/retrieve a natively-stored vertex buffer.
- */
- public nativeVertexBuffer?: any;
- }
- // TODO: change this to match bgfx.
- // Must match Filter enum in SpectreEngine.h.
- class NativeFilter {
- public static readonly POINT = 0;
- public static readonly MINPOINT_MAGPOINT_MIPPOINT = NativeFilter.POINT;
- public static readonly BILINEAR = 1;
- public static readonly MINLINEAR_MAGLINEAR_MIPPOINT = NativeFilter.BILINEAR;
- public static readonly TRILINEAR = 2;
- public static readonly MINLINEAR_MAGLINEAR_MIPLINEAR = NativeFilter.TRILINEAR;
- public static readonly ANISOTROPIC = 3;
- public static readonly POINT_COMPARE = 4;
- public static readonly TRILINEAR_COMPARE = 5;
- public static readonly MINBILINEAR_MAGPOINT = 6;
- public static readonly MINLINEAR_MAGPOINT_MIPLINEAR = NativeFilter.MINBILINEAR_MAGPOINT;
- public static readonly MINPOINT_MAGPOINT_MIPLINEAR = 7;
- public static readonly MINPOINT_MAGLINEAR_MIPPOINT = 8;
- public static readonly MINPOINT_MAGLINEAR_MIPLINEAR = 9;
- public static readonly MINLINEAR_MAGPOINT_MIPPOINT = 10;
- }
- // depth test values
- // Values match bgfx defines
- class DepthTest {
- public static readonly DEPTH_TEST_LESS = 16;
- public static readonly DEPTH_TEST_LEQUAL = 32;
- public static readonly DEPTH_TEST_EQUAL = 48;
- public static readonly DEPTH_TEST_GEQUAL = 64;
- public static readonly DEPTH_TEST_GREATER = 80;
- public static readonly DEPTH_TEST_NOTEQUAL = 96;
- public static readonly DEPTH_TEST_NEVER = 112;
- public static readonly DEPTH_TEST_ALWAYS = 128;
- }
- // these flags match bgfx.
- class NativeClearFlags
- {
- public static readonly CLEAR_COLOR = 1;
- public static readonly CLEAR_DEPTH = 2;
- public static readonly CLEAR_STENCIL = 4;
- }
- // TODO: change this to match bgfx.
- // Must match AddressMode enum in SpectreEngine.h.
- class NativeAddressMode {
- public static readonly WRAP = 0;
- public static readonly MIRROR = 1;
- public static readonly CLAMP = 2;
- public static readonly BORDER = 3;
- public static readonly MIRROR_ONCE = 4;
- }
- class NativeTextureFormat {
- public static readonly RGBA8 = 0;
- public static readonly RGBA32F = 1;
- }
- /** @hidden */
- class NativeTexture extends InternalTexture {
- public getInternalTexture(): InternalTexture {
- return this;
- }
- public getViewCount(): number {
- return 1;
- }
- }
- /** @hidden */
- declare var _native: any;
- /** @hidden */
- export class NativeEngine extends Engine {
- private readonly _native: INativeEngine = new _native.Engine();
- /** Defines the invalid handle returned by bgfx when resource creation goes wrong */
- private readonly INVALID_HANDLE = 65535;
- private _boundBuffersVertexArray: any = null;
- private _currentDepthTest: number = DepthTest.DEPTH_TEST_LEQUAL;
- public getHardwareScalingLevel(): number {
- return 1.0;
- }
- public constructor() {
- super(null);
- this._webGLVersion = 2;
- this.disableUniformBuffers = true;
- // TODO: Initialize this more correctly based on the hardware capabilities.
- // Init caps
- this._caps = {
- maxTexturesImageUnits: 16,
- maxVertexTextureImageUnits: 16,
- maxCombinedTexturesImageUnits: 32,
- maxTextureSize: 512,
- maxCubemapTextureSize: 512,
- maxRenderTextureSize: 512,
- maxVertexAttribs: 16,
- maxVaryingVectors: 16,
- maxFragmentUniformVectors: 16,
- maxVertexUniformVectors: 16,
- standardDerivatives: true,
- astc: null,
- pvrtc: null,
- etc1: null,
- etc2: null,
- bptc: null,
- maxAnisotropy: 16, // TODO: Retrieve this smartly. Currently set to D3D11 maximum allowable value.
- uintIndices: true,
- fragmentDepthSupported: false,
- highPrecisionShaderSupported: true,
- colorBufferFloat: false,
- textureFloat: false,
- textureFloatLinearFiltering: false,
- textureFloatRender: false,
- textureHalfFloat: false,
- textureHalfFloatLinearFiltering: false,
- textureHalfFloatRender: false,
- textureLOD: true,
- drawBuffersExtension: false,
- depthTextureExtension: false,
- vertexArrayObject: true,
- instancedArrays: false,
- canUseTimestampForTimerQuery: false,
- blendMinMax: false,
- maxMSAASamples: 1
- };
- Tools.Log("Babylon Native (v" + Engine.Version + ") launched");
- // Wrappers
- if (typeof URL === "undefined") {
- (window.URL as any) = {
- createObjectURL: function() { },
- revokeObjectURL: function() { }
- };
- }
- if (typeof Blob === "undefined") {
- (window.Blob as any) = function() { };
- }
- // Shader processor
- this._shaderProcessor = new WebGL2ShaderProcessor();
- }
- public dispose(): void {
- super.dispose();
- if (this._boundBuffersVertexArray) {
- this._native.deleteVertexArray(this._boundBuffersVertexArray);
- }
- this._native.dispose();
- }
- /**
- * Can be used to override the current requestAnimationFrame requester.
- * @hidden
- */
- protected _queueNewFrame(bindedRenderFunction: any, requester?: any): number {
- // Use the provided requestAnimationFrame, unless the requester is the window. In that case, we will default to the Babylon Native version of requestAnimationFrame.
- if (requester.requestAnimationFrame && requester !== window) {
- requester.requestAnimationFrame(bindedRenderFunction);
- } else {
- this._native.requestAnimationFrame(bindedRenderFunction);
- }
- return 0;
- }
- /**
- * Override default engine behavior.
- * @param color
- * @param backBuffer
- * @param depth
- * @param stencil
- */
- public _bindUnboundFramebuffer(framebuffer: Nullable<WebGLFramebuffer>) {
- if (this._currentFramebuffer !== framebuffer) {
- if (this._currentFramebuffer) {
- this._native.unbindFramebuffer(this._currentFramebuffer!);
- }
- if (framebuffer) {
- this._native.bindFramebuffer(framebuffer);
- }
- this._currentFramebuffer = framebuffer;
- }
- }
- /**
- * Gets host document
- * @returns the host document object
- */
- public getHostDocument(): Nullable<Document> {
- return null;
- }
- public clear(color: Nullable<IColor4Like>, backBuffer: boolean, depth: boolean, stencil: boolean = false): void {
- var mode = 0;
- if (backBuffer && color) {
- this._native.clearColor(color.r, color.g, color.b, color.a !== undefined ? color.a : 1.0);
- mode |= NativeClearFlags.CLEAR_COLOR;
- }
- if (depth) {
- this._native.clearDepth(1.0);
- mode |= NativeClearFlags.CLEAR_DEPTH;
- }
- if (stencil) {
- this._native.clearStencil(0);
- mode |= NativeClearFlags.CLEAR_STENCIL;
- }
- this._native.clear(mode);
- }
- public createIndexBuffer(indices: IndicesArray, updateable?: boolean): NativeDataBuffer {
- const data = this._normalizeIndexData(indices);
- const buffer = new NativeDataBuffer();
- buffer.references = 1;
- buffer.is32Bits = (data.BYTES_PER_ELEMENT === 4);
- if (data.length) {
- buffer.nativeIndexBuffer = this._native.createIndexBuffer(data, updateable ?? false);
- if (buffer.nativeVertexBuffer === this.INVALID_HANDLE) {
- throw new Error("Could not create a native index buffer.");
- }
- } else {
- buffer.nativeVertexBuffer = this.INVALID_HANDLE;
- }
- return buffer;
- }
- public createVertexBuffer(data: DataArray, updateable?: boolean): NativeDataBuffer {
- const buffer = new NativeDataBuffer();
- buffer.references = 1;
- buffer.nativeVertexBuffer = this._native.createVertexBuffer(ArrayBuffer.isView(data) ? data : new Float32Array(data), updateable ?? false);
- if (buffer.nativeVertexBuffer === this.INVALID_HANDLE) {
- throw new Error("Could not create a native vertex buffer.");
- }
- return buffer;
- }
- protected _recordVertexArrayObject(vertexArray: any, vertexBuffers: { [key: string]: VertexBuffer; }, indexBuffer: Nullable<NativeDataBuffer>, effect: Effect): void {
- if (indexBuffer) {
- this._native.recordIndexBuffer(vertexArray, indexBuffer.nativeIndexBuffer);
- }
- const attributes = effect.getAttributesNames();
- for (let index = 0; index < attributes.length; index++) {
- const location = effect.getAttributeLocation(index);
- if (location >= 0) {
- const kind = attributes[index];
- const vertexBuffer = vertexBuffers[kind];
- if (vertexBuffer) {
- const buffer = vertexBuffer.getBuffer() as Nullable<NativeDataBuffer>;
- if (buffer) {
- this._native.recordVertexBuffer(
- vertexArray,
- buffer.nativeVertexBuffer,
- location,
- vertexBuffer.byteOffset,
- vertexBuffer.byteStride,
- vertexBuffer.getSize(),
- vertexBuffer.type,
- vertexBuffer.normalized);
- }
- }
- }
- }
- }
- public bindBuffers(vertexBuffers: { [key: string]: VertexBuffer; }, indexBuffer: Nullable<NativeDataBuffer>, effect: Effect): void {
- if (this._boundBuffersVertexArray) {
- this._native.deleteVertexArray(this._boundBuffersVertexArray);
- }
- this._boundBuffersVertexArray = this._native.createVertexArray();
- this._recordVertexArrayObject(this._boundBuffersVertexArray, vertexBuffers, indexBuffer, effect);
- this._native.bindVertexArray(this._boundBuffersVertexArray);
- }
- public recordVertexArrayObject(vertexBuffers: { [key: string]: VertexBuffer; }, indexBuffer: Nullable<NativeDataBuffer>, effect: Effect): WebGLVertexArrayObject {
- const vertexArray = this._native.createVertexArray();
- this._recordVertexArrayObject(vertexArray, vertexBuffers, indexBuffer, effect);
- return vertexArray;
- }
- public bindVertexArrayObject(vertexArray: WebGLVertexArrayObject): void {
- this._native.bindVertexArray(vertexArray);
- }
- public releaseVertexArrayObject(vertexArray: WebGLVertexArrayObject) {
- this._native.deleteVertexArray(vertexArray);
- }
- public getAttributes(pipelineContext: IPipelineContext, attributesNames: string[]): number[] {
- const nativePipelineContext = pipelineContext as NativePipelineContext;
- return this._native.getAttributes(nativePipelineContext.nativeProgram, attributesNames);
- }
- /**
- * Draw a list of indexed primitives
- * @param fillMode defines the primitive to use
- * @param indexStart defines the starting index
- * @param indexCount defines the number of index to draw
- * @param instancesCount defines the number of instances to draw (if instanciation is enabled)
- */
- public drawElementsType(fillMode: number, indexStart: number, indexCount: number, instancesCount?: number): void {
- // Apply states
- this._drawCalls.addCount(1, false);
- // TODO: Make this implementation more robust like core Engine version.
- // Render
- //var indexFormat = this._uintIndicesCurrentlySet ? this._gl.UNSIGNED_INT : this._gl.UNSIGNED_SHORT;
- //var mult = this._uintIndicesCurrentlySet ? 4 : 2;
- // if (instancesCount) {
- // this._gl.drawElementsInstanced(drawMode, indexCount, indexFormat, indexStart * mult, instancesCount);
- // } else {
- this._native.drawIndexed(fillMode, indexStart, indexCount);
- // }
- }
- /**
- * Draw a list of unindexed primitives
- * @param fillMode defines the primitive to use
- * @param verticesStart defines the index of first vertex to draw
- * @param verticesCount defines the count of vertices to draw
- * @param instancesCount defines the number of instances to draw (if instanciation is enabled)
- */
- public drawArraysType(fillMode: number, verticesStart: number, verticesCount: number, instancesCount?: number): void {
- // Apply states
- this._drawCalls.addCount(1, false);
- // TODO: Make this implementation more robust like core Engine version.
- // if (instancesCount) {
- // this._gl.drawArraysInstanced(drawMode, verticesStart, verticesCount, instancesCount);
- // } else {
- this._native.draw(fillMode, verticesStart, verticesCount);
- // }
- }
- public createPipelineContext(): IPipelineContext {
- return new NativePipelineContext();
- }
- public _preparePipelineContext(pipelineContext: IPipelineContext, vertexSourceCode: string, fragmentSourceCode: string, createAsRaw: boolean, rebuildRebind: any, defines: Nullable<string>, transformFeedbackVaryings: Nullable<string[]>) {
- const nativePipelineContext = pipelineContext as NativePipelineContext;
- if (createAsRaw) {
- nativePipelineContext.nativeProgram = this.createRawShaderProgram(pipelineContext, vertexSourceCode, fragmentSourceCode, undefined, transformFeedbackVaryings);
- }
- else {
- nativePipelineContext.nativeProgram = this.createShaderProgram(pipelineContext, vertexSourceCode, fragmentSourceCode, defines, undefined, transformFeedbackVaryings);
- }
- }
- /** @hidden */
- public _isRenderingStateCompiled(pipelineContext: IPipelineContext): boolean {
- // TODO: support async shader compilcation
- return true;
- }
- /** @hidden */
- public _executeWhenRenderingStateIsCompiled(pipelineContext: IPipelineContext, action: () => void) {
- // TODO: support async shader compilcation
- action();
- }
- public createRawShaderProgram(pipelineContext: IPipelineContext, vertexCode: string, fragmentCode: string, context?: WebGLRenderingContext, transformFeedbackVaryings: Nullable<string[]> = null): any {
- throw new Error("Not Supported");
- }
- public createShaderProgram(pipelineContext: IPipelineContext, vertexCode: string, fragmentCode: string, defines: Nullable<string>, context?: WebGLRenderingContext, transformFeedbackVaryings: Nullable<string[]> = null): any {
- this.onBeforeShaderCompilationObservable.notifyObservers(this);
- const vertexInliner = new ShaderCodeInliner(vertexCode);
- vertexInliner.processCode();
- vertexCode = vertexInliner.code;
- const fragmentInliner = new ShaderCodeInliner(fragmentCode);
- fragmentInliner.processCode();
- fragmentCode = fragmentInliner.code;
- vertexCode = ThinEngine._ConcatenateShader(vertexCode, defines);
- fragmentCode = ThinEngine._ConcatenateShader(fragmentCode, defines);
- const program = this._native.createProgram(vertexCode, fragmentCode);
- this.onAfterShaderCompilationObservable.notifyObservers(this);
- return program;
- }
- protected _setProgram(program: WebGLProgram): void {
- if (this._currentProgram !== program) {
- this._native.setProgram(program);
- this._currentProgram = program;
- }
- }
- public _releaseEffect(effect: Effect): void {
- // TODO
- }
- public _deletePipelineContext(pipelineContext: IPipelineContext): void {
- // TODO
- }
- public getUniforms(pipelineContext: IPipelineContext, uniformsNames: string[]): WebGLUniformLocation[] {
- const nativePipelineContext = pipelineContext as NativePipelineContext;
- return this._native.getUniforms(nativePipelineContext.nativeProgram, uniformsNames);
- }
- public bindUniformBlock(pipelineContext: IPipelineContext, blockName: string, index: number): void {
- // TODO
- throw new Error("Not Implemented");
- }
- public bindSamplers(effect: Effect): void {
- const nativePipelineContext = effect.getPipelineContext() as NativePipelineContext;
- this._setProgram(nativePipelineContext.nativeProgram);
- // TODO: share this with engine?
- var samplers = effect.getSamplers();
- for (var index = 0; index < samplers.length; index++) {
- var uniform = effect.getUniform(samplers[index]);
- if (uniform) {
- this._boundUniforms[index] = uniform;
- }
- }
- this._currentEffect = null;
- }
- public setMatrix(uniform: WebGLUniformLocation, matrix: IMatrixLike): void {
- if (!uniform) {
- return;
- }
- this._native.setMatrix(uniform, matrix.toArray() as Float32Array);
- }
- public getRenderWidth(useScreen = false): number {
- if (!useScreen && this._currentRenderTarget) {
- return this._currentRenderTarget.width;
- }
- return this._native.getRenderWidth();
- }
- public getRenderHeight(useScreen = false): number {
- if (!useScreen && this._currentRenderTarget) {
- return this._currentRenderTarget.height;
- }
- return this._native.getRenderHeight();
- }
- public setViewport(viewport: IViewportLike, requiredWidth?: number, requiredHeight?: number): void {
- this._cachedViewport = viewport;
- this._native.setViewPort(viewport.x, viewport.y, viewport.width, viewport.height);
- }
- public setState(culling: boolean, zOffset: number = 0, force?: boolean, reverseSide = false): void {
- this._native.setState(culling, zOffset, reverseSide);
- }
- /**
- * Set the z offset to apply to current rendering
- * @param value defines the offset to apply
- */
- public setZOffset(value: number): void {
- this._native.setZOffset(value);
- }
- /**
- * Gets the current value of the zOffset
- * @returns the current zOffset state
- */
- public getZOffset(): number {
- return this._native.getZOffset();
- }
- /**
- * Enable or disable depth buffering
- * @param enable defines the state to set
- */
- public setDepthBuffer(enable: boolean): void {
- this._native.setDepthTest(enable ? this._currentDepthTest : DepthTest.DEPTH_TEST_ALWAYS);
- }
- /**
- * Gets a boolean indicating if depth writing is enabled
- * @returns the current depth writing state
- */
- public getDepthWrite(): boolean {
- return this._native.getDepthWrite();
- }
- public setDepthFunctionToGreater(): void {
- this._currentDepthTest = DepthTest.DEPTH_TEST_GREATER;
- this._native.setDepthTest(this._currentDepthTest);
- }
- public setDepthFunctionToGreaterOrEqual(): void {
- this._currentDepthTest = DepthTest.DEPTH_TEST_GEQUAL;
- this._native.setDepthTest(this._currentDepthTest);
- }
- public setDepthFunctionToLess(): void {
- this._currentDepthTest = DepthTest.DEPTH_TEST_LESS;
- this._native.setDepthTest(this._currentDepthTest);
- }
- public setDepthFunctionToLessOrEqual(): void {
- this._currentDepthTest = DepthTest.DEPTH_TEST_LEQUAL;
- this._native.setDepthTest(this._currentDepthTest);
- }
- /**
- * Enable or disable depth writing
- * @param enable defines the state to set
- */
- public setDepthWrite(enable: boolean): void {
- this._native.setDepthWrite(enable);
- }
- /**
- * Enable or disable color writing
- * @param enable defines the state to set
- */
- public setColorWrite(enable: boolean): void {
- this._native.setColorWrite(enable);
- this._colorWrite = enable;
- }
- /**
- * Gets a boolean indicating if color writing is enabled
- * @returns the current color writing state
- */
- public getColorWrite(): boolean {
- return this._colorWrite;
- }
- /**
- * Sets alpha constants used by some alpha blending modes
- * @param r defines the red component
- * @param g defines the green component
- * @param b defines the blue component
- * @param a defines the alpha component
- */
- public setAlphaConstants(r: number, g: number, b: number, a: number) {
- throw new Error("Setting alpha blend constant color not yet implemented.");
- }
- /**
- * Sets the current alpha mode
- * @param mode defines the mode to use (one of the BABYLON.Constants.ALPHA_XXX)
- * @param noDepthWriteChange defines if depth writing state should remains unchanged (false by default)
- * @see https://doc.babylonjs.com/resources/transparency_and_how_meshes_are_rendered
- */
- public setAlphaMode(mode: number, noDepthWriteChange: boolean = false): void {
- if (this._alphaMode === mode) {
- return;
- }
- this._native.setBlendMode(mode);
- if (!noDepthWriteChange) {
- this.setDepthWrite(mode === Constants.ALPHA_DISABLE);
- }
- this._alphaMode = mode;
- }
- /**
- * Gets the current alpha mode
- * @see https://doc.babylonjs.com/resources/transparency_and_how_meshes_are_rendered
- * @returns the current alpha mode
- */
- public getAlphaMode(): number {
- return this._alphaMode;
- }
- public setInt(uniform: WebGLUniformLocation, int: number): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setInt(uniform, int);
- return true;
- }
- public setIntArray(uniform: WebGLUniformLocation, array: Int32Array): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setIntArray(uniform, array);
- return true;
- }
- public setIntArray2(uniform: WebGLUniformLocation, array: Int32Array): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setIntArray2(uniform, array);
- return true;
- }
- public setIntArray3(uniform: WebGLUniformLocation, array: Int32Array): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setIntArray3(uniform, array);
- return true;
- }
- public setIntArray4(uniform: WebGLUniformLocation, array: Int32Array): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setIntArray4(uniform, array);
- return true;
- }
- public setFloatArray(uniform: WebGLUniformLocation, array: Float32Array): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloatArray(uniform, array);
- return true;
- }
- public setFloatArray2(uniform: WebGLUniformLocation, array: Float32Array): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloatArray2(uniform, array);
- return true;
- }
- public setFloatArray3(uniform: WebGLUniformLocation, array: Float32Array): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloatArray3(uniform, array);
- return true;
- }
- public setFloatArray4(uniform: WebGLUniformLocation, array: Float32Array): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloatArray4(uniform, array);
- return true;
- }
- public setArray(uniform: WebGLUniformLocation, array: number[]): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloatArray(uniform, array);
- return true;
- }
- public setArray2(uniform: WebGLUniformLocation, array: number[]): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloatArray2(uniform, array);
- return true;
- }
- public setArray3(uniform: WebGLUniformLocation, array: number[]): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloatArray3(uniform, array);
- return true;
- }
- public setArray4(uniform: WebGLUniformLocation, array: number[]): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloatArray4(uniform, array);
- return true;
- }
- public setMatrices(uniform: WebGLUniformLocation, matrices: Float32Array): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setMatrices(uniform, matrices);
- return true;
- }
- public setMatrix3x3(uniform: WebGLUniformLocation, matrix: Float32Array): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setMatrix3x3(uniform, matrix);
- return true;
- }
- public setMatrix2x2(uniform: WebGLUniformLocation, matrix: Float32Array): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setMatrix2x2(uniform, matrix);
- return true;
- }
- public setFloat(uniform: WebGLUniformLocation, value: number): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloat(uniform, value);
- return true;
- }
- public setFloat2(uniform: WebGLUniformLocation, x: number, y: number): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloat2(uniform, x, y);
- return true;
- }
- public setFloat3(uniform: WebGLUniformLocation, x: number, y: number, z: number): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloat3(uniform, x, y, z);
- return true;
- }
- public setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloat4(uniform, x, y, z, w);
- return true;
- }
- public setColor3(uniform: WebGLUniformLocation, color3: IColor3Like): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloat3(uniform, color3.r, color3.g, color3.b);
- return true;
- }
- public setColor4(uniform: WebGLUniformLocation, color3: IColor3Like, alpha: number): boolean {
- if (!uniform) {
- return false;
- }
- this._native.setFloat4(uniform, color3.r, color3.g, color3.b, alpha);
- return true;
- }
- public wipeCaches(bruteForce?: boolean): void {
- if (this.preventCacheWipeBetweenFrames) {
- return;
- }
- this.resetTextureCache();
- this._currentEffect = null;
- if (bruteForce) {
- this._currentProgram = null;
- this._stencilState.reset();
- this._depthCullingState.reset();
- this._alphaState.reset();
- }
- this._cachedVertexBuffers = null;
- this._cachedIndexBuffer = null;
- this._cachedEffectForVertexBuffers = null;
- }
- protected _createTexture(): WebGLTexture {
- return this._native.createTexture();
- }
- protected _deleteTexture(texture: Nullable<WebGLTexture>): void {
- this._native.deleteTexture(texture);
- }
- // TODO: Refactor to share more logic with babylon.engine.ts version.
- /**
- * Usually called from Texture.ts.
- * Passed information to create a WebGLTexture
- * @param url defines a value which contains one of the following:
- * * A conventional http URL, e.g. 'http://...' or 'file://...'
- * * A base64 string of in-line texture data, e.g. 'data:image/jpg;base64,/...'
- * * An indicator that data being passed using the buffer parameter, e.g. 'data:mytexture.jpg'
- * @param noMipmap defines a boolean indicating that no mipmaps shall be generated. Ignored for compressed textures. They must be in the file
- * @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)
- * @param scene needed for loading to the correct scene
- * @param samplingMode mode with should be used sample / access the texture (Default: Texture.TRILINEAR_SAMPLINGMODE)
- * @param onLoad optional callback to be called upon successful completion
- * @param onError optional callback to be called upon failure
- * @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
- * @param fallback an internal argument in case the function must be called again, due to etc1 not having alpha capabilities
- * @param format internal format. Default: RGB when extension is '.jpg' else RGBA. Ignored for compressed textures
- * @param forcedExtension defines the extension to use to pick the right loader
- * @param mimeType defines an optional mime type
- * @returns a InternalTexture for assignment back into BABYLON.Texture
- */
- public createTexture(url: Nullable<string>, noMipmap: boolean, invertY: boolean, scene: Nullable<ISceneLike>, samplingMode: number = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE,
- onLoad: Nullable<() => void> = null, onError: Nullable<(message: string, exception: any) => void> = null,
- buffer: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob | ImageBitmap> = null, fallback: Nullable<InternalTexture> = null, format: Nullable<number> = null,
- forcedExtension: Nullable<string> = null, mimeType?: string): InternalTexture {
- url = url || "";
- const fromData = url.substr(0, 5) === "data:";
- //const fromBlob = url.substr(0, 5) === "blob:";
- const isBase64 = fromData && url.indexOf(";base64,") !== -1;
- let texture = fallback ? fallback : new InternalTexture(this, InternalTextureSource.Url);
- const originalUrl = url;
- if (this._transformTextureUrl && !isBase64 && !fallback && !buffer) {
- url = this._transformTextureUrl(url);
- }
- // establish the file extension, if possible
- var lastDot = url.lastIndexOf('.');
- var extension = forcedExtension ? forcedExtension : (lastDot > -1 ? url.substring(lastDot).toLowerCase() : "");
- let loader: Nullable<IInternalTextureLoader> = null;
- for (let availableLoader of Engine._TextureLoaders) {
- if (availableLoader.canLoad(extension)) {
- loader = availableLoader;
- break;
- }
- }
- if (scene) {
- scene._addPendingData(texture);
- }
- texture.url = url;
- texture.generateMipMaps = !noMipmap;
- texture.samplingMode = samplingMode;
- texture.invertY = invertY;
- if (!this.doNotHandleContextLost) {
- // Keep a link to the buffer only if we plan to handle context lost
- texture._buffer = buffer;
- }
- let onLoadObserver: Nullable<Observer<InternalTexture>> = null;
- if (onLoad && !fallback) {
- onLoadObserver = texture.onLoadedObservable.add(onLoad);
- }
- if (!fallback) { this._internalTexturesCache.push(texture); }
- let onInternalError = (message?: string, exception?: any) => {
- if (scene) {
- scene._removePendingData(texture);
- }
- if (url === originalUrl) {
- if (onLoadObserver) {
- texture.onLoadedObservable.remove(onLoadObserver);
- }
- if (EngineStore.UseFallbackTexture) {
- this.createTexture(EngineStore.FallbackTexture, noMipmap, texture.invertY, scene, samplingMode, null, onError, buffer, texture);
- }
- if (onError) {
- onError((message || "Unknown error") + (EngineStore.UseFallbackTexture ? " - Fallback texture was used" : ""), exception);
- }
- }
- else {
- // fall back to the original url if the transformed url fails to load
- Logger.Warn(`Failed to load ${url}, falling back to ${originalUrl}`);
- this.createTexture(originalUrl, noMipmap, texture.invertY, scene, samplingMode, onLoad, onError, buffer, texture, format, forcedExtension, mimeType);
- }
- };
- // processing for non-image formats
- if (loader) {
- throw new Error("Loading textures from IInternalTextureLoader not yet implemented.");
- } else {
- const onload = (data: ArrayBufferView) => {
- const webGLTexture = texture._hardwareTexture?.underlyingResource;
- if (!webGLTexture) {
- if (scene) {
- scene._removePendingData(texture);
- }
- return;
- }
- this._native.loadTexture(webGLTexture, data, !noMipmap, invertY, () => {
- texture.baseWidth = this._native.getTextureWidth(webGLTexture);
- texture.baseHeight = this._native.getTextureHeight(webGLTexture);
- texture.width = texture.baseWidth;
- texture.height = texture.baseHeight;
- texture.isReady = true;
- var filter = this._getSamplingFilter(samplingMode);
- this._native.setTextureSampling(webGLTexture, filter);
- if (scene) {
- scene._removePendingData(texture);
- }
- texture.onLoadedObservable.notifyObservers(texture);
- texture.onLoadedObservable.clear();
- }, () => {
- throw new Error("Could not load a native texture.");
- });
- };
- if (fromData) {
- if (buffer instanceof ArrayBuffer) {
- onload(new Uint8Array(buffer));
- } else if (ArrayBuffer.isView(buffer)) {
- onload(buffer);
- } else if (typeof buffer === "string") {
- onload(new Uint8Array(Tools.DecodeBase64(buffer)));
- } else {
- throw new Error("Unsupported buffer type");
- }
- }
- else {
- if (isBase64) {
- onload(new Uint8Array(Tools.DecodeBase64(url)));
- }
- else {
- this._loadFile(url, (data) => onload(new Uint8Array(data as ArrayBuffer)), undefined, undefined, true, (request?: IWebRequest, exception?: any) => {
- onInternalError("Unable to load " + (request ? request.responseURL : url, exception));
- });
- }
- }
- }
- return texture;
- }
- /**
- * Creates a cube texture
- * @param rootUrl defines the url where the files to load is located
- * @param scene defines the current scene
- * @param files defines the list of files to load (1 per face)
- * @param noMipmap defines a boolean indicating that no mipmaps shall be generated (false by default)
- * @param onLoad defines an optional callback raised when the texture is loaded
- * @param onError defines an optional callback raised if there is an issue to load the texture
- * @param format defines the format of the data
- * @param forcedExtension defines the extension to use to pick the right loader
- * @param createPolynomials if a polynomial sphere should be created for the cube texture
- * @param lodScale defines the scale applied to environment texture. This manages the range of LOD level used for IBL according to the roughness
- * @param lodOffset defines the offset applied to environment texture. This manages first LOD level used for IBL according to the roughness
- * @param fallback defines texture to use while falling back when (compressed) texture file not found.
- * @returns the cube texture as an InternalTexture
- */
- public createCubeTexture(
- rootUrl: string,
- scene: Nullable<Scene>,
- files: Nullable<string[]>,
- noMipmap?: boolean,
- onLoad: Nullable<(data?: any) => void> = null,
- onError: Nullable<(message?: string, exception?: any) => void> = null,
- format?: number,
- forcedExtension: any = null,
- createPolynomials = false,
- lodScale: number = 0,
- lodOffset: number = 0,
- fallback: Nullable<InternalTexture> = null): InternalTexture
- {
- var texture = fallback ? fallback : new InternalTexture(this, InternalTextureSource.Cube);
- texture.isCube = true;
- texture.url = rootUrl;
- texture.generateMipMaps = !noMipmap;
- texture._lodGenerationScale = lodScale;
- texture._lodGenerationOffset = lodOffset;
- if (!this._doNotHandleContextLost) {
- texture._extension = forcedExtension;
- texture._files = files;
- }
- var lastDot = rootUrl.lastIndexOf('.');
- var extension = forcedExtension ? forcedExtension : (lastDot > -1 ? rootUrl.substring(lastDot).toLowerCase() : "");
- // TODO: use texture loader to load env files?
- if (extension === ".env") {
- const onloaddata = (data: ArrayBufferView) => {
- var info = EnvironmentTextureTools.GetEnvInfo(data)!;
- texture.width = info.width;
- texture.height = info.width;
- EnvironmentTextureTools.UploadEnvSpherical(texture, info);
- if (info.version !== 1) {
- throw new Error(`Unsupported babylon environment map version "${info.version}"`);
- }
- let specularInfo = info.specular as EnvironmentTextureSpecularInfoV1;
- if (!specularInfo) {
- throw new Error(`Nothing else parsed so far`);
- }
- texture._lodGenerationScale = specularInfo.lodGenerationScale;
- const imageData = EnvironmentTextureTools.CreateImageDataArrayBufferViews(data, info);
- texture.format = Constants.TEXTUREFORMAT_RGBA;
- texture.type = Constants.TEXTURETYPE_UNSIGNED_INT;
- texture.generateMipMaps = true;
- texture.getEngine().updateTextureSamplingMode(Texture.TRILINEAR_SAMPLINGMODE, texture);
- texture._isRGBD = true;
- texture.invertY = true;
- this._native.loadCubeTextureWithMips(texture._hardwareTexture!.underlyingResource, imageData, () => {
- texture.isReady = true;
- if (onLoad) {
- onLoad();
- }
- }, () => {
- throw new Error("Could not load a native cube texture.");
- });
- };
- if (files && files.length === 6) {
- throw new Error(`Multi-file loading not allowed on env files.`);
- }
- else {
- let onInternalError = (request?: IWebRequest, exception?: any) => {
- if (onError && request) {
- onError(request.status + " " + request.statusText, exception);
- }
- };
- this._loadFile(rootUrl, (data) => onloaddata(new Uint8Array(data as ArrayBuffer)), undefined, undefined, true, onInternalError);
- }
- }
- else {
- if (!files || files.length !== 6) {
- throw new Error("Cannot load cubemap because 6 files were not defined");
- }
- // Reorder from [+X, +Y, +Z, -X, -Y, -Z] to [+X, -X, +Y, -Y, +Z, -Z].
- const reorderedFiles = [files[0], files[3], files[1], files[4], files[2], files[5]];
- Promise.all(reorderedFiles.map((file) => Tools.LoadFileAsync(file).then((data) => new Uint8Array(data as ArrayBuffer)))).then((data) => {
- return new Promise((resolve, reject) => {
- this._native.loadCubeTexture(texture._hardwareTexture!.underlyingResource, data, !noMipmap, resolve, reject);
- });
- }).then(() => {
- texture.isReady = true;
- if (onLoad) {
- onLoad();
- }
- }, (error) => {
- if (onError) {
- onError(`Failed to load cubemap: ${error.message}`, error);
- }
- });
- }
- this._internalTexturesCache.push(texture);
- return texture;
- }
- // Returns a NativeFilter.XXXX value.
- private _getSamplingFilter(samplingMode: number): number {
- switch (samplingMode) {
- case Constants.TEXTURE_BILINEAR_SAMPLINGMODE:
- return NativeFilter.MINLINEAR_MAGLINEAR_MIPPOINT;
- case Constants.TEXTURE_TRILINEAR_SAMPLINGMODE:
- return NativeFilter.MINLINEAR_MAGLINEAR_MIPLINEAR;
- case Constants.TEXTURE_NEAREST_SAMPLINGMODE:
- return NativeFilter.MINPOINT_MAGPOINT_MIPLINEAR;
- case Constants.TEXTURE_NEAREST_NEAREST_MIPNEAREST:
- return NativeFilter.MINPOINT_MAGPOINT_MIPPOINT;
- case Constants.TEXTURE_NEAREST_LINEAR_MIPNEAREST:
- return NativeFilter.MINLINEAR_MAGPOINT_MIPPOINT;
- case Constants.TEXTURE_NEAREST_LINEAR_MIPLINEAR:
- return NativeFilter.MINLINEAR_MAGPOINT_MIPLINEAR;
- case Constants.TEXTURE_NEAREST_LINEAR:
- return NativeFilter.MINLINEAR_MAGPOINT_MIPLINEAR;
- case Constants.TEXTURE_NEAREST_NEAREST:
- return NativeFilter.MINPOINT_MAGPOINT_MIPPOINT;
- case Constants.TEXTURE_LINEAR_NEAREST_MIPNEAREST:
- return NativeFilter.MINPOINT_MAGLINEAR_MIPPOINT;
- case Constants.TEXTURE_LINEAR_NEAREST_MIPLINEAR:
- return NativeFilter.MINPOINT_MAGLINEAR_MIPLINEAR;
- case Constants.TEXTURE_LINEAR_LINEAR:
- return NativeFilter.MINLINEAR_MAGLINEAR_MIPLINEAR;
- case Constants.TEXTURE_LINEAR_NEAREST:
- return NativeFilter.MINPOINT_MAGLINEAR_MIPLINEAR;
- case Constants.TEXTURE_NEAREST_NEAREST_MIPLINEAR:
- return NativeFilter.MINPOINT_MAGPOINT_MIPLINEAR;
- case Constants.TEXTURE_LINEAR_LINEAR_MIPNEAREST:
- return NativeFilter.MINLINEAR_MAGLINEAR_MIPLINEAR;
- default:
- throw new Error("Unexpected sampling mode: " + samplingMode + ".");
- }
- }
- private static _GetNativeTextureFormat(format: number, type: number): number {
- if (format == Constants.TEXTUREFORMAT_RGBA && type == Constants.TEXTURETYPE_UNSIGNED_INT) {
- return NativeTextureFormat.RGBA8;
- }
- else if (format == Constants.TEXTUREFORMAT_RGBA && type == Constants.TEXTURETYPE_FLOAT) {
- return NativeTextureFormat.RGBA32F;
- }
- else {
- throw new Error("Unexpected texture format or type: format " + format + ", type " + type + ".");
- }
- }
- public createRenderTargetTexture(size: number | { width: number, height: number }, options: boolean | RenderTargetCreationOptions): NativeTexture {
- let fullOptions = new RenderTargetCreationOptions();
- if (options !== undefined && typeof options === "object") {
- fullOptions.generateMipMaps = options.generateMipMaps;
- fullOptions.generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
- fullOptions.generateStencilBuffer = fullOptions.generateDepthBuffer && options.generateStencilBuffer;
- fullOptions.type = options.type === undefined ? Constants.TEXTURETYPE_UNSIGNED_INT : options.type;
- fullOptions.samplingMode = options.samplingMode === undefined ? Constants.TEXTURE_TRILINEAR_SAMPLINGMODE : options.samplingMode;
- fullOptions.format = options.format === undefined ? Constants.TEXTUREFORMAT_RGBA : options.format;
- } else {
- fullOptions.generateMipMaps = <boolean>options;
- fullOptions.generateDepthBuffer = true;
- fullOptions.generateStencilBuffer = false;
- fullOptions.type = Constants.TEXTURETYPE_UNSIGNED_INT;
- fullOptions.samplingMode = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE;
- fullOptions.format = Constants.TEXTUREFORMAT_RGBA;
- }
- if (fullOptions.type === Constants.TEXTURETYPE_FLOAT && !this._caps.textureFloatLinearFiltering) {
- // if floating point linear (gl.FLOAT) then force to NEAREST_SAMPLINGMODE
- fullOptions.samplingMode = Constants.TEXTURE_NEAREST_SAMPLINGMODE;
- }
- else if (fullOptions.type === Constants.TEXTURETYPE_HALF_FLOAT && !this._caps.textureHalfFloatLinearFiltering) {
- // if floating point linear (HALF_FLOAT) then force to NEAREST_SAMPLINGMODE
- fullOptions.samplingMode = Constants.TEXTURE_NEAREST_SAMPLINGMODE;
- }
- var texture = new NativeTexture(this, InternalTextureSource.RenderTarget);
- var width = (<{ width: number, height: number }>size).width || <number>size;
- var height = (<{ width: number, height: number }>size).height || <number>size;
- if (fullOptions.type === Constants.TEXTURETYPE_FLOAT && !this._caps.textureFloat) {
- fullOptions.type = Constants.TEXTURETYPE_UNSIGNED_INT;
- Logger.Warn("Float textures are not supported. Render target forced to TEXTURETYPE_UNSIGNED_BYTE type");
- }
- var framebuffer = this._native.createFramebuffer(
- texture._hardwareTexture!.underlyingResource,
- width,
- height,
- NativeEngine._GetNativeTextureFormat(fullOptions.format, fullOptions.type),
- fullOptions.samplingMode!,
- fullOptions.generateStencilBuffer ? true : false,
- fullOptions.generateDepthBuffer,
- fullOptions.generateMipMaps ? true : false);
- texture._framebuffer = framebuffer;
- texture.baseWidth = width;
- texture.baseHeight = height;
- texture.width = width;
- texture.height = height;
- texture.isReady = true;
- texture.samples = 1;
- texture.generateMipMaps = fullOptions.generateMipMaps ? true : false;
- texture.samplingMode = fullOptions.samplingMode;
- texture.type = fullOptions.type;
- texture.format = fullOptions.format;
- texture._generateDepthBuffer = fullOptions.generateDepthBuffer;
- texture._generateStencilBuffer = fullOptions.generateStencilBuffer ? true : false;
- this._internalTexturesCache.push(texture);
- return texture;
- }
- public updateTextureSamplingMode(samplingMode: number, texture: InternalTexture): void {
- if (texture._hardwareTexture?.underlyingResource ?? false) {
- var filter = this._getSamplingFilter(samplingMode);
- this._native.setTextureSampling(texture._hardwareTexture?.underlyingResource, filter);
- }
- texture.samplingMode = samplingMode;
- }
- public bindFramebuffer(texture: InternalTexture, faceIndex?: number, requiredWidth?: number, requiredHeight?: number, forceFullscreenViewport?: boolean): void {
- if (faceIndex) {
- throw new Error("Cuboid frame buffers are not yet supported in NativeEngine.");
- }
- if (requiredWidth || requiredHeight) {
- throw new Error("Required width/height for frame buffers not yet supported in NativeEngine.");
- }
- if (forceFullscreenViewport) {
- throw new Error("forceFullscreenViewport for frame buffers not yet supported in NativeEngine.");
- }
- this._bindUnboundFramebuffer(texture._framebuffer);
- }
- public unBindFramebuffer(texture: InternalTexture, disableGenerateMipMaps = false, onBeforeUnbind?: () => void): void {
- if (disableGenerateMipMaps) {
- Logger.Warn("Disabling mipmap generation not yet supported in NativeEngine. Ignoring.");
- }
- if (onBeforeUnbind) {
- onBeforeUnbind();
- }
- this._bindUnboundFramebuffer(null);
- }
- public createDynamicVertexBuffer(data: DataArray): DataBuffer {
- return this.createVertexBuffer(data, true);
- }
- public updateDynamicIndexBuffer(indexBuffer: DataBuffer, indices: IndicesArray, offset: number = 0): void {
- const buffer = indexBuffer as NativeDataBuffer;
- const data = this._normalizeIndexData(indices);
- buffer.is32Bits = (data.BYTES_PER_ELEMENT === 4);
- this._native.updateDynamicIndexBuffer(buffer.nativeIndexBuffer, data, offset);
- }
- /**
- * Updates a dynamic vertex buffer.
- * @param vertexBuffer the vertex buffer to update
- * @param data the data used to update the vertex buffer
- * @param byteOffset the byte offset of the data (optional)
- * @param byteLength the byte length of the data (optional)
- */
- public updateDynamicVertexBuffer(vertexBuffer: DataBuffer, data: DataArray, byteOffset?: number, byteLength?: number): void {
- const buffer = vertexBuffer as NativeDataBuffer;
- const dataView = ArrayBuffer.isView(data) ? data : new Float32Array(data);
- this._native.updateDynamicVertexBuffer(
- buffer.nativeVertexBuffer,
- dataView,
- byteOffset ?? 0,
- byteLength ?? dataView.byteLength);
- }
- // TODO: Refactor to share more logic with base Engine implementation.
- protected _setTexture(channel: number, texture: Nullable<BaseTexture>, isPartOfTextureArray = false, depthStencilTexture = false): boolean {
- let uniform = this._boundUniforms[channel];
- if (!uniform) {
- return false;
- }
- // Not ready?
- if (!texture) {
- if (this._boundTexturesCache[channel] != null) {
- this._activeChannel = channel;
- this._native.setTexture(uniform, null);
- }
- return false;
- }
- // Video
- if ((<VideoTexture>texture).video) {
- this._activeChannel = channel;
- (<VideoTexture>texture).update();
- } else if (texture.delayLoadState === Constants.DELAYLOADSTATE_NOTLOADED) { // Delay loading
- texture.delayLoad();
- return false;
- }
- let internalTexture: InternalTexture;
- if (depthStencilTexture) {
- internalTexture = (<RenderTargetTexture>texture).depthStencilTexture!;
- } else if (texture.isReady()) {
- internalTexture = <InternalTexture>texture.getInternalTexture();
- } else if (texture.isCube) {
- internalTexture = this.emptyCubeTexture;
- } else if (texture.is3D) {
- internalTexture = this.emptyTexture3D;
- } else if (texture.is2DArray) {
- internalTexture = this.emptyTexture2DArray;
- } else {
- internalTexture = this.emptyTexture;
- }
- this._activeChannel = channel;
- if (!internalTexture ||
- !internalTexture._hardwareTexture?.underlyingResource) {
- return false;
- }
- this._native.setTextureWrapMode(
- internalTexture._hardwareTexture?.underlyingResource,
- this._getAddressMode(texture.wrapU),
- this._getAddressMode(texture.wrapV),
- this._getAddressMode(texture.wrapR));
- this._updateAnisotropicLevel(texture);
- this._native.setTexture(uniform, internalTexture._hardwareTexture?.underlyingResource);
- return true;
- }
- // TODO: Share more of this logic with the base implementation.
- // TODO: Rename to match naming in base implementation once refactoring allows different parameters.
- private _updateAnisotropicLevel(texture: BaseTexture) {
- var internalTexture = texture.getInternalTexture();
- var value = texture.anisotropicFilteringLevel;
- if (!internalTexture || !internalTexture._hardwareTexture?.underlyingResource) {
- return;
- }
- if (internalTexture._cachedAnisotropicFilteringLevel !== value) {
- this._native.setTextureAnisotropicLevel(internalTexture._hardwareTexture?.underlyingResource, value);
- internalTexture._cachedAnisotropicFilteringLevel = value;
- }
- }
- // Returns a NativeAddressMode.XXX value.
- private _getAddressMode(wrapMode: number): number {
- switch (wrapMode) {
- case Constants.TEXTURE_WRAP_ADDRESSMODE:
- return NativeAddressMode.WRAP;
- case Constants.TEXTURE_CLAMP_ADDRESSMODE:
- return NativeAddressMode.CLAMP;
- case Constants.TEXTURE_MIRROR_ADDRESSMODE:
- return NativeAddressMode.MIRROR;
- default:
- throw new Error("Unexpected wrap mode: " + wrapMode + ".");
- }
- }
- /** @hidden */
- public _bindTexture(channel: number, texture: InternalTexture): void {
- throw new Error("_bindTexture not implemented.");
- }
- protected _deleteBuffer(buffer: NativeDataBuffer): void {
- if (buffer.nativeIndexBuffer) {
- this._native.deleteIndexBuffer(buffer.nativeIndexBuffer);
- delete buffer.nativeIndexBuffer;
- }
- if (buffer.nativeVertexBuffer) {
- this._native.deleteVertexBuffer(buffer.nativeVertexBuffer);
- delete buffer.nativeVertexBuffer;
- }
- }
- public releaseEffects() {
- // TODO
- }
- /** @hidden */
- public _uploadCompressedDataToTextureDirectly(texture: InternalTexture, internalFormat: number, width: number, height: number, data: ArrayBufferView, faceIndex: number = 0, lod: number = 0) {
- throw new Error("_uploadCompressedDataToTextureDirectly not implemented.");
- }
- /** @hidden */
- public _uploadDataToTextureDirectly(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
- throw new Error("_uploadDataToTextureDirectly not implemented.");
- }
- /** @hidden */
- public _uploadArrayBufferViewToTexture(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
- throw new Error("_uploadArrayBufferViewToTexture not implemented.");
- }
- /** @hidden */
- public _uploadImageToTexture(texture: InternalTexture, image: HTMLImageElement, faceIndex: number = 0, lod: number = 0) {
- throw new Error("_uploadArrayBufferViewToTexture not implemented.");
- }
- }
|