12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376 |
- import { EngineStore } from './engineStore';
- import { IInternalTextureLoader } from '../Materials/Textures/internalTextureLoader';
- import { Effect, EffectCreationOptions } from '../Materials/effect';
- import { _DevTools } from '../Misc/devTools';
- import { IShaderProcessor } from './Processors/iShaderProcessor';
- import { UniformBuffer } from '../Materials/uniformBuffer';
- import { Nullable, DataArray, IndicesArray } from '../types';
- import { EngineCapabilities } from './engineCapabilities';
- import { PerfCounter } from '../Misc/perfCounter';
- import { Observable } from '../Misc/observable';
- import { DepthCullingState } from '../States/depthCullingState';
- import { StencilState } from '../States/stencilState';
- import { AlphaState } from '../States/alphaCullingState';
- import { Constants } from './constants';
- import { InternalTexture } from '../Materials/Textures/internalTexture';
- import { IViewportLike, IColor4Like } from '../Maths/math.like';
- import { DataBuffer } from '../Meshes/dataBuffer';
- import { IFileRequest } from '../Misc/fileRequest';
- import { Logger } from '../Misc/logger';
- import { DomManagement } from '../Misc/domManagement';
- import { WebGL2ShaderProcessor } from './WebGL/webGL2ShaderProcessors';
- import { WebGLDataBuffer } from '../Meshes/WebGL/webGLDataBuffer';
- import { IPipelineContext } from './IPipelineContext';
- import { WebGLPipelineContext } from './WebGL/webGLPipelineContext';
- import { VertexBuffer } from '../Meshes/buffer';
- import { InstancingAttributeInfo } from './instancingAttributeInfo';
- import { WebRequest } from '../Misc/webRequest';
- import { FileTools } from '../Misc/fileTools';
- import { DepthTextureCreationOptions } from './depthTextureCreationOptions';
- import { BaseTexture } from '../Materials/Textures/baseTexture';
- import { IOfflineProvider } from '../Offline/IOfflineProvider';
- import { IEffectFallbacks } from '../Materials/iEffectFallbacks';
- declare type Observer<T> = import("../Misc/observable").Observer<T>;
- declare type VideoTexture = import("../Materials/Textures/videoTexture").VideoTexture;
- declare type RenderTargetTexture = import("../Materials/Textures/renderTargetTexture").RenderTargetTexture;
- /**
- * Defines the interface used by objects working like Scene
- * @hidden
- */
- interface ISceneLike {
- _addPendingData(data: any): void;
- _removePendingData(data: any): void;
- offlineProvider: IOfflineProvider;
- }
- /**
- * Keeps track of all the buffer info used in engine.
- */
- class BufferPointer {
- public active: boolean;
- public index: number;
- public size: number;
- public type: number;
- public normalized: boolean;
- public stride: number;
- public offset: number;
- public buffer: WebGLBuffer;
- }
- /** Interface defining initialization parameters for Engine class */
- export interface EngineOptions extends WebGLContextAttributes {
- /**
- * Defines if the engine should no exceed a specified device ratio
- * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio
- */
- limitDeviceRatio?: number;
- /**
- * Defines if webvr should be enabled automatically
- * @see http://doc.babylonjs.com/how_to/webvr_camera
- */
- autoEnableWebVR?: boolean;
- /**
- * Defines if webgl2 should be turned off even if supported
- * @see http://doc.babylonjs.com/features/webgl2
- */
- disableWebGL2Support?: boolean;
- /**
- * Defines if webaudio should be initialized as well
- * @see http://doc.babylonjs.com/how_to/playing_sounds_and_music
- */
- audioEngine?: boolean;
- /**
- * Defines if animations should run using a deterministic lock step
- * @see http://doc.babylonjs.com/babylon101/animations#deterministic-lockstep
- */
- deterministicLockstep?: boolean;
- /** Defines the maximum steps to use with deterministic lock step mode */
- lockstepMaxSteps?: number;
- /**
- * Defines that engine should ignore context lost events
- * If this event happens when this parameter is true, you will have to reload the page to restore rendering
- */
- doNotHandleContextLost?: boolean;
- /**
- * Defines that engine should ignore modifying touch action attribute and style
- * If not handle, you might need to set it up on your side for expected touch devices behavior.
- */
- doNotHandleTouchAction?: boolean;
- /**
- * Defines that engine should compile shaders with high precision floats (if supported). True by default
- */
- useHighPrecisionFloats?: boolean;
- }
- /**
- * The base engine class (root of all engines)
- */
- export class ThinEngine {
- /** Use this array to turn off some WebGL2 features on known buggy browsers version */
- public static ExceptionList = [
- { key: "Chrome\/63\.0", capture: "63\\.0\\.3239\\.(\\d+)", captureConstraint: 108, targets: ["uniformBuffer"] },
- { key: "Firefox\/58", capture: null, captureConstraint: null, targets: ["uniformBuffer"] },
- { key: "Firefox\/59", capture: null, captureConstraint: null, targets: ["uniformBuffer"] },
- { key: "Chrome\/72.+?Mobile", capture: null, captureConstraint: null, targets: ["vao"] },
- { key: "Chrome\/73.+?Mobile", capture: null, captureConstraint: null, targets: ["vao"] },
- { key: "Chrome\/74.+?Mobile", capture: null, captureConstraint: null, targets: ["vao"] },
- { key: "Mac OS.+Chrome\/71", capture: null, captureConstraint: null, targets: ["vao"] },
- { key: "Mac OS.+Chrome\/72", capture: null, captureConstraint: null, targets: ["vao"] }
- ];
- /** @hidden */
- public static _TextureLoaders: IInternalTextureLoader[] = [];
- /**
- * Returns the current npm package of the sdk
- */
- // Not mixed with Version for tooling purpose.
- public static get NpmPackage(): string {
- return "babylonjs@4.1.0-alpha.21";
- }
- /**
- * Returns the current version of the framework
- */
- public static get Version(): string {
- return "4.1.0-alpha.21";
- }
- /**
- * Returns a string describing the current engine
- */
- public get description(): string {
- let description = "WebGL" + this.webGLVersion;
- if (this._caps.parallelShaderCompile) {
- description += " - Parallel shader compilation";
- }
- return description;
- }
- // Updatable statics so stick with vars here
- /**
- * Gets or sets the epsilon value used by collision engine
- */
- public static CollisionsEpsilon = 0.001;
- /**
- * Gets or sets the relative url used to load shaders if using the engine in non-minified mode
- */
- public static get ShadersRepository(): string {
- return Effect.ShadersRepository;
- }
- public static set ShadersRepository(value: string) {
- Effect.ShadersRepository = value;
- }
- // Public members
- /** @hidden */
- public _shaderProcessor: IShaderProcessor;
- /**
- * Gets or sets a boolean that indicates if textures must be forced to power of 2 size even if not required
- */
- public forcePOTTextures = false;
- /**
- * Gets a boolean indicating if the engine is currently rendering in fullscreen mode
- */
- public isFullscreen = false;
- /**
- * Gets or sets a boolean indicating if back faces must be culled (true by default)
- */
- public cullBackFaces = true;
- /**
- * Gets or sets a boolean indicating if the engine must keep rendering even if the window is not in foregroun
- */
- public renderEvenInBackground = true;
- /**
- * Gets or sets a boolean indicating that cache can be kept between frames
- */
- public preventCacheWipeBetweenFrames = false;
- /** Gets or sets a boolean indicating if the engine should validate programs after compilation */
- public validateShaderPrograms = false;
- // Uniform buffers list
- /**
- * Gets or sets a boolean indicating that uniform buffers must be disabled even if they are supported
- */
- public disableUniformBuffers = false;
- /** @hidden */
- public _uniformBuffers = new Array<UniformBuffer>();
- /**
- * Gets a boolean indicating that the engine supports uniform buffers
- * @see http://doc.babylonjs.com/features/webgl2#uniform-buffer-objets
- */
- public get supportsUniformBuffers(): boolean {
- return this.webGLVersion > 1 && !this.disableUniformBuffers;
- }
- // Private Members
- /** @hidden */
- public _gl: WebGLRenderingContext;
- protected _renderingCanvas: Nullable<HTMLCanvasElement>;
- protected _windowIsBackground = false;
- protected _webGLVersion = 1.0;
- protected _highPrecisionShadersAllowed = true;
- /** @hidden */
- public get _shouldUseHighPrecisionShader(): boolean {
- return this._caps.highPrecisionShaderSupported && this._highPrecisionShadersAllowed;
- }
- /**
- * Gets a boolean indicating that only power of 2 textures are supported
- * Please note that you can still use non power of 2 textures but in this case the engine will forcefully convert them
- */
- public get needPOTTextures(): boolean {
- return this._webGLVersion < 2 || this.forcePOTTextures;
- }
- /** @hidden */
- public _badOS = false;
- /** @hidden */
- public _badDesktopOS = false;
- private _hardwareScalingLevel: number;
- /** @hidden */
- public _caps: EngineCapabilities;
- private _isStencilEnable: boolean;
- protected _colorWrite = true;
- /** @hidden */
- public _drawCalls = new PerfCounter();
- private _glVersion: string;
- private _glRenderer: string;
- private _glVendor: string;
- /** @hidden */
- public _videoTextureSupported: boolean;
- protected _renderingQueueLaunched = false;
- protected _activeRenderLoops = new Array<() => void>();
- // Lost context
- /**
- * Observable signaled when a context lost event is raised
- */
- public onContextLostObservable = new Observable<ThinEngine>();
- /**
- * Observable signaled when a context restored event is raised
- */
- public onContextRestoredObservable = new Observable<ThinEngine>();
- private _onContextLost: (evt: Event) => void;
- private _onContextRestored: (evt: Event) => void;
- protected _contextWasLost = false;
- /** @hidden */
- public _doNotHandleContextLost = false;
- /**
- * Gets or sets a boolean indicating if resources should be retained to be able to handle context lost events
- * @see http://doc.babylonjs.com/how_to/optimizing_your_scene#handling-webgl-context-lost
- */
- public get doNotHandleContextLost(): boolean {
- return this._doNotHandleContextLost;
- }
- public set doNotHandleContextLost(value: boolean) {
- this._doNotHandleContextLost = value;
- }
- /**
- * Gets or sets a boolean indicating that vertex array object must be disabled even if they are supported
- */
- public disableVertexArrayObjects = false;
- // States
- /** @hidden */
- protected _depthCullingState = new DepthCullingState();
- /** @hidden */
- protected _stencilState = new StencilState();
- /** @hidden */
- protected _alphaState = new AlphaState();
- // Cache
- /** @hidden */
- public _internalTexturesCache = new Array<InternalTexture>();
- /** @hidden */
- protected _activeChannel = 0;
- private _currentTextureChannel = -1;
- /** @hidden */
- protected _boundTexturesCache: { [key: string]: Nullable<InternalTexture> } = {};
- /** @hidden */
- protected _currentEffect: Nullable<Effect>;
- /** @hidden */
- protected _currentProgram: Nullable<WebGLProgram>;
- private _compiledEffects: { [key: string]: Effect } = {};
- private _vertexAttribArraysEnabled: boolean[] = [];
- /** @hidden */
- protected _cachedViewport: Nullable<IViewportLike>;
- private _cachedVertexArrayObject: Nullable<WebGLVertexArrayObject>;
- /** @hidden */
- protected _cachedVertexBuffers: any;
- /** @hidden */
- protected _cachedIndexBuffer: Nullable<DataBuffer>;
- /** @hidden */
- protected _cachedEffectForVertexBuffers: Nullable<Effect>;
- /** @hidden */
- public _currentRenderTarget: Nullable<InternalTexture>;
- private _uintIndicesCurrentlySet = false;
- private _currentBoundBuffer = new Array<Nullable<WebGLBuffer>>();
- /** @hidden */
- protected _currentFramebuffer: Nullable<WebGLFramebuffer> = null;
- private _currentBufferPointers = new Array<BufferPointer>();
- private _currentInstanceLocations = new Array<number>();
- private _currentInstanceBuffers = new Array<DataBuffer>();
- private _textureUnits: Int32Array;
- /** @hidden */
- public _workingCanvas: Nullable<HTMLCanvasElement>;
- /** @hidden */
- public _workingContext: Nullable<CanvasRenderingContext2D>;
- /** @hidden */
- public _bindedRenderFunction: any;
- private _vaoRecordInProgress = false;
- private _mustWipeVertexAttributes = false;
- private _emptyTexture: Nullable<InternalTexture>;
- private _emptyCubeTexture: Nullable<InternalTexture>;
- private _emptyTexture3D: Nullable<InternalTexture>;
- /** @hidden */
- public _frameHandler: number;
- private _nextFreeTextureSlots = new Array<number>();
- private _maxSimultaneousTextures = 0;
- private _activeRequests = new Array<IFileRequest>();
- // Hardware supported Compressed Textures
- protected _texturesSupported = new Array<string>();
- /** @hidden */
- public _textureFormatInUse: Nullable<string>;
- protected get _supportsHardwareTextureRescaling() {
- return false;
- }
- /**
- * Gets the list of texture formats supported
- */
- public get texturesSupported(): Array<string> {
- return this._texturesSupported;
- }
- /**
- * Gets the list of texture formats in use
- */
- public get textureFormatInUse(): Nullable<string> {
- return this._textureFormatInUse;
- }
- /**
- * Gets the current viewport
- */
- public get currentViewport(): Nullable<IViewportLike> {
- return this._cachedViewport;
- }
- /**
- * Gets the default empty texture
- */
- public get emptyTexture(): InternalTexture {
- if (!this._emptyTexture) {
- this._emptyTexture = this.createRawTexture(new Uint8Array(4), 1, 1, Constants.TEXTUREFORMAT_RGBA, false, false, Constants.TEXTURE_NEAREST_SAMPLINGMODE);
- }
- return this._emptyTexture;
- }
- /**
- * Gets the default empty 3D texture
- */
- public get emptyTexture3D(): InternalTexture {
- if (!this._emptyTexture3D) {
- this._emptyTexture3D = this.createRawTexture3D(new Uint8Array(4), 1, 1, 1, Constants.TEXTUREFORMAT_RGBA, false, false, Constants.TEXTURE_NEAREST_SAMPLINGMODE);
- }
- return this._emptyTexture3D;
- }
- /**
- * Gets the default empty cube texture
- */
- public get emptyCubeTexture(): InternalTexture {
- if (!this._emptyCubeTexture) {
- var faceData = new Uint8Array(4);
- var cubeData = [faceData, faceData, faceData, faceData, faceData, faceData];
- this._emptyCubeTexture = this.createRawCubeTexture(cubeData, 1, Constants.TEXTUREFORMAT_RGBA, Constants.TEXTURETYPE_UNSIGNED_INT, false, false, Constants.TEXTURE_NEAREST_SAMPLINGMODE);
- }
- return this._emptyCubeTexture;
- }
- /**
- * Defines whether the engine has been created with the premultipliedAlpha option on or not.
- */
- public readonly premultipliedAlpha: boolean = true;
- /**
- * Creates a new engine
- * @param canvasOrContext defines the canvas or WebGL context to use for rendering. If you provide a WebGL context, Babylon.js will not hook events on the canvas (like pointers, keyboards, etc...) so no event observables will be available. This is mostly used when Babylon.js is used as a plugin on a system which alreay used the WebGL context
- * @param antialias defines enable antialiasing (default: false)
- * @param options defines further options to be sent to the getContext() function
- * @param adaptToDeviceRatio defines whether to adapt to the device's viewport characteristics (default: false)
- */
- constructor(canvasOrContext: Nullable<HTMLCanvasElement | WebGLRenderingContext>, antialias?: boolean, options?: EngineOptions, adaptToDeviceRatio: boolean = false) {
- let canvas: Nullable<HTMLCanvasElement> = null;
- if (!canvasOrContext) {
- return;
- }
- options = options || {};
- if ((<HTMLCanvasElement>canvasOrContext).getContext) {
- canvas = <HTMLCanvasElement>canvasOrContext;
- this._renderingCanvas = canvas;
- if (antialias != null) {
- options.antialias = antialias;
- }
- if (options.deterministicLockstep === undefined) {
- options.deterministicLockstep = false;
- }
- if (options.lockstepMaxSteps === undefined) {
- options.lockstepMaxSteps = 4;
- }
- if (options.preserveDrawingBuffer === undefined) {
- options.preserveDrawingBuffer = false;
- }
- if (options.audioEngine === undefined) {
- options.audioEngine = true;
- }
- if (options.stencil === undefined) {
- options.stencil = true;
- }
- if (options.premultipliedAlpha === false) {
- this.premultipliedAlpha = false;
- }
- this._doNotHandleContextLost = options.doNotHandleContextLost ? true : false;
- // Exceptions
- if (navigator && navigator.userAgent) {
- let ua = navigator.userAgent;
- for (var exception of ThinEngine.ExceptionList) {
- let key = exception.key;
- let targets = exception.targets;
- let check = new RegExp(key);
- if (check.test(ua)) {
- if (exception.capture && exception.captureConstraint) {
- let capture = exception.capture;
- let constraint = exception.captureConstraint;
- let regex = new RegExp(capture);
- let matches = regex.exec(ua);
- if (matches && matches.length > 0) {
- let capturedValue = parseInt(matches[matches.length - 1]);
- if (capturedValue >= constraint) {
- continue;
- }
- }
- }
- for (var target of targets) {
- switch (target) {
- case "uniformBuffer":
- this.disableUniformBuffers = true;
- break;
- case "vao":
- this.disableVertexArrayObjects = true;
- break;
- }
- }
- }
- }
- }
- // Context lost
- if (!this._doNotHandleContextLost) {
- this._onContextLost = (evt: Event) => {
- evt.preventDefault();
- this._contextWasLost = true;
- Logger.Warn("WebGL context lost.");
- this.onContextLostObservable.notifyObservers(this);
- };
- this._onContextRestored = () => {
- // Adding a timeout to avoid race condition at browser level
- setTimeout(() => {
- // Rebuild gl context
- this._initGLContext();
- // Rebuild effects
- this._rebuildEffects();
- // Rebuild textures
- this._rebuildInternalTextures();
- // Rebuild buffers
- this._rebuildBuffers();
- // Cache
- this.wipeCaches(true);
- Logger.Warn("WebGL context successfully restored.");
- this.onContextRestoredObservable.notifyObservers(this);
- this._contextWasLost = false;
- }, 0);
- };
- canvas.addEventListener("webglcontextlost", this._onContextLost, false);
- canvas.addEventListener("webglcontextrestored", this._onContextRestored, false);
- options.powerPreference = "high-performance";
- }
- // GL
- if (!options.disableWebGL2Support) {
- try {
- this._gl = <any>(canvas.getContext("webgl2", options) || canvas.getContext("experimental-webgl2", options));
- if (this._gl) {
- this._webGLVersion = 2.0;
- // Prevent weird browsers to lie :-)
- if (!this._gl.deleteQuery) {
- this._webGLVersion = 1.0;
- }
- }
- } catch (e) {
- // Do nothing
- }
- }
- if (!this._gl) {
- if (!canvas) {
- throw new Error("The provided canvas is null or undefined.");
- }
- try {
- this._gl = <WebGLRenderingContext>(canvas.getContext("webgl", options) || canvas.getContext("experimental-webgl", options));
- } catch (e) {
- throw new Error("WebGL not supported");
- }
- }
- if (!this._gl) {
- throw new Error("WebGL not supported");
- }
- // Ensures a consistent color space unpacking of textures cross browser.
- this._gl.pixelStorei(this._gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, this._gl.NONE);
- } else {
- this._gl = <WebGLRenderingContext>canvasOrContext;
- this._renderingCanvas = this._gl.canvas as HTMLCanvasElement;
- if (this._gl.renderbufferStorageMultisample) {
- this._webGLVersion = 2.0;
- }
- const attributes = this._gl.getContextAttributes();
- if (attributes) {
- options.stencil = attributes.stencil;
- }
- }
- if (options.useHighPrecisionFloats !== undefined) {
- this._highPrecisionShadersAllowed = options.useHighPrecisionFloats;
- }
- // Viewport
- const devicePixelRatio = DomManagement.IsWindowObjectExist() ? (window.devicePixelRatio || 1.0) : 1.0;
- var limitDeviceRatio = options.limitDeviceRatio || devicePixelRatio;
- this._hardwareScalingLevel = adaptToDeviceRatio ? 1.0 / Math.min(limitDeviceRatio, devicePixelRatio) : 1.0;
- this.resize();
- this._isStencilEnable = options.stencil ? true : false;
- this._initGLContext();
- // Prepare buffer pointers
- for (var i = 0; i < this._caps.maxVertexAttribs; i++) {
- this._currentBufferPointers[i] = new BufferPointer();
- }
- // Shader processor
- if (this.webGLVersion > 1) {
- this._shaderProcessor = new WebGL2ShaderProcessor();
- }
- // Detect if we are running on a faulty buggy OS.
- this._badOS = /iPad/i.test(navigator.userAgent) || /iPhone/i.test(navigator.userAgent);
- // Detect if we are running on a faulty buggy desktop OS.
- this._badDesktopOS = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
- console.log(`Babylon.js v${ThinEngine.Version} - ${this.description}`);
- }
- private _rebuildInternalTextures(): void {
- let currentState = this._internalTexturesCache.slice(); // Do a copy because the rebuild will add proxies
- for (var internalTexture of currentState) {
- internalTexture._rebuild();
- }
- }
- private _rebuildEffects(): void {
- for (var key in this._compiledEffects) {
- let effect = <Effect>this._compiledEffects[key];
- effect._prepareEffect();
- }
- Effect.ResetCache();
- }
- /**
- * Gets a boolean indicating if all created effects are ready
- * @returns true if all effects are ready
- */
- public areAllEffectsReady(): boolean {
- for (var key in this._compiledEffects) {
- let effect = <Effect>this._compiledEffects[key];
- if (!effect.isReady()) {
- return false;
- }
- }
- return true;
- }
- protected _rebuildBuffers(): void {
- // Uniforms
- for (var uniformBuffer of this._uniformBuffers) {
- uniformBuffer._rebuild();
- }
- }
- private _initGLContext(): void {
- // Caps
- this._caps = new EngineCapabilities();
- this._caps.maxTexturesImageUnits = this._gl.getParameter(this._gl.MAX_TEXTURE_IMAGE_UNITS);
- this._caps.maxCombinedTexturesImageUnits = this._gl.getParameter(this._gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS);
- this._caps.maxVertexTextureImageUnits = this._gl.getParameter(this._gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS);
- this._caps.maxTextureSize = this._gl.getParameter(this._gl.MAX_TEXTURE_SIZE);
- this._caps.maxCubemapTextureSize = this._gl.getParameter(this._gl.MAX_CUBE_MAP_TEXTURE_SIZE);
- this._caps.maxRenderTextureSize = this._gl.getParameter(this._gl.MAX_RENDERBUFFER_SIZE);
- this._caps.maxVertexAttribs = this._gl.getParameter(this._gl.MAX_VERTEX_ATTRIBS);
- this._caps.maxVaryingVectors = this._gl.getParameter(this._gl.MAX_VARYING_VECTORS);
- this._caps.maxFragmentUniformVectors = this._gl.getParameter(this._gl.MAX_FRAGMENT_UNIFORM_VECTORS);
- this._caps.maxVertexUniformVectors = this._gl.getParameter(this._gl.MAX_VERTEX_UNIFORM_VECTORS);
- // Infos
- this._glVersion = this._gl.getParameter(this._gl.VERSION);
- var rendererInfo: any = this._gl.getExtension("WEBGL_debug_renderer_info");
- if (rendererInfo != null) {
- this._glRenderer = this._gl.getParameter(rendererInfo.UNMASKED_RENDERER_WEBGL);
- this._glVendor = this._gl.getParameter(rendererInfo.UNMASKED_VENDOR_WEBGL);
- }
- if (!this._glVendor) {
- this._glVendor = "Unknown vendor";
- }
- if (!this._glRenderer) {
- this._glRenderer = "Unknown renderer";
- }
- // Constants
- this._gl.HALF_FLOAT_OES = 0x8D61; // Half floating-point type (16-bit).
- if (this._gl.RGBA16F !== 0x881A) {
- this._gl.RGBA16F = 0x881A; // RGBA 16-bit floating-point color-renderable internal sized format.
- }
- if (this._gl.RGBA32F !== 0x8814) {
- this._gl.RGBA32F = 0x8814; // RGBA 32-bit floating-point color-renderable internal sized format.
- }
- if (this._gl.DEPTH24_STENCIL8 !== 35056) {
- this._gl.DEPTH24_STENCIL8 = 35056;
- }
- // Extensions
- this._caps.standardDerivatives = this._webGLVersion > 1 || (this._gl.getExtension('OES_standard_derivatives') !== null);
- this._caps.astc = this._gl.getExtension('WEBGL_compressed_texture_astc') || this._gl.getExtension('WEBKIT_WEBGL_compressed_texture_astc');
- this._caps.s3tc = this._gl.getExtension('WEBGL_compressed_texture_s3tc') || this._gl.getExtension('WEBKIT_WEBGL_compressed_texture_s3tc');
- this._caps.pvrtc = this._gl.getExtension('WEBGL_compressed_texture_pvrtc') || this._gl.getExtension('WEBKIT_WEBGL_compressed_texture_pvrtc');
- this._caps.etc1 = this._gl.getExtension('WEBGL_compressed_texture_etc1') || this._gl.getExtension('WEBKIT_WEBGL_compressed_texture_etc1');
- this._caps.etc2 = this._gl.getExtension('WEBGL_compressed_texture_etc') || this._gl.getExtension('WEBKIT_WEBGL_compressed_texture_etc') ||
- this._gl.getExtension('WEBGL_compressed_texture_es3_0'); // also a requirement of OpenGL ES 3
- this._caps.textureAnisotropicFilterExtension = this._gl.getExtension('EXT_texture_filter_anisotropic') || this._gl.getExtension('WEBKIT_EXT_texture_filter_anisotropic') || this._gl.getExtension('MOZ_EXT_texture_filter_anisotropic');
- this._caps.maxAnisotropy = this._caps.textureAnisotropicFilterExtension ? this._gl.getParameter(this._caps.textureAnisotropicFilterExtension.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 0;
- this._caps.uintIndices = this._webGLVersion > 1 || this._gl.getExtension('OES_element_index_uint') !== null;
- this._caps.fragmentDepthSupported = this._webGLVersion > 1 || this._gl.getExtension('EXT_frag_depth') !== null;
- this._caps.highPrecisionShaderSupported = false;
- this._caps.timerQuery = this._gl.getExtension('EXT_disjoint_timer_query_webgl2') || this._gl.getExtension("EXT_disjoint_timer_query");
- if (this._caps.timerQuery) {
- if (this._webGLVersion === 1) {
- this._gl.getQuery = (<any>this._caps.timerQuery).getQueryEXT.bind(this._caps.timerQuery);
- }
- this._caps.canUseTimestampForTimerQuery = this._gl.getQuery(this._caps.timerQuery.TIMESTAMP_EXT, this._caps.timerQuery.QUERY_COUNTER_BITS_EXT) > 0;
- }
- // Checks if some of the format renders first to allow the use of webgl inspector.
- this._caps.colorBufferFloat = this._webGLVersion > 1 && this._gl.getExtension('EXT_color_buffer_float');
- this._caps.textureFloat = (this._webGLVersion > 1 || this._gl.getExtension('OES_texture_float')) ? true : false;
- this._caps.textureFloatLinearFiltering = this._caps.textureFloat && this._gl.getExtension('OES_texture_float_linear') ? true : false;
- this._caps.textureFloatRender = this._caps.textureFloat && this._canRenderToFloatFramebuffer() ? true : false;
- this._caps.textureHalfFloat = (this._webGLVersion > 1 || this._gl.getExtension('OES_texture_half_float')) ? true : false;
- this._caps.textureHalfFloatLinearFiltering = (this._webGLVersion > 1 || (this._caps.textureHalfFloat && this._gl.getExtension('OES_texture_half_float_linear'))) ? true : false;
- if (this._webGLVersion > 1) {
- this._gl.HALF_FLOAT_OES = 0x140B;
- }
- this._caps.textureHalfFloatRender = this._caps.textureHalfFloat && this._canRenderToHalfFloatFramebuffer();
- this._caps.textureLOD = (this._webGLVersion > 1 || this._gl.getExtension('EXT_shader_texture_lod')) ? true : false;
- this._caps.multiview = this._gl.getExtension('OVR_multiview2');
- // Draw buffers
- if (this._webGLVersion > 1) {
- this._caps.drawBuffersExtension = true;
- this._caps.maxMSAASamples = this._gl.getParameter(this._gl.MAX_SAMPLES);
- } else {
- var drawBuffersExtension = this._gl.getExtension('WEBGL_draw_buffers');
- if (drawBuffersExtension !== null) {
- this._caps.drawBuffersExtension = true;
- this._gl.drawBuffers = drawBuffersExtension.drawBuffersWEBGL.bind(drawBuffersExtension);
- this._gl.DRAW_FRAMEBUFFER = this._gl.FRAMEBUFFER;
- for (var i = 0; i < 16; i++) {
- (<any>this._gl)["COLOR_ATTACHMENT" + i + "_WEBGL"] = (<any>drawBuffersExtension)["COLOR_ATTACHMENT" + i + "_WEBGL"];
- }
- } else {
- this._caps.drawBuffersExtension = false;
- }
- }
- // Shader compiler threads
- this._caps.parallelShaderCompile = this._gl.getExtension('KHR_parallel_shader_compile');
- // Depth Texture
- if (this._webGLVersion > 1) {
- this._caps.depthTextureExtension = true;
- } else {
- var depthTextureExtension = this._gl.getExtension('WEBGL_depth_texture');
- if (depthTextureExtension != null) {
- this._caps.depthTextureExtension = true;
- this._gl.UNSIGNED_INT_24_8 = depthTextureExtension.UNSIGNED_INT_24_8_WEBGL;
- }
- }
- // Vertex array object
- if (this.disableVertexArrayObjects) {
- this._caps.vertexArrayObject = false;
- } else if (this._webGLVersion > 1) {
- this._caps.vertexArrayObject = true;
- } else {
- var vertexArrayObjectExtension = this._gl.getExtension('OES_vertex_array_object');
- if (vertexArrayObjectExtension != null) {
- this._caps.vertexArrayObject = true;
- this._gl.createVertexArray = vertexArrayObjectExtension.createVertexArrayOES.bind(vertexArrayObjectExtension);
- this._gl.bindVertexArray = vertexArrayObjectExtension.bindVertexArrayOES.bind(vertexArrayObjectExtension);
- this._gl.deleteVertexArray = vertexArrayObjectExtension.deleteVertexArrayOES.bind(vertexArrayObjectExtension);
- } else {
- this._caps.vertexArrayObject = false;
- }
- }
- // Instances count
- if (this._webGLVersion > 1) {
- this._caps.instancedArrays = true;
- } else {
- var instanceExtension = <ANGLE_instanced_arrays>this._gl.getExtension('ANGLE_instanced_arrays');
- if (instanceExtension != null) {
- this._caps.instancedArrays = true;
- this._gl.drawArraysInstanced = instanceExtension.drawArraysInstancedANGLE.bind(instanceExtension);
- this._gl.drawElementsInstanced = instanceExtension.drawElementsInstancedANGLE.bind(instanceExtension);
- this._gl.vertexAttribDivisor = instanceExtension.vertexAttribDivisorANGLE.bind(instanceExtension);
- } else {
- this._caps.instancedArrays = false;
- }
- }
- // Intelligently add supported compressed formats in order to check for.
- // Check for ASTC support first as it is most powerful and to be very cross platform.
- // Next PVRTC & DXT, which are probably superior to ETC1/2.
- // Likely no hardware which supports both PVR & DXT, so order matters little.
- // ETC2 is newer and handles ETC1 (no alpha capability), so check for first.
- if (this._caps.astc) { this.texturesSupported.push('-astc.ktx'); }
- if (this._caps.s3tc) { this.texturesSupported.push('-dxt.ktx'); }
- if (this._caps.pvrtc) { this.texturesSupported.push('-pvrtc.ktx'); }
- if (this._caps.etc2) { this.texturesSupported.push('-etc2.ktx'); }
- if (this._caps.etc1) { this.texturesSupported.push('-etc1.ktx'); }
- if (this._gl.getShaderPrecisionFormat) {
- var vertex_highp = this._gl.getShaderPrecisionFormat(this._gl.VERTEX_SHADER, this._gl.HIGH_FLOAT);
- var fragment_highp = this._gl.getShaderPrecisionFormat(this._gl.FRAGMENT_SHADER, this._gl.HIGH_FLOAT);
- if (vertex_highp && fragment_highp) {
- this._caps.highPrecisionShaderSupported = vertex_highp.precision !== 0 && fragment_highp.precision !== 0;
- }
- }
- if (this._webGLVersion > 1) {
- this._caps.blendMinMax = true;
- }
- else {
- const blendMinMaxExtension = this._gl.getExtension('EXT_blend_minmax');
- if (blendMinMaxExtension != null) {
- this._caps.blendMinMax = true;
- this._gl.MAX = blendMinMaxExtension.MAX_EXT;
- this._gl.MIN = blendMinMaxExtension.MIN_EXT;
- } else {
- this._caps.blendMinMax = false;
- }
- }
- // Depth buffer
- this._depthCullingState.depthTest = true;
- this._depthCullingState.depthFunc = this._gl.LEQUAL;
- this._depthCullingState.depthMask = true;
- // Texture maps
- this._maxSimultaneousTextures = this._caps.maxCombinedTexturesImageUnits;
- for (let slot = 0; slot < this._maxSimultaneousTextures; slot++) {
- this._nextFreeTextureSlots.push(slot);
- }
- }
- /**
- * Gets version of the current webGL context
- */
- public get webGLVersion(): number {
- return this._webGLVersion;
- }
- /**
- * Gets a string idenfifying the name of the class
- * @returns "Engine" string
- */
- public getClassName(): string {
- return "Engine";
- }
- /**
- * Returns true if the stencil buffer has been enabled through the creation option of the context.
- */
- public get isStencilEnable(): boolean {
- return this._isStencilEnable;
- }
- /** @hidden */
- public _prepareWorkingCanvas(): void {
- if (this._workingCanvas) {
- return;
- }
- this._workingCanvas = document.createElement("canvas");
- let context = this._workingCanvas.getContext("2d");
- if (context) {
- this._workingContext = context;
- }
- }
- /**
- * Reset the texture cache to empty state
- */
- public resetTextureCache() {
- for (var key in this._boundTexturesCache) {
- if (!this._boundTexturesCache.hasOwnProperty(key)) {
- continue;
- }
- this._boundTexturesCache[key] = null;
- }
- this._currentTextureChannel = -1;
- }
- /**
- * Gets an object containing information about the current webGL context
- * @returns an object containing the vender, the renderer and the version of the current webGL context
- */
- public getGlInfo() {
- return {
- vendor: this._glVendor,
- renderer: this._glRenderer,
- version: this._glVersion
- };
- }
- /**
- * Defines the hardware scaling level.
- * By default the hardware scaling level is computed from the window device ratio.
- * if level = 1 then the engine will render at the exact resolution of the canvas. If level = 0.5 then the engine will render at twice the size of the canvas.
- * @param level defines the level to use
- */
- public setHardwareScalingLevel(level: number): void {
- this._hardwareScalingLevel = level;
- this.resize();
- }
- /**
- * Gets the current hardware scaling level.
- * By default the hardware scaling level is computed from the window device ratio.
- * if level = 1 then the engine will render at the exact resolution of the canvas. If level = 0.5 then the engine will render at twice the size of the canvas.
- * @returns a number indicating the current hardware scaling level
- */
- public getHardwareScalingLevel(): number {
- return this._hardwareScalingLevel;
- }
- /**
- * Gets the list of loaded textures
- * @returns an array containing all loaded textures
- */
- public getLoadedTexturesCache(): InternalTexture[] {
- return this._internalTexturesCache;
- }
- /**
- * Gets the object containing all engine capabilities
- * @returns the EngineCapabilities object
- */
- public getCaps(): EngineCapabilities {
- return this._caps;
- }
- /**
- * stop executing a render loop function and remove it from the execution array
- * @param renderFunction defines the function to be removed. If not provided all functions will be removed.
- */
- public stopRenderLoop(renderFunction?: () => void): void {
- if (!renderFunction) {
- this._activeRenderLoops = [];
- return;
- }
- var index = this._activeRenderLoops.indexOf(renderFunction);
- if (index >= 0) {
- this._activeRenderLoops.splice(index, 1);
- }
- }
- /** @hidden */
- public _renderLoop(): void {
- if (!this._contextWasLost) {
- var shouldRender = true;
- if (!this.renderEvenInBackground && this._windowIsBackground) {
- shouldRender = false;
- }
- if (shouldRender) {
- // Start new frame
- this.beginFrame();
- for (var index = 0; index < this._activeRenderLoops.length; index++) {
- var renderFunction = this._activeRenderLoops[index];
- renderFunction();
- }
- // Present
- this.endFrame();
- }
- }
- if (this._activeRenderLoops.length > 0) {
- this._frameHandler = this._queueNewFrame(this._bindedRenderFunction, this.getHostWindow());
- } else {
- this._renderingQueueLaunched = false;
- }
- }
- /**
- * Gets the HTML canvas attached with the current webGL context
- * @returns a HTML canvas
- */
- public getRenderingCanvas(): Nullable<HTMLCanvasElement> {
- return this._renderingCanvas;
- }
- /**
- * Gets host window
- * @returns the host window object
- */
- public getHostWindow(): Window {
- if (this._renderingCanvas && this._renderingCanvas.ownerDocument && this._renderingCanvas.ownerDocument.defaultView) {
- return this._renderingCanvas.ownerDocument.defaultView;
- }
- return window;
- }
- /**
- * Gets the current render width
- * @param useScreen defines if screen size must be used (or the current render target if any)
- * @returns a number defining the current render width
- */
- public getRenderWidth(useScreen = false): number {
- if (!useScreen && this._currentRenderTarget) {
- return this._currentRenderTarget.width;
- }
- return this._gl.drawingBufferWidth;
- }
- /**
- * Gets the current render height
- * @param useScreen defines if screen size must be used (or the current render target if any)
- * @returns a number defining the current render height
- */
- public getRenderHeight(useScreen = false): number {
- if (!useScreen && this._currentRenderTarget) {
- return this._currentRenderTarget.height;
- }
- return this._gl.drawingBufferHeight;
- }
- /**
- * Can be used to override the current requestAnimationFrame requester.
- * @hidden
- */
- protected _queueNewFrame(bindedRenderFunction: any, requester?: any): number {
- return ThinEngine.QueueNewFrame(bindedRenderFunction, requester);
- }
- /**
- * Register and execute a render loop. The engine can have more than one render function
- * @param renderFunction defines the function to continuously execute
- */
- public runRenderLoop(renderFunction: () => void): void {
- if (this._activeRenderLoops.indexOf(renderFunction) !== -1) {
- return;
- }
- this._activeRenderLoops.push(renderFunction);
- if (!this._renderingQueueLaunched) {
- this._renderingQueueLaunched = true;
- this._bindedRenderFunction = this._renderLoop.bind(this);
- this._frameHandler = this._queueNewFrame(this._bindedRenderFunction, this.getHostWindow());
- }
- }
- /**
- * Clear the current render buffer or the current render target (if any is set up)
- * @param color defines the color to use
- * @param backBuffer defines if the back buffer must be cleared
- * @param depth defines if the depth buffer must be cleared
- * @param stencil defines if the stencil buffer must be cleared
- */
- public clear(color: Nullable<IColor4Like>, backBuffer: boolean, depth: boolean, stencil: boolean = false): void {
- this.applyStates();
- var mode = 0;
- if (backBuffer && color) {
- this._gl.clearColor(color.r, color.g, color.b, color.a !== undefined ? color.a : 1.0);
- mode |= this._gl.COLOR_BUFFER_BIT;
- }
- if (depth) {
- this._gl.clearDepth(1.0);
- mode |= this._gl.DEPTH_BUFFER_BIT;
- }
- if (stencil) {
- this._gl.clearStencil(0);
- mode |= this._gl.STENCIL_BUFFER_BIT;
- }
- this._gl.clear(mode);
- }
- private _viewportCached = { x: 0, y: 0, z: 0, w: 0 };
- /** @hidden */
- public _viewport(x: number, y: number, width: number, height: number): void {
- if (x !== this._viewportCached.x ||
- y !== this._viewportCached.y ||
- width !== this._viewportCached.z ||
- height !== this._viewportCached.w) {
- this._viewportCached.x = x;
- this._viewportCached.y = y;
- this._viewportCached.z = width;
- this._viewportCached.w = height;
- this._gl.viewport(x, y, width, height);
- }
- }
- /**
- * Set the WebGL's viewport
- * @param viewport defines the viewport element to be used
- * @param requiredWidth defines the width required for rendering. If not provided the rendering canvas' width is used
- * @param requiredHeight defines the height required for rendering. If not provided the rendering canvas' height is used
- */
- public setViewport(viewport: IViewportLike, requiredWidth?: number, requiredHeight?: number): void {
- var width = requiredWidth || this.getRenderWidth();
- var height = requiredHeight || this.getRenderHeight();
- var x = viewport.x || 0;
- var y = viewport.y || 0;
- this._cachedViewport = viewport;
- this._viewport(x * width, y * height, width * viewport.width, height * viewport.height);
- }
- /**
- * Begin a new frame
- */
- public beginFrame(): void {
- }
- /**
- * Enf the current frame
- */
- public endFrame(): void {
- // Force a flush in case we are using a bad OS.
- if (this._badOS) {
- this.flushFramebuffer();
- }
- }
- /**
- * Resize the view according to the canvas' size
- */
- public resize(): void {
- var width = this._renderingCanvas ? this._renderingCanvas.clientWidth : window.innerWidth;
- var height = this._renderingCanvas ? this._renderingCanvas.clientHeight : window.innerHeight;
- this.setSize(width / this._hardwareScalingLevel, height / this._hardwareScalingLevel);
- }
- /**
- * Force a specific size of the canvas
- * @param width defines the new canvas' width
- * @param height defines the new canvas' height
- */
- public setSize(width: number, height: number): void {
- if (!this._renderingCanvas) {
- return;
- }
- width = width | 0;
- height = height | 0;
- if (this._renderingCanvas.width === width && this._renderingCanvas.height === height) {
- return;
- }
- this._renderingCanvas.width = width;
- this._renderingCanvas.height = height;
- }
- /**
- * Binds the frame buffer to the specified texture.
- * @param texture The texture to render to or null for the default canvas
- * @param faceIndex The face of the texture to render to in case of cube texture
- * @param requiredWidth The width of the target to render to
- * @param requiredHeight The height of the target to render to
- * @param forceFullscreenViewport Forces the viewport to be the entire texture/screen if true
- * @param depthStencilTexture The depth stencil texture to use to render
- * @param lodLevel defines le lod level to bind to the frame buffer
- */
- public bindFramebuffer(texture: InternalTexture, faceIndex?: number, requiredWidth?: number, requiredHeight?: number, forceFullscreenViewport?: boolean, depthStencilTexture?: InternalTexture, lodLevel = 0): void {
- if (this._currentRenderTarget) {
- this.unBindFramebuffer(this._currentRenderTarget);
- }
- this._currentRenderTarget = texture;
- this._bindUnboundFramebuffer(texture._MSAAFramebuffer ? texture._MSAAFramebuffer : texture._framebuffer);
- var gl = this._gl;
- if (texture.isCube) {
- if (faceIndex === undefined) {
- faceIndex = 0;
- }
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex, texture._webGLTexture, lodLevel);
- if (depthStencilTexture) {
- if (depthStencilTexture._generateStencilBuffer) {
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex, depthStencilTexture._webGLTexture, lodLevel);
- }
- else {
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex, depthStencilTexture._webGLTexture, lodLevel);
- }
- }
- }
- if (this._cachedViewport && !forceFullscreenViewport) {
- this.setViewport(this._cachedViewport, requiredWidth, requiredHeight);
- } else {
- if (!requiredWidth) {
- requiredWidth = texture.width;
- if (lodLevel) {
- requiredWidth = requiredWidth / Math.pow(2, lodLevel);
- }
- }
- if (!requiredHeight) {
- requiredHeight = texture.height;
- if (lodLevel) {
- requiredHeight = requiredHeight / Math.pow(2, lodLevel);
- }
- }
- this._viewport(0, 0, requiredWidth, requiredHeight);
- }
- this.wipeCaches();
- }
- /** @hidden */
- public _bindUnboundFramebuffer(framebuffer: Nullable<WebGLFramebuffer>) {
- if (this._currentFramebuffer !== framebuffer) {
- this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, framebuffer);
- this._currentFramebuffer = framebuffer;
- }
- }
- /**
- * Unbind the current render target texture from the webGL context
- * @param texture defines the render target texture to unbind
- * @param disableGenerateMipMaps defines a boolean indicating that mipmaps must not be generated
- * @param onBeforeUnbind defines a function which will be called before the effective unbind
- */
- public unBindFramebuffer(texture: InternalTexture, disableGenerateMipMaps = false, onBeforeUnbind?: () => void): void {
- this._currentRenderTarget = null;
- // If MSAA, we need to bitblt back to main texture
- var gl = this._gl;
- if (texture._MSAAFramebuffer) {
- gl.bindFramebuffer(gl.READ_FRAMEBUFFER, texture._MSAAFramebuffer);
- gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, texture._framebuffer);
- gl.blitFramebuffer(0, 0, texture.width, texture.height,
- 0, 0, texture.width, texture.height,
- gl.COLOR_BUFFER_BIT, gl.NEAREST);
- }
- if (texture.generateMipMaps && !disableGenerateMipMaps && !texture.isCube) {
- this._bindTextureDirectly(gl.TEXTURE_2D, texture, true);
- gl.generateMipmap(gl.TEXTURE_2D);
- this._bindTextureDirectly(gl.TEXTURE_2D, null);
- }
- if (onBeforeUnbind) {
- if (texture._MSAAFramebuffer) {
- // Bind the correct framebuffer
- this._bindUnboundFramebuffer(texture._framebuffer);
- }
- onBeforeUnbind();
- }
- this._bindUnboundFramebuffer(null);
- }
- /**
- * Force a webGL flush (ie. a flush of all waiting webGL commands)
- */
- public flushFramebuffer(): void {
- this._gl.flush();
- }
- /**
- * Unbind the current render target and bind the default framebuffer
- */
- public restoreDefaultFramebuffer(): void {
- if (this._currentRenderTarget) {
- this.unBindFramebuffer(this._currentRenderTarget);
- } else {
- this._bindUnboundFramebuffer(null);
- }
- if (this._cachedViewport) {
- this.setViewport(this._cachedViewport);
- }
- this.wipeCaches();
- }
- // VBOs
- private _resetVertexBufferBinding(): void {
- this.bindArrayBuffer(null);
- this._cachedVertexBuffers = null;
- }
- /**
- * Creates a vertex buffer
- * @param data the data for the vertex buffer
- * @returns the new WebGL static buffer
- */
- public createVertexBuffer(data: DataArray): DataBuffer {
- return this._createVertexBuffer(data, this._gl.STATIC_DRAW);
- }
- private _createVertexBuffer(data: DataArray, usage: number): DataBuffer {
- var vbo = this._gl.createBuffer();
- if (!vbo) {
- throw new Error("Unable to create vertex buffer");
- }
- let dataBuffer = new WebGLDataBuffer(vbo);
- this.bindArrayBuffer(dataBuffer);
- if (data instanceof Array) {
- this._gl.bufferData(this._gl.ARRAY_BUFFER, new Float32Array(data), this._gl.STATIC_DRAW);
- } else {
- this._gl.bufferData(this._gl.ARRAY_BUFFER, <ArrayBuffer>data, this._gl.STATIC_DRAW);
- }
- this._resetVertexBufferBinding();
- dataBuffer.references = 1;
- return dataBuffer;
- }
- /**
- * Creates a dynamic vertex buffer
- * @param data the data for the dynamic vertex buffer
- * @returns the new WebGL dynamic buffer
- */
- public createDynamicVertexBuffer(data: DataArray): DataBuffer {
- return this._createVertexBuffer(data, this._gl.DYNAMIC_DRAW);
- }
- /**
- * Update a dynamic index buffer
- * @param indexBuffer defines the target index buffer
- * @param indices defines the data to update
- * @param offset defines the offset in the target index buffer where update should start
- */
- public updateDynamicIndexBuffer(indexBuffer: DataBuffer, indices: IndicesArray, offset: number = 0): void {
- // Force cache update
- this._currentBoundBuffer[this._gl.ELEMENT_ARRAY_BUFFER] = null;
- this.bindIndexBuffer(indexBuffer);
- var arrayBuffer;
- if (indices instanceof Uint16Array || indices instanceof Uint32Array) {
- arrayBuffer = indices;
- } else {
- arrayBuffer = indexBuffer.is32Bits ? new Uint32Array(indices) : new Uint16Array(indices);
- }
- this._gl.bufferData(this._gl.ELEMENT_ARRAY_BUFFER, arrayBuffer, this._gl.DYNAMIC_DRAW);
- this._resetIndexBufferBinding();
- }
- /**
- * 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
- * @param byteLength the byte length of the data
- */
- public updateDynamicVertexBuffer(vertexBuffer: DataBuffer, data: DataArray, byteOffset?: number, byteLength?: number): void {
- this.bindArrayBuffer(vertexBuffer);
- if (byteOffset === undefined) {
- byteOffset = 0;
- }
- if (byteLength === undefined) {
- if (data instanceof Array) {
- this._gl.bufferSubData(this._gl.ARRAY_BUFFER, byteOffset, new Float32Array(data));
- } else {
- this._gl.bufferSubData(this._gl.ARRAY_BUFFER, byteOffset, <ArrayBuffer>data);
- }
- } else {
- if (data instanceof Array) {
- this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, new Float32Array(data).subarray(byteOffset, byteOffset + byteLength));
- } else {
- if (data instanceof ArrayBuffer) {
- data = new Uint8Array(data, byteOffset, byteLength);
- } else {
- data = new Uint8Array(data.buffer, data.byteOffset + byteOffset, byteLength);
- }
- this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, <ArrayBuffer>data);
- }
- }
- this._resetVertexBufferBinding();
- }
- private _resetIndexBufferBinding(): void {
- this.bindIndexBuffer(null);
- this._cachedIndexBuffer = null;
- }
- /**
- * Creates a new index buffer
- * @param indices defines the content of the index buffer
- * @param updatable defines if the index buffer must be updatable
- * @returns a new webGL buffer
- */
- public createIndexBuffer(indices: IndicesArray, updatable?: boolean): DataBuffer {
- var vbo = this._gl.createBuffer();
- let dataBuffer = new WebGLDataBuffer(vbo!);
- if (!vbo) {
- throw new Error("Unable to create index buffer");
- }
- this.bindIndexBuffer(dataBuffer);
- const data = this._normalizeIndexData(indices);
- this._gl.bufferData(this._gl.ELEMENT_ARRAY_BUFFER, data, updatable ? this._gl.DYNAMIC_DRAW : this._gl.STATIC_DRAW);
- this._resetIndexBufferBinding();
- dataBuffer.references = 1;
- dataBuffer.is32Bits = (data.BYTES_PER_ELEMENT === 4);
- return dataBuffer;
- }
- protected _normalizeIndexData(indices: IndicesArray): Uint16Array | Uint32Array
- {
- if (indices instanceof Uint16Array) {
- return indices;
- }
- // Check 32 bit support
- if (this._caps.uintIndices) {
- if (indices instanceof Uint32Array) {
- return indices;
- } else {
- // number[] or Int32Array, check if 32 bit is necessary
- for (var index = 0; index < indices.length; index++) {
- if (indices[index] >= 65535) {
- return new Uint32Array(indices);
- }
- }
- return new Uint16Array(indices);
- }
- }
- // No 32 bit support, force conversion to 16 bit (values greater 16 bit are lost)
- return new Uint16Array(indices);
- }
- /**
- * Bind a webGL buffer to the webGL context
- * @param buffer defines the buffer to bind
- */
- public bindArrayBuffer(buffer: Nullable<DataBuffer>): void {
- if (!this._vaoRecordInProgress) {
- this._unbindVertexArrayObject();
- }
- this.bindBuffer(buffer, this._gl.ARRAY_BUFFER);
- }
- /**
- * Bind a specific block at a given index in a specific shader program
- * @param pipelineContext defines the pipeline context to use
- * @param blockName defines the block name
- * @param index defines the index where to bind the block
- */
- public bindUniformBlock(pipelineContext: IPipelineContext, blockName: string, index: number): void {
- let program = (pipelineContext as WebGLPipelineContext).program!;
- var uniformLocation = this._gl.getUniformBlockIndex(program, blockName);
- this._gl.uniformBlockBinding(program, uniformLocation, index);
- }
- private bindIndexBuffer(buffer: Nullable<DataBuffer>): void {
- if (!this._vaoRecordInProgress) {
- this._unbindVertexArrayObject();
- }
- this.bindBuffer(buffer, this._gl.ELEMENT_ARRAY_BUFFER);
- }
- private bindBuffer(buffer: Nullable<DataBuffer>, target: number): void {
- if (this._vaoRecordInProgress || this._currentBoundBuffer[target] !== buffer) {
- this._gl.bindBuffer(target, buffer ? buffer.underlyingResource : null);
- this._currentBoundBuffer[target] = buffer;
- }
- }
- /**
- * update the bound buffer with the given data
- * @param data defines the data to update
- */
- public updateArrayBuffer(data: Float32Array): void {
- this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, data);
- }
- private _vertexAttribPointer(buffer: DataBuffer, indx: number, size: number, type: number, normalized: boolean, stride: number, offset: number): void {
- var pointer = this._currentBufferPointers[indx];
- var changed = false;
- if (!pointer.active) {
- changed = true;
- pointer.active = true;
- pointer.index = indx;
- pointer.size = size;
- pointer.type = type;
- pointer.normalized = normalized;
- pointer.stride = stride;
- pointer.offset = offset;
- pointer.buffer = buffer;
- } else {
- if (pointer.buffer !== buffer) { pointer.buffer = buffer; changed = true; }
- if (pointer.size !== size) { pointer.size = size; changed = true; }
- if (pointer.type !== type) { pointer.type = type; changed = true; }
- if (pointer.normalized !== normalized) { pointer.normalized = normalized; changed = true; }
- if (pointer.stride !== stride) { pointer.stride = stride; changed = true; }
- if (pointer.offset !== offset) { pointer.offset = offset; changed = true; }
- }
- if (changed || this._vaoRecordInProgress) {
- this.bindArrayBuffer(buffer);
- this._gl.vertexAttribPointer(indx, size, type, normalized, stride, offset);
- }
- }
- private _bindIndexBufferWithCache(indexBuffer: Nullable<DataBuffer>): void {
- if (indexBuffer == null) {
- return;
- }
- if (this._cachedIndexBuffer !== indexBuffer) {
- this._cachedIndexBuffer = indexBuffer;
- this.bindIndexBuffer(indexBuffer);
- this._uintIndicesCurrentlySet = indexBuffer.is32Bits;
- }
- }
- private _bindVertexBuffersAttributes(vertexBuffers: { [key: string]: Nullable<VertexBuffer> }, effect: Effect): void {
- var attributes = effect.getAttributesNames();
- if (!this._vaoRecordInProgress) {
- this._unbindVertexArrayObject();
- }
- this.unbindAllAttributes();
- for (var index = 0; index < attributes.length; index++) {
- var order = effect.getAttributeLocation(index);
- if (order >= 0) {
- var vertexBuffer = vertexBuffers[attributes[index]];
- if (!vertexBuffer) {
- continue;
- }
- this._gl.enableVertexAttribArray(order);
- if (!this._vaoRecordInProgress) {
- this._vertexAttribArraysEnabled[order] = true;
- }
- var buffer = vertexBuffer.getBuffer();
- if (buffer) {
- this._vertexAttribPointer(buffer, order, vertexBuffer.getSize(), vertexBuffer.type, vertexBuffer.normalized, vertexBuffer.byteStride, vertexBuffer.byteOffset);
- if (vertexBuffer.getIsInstanced()) {
- this._gl.vertexAttribDivisor(order, vertexBuffer.getInstanceDivisor());
- if (!this._vaoRecordInProgress) {
- this._currentInstanceLocations.push(order);
- this._currentInstanceBuffers.push(buffer);
- }
- }
- }
- }
- }
- }
- /**
- * Records a vertex array object
- * @see http://doc.babylonjs.com/features/webgl2#vertex-array-objects
- * @param vertexBuffers defines the list of vertex buffers to store
- * @param indexBuffer defines the index buffer to store
- * @param effect defines the effect to store
- * @returns the new vertex array object
- */
- public recordVertexArrayObject(vertexBuffers: { [key: string]: VertexBuffer; }, indexBuffer: Nullable<DataBuffer>, effect: Effect): WebGLVertexArrayObject {
- var vao = this._gl.createVertexArray();
- this._vaoRecordInProgress = true;
- this._gl.bindVertexArray(vao);
- this._mustWipeVertexAttributes = true;
- this._bindVertexBuffersAttributes(vertexBuffers, effect);
- this.bindIndexBuffer(indexBuffer);
- this._vaoRecordInProgress = false;
- this._gl.bindVertexArray(null);
- return vao;
- }
- /**
- * Bind a specific vertex array object
- * @see http://doc.babylonjs.com/features/webgl2#vertex-array-objects
- * @param vertexArrayObject defines the vertex array object to bind
- * @param indexBuffer defines the index buffer to bind
- */
- public bindVertexArrayObject(vertexArrayObject: WebGLVertexArrayObject, indexBuffer: Nullable<DataBuffer>): void {
- if (this._cachedVertexArrayObject !== vertexArrayObject) {
- this._cachedVertexArrayObject = vertexArrayObject;
- this._gl.bindVertexArray(vertexArrayObject);
- this._cachedVertexBuffers = null;
- this._cachedIndexBuffer = null;
- this._uintIndicesCurrentlySet = indexBuffer != null && indexBuffer.is32Bits;
- this._mustWipeVertexAttributes = true;
- }
- }
- /**
- * Bind webGl buffers directly to the webGL context
- * @param vertexBuffer defines the vertex buffer to bind
- * @param indexBuffer defines the index buffer to bind
- * @param vertexDeclaration defines the vertex declaration to use with the vertex buffer
- * @param vertexStrideSize defines the vertex stride of the vertex buffer
- * @param effect defines the effect associated with the vertex buffer
- */
- public bindBuffersDirectly(vertexBuffer: DataBuffer, indexBuffer: DataBuffer, vertexDeclaration: number[], vertexStrideSize: number, effect: Effect): void {
- if (this._cachedVertexBuffers !== vertexBuffer || this._cachedEffectForVertexBuffers !== effect) {
- this._cachedVertexBuffers = vertexBuffer;
- this._cachedEffectForVertexBuffers = effect;
- let attributesCount = effect.getAttributesCount();
- this._unbindVertexArrayObject();
- this.unbindAllAttributes();
- var offset = 0;
- for (var index = 0; index < attributesCount; index++) {
- if (index < vertexDeclaration.length) {
- var order = effect.getAttributeLocation(index);
- if (order >= 0) {
- this._gl.enableVertexAttribArray(order);
- this._vertexAttribArraysEnabled[order] = true;
- this._vertexAttribPointer(vertexBuffer, order, vertexDeclaration[index], this._gl.FLOAT, false, vertexStrideSize, offset);
- }
- offset += vertexDeclaration[index] * 4;
- }
- }
- }
- this._bindIndexBufferWithCache(indexBuffer);
- }
- private _unbindVertexArrayObject(): void {
- if (!this._cachedVertexArrayObject) {
- return;
- }
- this._cachedVertexArrayObject = null;
- this._gl.bindVertexArray(null);
- }
- /**
- * Bind a list of vertex buffers to the webGL context
- * @param vertexBuffers defines the list of vertex buffers to bind
- * @param indexBuffer defines the index buffer to bind
- * @param effect defines the effect associated with the vertex buffers
- */
- public bindBuffers(vertexBuffers: { [key: string]: Nullable<VertexBuffer> }, indexBuffer: Nullable<DataBuffer>, effect: Effect): void {
- if (this._cachedVertexBuffers !== vertexBuffers || this._cachedEffectForVertexBuffers !== effect) {
- this._cachedVertexBuffers = vertexBuffers;
- this._cachedEffectForVertexBuffers = effect;
- this._bindVertexBuffersAttributes(vertexBuffers, effect);
- }
- this._bindIndexBufferWithCache(indexBuffer);
- }
- /**
- * Unbind all instance attributes
- */
- public unbindInstanceAttributes() {
- var boundBuffer;
- for (var i = 0, ul = this._currentInstanceLocations.length; i < ul; i++) {
- var instancesBuffer = this._currentInstanceBuffers[i];
- if (boundBuffer != instancesBuffer && instancesBuffer.references) {
- boundBuffer = instancesBuffer;
- this.bindArrayBuffer(instancesBuffer);
- }
- var offsetLocation = this._currentInstanceLocations[i];
- this._gl.vertexAttribDivisor(offsetLocation, 0);
- }
- this._currentInstanceBuffers.length = 0;
- this._currentInstanceLocations.length = 0;
- }
- /**
- * Release and free the memory of a vertex array object
- * @param vao defines the vertex array object to delete
- */
- public releaseVertexArrayObject(vao: WebGLVertexArrayObject) {
- this._gl.deleteVertexArray(vao);
- }
- /** @hidden */
- public _releaseBuffer(buffer: DataBuffer): boolean {
- buffer.references--;
- if (buffer.references === 0) {
- this._deleteBuffer(buffer);
- return true;
- }
- return false;
- }
- protected _deleteBuffer(buffer: DataBuffer): void {
- this._gl.deleteBuffer(buffer.underlyingResource);
- }
- /**
- * Creates a webGL buffer to use with instanciation
- * @param capacity defines the size of the buffer
- * @returns the webGL buffer
- */
- public createInstancesBuffer(capacity: number): DataBuffer {
- var buffer = this._gl.createBuffer();
- if (!buffer) {
- throw new Error("Unable to create instance buffer");
- }
- var result = new WebGLDataBuffer(buffer);
- result.capacity = capacity;
- this.bindArrayBuffer(result);
- this._gl.bufferData(this._gl.ARRAY_BUFFER, capacity, this._gl.DYNAMIC_DRAW);
- return result;
- }
- /**
- * Delete a webGL buffer used with instanciation
- * @param buffer defines the webGL buffer to delete
- */
- public deleteInstancesBuffer(buffer: WebGLBuffer): void {
- this._gl.deleteBuffer(buffer);
- }
- /**
- * Update the content of a webGL buffer used with instanciation and bind it to the webGL context
- * @param instancesBuffer defines the webGL buffer to update and bind
- * @param data defines the data to store in the buffer
- * @param offsetLocations defines the offsets or attributes information used to determine where data must be stored in the buffer
- */
- public updateAndBindInstancesBuffer(instancesBuffer: DataBuffer, data: Float32Array, offsetLocations: number[] | InstancingAttributeInfo[]): void {
- this.bindArrayBuffer(instancesBuffer);
- if (data) {
- this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, data);
- }
- if ((<any>offsetLocations[0]).index !== undefined) {
- let stride = 0;
- for (let i = 0; i < offsetLocations.length; i++) {
- let ai = <InstancingAttributeInfo>offsetLocations[i];
- stride += ai.attributeSize * 4;
- }
- for (let i = 0; i < offsetLocations.length; i++) {
- let ai = <InstancingAttributeInfo>offsetLocations[i];
- if (!this._vertexAttribArraysEnabled[ai.index]) {
- this._gl.enableVertexAttribArray(ai.index);
- this._vertexAttribArraysEnabled[ai.index] = true;
- }
- this._vertexAttribPointer(instancesBuffer, ai.index, ai.attributeSize, ai.attribyteType || this._gl.FLOAT, ai.normalized || false, stride, ai.offset);
- this._gl.vertexAttribDivisor(ai.index, 1);
- this._currentInstanceLocations.push(ai.index);
- this._currentInstanceBuffers.push(instancesBuffer);
- }
- } else {
- for (let index = 0; index < 4; index++) {
- let offsetLocation = <number>offsetLocations[index];
- if (!this._vertexAttribArraysEnabled[offsetLocation]) {
- this._gl.enableVertexAttribArray(offsetLocation);
- this._vertexAttribArraysEnabled[offsetLocation] = true;
- }
- this._vertexAttribPointer(instancesBuffer, offsetLocation, 4, this._gl.FLOAT, false, 64, index * 16);
- this._gl.vertexAttribDivisor(offsetLocation, 1);
- this._currentInstanceLocations.push(offsetLocation);
- this._currentInstanceBuffers.push(instancesBuffer);
- }
- }
- }
- /**
- * Apply all cached states (depth, culling, stencil and alpha)
- */
- public applyStates() {
- this._depthCullingState.apply(this._gl);
- this._stencilState.apply(this._gl);
- this._alphaState.apply(this._gl);
- }
- /**
- * Send a draw order
- * @param useTriangles defines if triangles must be used to draw (else wireframe will be used)
- * @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 draw(useTriangles: boolean, indexStart: number, indexCount: number, instancesCount?: number): void {
- this.drawElementsType(useTriangles ? Constants.MATERIAL_TriangleFillMode : Constants.MATERIAL_WireFrameFillMode, indexStart, indexCount, instancesCount);
- }
- /**
- * Draw a list of points
- * @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 drawPointClouds(verticesStart: number, verticesCount: number, instancesCount?: number): void {
- this.drawArraysType(Constants.MATERIAL_PointFillMode, verticesStart, verticesCount, instancesCount);
- }
- /**
- * Draw a list of unindexed primitives
- * @param useTriangles defines if triangles must be used to draw (else wireframe will be used)
- * @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 drawUnIndexed(useTriangles: boolean, verticesStart: number, verticesCount: number, instancesCount?: number): void {
- this.drawArraysType(useTriangles ? Constants.MATERIAL_TriangleFillMode : Constants.MATERIAL_WireFrameFillMode, verticesStart, verticesCount, instancesCount);
- }
- /**
- * 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.applyStates();
- this._drawCalls.addCount(1, false);
- // Render
- const drawMode = this._drawMode(fillMode);
- 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._gl.drawElements(drawMode, indexCount, indexFormat, indexStart * mult);
- }
- }
- /**
- * 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.applyStates();
- this._drawCalls.addCount(1, false);
- const drawMode = this._drawMode(fillMode);
- if (instancesCount) {
- this._gl.drawArraysInstanced(drawMode, verticesStart, verticesCount, instancesCount);
- } else {
- this._gl.drawArrays(drawMode, verticesStart, verticesCount);
- }
- }
- private _drawMode(fillMode: number): number {
- switch (fillMode) {
- // Triangle views
- case Constants.MATERIAL_TriangleFillMode:
- return this._gl.TRIANGLES;
- case Constants.MATERIAL_PointFillMode:
- return this._gl.POINTS;
- case Constants.MATERIAL_WireFrameFillMode:
- return this._gl.LINES;
- // Draw modes
- case Constants.MATERIAL_PointListDrawMode:
- return this._gl.POINTS;
- case Constants.MATERIAL_LineListDrawMode:
- return this._gl.LINES;
- case Constants.MATERIAL_LineLoopDrawMode:
- return this._gl.LINE_LOOP;
- case Constants.MATERIAL_LineStripDrawMode:
- return this._gl.LINE_STRIP;
- case Constants.MATERIAL_TriangleStripDrawMode:
- return this._gl.TRIANGLE_STRIP;
- case Constants.MATERIAL_TriangleFanDrawMode:
- return this._gl.TRIANGLE_FAN;
- default:
- return this._gl.TRIANGLES;
- }
- }
- // Shaders
- /** @hidden */
- public _releaseEffect(effect: Effect): void {
- if (this._compiledEffects[effect._key]) {
- delete this._compiledEffects[effect._key];
- this._deletePipelineContext(effect.getPipelineContext() as WebGLPipelineContext);
- }
- }
- /** @hidden */
- public _deletePipelineContext(pipelineContext: IPipelineContext): void {
- let webGLPipelineContext = pipelineContext as WebGLPipelineContext;
- if (webGLPipelineContext && webGLPipelineContext.program) {
- webGLPipelineContext.program.__SPECTOR_rebuildProgram = null;
- this._gl.deleteProgram(webGLPipelineContext.program);
- }
- }
- /**
- * Create a new effect (used to store vertex/fragment shaders)
- * @param baseName defines the base name of the effect (The name of file without .fragment.fx or .vertex.fx)
- * @param attributesNamesOrOptions defines either a list of attribute names or an EffectCreationOptions object
- * @param uniformsNamesOrEngine defines either a list of uniform names or the engine to use
- * @param samplers defines an array of string used to represent textures
- * @param defines defines the string containing the defines to use to compile the shaders
- * @param fallbacks defines the list of potential fallbacks to use if shader conmpilation fails
- * @param onCompiled defines a function to call when the effect creation is successful
- * @param onError defines a function to call when the effect creation has failed
- * @param indexParameters defines an object containing the index values to use to compile shaders (like the maximum number of simultaneous lights)
- * @returns the new Effect
- */
- public createEffect(baseName: any, attributesNamesOrOptions: string[] | EffectCreationOptions, uniformsNamesOrEngine: string[] | ThinEngine, samplers?: string[], defines?: string,
- fallbacks?: IEffectFallbacks,
- onCompiled?: Nullable<(effect: Effect) => void>, onError?: Nullable<(effect: Effect, errors: string) => void>, indexParameters?: any): Effect {
- var vertex = baseName.vertexElement || baseName.vertex || baseName;
- var fragment = baseName.fragmentElement || baseName.fragment || baseName;
- var name = vertex + "+" + fragment + "@" + (defines ? defines : (<EffectCreationOptions>attributesNamesOrOptions).defines);
- if (this._compiledEffects[name]) {
- var compiledEffect = <Effect>this._compiledEffects[name];
- if (onCompiled && compiledEffect.isReady()) {
- onCompiled(compiledEffect);
- }
- return compiledEffect;
- }
- var effect = new Effect(baseName, attributesNamesOrOptions, uniformsNamesOrEngine, samplers, this, defines, fallbacks, onCompiled, onError, indexParameters);
- effect._key = name;
- this._compiledEffects[name] = effect;
- return effect;
- }
- protected static _ConcatenateShader(source: string, defines: Nullable<string>, shaderVersion: string = ""): string {
- return shaderVersion + (defines ? defines + "\n" : "") + source;
- }
- private _compileShader(source: string, type: string, defines: Nullable<string>, shaderVersion: string): WebGLShader {
- return this._compileRawShader(ThinEngine._ConcatenateShader(source, defines, shaderVersion), type);
- }
- private _compileRawShader(source: string, type: string): WebGLShader {
- var gl = this._gl;
- var shader = gl.createShader(type === "vertex" ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);
- if (!shader) {
- throw new Error("Something went wrong while compile the shader.");
- }
- gl.shaderSource(shader, source);
- gl.compileShader(shader);
- return shader;
- }
- /**
- * Directly creates a webGL program
- * @param pipelineContext defines the pipeline context to attach to
- * @param vertexCode defines the vertex shader code to use
- * @param fragmentCode defines the fragment shader code to use
- * @param context defines the webGL context to use (if not set, the current one will be used)
- * @param transformFeedbackVaryings defines the list of transform feedback varyings to use
- * @returns the new webGL program
- */
- public createRawShaderProgram(pipelineContext: IPipelineContext, vertexCode: string, fragmentCode: string, context?: WebGLRenderingContext, transformFeedbackVaryings: Nullable<string[]> = null): WebGLProgram {
- context = context || this._gl;
- var vertexShader = this._compileRawShader(vertexCode, "vertex");
- var fragmentShader = this._compileRawShader(fragmentCode, "fragment");
- return this._createShaderProgram(pipelineContext as WebGLPipelineContext, vertexShader, fragmentShader, context, transformFeedbackVaryings);
- }
- /**
- * Creates a webGL program
- * @param pipelineContext defines the pipeline context to attach to
- * @param vertexCode defines the vertex shader code to use
- * @param fragmentCode defines the fragment shader code to use
- * @param defines defines the string containing the defines to use to compile the shaders
- * @param context defines the webGL context to use (if not set, the current one will be used)
- * @param transformFeedbackVaryings defines the list of transform feedback varyings to use
- * @returns the new webGL program
- */
- public createShaderProgram(pipelineContext: IPipelineContext, vertexCode: string, fragmentCode: string, defines: Nullable<string>, context?: WebGLRenderingContext, transformFeedbackVaryings: Nullable<string[]> = null): WebGLProgram {
- context = context || this._gl;
- var shaderVersion = (this._webGLVersion > 1) ? "#version 300 es\n#define WEBGL2 \n" : "";
- var vertexShader = this._compileShader(vertexCode, "vertex", defines, shaderVersion);
- var fragmentShader = this._compileShader(fragmentCode, "fragment", defines, shaderVersion);
- return this._createShaderProgram(pipelineContext as WebGLPipelineContext, vertexShader, fragmentShader, context, transformFeedbackVaryings);
- }
- /**
- * Creates a new pipeline context
- * @returns the new pipeline
- */
- public createPipelineContext(): IPipelineContext {
- var pipelineContext = new WebGLPipelineContext();
- pipelineContext.engine = this;
- if (this._caps.parallelShaderCompile) {
- pipelineContext.isParallelCompiled = true;
- }
- return pipelineContext;
- }
- protected _createShaderProgram(pipelineContext: WebGLPipelineContext, vertexShader: WebGLShader, fragmentShader: WebGLShader, context: WebGLRenderingContext, transformFeedbackVaryings: Nullable<string[]> = null): WebGLProgram {
- var shaderProgram = context.createProgram();
- pipelineContext.program = shaderProgram;
- if (!shaderProgram) {
- throw new Error("Unable to create program");
- }
- context.attachShader(shaderProgram, vertexShader);
- context.attachShader(shaderProgram, fragmentShader);
- context.linkProgram(shaderProgram);
- pipelineContext.context = context;
- pipelineContext.vertexShader = vertexShader;
- pipelineContext.fragmentShader = fragmentShader;
- if (!pipelineContext.isParallelCompiled) {
- this._finalizePipelineContext(pipelineContext);
- }
- return shaderProgram;
- }
- protected _finalizePipelineContext(pipelineContext: WebGLPipelineContext) {
- const context = pipelineContext.context!;
- const vertexShader = pipelineContext.vertexShader!;
- const fragmentShader = pipelineContext.fragmentShader!;
- const program = pipelineContext.program!;
- var linked = context.getProgramParameter(program, context.LINK_STATUS);
- if (!linked) { // Get more info
- // Vertex
- if (!this._gl.getShaderParameter(vertexShader, this._gl.COMPILE_STATUS)) {
- let log = this._gl.getShaderInfoLog(vertexShader);
- if (log) {
- throw new Error("VERTEX SHADER " + log);
- }
- }
- // Fragment
- if (!this._gl.getShaderParameter(fragmentShader, this._gl.COMPILE_STATUS)) {
- let log = this._gl.getShaderInfoLog(fragmentShader);
- if (log) {
- throw new Error("FRAGMENT SHADER " + log);
- }
- }
- var error = context.getProgramInfoLog(program);
- if (error) {
- throw new Error(error);
- }
- }
- if (this.validateShaderPrograms) {
- context.validateProgram(program);
- var validated = context.getProgramParameter(program, context.VALIDATE_STATUS);
- if (!validated) {
- var error = context.getProgramInfoLog(program);
- if (error) {
- throw new Error(error);
- }
- }
- }
- context.deleteShader(vertexShader);
- context.deleteShader(fragmentShader);
- pipelineContext.vertexShader = undefined;
- pipelineContext.fragmentShader = undefined;
- if (pipelineContext.onCompiled) {
- pipelineContext.onCompiled();
- pipelineContext.onCompiled = undefined;
- }
- }
- /** @hidden */
- public _preparePipelineContext(pipelineContext: IPipelineContext, vertexSourceCode: string, fragmentSourceCode: string, createAsRaw: boolean,
- rebuildRebind: any,
- defines: Nullable<string>,
- transformFeedbackVaryings: Nullable<string[]>) {
- let webGLRenderingState = pipelineContext as WebGLPipelineContext;
- if (createAsRaw) {
- webGLRenderingState.program = this.createRawShaderProgram(webGLRenderingState, vertexSourceCode, fragmentSourceCode, undefined, transformFeedbackVaryings);
- }
- else {
- webGLRenderingState.program = this.createShaderProgram(webGLRenderingState, vertexSourceCode, fragmentSourceCode, defines, undefined, transformFeedbackVaryings);
- }
- webGLRenderingState.program.__SPECTOR_rebuildProgram = rebuildRebind;
- }
- /** @hidden */
- public _isRenderingStateCompiled(pipelineContext: IPipelineContext): boolean {
- let webGLPipelineContext = pipelineContext as WebGLPipelineContext;
- if (this._gl.getProgramParameter(webGLPipelineContext.program!, this._caps.parallelShaderCompile.COMPLETION_STATUS_KHR)) {
- this._finalizePipelineContext(webGLPipelineContext);
- return true;
- }
- return false;
- }
- /** @hidden */
- public _executeWhenRenderingStateIsCompiled(pipelineContext: IPipelineContext, action: () => void) {
- let webGLPipelineContext = pipelineContext as WebGLPipelineContext;
- if (!webGLPipelineContext.isParallelCompiled) {
- action();
- return;
- }
- webGLPipelineContext.onCompiled = action;
- }
- /**
- * Gets the list of webGL uniform locations associated with a specific program based on a list of uniform names
- * @param pipelineContext defines the pipeline context to use
- * @param uniformsNames defines the list of uniform names
- * @returns an array of webGL uniform locations
- */
- public getUniforms(pipelineContext: IPipelineContext, uniformsNames: string[]): Nullable<WebGLUniformLocation>[] {
- var results = new Array<Nullable<WebGLUniformLocation>>();
- let webGLPipelineContext = pipelineContext as WebGLPipelineContext;
- for (var index = 0; index < uniformsNames.length; index++) {
- results.push(this._gl.getUniformLocation(webGLPipelineContext.program!, uniformsNames[index]));
- }
- return results;
- }
- /**
- * Gets the lsit of active attributes for a given webGL program
- * @param pipelineContext defines the pipeline context to use
- * @param attributesNames defines the list of attribute names to get
- * @returns an array of indices indicating the offset of each attribute
- */
- public getAttributes(pipelineContext: IPipelineContext, attributesNames: string[]): number[] {
- var results = [];
- let webGLPipelineContext = pipelineContext as WebGLPipelineContext;
- for (var index = 0; index < attributesNames.length; index++) {
- try {
- results.push(this._gl.getAttribLocation(webGLPipelineContext.program!, attributesNames[index]));
- } catch (e) {
- results.push(-1);
- }
- }
- return results;
- }
- /**
- * Activates an effect, mkaing it the current one (ie. the one used for rendering)
- * @param effect defines the effect to activate
- */
- public enableEffect(effect: Nullable<Effect>): void {
- if (!effect || effect === this._currentEffect) {
- return;
- }
- // Use program
- this.bindSamplers(effect);
- this._currentEffect = effect;
- if (effect.onBind) {
- effect.onBind(effect);
- }
- if (effect._onBindObservable) {
- effect._onBindObservable.notifyObservers(effect);
- }
- }
- /**
- * Set the value of an uniform to an array of int32
- * @param uniform defines the webGL uniform location where to store the value
- * @param array defines the array of int32 to store
- */
- public setIntArray(uniform: Nullable<WebGLUniformLocation>, array: Int32Array): void {
- if (!uniform) {
- return;
- }
- this._gl.uniform1iv(uniform, array);
- }
- /**
- * Set the value of an uniform to an array of int32 (stored as vec2)
- * @param uniform defines the webGL uniform location where to store the value
- * @param array defines the array of int32 to store
- */
- public setIntArray2(uniform: Nullable<WebGLUniformLocation>, array: Int32Array): void {
- if (!uniform || array.length % 2 !== 0) {
- return;
- }
- this._gl.uniform2iv(uniform, array);
- }
- /**
- * Set the value of an uniform to an array of int32 (stored as vec3)
- * @param uniform defines the webGL uniform location where to store the value
- * @param array defines the array of int32 to store
- */
- public setIntArray3(uniform: Nullable<WebGLUniformLocation>, array: Int32Array): void {
- if (!uniform || array.length % 3 !== 0) {
- return;
- }
- this._gl.uniform3iv(uniform, array);
- }
- /**
- * Set the value of an uniform to an array of int32 (stored as vec4)
- * @param uniform defines the webGL uniform location where to store the value
- * @param array defines the array of int32 to store
- */
- public setIntArray4(uniform: Nullable<WebGLUniformLocation>, array: Int32Array): void {
- if (!uniform || array.length % 4 !== 0) {
- return;
- }
- this._gl.uniform4iv(uniform, array);
- }
- /**
- * Set the value of an uniform to an array of float32
- * @param uniform defines the webGL uniform location where to store the value
- * @param array defines the array of float32 to store
- */
- public setFloatArray(uniform: Nullable<WebGLUniformLocation>, array: Float32Array): void {
- if (!uniform) {
- return;
- }
- this._gl.uniform1fv(uniform, array);
- }
- /**
- * Set the value of an uniform to an array of float32 (stored as vec2)
- * @param uniform defines the webGL uniform location where to store the value
- * @param array defines the array of float32 to store
- */
- public setFloatArray2(uniform: Nullable<WebGLUniformLocation>, array: Float32Array): void {
- if (!uniform || array.length % 2 !== 0) {
- return;
- }
- this._gl.uniform2fv(uniform, array);
- }
- /**
- * Set the value of an uniform to an array of float32 (stored as vec3)
- * @param uniform defines the webGL uniform location where to store the value
- * @param array defines the array of float32 to store
- */
- public setFloatArray3(uniform: Nullable<WebGLUniformLocation>, array: Float32Array): void {
- if (!uniform || array.length % 3 !== 0) {
- return;
- }
- this._gl.uniform3fv(uniform, array);
- }
- /**
- * Set the value of an uniform to an array of float32 (stored as vec4)
- * @param uniform defines the webGL uniform location where to store the value
- * @param array defines the array of float32 to store
- */
- public setFloatArray4(uniform: Nullable<WebGLUniformLocation>, array: Float32Array): void {
- if (!uniform || array.length % 4 !== 0) {
- return;
- }
- this._gl.uniform4fv(uniform, array);
- }
- /**
- * Set the value of an uniform to an array of number
- * @param uniform defines the webGL uniform location where to store the value
- * @param array defines the array of number to store
- */
- public setArray(uniform: Nullable<WebGLUniformLocation>, array: number[]): void {
- if (!uniform) {
- return;
- }
- this._gl.uniform1fv(uniform, <any>array);
- }
- /**
- * Set the value of an uniform to an array of number (stored as vec2)
- * @param uniform defines the webGL uniform location where to store the value
- * @param array defines the array of number to store
- */
- public setArray2(uniform: Nullable<WebGLUniformLocation>, array: number[]): void {
- if (!uniform || array.length % 2 !== 0) {
- return;
- }
- this._gl.uniform2fv(uniform, <any>array);
- }
- /**
- * Set the value of an uniform to an array of number (stored as vec3)
- * @param uniform defines the webGL uniform location where to store the value
- * @param array defines the array of number to store
- */
- public setArray3(uniform: Nullable<WebGLUniformLocation>, array: number[]): void {
- if (!uniform || array.length % 3 !== 0) {
- return;
- }
- this._gl.uniform3fv(uniform, <any>array);
- }
- /**
- * Set the value of an uniform to an array of number (stored as vec4)
- * @param uniform defines the webGL uniform location where to store the value
- * @param array defines the array of number to store
- */
- public setArray4(uniform: Nullable<WebGLUniformLocation>, array: number[]): void {
- if (!uniform || array.length % 4 !== 0) {
- return;
- }
- this._gl.uniform4fv(uniform, <any>array);
- }
- /**
- * Set the value of an uniform to an array of float32 (stored as matrices)
- * @param uniform defines the webGL uniform location where to store the value
- * @param matrices defines the array of float32 to store
- */
- public setMatrices(uniform: Nullable<WebGLUniformLocation>, matrices: Float32Array): void {
- if (!uniform) {
- return;
- }
- this._gl.uniformMatrix4fv(uniform, false, matrices);
- }
- /**
- * Set the value of an uniform to a matrix (3x3)
- * @param uniform defines the webGL uniform location where to store the value
- * @param matrix defines the Float32Array representing the 3x3 matrix to store
- */
- public setMatrix3x3(uniform: Nullable<WebGLUniformLocation>, matrix: Float32Array): void {
- if (!uniform) {
- return;
- }
- this._gl.uniformMatrix3fv(uniform, false, matrix);
- }
- /**
- * Set the value of an uniform to a matrix (2x2)
- * @param uniform defines the webGL uniform location where to store the value
- * @param matrix defines the Float32Array representing the 2x2 matrix to store
- */
- public setMatrix2x2(uniform: Nullable<WebGLUniformLocation>, matrix: Float32Array): void {
- if (!uniform) {
- return;
- }
- this._gl.uniformMatrix2fv(uniform, false, matrix);
- }
- /**
- * Set the value of an uniform to a number (int)
- * @param uniform defines the webGL uniform location where to store the value
- * @param value defines the int number to store
- */
- public setInt(uniform: Nullable<WebGLUniformLocation>, value: number): void {
- if (!uniform) {
- return;
- }
- this._gl.uniform1i(uniform, value);
- }
- /**
- * Set the value of an uniform to a number (float)
- * @param uniform defines the webGL uniform location where to store the value
- * @param value defines the float number to store
- */
- public setFloat(uniform: Nullable<WebGLUniformLocation>, value: number): void {
- if (!uniform) {
- return;
- }
- this._gl.uniform1f(uniform, value);
- }
- /**
- * Set the value of an uniform to a vec2
- * @param uniform defines the webGL uniform location where to store the value
- * @param x defines the 1st component of the value
- * @param y defines the 2nd component of the value
- */
- public setFloat2(uniform: Nullable<WebGLUniformLocation>, x: number, y: number): void {
- if (!uniform) {
- return;
- }
- this._gl.uniform2f(uniform, x, y);
- }
- /**
- * Set the value of an uniform to a vec3
- * @param uniform defines the webGL uniform location where to store the value
- * @param x defines the 1st component of the value
- * @param y defines the 2nd component of the value
- * @param z defines the 3rd component of the value
- */
- public setFloat3(uniform: Nullable<WebGLUniformLocation>, x: number, y: number, z: number): void {
- if (!uniform) {
- return;
- }
- this._gl.uniform3f(uniform, x, y, z);
- }
- /**
- * Set the value of an uniform to a boolean
- * @param uniform defines the webGL uniform location where to store the value
- * @param bool defines the boolean to store
- */
- public setBool(uniform: Nullable<WebGLUniformLocation>, bool: number): void {
- if (!uniform) {
- return;
- }
- this._gl.uniform1i(uniform, bool);
- }
- /**
- * Set the value of an uniform to a vec4
- * @param uniform defines the webGL uniform location where to store the value
- * @param x defines the 1st component of the value
- * @param y defines the 2nd component of the value
- * @param z defines the 3rd component of the value
- * @param w defines the 4th component of the value
- */
- public setFloat4(uniform: Nullable<WebGLUniformLocation>, x: number, y: number, z: number, w: number): void {
- if (!uniform) {
- return;
- }
- this._gl.uniform4f(uniform, x, y, z, w);
- }
- /**
- * Sets a Color4 on a uniform variable
- * @param uniform defines the uniform location
- * @param color4 defines the value to be set
- */
- public setDirectColor4(uniform: Nullable<WebGLUniformLocation>, color4: IColor4Like): void {
- if (!uniform) {
- return;
- }
- this._gl.uniform4f(uniform, color4.r, color4.g, color4.b, color4.a);
- }
- // States
- /**
- * Gets the depth culling state manager
- */
- public get depthCullingState(): DepthCullingState {
- return this._depthCullingState;
- }
- /**
- * Gets the alpha state manager
- */
- public get alphaState(): AlphaState {
- return this._alphaState;
- }
- /**
- * Gets the stencil state manager
- */
- public get stencilState(): StencilState {
- return this._stencilState;
- }
- // Textures
- /**
- * Clears the list of texture accessible through engine.
- * This can help preventing texture load conflict due to name collision.
- */
- public clearInternalTexturesCache() {
- this._internalTexturesCache = [];
- }
- /**
- * Force the entire cache to be cleared
- * You should not have to use this function unless your engine needs to share the webGL context with another engine
- * @param bruteForce defines a boolean to force clearing ALL caches (including stencil, detoh and alpha states)
- */
- public wipeCaches(bruteForce?: boolean): void {
- if (this.preventCacheWipeBetweenFrames && !bruteForce) {
- return;
- }
- this._currentEffect = null;
- this._viewportCached.x = 0;
- this._viewportCached.y = 0;
- this._viewportCached.z = 0;
- this._viewportCached.w = 0;
- if (bruteForce) {
- this.resetTextureCache();
- this._currentProgram = null;
- this._stencilState.reset();
- this._depthCullingState.reset();
- this._depthCullingState.depthFunc = this._gl.LEQUAL;
- this._alphaState.reset();
- this._unpackFlipYCached = null;
- }
- this._resetVertexBufferBinding();
- this._cachedIndexBuffer = null;
- this._cachedEffectForVertexBuffers = null;
- this._unbindVertexArrayObject();
- this.bindIndexBuffer(null);
- }
- /** @hidden */
- public _getSamplingParameters(samplingMode: number, generateMipMaps: boolean): { min: number; mag: number } {
- var gl = this._gl;
- var magFilter = gl.NEAREST;
- var minFilter = gl.NEAREST;
- switch (samplingMode) {
- case Constants.TEXTURE_BILINEAR_SAMPLINGMODE:
- magFilter = gl.LINEAR;
- if (generateMipMaps) {
- minFilter = gl.LINEAR_MIPMAP_NEAREST;
- } else {
- minFilter = gl.LINEAR;
- }
- break;
- case Constants.TEXTURE_TRILINEAR_SAMPLINGMODE:
- magFilter = gl.LINEAR;
- if (generateMipMaps) {
- minFilter = gl.LINEAR_MIPMAP_LINEAR;
- } else {
- minFilter = gl.LINEAR;
- }
- break;
- case Constants.TEXTURE_NEAREST_SAMPLINGMODE:
- magFilter = gl.NEAREST;
- if (generateMipMaps) {
- minFilter = gl.NEAREST_MIPMAP_LINEAR;
- } else {
- minFilter = gl.NEAREST;
- }
- break;
- case Constants.TEXTURE_NEAREST_NEAREST_MIPNEAREST:
- magFilter = gl.NEAREST;
- if (generateMipMaps) {
- minFilter = gl.NEAREST_MIPMAP_NEAREST;
- } else {
- minFilter = gl.NEAREST;
- }
- break;
- case Constants.TEXTURE_NEAREST_LINEAR_MIPNEAREST:
- magFilter = gl.NEAREST;
- if (generateMipMaps) {
- minFilter = gl.LINEAR_MIPMAP_NEAREST;
- } else {
- minFilter = gl.LINEAR;
- }
- break;
- case Constants.TEXTURE_NEAREST_LINEAR_MIPLINEAR:
- magFilter = gl.NEAREST;
- if (generateMipMaps) {
- minFilter = gl.LINEAR_MIPMAP_LINEAR;
- } else {
- minFilter = gl.LINEAR;
- }
- break;
- case Constants.TEXTURE_NEAREST_LINEAR:
- magFilter = gl.NEAREST;
- minFilter = gl.LINEAR;
- break;
- case Constants.TEXTURE_NEAREST_NEAREST:
- magFilter = gl.NEAREST;
- minFilter = gl.NEAREST;
- break;
- case Constants.TEXTURE_LINEAR_NEAREST_MIPNEAREST:
- magFilter = gl.LINEAR;
- if (generateMipMaps) {
- minFilter = gl.NEAREST_MIPMAP_NEAREST;
- } else {
- minFilter = gl.NEAREST;
- }
- break;
- case Constants.TEXTURE_LINEAR_NEAREST_MIPLINEAR:
- magFilter = gl.LINEAR;
- if (generateMipMaps) {
- minFilter = gl.NEAREST_MIPMAP_LINEAR;
- } else {
- minFilter = gl.NEAREST;
- }
- break;
- case Constants.TEXTURE_LINEAR_LINEAR:
- magFilter = gl.LINEAR;
- minFilter = gl.LINEAR;
- break;
- case Constants.TEXTURE_LINEAR_NEAREST:
- magFilter = gl.LINEAR;
- minFilter = gl.NEAREST;
- break;
- }
- return {
- min: minFilter,
- mag: magFilter
- };
- }
- /** @hidden */
- public _createTexture(): WebGLTexture {
- let texture = this._gl.createTexture();
- if (!texture) {
- throw new Error("Unable to create texture");
- }
- return texture;
- }
- /**
- * Usually called from Texture.ts.
- * Passed information to create a WebGLTexture
- * @param urlArg 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 excludeLoaders array of texture loaders that should be excluded when picking a loader for the texture (default: empty array)
- * @returns a InternalTexture for assignment back into BABYLON.Texture
- */
- public createTexture(urlArg: 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> = null, fallback: Nullable<InternalTexture> = null, format: Nullable<number> = null,
- forcedExtension: Nullable<string> = null, excludeLoaders: Array<IInternalTextureLoader> = []): InternalTexture {
- var url = String(urlArg); // assign a new string, so that the original is still available in case of fallback
- var fromData = url.substr(0, 5) === "data:";
- var fromBlob = url.substr(0, 5) === "blob:";
- var isBase64 = fromData && url.indexOf(";base64,") !== -1;
- let texture = fallback ? fallback : new InternalTexture(this, InternalTexture.DATASOURCE_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 ThinEngine._TextureLoaders) {
- if (excludeLoaders.indexOf(availableLoader) === -1 && availableLoader.canLoad(extension, this._textureFormatInUse, fallback, isBase64, buffer ? true : false)) {
- loader = availableLoader;
- break;
- }
- }
- if (loader) {
- url = loader.transformUrl(url, this._textureFormatInUse);
- }
- 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);
- }
- let customFallback = false;
- if (loader) {
- const fallbackUrl = loader.getFallbackTextureUrl(url, this._textureFormatInUse);
- if (fallbackUrl) {
- // Add Back
- customFallback = true;
- excludeLoaders.push(loader);
- this.createTexture(urlArg, noMipmap, texture.invertY, scene, samplingMode, null, onError, buffer, texture, undefined, undefined, excludeLoaders);
- return;
- }
- }
- if (!customFallback) {
- if (onLoadObserver) {
- texture.onLoadedObservable.remove(onLoadObserver);
- }
- if (EngineStore.UseFallbackTexture) {
- this.createTexture(EngineStore.FallbackTexture, noMipmap, texture.invertY, scene, samplingMode, null, onError, buffer, texture);
- return;
- }
- }
- if (onError) {
- onError(message || "Unknown error", exception);
- }
- };
- // processing for non-image formats
- if (loader) {
- var callback = (data: string | ArrayBuffer) => {
- loader!.loadData(data as ArrayBuffer, texture, (width: number, height: number, loadMipmap: boolean, isCompressed: boolean, done: () => void, loadFailed) => {
- if (loadFailed) {
- onInternalError("TextureLoader failed to load data");
- } else {
- this._prepareWebGLTexture(texture, scene, width, height, texture.invertY, !loadMipmap, isCompressed, () => {
- done();
- return false;
- }, samplingMode);
- }
- });
- };
- if (!buffer) {
- this._loadFile(url, callback, undefined, scene ? scene.offlineProvider : undefined, true, (request?: WebRequest, exception?: any) => {
- onInternalError("Unable to load " + (request ? request.responseURL : url, exception));
- });
- } else {
- //callback(buffer as ArrayBuffer);
- if (buffer instanceof ArrayBuffer) {
- callback(buffer);
- }
- else {
- if (onError) {
- onError("Unable to load: only ArrayBuffer supported here", null);
- }
- }
- }
- } else {
- var onload = (img: HTMLImageElement) => {
- if (fromBlob && !this._doNotHandleContextLost) {
- // We need to store the image if we need to rebuild the texture
- // in case of a webgl context lost
- texture._buffer = img;
- }
- this._prepareWebGLTexture(texture, scene, img.width, img.height, texture.invertY, noMipmap, false, (potWidth, potHeight, continuationCallback) => {
- let gl = this._gl;
- var isPot = (img.width === potWidth && img.height === potHeight);
- let internalFormat = format ? this._getInternalFormat(format) : ((extension === ".jpg") ? gl.RGB : gl.RGBA);
- if (isPot) {
- gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, internalFormat, gl.UNSIGNED_BYTE, img);
- return false;
- }
- let maxTextureSize = this._caps.maxTextureSize;
- if (img.width > maxTextureSize || img.height > maxTextureSize || !this._supportsHardwareTextureRescaling) {
- this._prepareWorkingCanvas();
- if (!this._workingCanvas || !this._workingContext) {
- return false;
- }
- this._workingCanvas.width = potWidth;
- this._workingCanvas.height = potHeight;
- this._workingContext.drawImage(img, 0, 0, img.width, img.height, 0, 0, potWidth, potHeight);
- gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, internalFormat, gl.UNSIGNED_BYTE, this._workingCanvas);
- texture.width = potWidth;
- texture.height = potHeight;
- return false;
- } else {
- // Using shaders when possible to rescale because canvas.drawImage is lossy
- let source = new InternalTexture(this, InternalTexture.DATASOURCE_TEMP);
- this._bindTextureDirectly(gl.TEXTURE_2D, source, true);
- gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, internalFormat, gl.UNSIGNED_BYTE, img);
- this._rescaleTexture(source, texture, scene, internalFormat, () => {
- this._releaseTexture(source);
- this._bindTextureDirectly(gl.TEXTURE_2D, texture, true);
- continuationCallback();
- });
- }
- return true;
- }, samplingMode);
- };
- if (!fromData || isBase64) {
- if (buffer instanceof HTMLImageElement) {
- onload(buffer);
- } else {
- FileTools.LoadImage(url, onload, onInternalError, scene ? scene.offlineProvider : null);
- }
- }
- else if (typeof buffer === "string" || buffer instanceof ArrayBuffer || ArrayBuffer.isView(buffer) || buffer instanceof Blob) {
- FileTools.LoadImage(buffer, onload, onInternalError, scene ? scene.offlineProvider : null);
- }
- else {
- onload(<HTMLImageElement>buffer);
- }
- }
- return texture;
- }
- /**
- * @hidden
- */
- public _rescaleTexture(source: InternalTexture, destination: InternalTexture, scene: Nullable<any>, internalFormat: number, onComplete: () => void): void {
- }
- /**
- * Creates a raw texture
- * @param data defines the data to store in the texture
- * @param width defines the width of the texture
- * @param height defines the height of the texture
- * @param format defines the format of the data
- * @param generateMipMaps defines if the engine should generate the mip levels
- * @param invertY defines if data must be stored with Y axis inverted
- * @param samplingMode defines the required sampling mode (Texture.NEAREST_SAMPLINGMODE by default)
- * @param compression defines the compression used (null by default)
- * @param type defines the type fo the data (Engine.TEXTURETYPE_UNSIGNED_INT by default)
- * @returns the raw texture inside an InternalTexture
- */
- public createRawTexture(data: Nullable<ArrayBufferView>, width: number, height: number, format: number, generateMipMaps: boolean, invertY: boolean, samplingMode: number, compression: Nullable<string> = null, type: number = Constants.TEXTURETYPE_UNSIGNED_INT): InternalTexture {
- throw _DevTools.WarnImport("Engine.RawTexture");
- }
- /**
- * Creates a new raw cube texture
- * @param data defines the array of data to use to create each face
- * @param size defines the size of the textures
- * @param format defines the format of the data
- * @param type defines the type of the data (like Engine.TEXTURETYPE_UNSIGNED_INT)
- * @param generateMipMaps defines if the engine should generate the mip levels
- * @param invertY defines if data must be stored with Y axis inverted
- * @param samplingMode defines the required sampling mode (like Texture.NEAREST_SAMPLINGMODE)
- * @param compression defines the compression used (null by default)
- * @returns the cube texture as an InternalTexture
- */
- public createRawCubeTexture(data: Nullable<ArrayBufferView[]>, size: number, format: number, type: number,
- generateMipMaps: boolean, invertY: boolean, samplingMode: number,
- compression: Nullable<string> = null): InternalTexture {
- throw _DevTools.WarnImport("Engine.RawTexture");
- }
- /**
- * Creates a new raw 3D texture
- * @param data defines the data used to create the texture
- * @param width defines the width of the texture
- * @param height defines the height of the texture
- * @param depth defines the depth of the texture
- * @param format defines the format of the texture
- * @param generateMipMaps defines if the engine must generate mip levels
- * @param invertY defines if data must be stored with Y axis inverted
- * @param samplingMode defines the required sampling mode (like Texture.NEAREST_SAMPLINGMODE)
- * @param compression defines the compressed used (can be null)
- * @param textureType defines the compressed used (can be null)
- * @returns a new raw 3D texture (stored in an InternalTexture)
- */
- public createRawTexture3D(data: Nullable<ArrayBufferView>, width: number, height: number, depth: number, format: number, generateMipMaps: boolean, invertY: boolean, samplingMode: number, compression: Nullable<string> = null, textureType = Constants.TEXTURETYPE_UNSIGNED_INT): InternalTexture {
- throw _DevTools.WarnImport("Engine.RawTexture");
- }
- private _unpackFlipYCached: Nullable<boolean> = null;
- /**
- * In case you are sharing the context with other applications, it might
- * be interested to not cache the unpack flip y state to ensure a consistent
- * value would be set.
- */
- public enableUnpackFlipYCached = true;
- /** @hidden */
- public _unpackFlipY(value: boolean): void {
- if (this._unpackFlipYCached !== value) {
- this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, value ? 1 : 0);
- if (this.enableUnpackFlipYCached) {
- this._unpackFlipYCached = value;
- }
- }
- }
- /** @hidden */
- public _getUnpackAlignement(): number {
- return this._gl.getParameter(this._gl.UNPACK_ALIGNMENT);
- }
- /**
- * Update the sampling mode of a given texture
- * @param samplingMode defines the required sampling mode
- * @param texture defines the texture to update
- */
- public updateTextureSamplingMode(samplingMode: number, texture: InternalTexture): void {
- var filters = this._getSamplingParameters(samplingMode, texture.generateMipMaps);
- if (texture.isCube) {
- this._setTextureParameterInteger(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_MAG_FILTER, filters.mag, texture);
- this._setTextureParameterInteger(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_MIN_FILTER, filters.min);
- this._bindTextureDirectly(this._gl.TEXTURE_CUBE_MAP, null);
- } else if (texture.is3D) {
- this._setTextureParameterInteger(this._gl.TEXTURE_3D, this._gl.TEXTURE_MAG_FILTER, filters.mag, texture);
- this._setTextureParameterInteger(this._gl.TEXTURE_3D, this._gl.TEXTURE_MIN_FILTER, filters.min);
- this._bindTextureDirectly(this._gl.TEXTURE_3D, null);
- } else {
- this._setTextureParameterInteger(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, filters.mag, texture);
- this._setTextureParameterInteger(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, filters.min);
- this._bindTextureDirectly(this._gl.TEXTURE_2D, null);
- }
- texture.samplingMode = samplingMode;
- }
- /**
- * Updates a depth texture Comparison Mode and Function.
- * If the comparison Function is equal to 0, the mode will be set to none.
- * Otherwise, this only works in webgl 2 and requires a shadow sampler in the shader.
- * @param texture The texture to set the comparison function for
- * @param comparisonFunction The comparison function to set, 0 if no comparison required
- */
- public updateTextureComparisonFunction(texture: InternalTexture, comparisonFunction: number): void {
- if (this.webGLVersion === 1) {
- Logger.Error("WebGL 1 does not support texture comparison.");
- return;
- }
- var gl = this._gl;
- if (texture.isCube) {
- this._bindTextureDirectly(this._gl.TEXTURE_CUBE_MAP, texture, true);
- if (comparisonFunction === 0) {
- gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_COMPARE_FUNC, Constants.LEQUAL);
- gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_COMPARE_MODE, gl.NONE);
- }
- else {
- gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_COMPARE_FUNC, comparisonFunction);
- gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_COMPARE_MODE, gl.COMPARE_REF_TO_TEXTURE);
- }
- this._bindTextureDirectly(this._gl.TEXTURE_CUBE_MAP, null);
- } else {
- this._bindTextureDirectly(this._gl.TEXTURE_2D, texture, true);
- if (comparisonFunction === 0) {
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_COMPARE_FUNC, Constants.LEQUAL);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_COMPARE_MODE, gl.NONE);
- }
- else {
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_COMPARE_FUNC, comparisonFunction);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_COMPARE_MODE, gl.COMPARE_REF_TO_TEXTURE);
- }
- this._bindTextureDirectly(this._gl.TEXTURE_2D, null);
- }
- texture._comparisonFunction = comparisonFunction;
- }
- /** @hidden */
- public _setupDepthStencilTexture(internalTexture: InternalTexture, size: number | { width: number, height: number }, generateStencil: boolean, bilinearFiltering: boolean, comparisonFunction: number): void {
- var width = (<{ width: number, height: number }>size).width || <number>size;
- var height = (<{ width: number, height: number }>size).height || <number>size;
- internalTexture.baseWidth = width;
- internalTexture.baseHeight = height;
- internalTexture.width = width;
- internalTexture.height = height;
- internalTexture.isReady = true;
- internalTexture.samples = 1;
- internalTexture.generateMipMaps = false;
- internalTexture._generateDepthBuffer = true;
- internalTexture._generateStencilBuffer = generateStencil;
- internalTexture.samplingMode = bilinearFiltering ? Constants.TEXTURE_BILINEAR_SAMPLINGMODE : Constants.TEXTURE_NEAREST_SAMPLINGMODE;
- internalTexture.type = Constants.TEXTURETYPE_UNSIGNED_INT;
- internalTexture._comparisonFunction = comparisonFunction;
- var gl = this._gl;
- var target = internalTexture.isCube ? gl.TEXTURE_CUBE_MAP : gl.TEXTURE_2D;
- var samplingParameters = this._getSamplingParameters(internalTexture.samplingMode, false);
- gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, samplingParameters.mag);
- gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, samplingParameters.min);
- gl.texParameteri(target, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
- gl.texParameteri(target, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
- if (comparisonFunction === 0) {
- gl.texParameteri(target, gl.TEXTURE_COMPARE_FUNC, Constants.LEQUAL);
- gl.texParameteri(target, gl.TEXTURE_COMPARE_MODE, gl.NONE);
- }
- else {
- gl.texParameteri(target, gl.TEXTURE_COMPARE_FUNC, comparisonFunction);
- gl.texParameteri(target, gl.TEXTURE_COMPARE_MODE, gl.COMPARE_REF_TO_TEXTURE);
- }
- }
- /**
- * Creates a depth stencil texture.
- * This is only available in WebGL 2 or with the depth texture extension available.
- * @param size The size of face edge in the texture.
- * @param options The options defining the texture.
- * @returns The texture
- */
- public createDepthStencilTexture(size: number | { width: number, height: number }, options: DepthTextureCreationOptions): InternalTexture {
- if (options.isCube) {
- let width = (<{ width: number, height: number }>size).width || <number>size;
- return this._createDepthStencilCubeTexture(width, options);
- }
- else {
- return this._createDepthStencilTexture(size, options);
- }
- }
- /**
- * Creates a depth stencil texture.
- * This is only available in WebGL 2 or with the depth texture extension available.
- * @param size The size of face edge in the texture.
- * @param options The options defining the texture.
- * @returns The texture
- */
- private _createDepthStencilTexture(size: number | { width: number, height: number }, options: DepthTextureCreationOptions): InternalTexture {
- var internalTexture = new InternalTexture(this, InternalTexture.DATASOURCE_DEPTHTEXTURE);
- if (!this._caps.depthTextureExtension) {
- Logger.Error("Depth texture is not supported by your browser or hardware.");
- return internalTexture;
- }
- var internalOptions = {
- bilinearFiltering: false,
- comparisonFunction: 0,
- generateStencil: false,
- ...options
- };
- var gl = this._gl;
- this._bindTextureDirectly(gl.TEXTURE_2D, internalTexture, true);
- this._setupDepthStencilTexture(internalTexture, size, internalOptions.generateStencil, internalOptions.bilinearFiltering, internalOptions.comparisonFunction);
- if (this.webGLVersion > 1) {
- if (internalOptions.generateStencil) {
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH24_STENCIL8, internalTexture.width, internalTexture.height, 0, gl.DEPTH_STENCIL, gl.UNSIGNED_INT_24_8, null);
- }
- else {
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT24, internalTexture.width, internalTexture.height, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_INT, null);
- }
- }
- else {
- if (internalOptions.generateStencil) {
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_STENCIL, internalTexture.width, internalTexture.height, 0, gl.DEPTH_STENCIL, gl.UNSIGNED_INT_24_8, null);
- }
- else {
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT, internalTexture.width, internalTexture.height, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_INT, null);
- }
- }
- this._bindTextureDirectly(gl.TEXTURE_2D, null);
- return internalTexture;
- }
- /**
- * Sets the frame buffer Depth / Stencil attachement of the render target to the defined depth stencil texture.
- * @param renderTarget The render target to set the frame buffer for
- */
- public setFrameBufferDepthStencilTexture(renderTarget: RenderTargetTexture): void {
- // Create the framebuffer
- var internalTexture = renderTarget.getInternalTexture();
- if (!internalTexture || !internalTexture._framebuffer || !renderTarget.depthStencilTexture) {
- return;
- }
- var gl = this._gl;
- var depthStencilTexture = renderTarget.depthStencilTexture;
- this._bindUnboundFramebuffer(internalTexture._framebuffer);
- if (depthStencilTexture.isCube) {
- if (depthStencilTexture._generateStencilBuffer) {
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.TEXTURE_CUBE_MAP_POSITIVE_X, depthStencilTexture._webGLTexture, 0);
- }
- else {
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_CUBE_MAP_POSITIVE_X, depthStencilTexture._webGLTexture, 0);
- }
- }
- else {
- if (depthStencilTexture._generateStencilBuffer) {
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.TEXTURE_2D, depthStencilTexture._webGLTexture, 0);
- }
- else {
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, depthStencilTexture._webGLTexture, 0);
- }
- }
- this._bindUnboundFramebuffer(null);
- }
- /** @hidden */
- public _setupFramebufferDepthAttachments(generateStencilBuffer: boolean, generateDepthBuffer: boolean, width: number, height: number, samples = 1): Nullable<WebGLRenderbuffer> {
- var depthStencilBuffer: Nullable<WebGLRenderbuffer> = null;
- var gl = this._gl;
- // Create the depth/stencil buffer
- if (generateStencilBuffer) {
- depthStencilBuffer = gl.createRenderbuffer();
- gl.bindRenderbuffer(gl.RENDERBUFFER, depthStencilBuffer);
- if (samples > 1) {
- gl.renderbufferStorageMultisample(gl.RENDERBUFFER, samples, gl.DEPTH24_STENCIL8, width, height);
- } else {
- gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, width, height);
- }
- gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, depthStencilBuffer);
- }
- else if (generateDepthBuffer) {
- depthStencilBuffer = gl.createRenderbuffer();
- gl.bindRenderbuffer(gl.RENDERBUFFER, depthStencilBuffer);
- if (samples > 1) {
- gl.renderbufferStorageMultisample(gl.RENDERBUFFER, samples, gl.DEPTH_COMPONENT16, width, height);
- } else {
- gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
- }
- gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthStencilBuffer);
- }
- return depthStencilBuffer;
- }
- /** @hidden */
- public _uploadCompressedDataToTextureDirectly(texture: InternalTexture, internalFormat: number, width: number, height: number, data: ArrayBufferView, faceIndex: number = 0, lod: number = 0) {
- var gl = this._gl;
- var target = gl.TEXTURE_2D;
- if (texture.isCube) {
- target = gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex;
- }
- this._gl.compressedTexImage2D(target, lod, internalFormat, width, height, 0, <DataView>data);
- }
- /** @hidden */
- public _uploadDataToTextureDirectly(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0, babylonInternalFormat?: number, useTextureWidthAndHeight = false): void {
- var gl = this._gl;
- var textureType = this._getWebGLTextureType(texture.type);
- var format = this._getInternalFormat(texture.format);
- var internalFormat = babylonInternalFormat === undefined ? this._getRGBABufferInternalSizedFormat(texture.type, format) : this._getInternalFormat(babylonInternalFormat);
- this._unpackFlipY(texture.invertY);
- var target = gl.TEXTURE_2D;
- if (texture.isCube) {
- target = gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex;
- }
- const lodMaxWidth = Math.round(Math.log(texture.width) * Math.LOG2E);
- const lodMaxHeight = Math.round(Math.log(texture.height) * Math.LOG2E);
- const width = useTextureWidthAndHeight ? texture.width : Math.pow(2, Math.max(lodMaxWidth - lod, 0));
- const height = useTextureWidthAndHeight ? texture.height : Math.pow(2, Math.max(lodMaxHeight - lod, 0));
- gl.texImage2D(target, lod, internalFormat, width, height, 0, format, textureType, imageData);
- }
- /** @hidden */
- public _uploadArrayBufferViewToTexture(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
- var gl = this._gl;
- var bindTarget = texture.isCube ? gl.TEXTURE_CUBE_MAP : gl.TEXTURE_2D;
- this._bindTextureDirectly(bindTarget, texture, true);
- this._uploadDataToTextureDirectly(texture, imageData, faceIndex, lod);
- this._bindTextureDirectly(bindTarget, null, true);
- }
- /** @hidden */
- public _uploadImageToTexture(texture: InternalTexture, image: HTMLImageElement, faceIndex: number = 0, lod: number = 0) {
- var gl = this._gl;
- var textureType = this._getWebGLTextureType(texture.type);
- var format = this._getInternalFormat(texture.format);
- var internalFormat = this._getRGBABufferInternalSizedFormat(texture.type, format);
- var bindTarget = texture.isCube ? gl.TEXTURE_CUBE_MAP : gl.TEXTURE_2D;
- this._bindTextureDirectly(bindTarget, texture, true);
- this._unpackFlipY(texture.invertY);
- var target = gl.TEXTURE_2D;
- if (texture.isCube) {
- target = gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex;
- }
- gl.texImage2D(target, lod, internalFormat, format, textureType, image);
- this._bindTextureDirectly(bindTarget, null, true);
- }
- /**
- * @hidden
- */
- public _setCubeMapTextureParams(loadMipmap: boolean): void {
- var gl = this._gl;
- gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
- gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, loadMipmap ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR);
- gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
- gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
- this._bindTextureDirectly(gl.TEXTURE_CUBE_MAP, null);
- }
- protected _prepareWebGLTextureContinuation(texture: InternalTexture, scene: Nullable<ISceneLike>, noMipmap: boolean, isCompressed: boolean, samplingMode: number): void {
- var gl = this._gl;
- if (!gl) {
- return;
- }
- var filters = this._getSamplingParameters(samplingMode, !noMipmap);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filters.mag);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filters.min);
- if (!noMipmap && !isCompressed) {
- gl.generateMipmap(gl.TEXTURE_2D);
- }
- this._bindTextureDirectly(gl.TEXTURE_2D, null);
- // this.resetTextureCache();
- if (scene) {
- scene._removePendingData(texture);
- }
- texture.onLoadedObservable.notifyObservers(texture);
- texture.onLoadedObservable.clear();
- }
- private _prepareWebGLTexture(texture: InternalTexture, scene: Nullable<ISceneLike>, width: number, height: number, invertY: boolean, noMipmap: boolean, isCompressed: boolean,
- processFunction: (width: number, height: number, continuationCallback: () => void) => boolean, samplingMode: number = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE): void {
- var maxTextureSize = this.getCaps().maxTextureSize;
- var potWidth = Math.min(maxTextureSize, this.needPOTTextures ? ThinEngine.GetExponentOfTwo(width, maxTextureSize) : width);
- var potHeight = Math.min(maxTextureSize, this.needPOTTextures ? ThinEngine.GetExponentOfTwo(height, maxTextureSize) : height);
- var gl = this._gl;
- if (!gl) {
- return;
- }
- if (!texture._webGLTexture) {
- // this.resetTextureCache();
- if (scene) {
- scene._removePendingData(texture);
- }
- return;
- }
- this._bindTextureDirectly(gl.TEXTURE_2D, texture, true);
- this._unpackFlipY(invertY === undefined ? true : (invertY ? true : false));
- texture.baseWidth = width;
- texture.baseHeight = height;
- texture.width = potWidth;
- texture.height = potHeight;
- texture.isReady = true;
- if (processFunction(potWidth, potHeight, () => {
- this._prepareWebGLTextureContinuation(texture, scene, noMipmap, isCompressed, samplingMode);
- })) {
- // Returning as texture needs extra async steps
- return;
- }
- this._prepareWebGLTextureContinuation(texture, scene, noMipmap, isCompressed, samplingMode);
- }
- /** @hidden */
- public _releaseFramebufferObjects(texture: InternalTexture): void {
- var gl = this._gl;
- if (texture._framebuffer) {
- gl.deleteFramebuffer(texture._framebuffer);
- texture._framebuffer = null;
- }
- if (texture._depthStencilBuffer) {
- gl.deleteRenderbuffer(texture._depthStencilBuffer);
- texture._depthStencilBuffer = null;
- }
- if (texture._MSAAFramebuffer) {
- gl.deleteFramebuffer(texture._MSAAFramebuffer);
- texture._MSAAFramebuffer = null;
- }
- if (texture._MSAARenderBuffer) {
- gl.deleteRenderbuffer(texture._MSAARenderBuffer);
- texture._MSAARenderBuffer = null;
- }
- }
- /** @hidden */
- public _releaseTexture(texture: InternalTexture): void {
- this._releaseFramebufferObjects(texture);
- this._deleteTexture(texture._webGLTexture);
- // Unbind channels
- this.unbindAllTextures();
- var index = this._internalTexturesCache.indexOf(texture);
- if (index !== -1) {
- this._internalTexturesCache.splice(index, 1);
- }
- // Integrated fixed lod samplers.
- if (texture._lodTextureHigh) {
- texture._lodTextureHigh.dispose();
- }
- if (texture._lodTextureMid) {
- texture._lodTextureMid.dispose();
- }
- if (texture._lodTextureLow) {
- texture._lodTextureLow.dispose();
- }
- // Integrated irradiance map.
- if (texture._irradianceTexture) {
- texture._irradianceTexture.dispose();
- }
- }
- protected _deleteTexture(texture: Nullable<WebGLTexture>): void {
- this._gl.deleteTexture(texture);
- }
- protected _setProgram(program: WebGLProgram): void {
- if (this._currentProgram !== program) {
- this._gl.useProgram(program);
- this._currentProgram = program;
- }
- }
- protected _boundUniforms: { [key: number]: WebGLUniformLocation } = {};
- /**
- * Binds an effect to the webGL context
- * @param effect defines the effect to bind
- */
- public bindSamplers(effect: Effect): void {
- let webGLPipelineContext = effect.getPipelineContext() as WebGLPipelineContext;
- this._setProgram(webGLPipelineContext.program!);
- 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;
- }
- private _activateCurrentTexture() {
- if (this._currentTextureChannel !== this._activeChannel) {
- this._gl.activeTexture(this._gl.TEXTURE0 + this._activeChannel);
- this._currentTextureChannel = this._activeChannel;
- }
- }
- /** @hidden */
- public _bindTextureDirectly(target: number, texture: Nullable<InternalTexture>, forTextureDataUpdate = false, force = false): boolean {
- var wasPreviouslyBound = false;
- let isTextureForRendering = texture && texture._associatedChannel > -1;
- if (forTextureDataUpdate && isTextureForRendering) {
- this._activeChannel = texture!._associatedChannel;
- }
- let currentTextureBound = this._boundTexturesCache[this._activeChannel];
- if (currentTextureBound !== texture || force) {
- this._activateCurrentTexture();
- if (texture && texture.isMultiview) {
- this._gl.bindTexture(target, texture ? texture._colorTextureArray : null);
- } else {
- this._gl.bindTexture(target, texture ? texture._webGLTexture : null);
- }
- this._boundTexturesCache[this._activeChannel] = texture;
- if (texture) {
- texture._associatedChannel = this._activeChannel;
- }
- } else if (forTextureDataUpdate) {
- wasPreviouslyBound = true;
- this._activateCurrentTexture();
- }
- if (isTextureForRendering && !forTextureDataUpdate) {
- this._bindSamplerUniformToChannel(texture!._associatedChannel, this._activeChannel);
- }
- return wasPreviouslyBound;
- }
- /** @hidden */
- public _bindTexture(channel: number, texture: Nullable<InternalTexture>): void {
- if (channel === undefined) {
- return;
- }
- if (texture) {
- texture._associatedChannel = channel;
- }
- this._activeChannel = channel;
- this._bindTextureDirectly(this._gl.TEXTURE_2D, texture);
- }
- /**
- * Unbind all textures from the webGL context
- */
- public unbindAllTextures(): void {
- for (var channel = 0; channel < this._maxSimultaneousTextures; channel++) {
- this._activeChannel = channel;
- this._bindTextureDirectly(this._gl.TEXTURE_2D, null);
- this._bindTextureDirectly(this._gl.TEXTURE_CUBE_MAP, null);
- if (this.webGLVersion > 1) {
- this._bindTextureDirectly(this._gl.TEXTURE_3D, null);
- }
- }
- }
- /**
- * Sets a texture to the according uniform.
- * @param channel The texture channel
- * @param uniform The uniform to set
- * @param texture The texture to apply
- */
- public setTexture(channel: number, uniform: Nullable<WebGLUniformLocation>, texture: Nullable<BaseTexture>): void {
- if (channel === undefined) {
- return;
- }
- if (uniform) {
- this._boundUniforms[channel] = uniform;
- }
- this._setTexture(channel, texture);
- }
- private _bindSamplerUniformToChannel(sourceSlot: number, destination: number) {
- let uniform = this._boundUniforms[sourceSlot];
- if (!uniform || uniform._currentState === destination) {
- return;
- }
- this._gl.uniform1i(uniform, destination);
- uniform._currentState = destination;
- }
- private _getTextureWrapMode(mode: number): number {
- switch (mode) {
- case Constants.TEXTURE_WRAP_ADDRESSMODE:
- return this._gl.REPEAT;
- case Constants.TEXTURE_CLAMP_ADDRESSMODE:
- return this._gl.CLAMP_TO_EDGE;
- case Constants.TEXTURE_MIRROR_ADDRESSMODE:
- return this._gl.MIRRORED_REPEAT;
- }
- return this._gl.REPEAT;
- }
- protected _setTexture(channel: number, texture: Nullable<BaseTexture>, isPartOfTextureArray = false, depthStencilTexture = false): boolean {
- // Not ready?
- if (!texture) {
- if (this._boundTexturesCache[channel] != null) {
- this._activeChannel = channel;
- this._bindTextureDirectly(this._gl.TEXTURE_2D, null);
- this._bindTextureDirectly(this._gl.TEXTURE_CUBE_MAP, null);
- if (this.webGLVersion > 1) {
- this._bindTextureDirectly(this._gl.TEXTURE_3D, 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 {
- internalTexture = this.emptyTexture;
- }
- if (!isPartOfTextureArray && internalTexture) {
- internalTexture._associatedChannel = channel;
- }
- let needToBind = true;
- if (this._boundTexturesCache[channel] === internalTexture) {
- if (!isPartOfTextureArray) {
- this._bindSamplerUniformToChannel(internalTexture._associatedChannel, channel);
- }
- needToBind = false;
- }
- this._activeChannel = channel;
- if (internalTexture && internalTexture.isMultiview) {
- if (needToBind) {
- this._bindTextureDirectly(this._gl.TEXTURE_2D_ARRAY, internalTexture, isPartOfTextureArray);
- }
- } else if (internalTexture && internalTexture.is3D) {
- if (needToBind) {
- this._bindTextureDirectly(this._gl.TEXTURE_3D, internalTexture, isPartOfTextureArray);
- }
- if (internalTexture && internalTexture._cachedWrapU !== texture.wrapU) {
- internalTexture._cachedWrapU = texture.wrapU;
- this._setTextureParameterInteger(this._gl.TEXTURE_3D, this._gl.TEXTURE_WRAP_S, this._getTextureWrapMode(texture.wrapU), internalTexture);
- }
- if (internalTexture && internalTexture._cachedWrapV !== texture.wrapV) {
- internalTexture._cachedWrapV = texture.wrapV;
- this._setTextureParameterInteger(this._gl.TEXTURE_3D, this._gl.TEXTURE_WRAP_T, this._getTextureWrapMode(texture.wrapV), internalTexture);
- }
- if (internalTexture && internalTexture._cachedWrapR !== texture.wrapR) {
- internalTexture._cachedWrapR = texture.wrapR;
- this._setTextureParameterInteger(this._gl.TEXTURE_3D, this._gl.TEXTURE_WRAP_R, this._getTextureWrapMode(texture.wrapR), internalTexture);
- }
- this._setAnisotropicLevel(this._gl.TEXTURE_3D, texture);
- }
- else if (internalTexture && internalTexture.isCube) {
- if (needToBind) {
- this._bindTextureDirectly(this._gl.TEXTURE_CUBE_MAP, internalTexture, isPartOfTextureArray);
- }
- if (internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
- internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
- // CUBIC_MODE and SKYBOX_MODE both require CLAMP_TO_EDGE. All other modes use REPEAT.
- var textureWrapMode = (texture.coordinatesMode !== Constants.TEXTURE_CUBIC_MODE && texture.coordinatesMode !== Constants.TEXTURE_SKYBOX_MODE) ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE;
- this._setTextureParameterInteger(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_S, textureWrapMode, internalTexture);
- this._setTextureParameterInteger(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_T, textureWrapMode);
- }
- this._setAnisotropicLevel(this._gl.TEXTURE_CUBE_MAP, texture);
- } else {
- if (needToBind) {
- this._bindTextureDirectly(this._gl.TEXTURE_2D, internalTexture, isPartOfTextureArray);
- }
- if (internalTexture && internalTexture._cachedWrapU !== texture.wrapU) {
- internalTexture._cachedWrapU = texture.wrapU;
- this._setTextureParameterInteger(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._getTextureWrapMode(texture.wrapU), internalTexture);
- }
- if (internalTexture && internalTexture._cachedWrapV !== texture.wrapV) {
- internalTexture._cachedWrapV = texture.wrapV;
- this._setTextureParameterInteger(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._getTextureWrapMode(texture.wrapV), internalTexture);
- }
- this._setAnisotropicLevel(this._gl.TEXTURE_2D, texture);
- }
- return true;
- }
- /**
- * Sets an array of texture to the webGL context
- * @param channel defines the channel where the texture array must be set
- * @param uniform defines the associated uniform location
- * @param textures defines the array of textures to bind
- */
- public setTextureArray(channel: number, uniform: Nullable<WebGLUniformLocation>, textures: BaseTexture[]): void {
- if (channel === undefined || !uniform) {
- return;
- }
- if (!this._textureUnits || this._textureUnits.length !== textures.length) {
- this._textureUnits = new Int32Array(textures.length);
- }
- for (let i = 0; i < textures.length; i++) {
- let texture = textures[i].getInternalTexture();
- if (texture) {
- this._textureUnits[i] = channel + i;
- texture._associatedChannel = channel + i;
- } else {
- this._textureUnits[i] = -1;
- }
- }
- this._gl.uniform1iv(uniform, this._textureUnits);
- for (var index = 0; index < textures.length; index++) {
- this._setTexture(this._textureUnits[index], textures[index], true);
- }
- }
- /** @hidden */
- public _setAnisotropicLevel(target: number, texture: BaseTexture) {
- var internalTexture = texture.getInternalTexture();
- if (!internalTexture) {
- return;
- }
- var anisotropicFilterExtension = this._caps.textureAnisotropicFilterExtension;
- var value = texture.anisotropicFilteringLevel;
- if (internalTexture.samplingMode !== Constants.TEXTURE_LINEAR_LINEAR_MIPNEAREST
- && internalTexture.samplingMode !== Constants.TEXTURE_LINEAR_LINEAR_MIPLINEAR
- && internalTexture.samplingMode !== Constants.TEXTURE_LINEAR_LINEAR) {
- value = 1; // Forcing the anisotropic to 1 because else webgl will force filters to linear
- }
- if (anisotropicFilterExtension && internalTexture._cachedAnisotropicFilteringLevel !== value) {
- this._setTextureParameterFloat(target, anisotropicFilterExtension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(value, this._caps.maxAnisotropy), internalTexture);
- internalTexture._cachedAnisotropicFilteringLevel = value;
- }
- }
- private _setTextureParameterFloat(target: number, parameter: number, value: number, texture: InternalTexture): void {
- this._bindTextureDirectly(target, texture, true, true);
- this._gl.texParameterf(target, parameter, value);
- }
- private _setTextureParameterInteger(target: number, parameter: number, value: number, texture?: InternalTexture) {
- if (texture) {
- this._bindTextureDirectly(target, texture, true, true);
- }
- this._gl.texParameteri(target, parameter, value);
- }
- /**
- * Unbind all vertex attributes from the webGL context
- */
- public unbindAllAttributes() {
- if (this._mustWipeVertexAttributes) {
- this._mustWipeVertexAttributes = false;
- for (var i = 0; i < this._caps.maxVertexAttribs; i++) {
- this._gl.disableVertexAttribArray(i);
- this._vertexAttribArraysEnabled[i] = false;
- this._currentBufferPointers[i].active = false;
- }
- return;
- }
- for (var i = 0, ul = this._vertexAttribArraysEnabled.length; i < ul; i++) {
- if (i >= this._caps.maxVertexAttribs || !this._vertexAttribArraysEnabled[i]) {
- continue;
- }
- this._gl.disableVertexAttribArray(i);
- this._vertexAttribArraysEnabled[i] = false;
- this._currentBufferPointers[i].active = false;
- }
- }
- /**
- * Force the engine to release all cached effects. This means that next effect compilation will have to be done completely even if a similar effect was already compiled
- */
- public releaseEffects() {
- for (var name in this._compiledEffects) {
- let webGLPipelineContext = this._compiledEffects[name].getPipelineContext() as WebGLPipelineContext;
- this._deletePipelineContext(webGLPipelineContext);
- }
- this._compiledEffects = {};
- }
- /**
- * Dispose and release all associated resources
- */
- public dispose(): void {
- this.stopRenderLoop();
- // Empty texture
- if (this._emptyTexture) {
- this._releaseTexture(this._emptyTexture);
- this._emptyTexture = null;
- }
- if (this._emptyCubeTexture) {
- this._releaseTexture(this._emptyCubeTexture);
- this._emptyCubeTexture = null;
- }
- // Release effects
- this.releaseEffects();
- // Unbind
- this.unbindAllAttributes();
- this._boundUniforms = [];
- // Events
- if (DomManagement.IsWindowObjectExist()) {
- if (this._renderingCanvas) {
- if (!this._doNotHandleContextLost) {
- this._renderingCanvas.removeEventListener("webglcontextlost", this._onContextLost);
- this._renderingCanvas.removeEventListener("webglcontextrestored", this._onContextRestored);
- }
- }
- }
- this._workingCanvas = null;
- this._workingContext = null;
- this._currentBufferPointers = [];
- this._renderingCanvas = null;
- this._currentProgram = null;
- this._bindedRenderFunction = null;
- Effect.ResetCache();
- // Abort active requests
- for (let request of this._activeRequests) {
- request.abort();
- }
- }
- /**
- * Attach a new callback raised when context lost event is fired
- * @param callback defines the callback to call
- */
- public attachContextLostEvent(callback: ((event: WebGLContextEvent) => void)): void {
- if (this._renderingCanvas) {
- this._renderingCanvas.addEventListener("webglcontextlost", <any>callback, false);
- }
- }
- /**
- * Attach a new callback raised when context restored event is fired
- * @param callback defines the callback to call
- */
- public attachContextRestoredEvent(callback: ((event: WebGLContextEvent) => void)): void {
- if (this._renderingCanvas) {
- this._renderingCanvas.addEventListener("webglcontextrestored", <any>callback, false);
- }
- }
- /**
- * Get the current error code of the webGL context
- * @returns the error code
- * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/getError
- */
- public getError(): number {
- return this._gl.getError();
- }
- private _canRenderToFloatFramebuffer(): boolean {
- if (this._webGLVersion > 1) {
- return this._caps.colorBufferFloat;
- }
- return this._canRenderToFramebuffer(Constants.TEXTURETYPE_FLOAT);
- }
- private _canRenderToHalfFloatFramebuffer(): boolean {
- if (this._webGLVersion > 1) {
- return this._caps.colorBufferFloat;
- }
- return this._canRenderToFramebuffer(Constants.TEXTURETYPE_HALF_FLOAT);
- }
- // Thank you : http://stackoverflow.com/questions/28827511/webgl-ios-render-to-floating-point-texture
- private _canRenderToFramebuffer(type: number): boolean {
- let gl = this._gl;
- //clear existing errors
- while (gl.getError() !== gl.NO_ERROR) { }
- let successful = true;
- let texture = gl.createTexture();
- gl.bindTexture(gl.TEXTURE_2D, texture);
- gl.texImage2D(gl.TEXTURE_2D, 0, this._getRGBABufferInternalSizedFormat(type), 1, 1, 0, gl.RGBA, this._getWebGLTextureType(type), null);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
- let fb = gl.createFramebuffer();
- gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
- let status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
- successful = successful && (status === gl.FRAMEBUFFER_COMPLETE);
- successful = successful && (gl.getError() === gl.NO_ERROR);
- //try render by clearing frame buffer's color buffer
- if (successful) {
- gl.clear(gl.COLOR_BUFFER_BIT);
- successful = successful && (gl.getError() === gl.NO_ERROR);
- }
- //try reading from frame to ensure render occurs (just creating the FBO is not sufficient to determine if rendering is supported)
- if (successful) {
- //in practice it's sufficient to just read from the backbuffer rather than handle potentially issues reading from the texture
- gl.bindFramebuffer(gl.FRAMEBUFFER, null);
- let readFormat = gl.RGBA;
- let readType = gl.UNSIGNED_BYTE;
- let buffer = new Uint8Array(4);
- gl.readPixels(0, 0, 1, 1, readFormat, readType, buffer);
- successful = successful && (gl.getError() === gl.NO_ERROR);
- }
- //clean up
- gl.deleteTexture(texture);
- gl.deleteFramebuffer(fb);
- gl.bindFramebuffer(gl.FRAMEBUFFER, null);
- //clear accumulated errors
- while (!successful && (gl.getError() !== gl.NO_ERROR)) { }
- return successful;
- }
- /** @hidden */
- public _getWebGLTextureType(type: number): number {
- if (this._webGLVersion === 1) {
- switch (type) {
- case Constants.TEXTURETYPE_FLOAT:
- return this._gl.FLOAT;
- case Constants.TEXTURETYPE_HALF_FLOAT:
- return this._gl.HALF_FLOAT_OES;
- case Constants.TEXTURETYPE_UNSIGNED_BYTE:
- return this._gl.UNSIGNED_BYTE;
- case Constants.TEXTURETYPE_UNSIGNED_SHORT_4_4_4_4:
- return this._gl.UNSIGNED_SHORT_4_4_4_4;
- case Constants.TEXTURETYPE_UNSIGNED_SHORT_5_5_5_1:
- return this._gl.UNSIGNED_SHORT_5_5_5_1;
- case Constants.TEXTURETYPE_UNSIGNED_SHORT_5_6_5:
- return this._gl.UNSIGNED_SHORT_5_6_5;
- }
- return this._gl.UNSIGNED_BYTE;
- }
- switch (type) {
- case Constants.TEXTURETYPE_BYTE:
- return this._gl.BYTE;
- case Constants.TEXTURETYPE_UNSIGNED_BYTE:
- return this._gl.UNSIGNED_BYTE;
- case Constants.TEXTURETYPE_SHORT:
- return this._gl.SHORT;
- case Constants.TEXTURETYPE_UNSIGNED_SHORT:
- return this._gl.UNSIGNED_SHORT;
- case Constants.TEXTURETYPE_INT:
- return this._gl.INT;
- case Constants.TEXTURETYPE_UNSIGNED_INTEGER: // Refers to UNSIGNED_INT
- return this._gl.UNSIGNED_INT;
- case Constants.TEXTURETYPE_FLOAT:
- return this._gl.FLOAT;
- case Constants.TEXTURETYPE_HALF_FLOAT:
- return this._gl.HALF_FLOAT;
- case Constants.TEXTURETYPE_UNSIGNED_SHORT_4_4_4_4:
- return this._gl.UNSIGNED_SHORT_4_4_4_4;
- case Constants.TEXTURETYPE_UNSIGNED_SHORT_5_5_5_1:
- return this._gl.UNSIGNED_SHORT_5_5_5_1;
- case Constants.TEXTURETYPE_UNSIGNED_SHORT_5_6_5:
- return this._gl.UNSIGNED_SHORT_5_6_5;
- case Constants.TEXTURETYPE_UNSIGNED_INT_2_10_10_10_REV:
- return this._gl.UNSIGNED_INT_2_10_10_10_REV;
- case Constants.TEXTURETYPE_UNSIGNED_INT_24_8:
- return this._gl.UNSIGNED_INT_24_8;
- case Constants.TEXTURETYPE_UNSIGNED_INT_10F_11F_11F_REV:
- return this._gl.UNSIGNED_INT_10F_11F_11F_REV;
- case Constants.TEXTURETYPE_UNSIGNED_INT_5_9_9_9_REV:
- return this._gl.UNSIGNED_INT_5_9_9_9_REV;
- case Constants.TEXTURETYPE_FLOAT_32_UNSIGNED_INT_24_8_REV:
- return this._gl.FLOAT_32_UNSIGNED_INT_24_8_REV;
- }
- return this._gl.UNSIGNED_BYTE;
- }
- /** @hidden */
- public _getInternalFormat(format: number): number {
- var internalFormat = this._gl.RGBA;
- switch (format) {
- case Constants.TEXTUREFORMAT_ALPHA:
- internalFormat = this._gl.ALPHA;
- break;
- case Constants.TEXTUREFORMAT_LUMINANCE:
- internalFormat = this._gl.LUMINANCE;
- break;
- case Constants.TEXTUREFORMAT_LUMINANCE_ALPHA:
- internalFormat = this._gl.LUMINANCE_ALPHA;
- break;
- case Constants.TEXTUREFORMAT_RED:
- internalFormat = this._gl.RED;
- break;
- case Constants.TEXTUREFORMAT_RG:
- internalFormat = this._gl.RG;
- break;
- case Constants.TEXTUREFORMAT_RGB:
- internalFormat = this._gl.RGB;
- break;
- case Constants.TEXTUREFORMAT_RGBA:
- internalFormat = this._gl.RGBA;
- break;
- }
- if (this._webGLVersion > 1) {
- switch (format) {
- case Constants.TEXTUREFORMAT_RED_INTEGER:
- internalFormat = this._gl.RED_INTEGER;
- break;
- case Constants.TEXTUREFORMAT_RG_INTEGER:
- internalFormat = this._gl.RG_INTEGER;
- break;
- case Constants.TEXTUREFORMAT_RGB_INTEGER:
- internalFormat = this._gl.RGB_INTEGER;
- break;
- case Constants.TEXTUREFORMAT_RGBA_INTEGER:
- internalFormat = this._gl.RGBA_INTEGER;
- break;
- }
- }
- return internalFormat;
- }
- /** @hidden */
- public _getRGBABufferInternalSizedFormat(type: number, format?: number): number {
- if (this._webGLVersion === 1) {
- if (format !== undefined) {
- switch (format) {
- case Constants.TEXTUREFORMAT_ALPHA:
- return this._gl.ALPHA;
- case Constants.TEXTUREFORMAT_LUMINANCE:
- return this._gl.LUMINANCE;
- case Constants.TEXTUREFORMAT_LUMINANCE_ALPHA:
- return this._gl.LUMINANCE_ALPHA;
- }
- }
- return this._gl.RGBA;
- }
- switch (type) {
- case Constants.TEXTURETYPE_BYTE:
- switch (format) {
- case Constants.TEXTUREFORMAT_RED:
- return this._gl.R8_SNORM;
- case Constants.TEXTUREFORMAT_RG:
- return this._gl.RG8_SNORM;
- case Constants.TEXTUREFORMAT_RGB:
- return this._gl.RGB8_SNORM;
- case Constants.TEXTUREFORMAT_RED_INTEGER:
- return this._gl.R8I;
- case Constants.TEXTUREFORMAT_RG_INTEGER:
- return this._gl.RG8I;
- case Constants.TEXTUREFORMAT_RGB_INTEGER:
- return this._gl.RGB8I;
- case Constants.TEXTUREFORMAT_RGBA_INTEGER:
- return this._gl.RGBA8I;
- default:
- return this._gl.RGBA8_SNORM;
- }
- case Constants.TEXTURETYPE_UNSIGNED_BYTE:
- switch (format) {
- case Constants.TEXTUREFORMAT_RED:
- return this._gl.R8;
- case Constants.TEXTUREFORMAT_RG:
- return this._gl.RG8;
- case Constants.TEXTUREFORMAT_RGB:
- return this._gl.RGB8; // By default. Other possibilities are RGB565, SRGB8.
- case Constants.TEXTUREFORMAT_RGBA:
- return this._gl.RGBA8; // By default. Other possibilities are RGB5_A1, RGBA4, SRGB8_ALPHA8.
- case Constants.TEXTUREFORMAT_RED_INTEGER:
- return this._gl.R8UI;
- case Constants.TEXTUREFORMAT_RG_INTEGER:
- return this._gl.RG8UI;
- case Constants.TEXTUREFORMAT_RGB_INTEGER:
- return this._gl.RGB8UI;
- case Constants.TEXTUREFORMAT_RGBA_INTEGER:
- return this._gl.RGBA8UI;
- case Constants.TEXTUREFORMAT_ALPHA:
- return this._gl.ALPHA;
- case Constants.TEXTUREFORMAT_LUMINANCE:
- return this._gl.LUMINANCE;
- case Constants.TEXTUREFORMAT_LUMINANCE_ALPHA:
- return this._gl.LUMINANCE_ALPHA;
- default:
- return this._gl.RGBA8;
- }
- case Constants.TEXTURETYPE_SHORT:
- switch (format) {
- case Constants.TEXTUREFORMAT_RED_INTEGER:
- return this._gl.R16I;
- case Constants.TEXTUREFORMAT_RG_INTEGER:
- return this._gl.RG16I;
- case Constants.TEXTUREFORMAT_RGB_INTEGER:
- return this._gl.RGB16I;
- case Constants.TEXTUREFORMAT_RGBA_INTEGER:
- return this._gl.RGBA16I;
- default:
- return this._gl.RGBA16I;
- }
- case Constants.TEXTURETYPE_UNSIGNED_SHORT:
- switch (format) {
- case Constants.TEXTUREFORMAT_RED_INTEGER:
- return this._gl.R16UI;
- case Constants.TEXTUREFORMAT_RG_INTEGER:
- return this._gl.RG16UI;
- case Constants.TEXTUREFORMAT_RGB_INTEGER:
- return this._gl.RGB16UI;
- case Constants.TEXTUREFORMAT_RGBA_INTEGER:
- return this._gl.RGBA16UI;
- default:
- return this._gl.RGBA16UI;
- }
- case Constants.TEXTURETYPE_INT:
- switch (format) {
- case Constants.TEXTUREFORMAT_RED_INTEGER:
- return this._gl.R32I;
- case Constants.TEXTUREFORMAT_RG_INTEGER:
- return this._gl.RG32I;
- case Constants.TEXTUREFORMAT_RGB_INTEGER:
- return this._gl.RGB32I;
- case Constants.TEXTUREFORMAT_RGBA_INTEGER:
- return this._gl.RGBA32I;
- default:
- return this._gl.RGBA32I;
- }
- case Constants.TEXTURETYPE_UNSIGNED_INTEGER: // Refers to UNSIGNED_INT
- switch (format) {
- case Constants.TEXTUREFORMAT_RED_INTEGER:
- return this._gl.R32UI;
- case Constants.TEXTUREFORMAT_RG_INTEGER:
- return this._gl.RG32UI;
- case Constants.TEXTUREFORMAT_RGB_INTEGER:
- return this._gl.RGB32UI;
- case Constants.TEXTUREFORMAT_RGBA_INTEGER:
- return this._gl.RGBA32UI;
- default:
- return this._gl.RGBA32UI;
- }
- case Constants.TEXTURETYPE_FLOAT:
- switch (format) {
- case Constants.TEXTUREFORMAT_RED:
- return this._gl.R32F; // By default. Other possibility is R16F.
- case Constants.TEXTUREFORMAT_RG:
- return this._gl.RG32F; // By default. Other possibility is RG16F.
- case Constants.TEXTUREFORMAT_RGB:
- return this._gl.RGB32F; // By default. Other possibilities are RGB16F, R11F_G11F_B10F, RGB9_E5.
- case Constants.TEXTUREFORMAT_RGBA:
- return this._gl.RGBA32F; // By default. Other possibility is RGBA16F.
- default:
- return this._gl.RGBA32F;
- }
- case Constants.TEXTURETYPE_HALF_FLOAT:
- switch (format) {
- case Constants.TEXTUREFORMAT_RED:
- return this._gl.R16F;
- case Constants.TEXTUREFORMAT_RG:
- return this._gl.RG16F;
- case Constants.TEXTUREFORMAT_RGB:
- return this._gl.RGB16F; // By default. Other possibilities are R11F_G11F_B10F, RGB9_E5.
- case Constants.TEXTUREFORMAT_RGBA:
- return this._gl.RGBA16F;
- default:
- return this._gl.RGBA16F;
- }
- case Constants.TEXTURETYPE_UNSIGNED_SHORT_5_6_5:
- return this._gl.RGB565;
- case Constants.TEXTURETYPE_UNSIGNED_INT_10F_11F_11F_REV:
- return this._gl.R11F_G11F_B10F;
- case Constants.TEXTURETYPE_UNSIGNED_INT_5_9_9_9_REV:
- return this._gl.RGB9_E5;
- case Constants.TEXTURETYPE_UNSIGNED_SHORT_4_4_4_4:
- return this._gl.RGBA4;
- case Constants.TEXTURETYPE_UNSIGNED_SHORT_5_5_5_1:
- return this._gl.RGB5_A1;
- case Constants.TEXTURETYPE_UNSIGNED_INT_2_10_10_10_REV:
- switch (format) {
- case Constants.TEXTUREFORMAT_RGBA:
- return this._gl.RGB10_A2; // By default. Other possibility is RGB5_A1.
- case Constants.TEXTUREFORMAT_RGBA_INTEGER:
- return this._gl.RGB10_A2UI;
- default:
- return this._gl.RGB10_A2;
- }
- }
- return this._gl.RGBA8;
- }
- /** @hidden */
- public _getRGBAMultiSampleBufferFormat(type: number): number {
- if (type === Constants.TEXTURETYPE_FLOAT) {
- return this._gl.RGBA32F;
- }
- else if (type === Constants.TEXTURETYPE_HALF_FLOAT) {
- return this._gl.RGBA16F;
- }
- return this._gl.RGBA8;
- }
- /** @hidden */
- public _loadFile(url: string, onSuccess: (data: string | ArrayBuffer, responseURL?: string) => void, onProgress?: (data: any) => void, offlineProvider?: IOfflineProvider, useArrayBuffer?: boolean, onError?: (request?: WebRequest, exception?: any) => void): IFileRequest {
- let request = FileTools.LoadFile(url, onSuccess, onProgress, offlineProvider, useArrayBuffer, onError);
- this._activeRequests.push(request);
- request.onCompleteObservable.add((request) => {
- this._activeRequests.splice(this._activeRequests.indexOf(request), 1);
- });
- return request;
- }
- // Statics
- /**
- * Gets a boolean indicating if the engine can be instanciated (ie. if a webGL context can be found)
- * @returns true if the engine can be created
- * @ignorenaming
- */
- public static isSupported(): boolean {
- try {
- var tempcanvas = document.createElement("canvas");
- var gl = tempcanvas.getContext("webgl") || tempcanvas.getContext("experimental-webgl");
- return gl != null && !!window.WebGLRenderingContext;
- } catch (e) {
- return false;
- }
- }
- /**
- * Find the next highest power of two.
- * @param x Number to start search from.
- * @return Next highest power of two.
- */
- public static CeilingPOT(x: number): number {
- x--;
- x |= x >> 1;
- x |= x >> 2;
- x |= x >> 4;
- x |= x >> 8;
- x |= x >> 16;
- x++;
- return x;
- }
- /**
- * Find the next lowest power of two.
- * @param x Number to start search from.
- * @return Next lowest power of two.
- */
- public static FloorPOT(x: number): number {
- x = x | (x >> 1);
- x = x | (x >> 2);
- x = x | (x >> 4);
- x = x | (x >> 8);
- x = x | (x >> 16);
- return x - (x >> 1);
- }
- /**
- * Find the nearest power of two.
- * @param x Number to start search from.
- * @return Next nearest power of two.
- */
- public static NearestPOT(x: number): number {
- var c = ThinEngine.CeilingPOT(x);
- var f = ThinEngine.FloorPOT(x);
- return (c - x) > (x - f) ? f : c;
- }
- /**
- * Get the closest exponent of two
- * @param value defines the value to approximate
- * @param max defines the maximum value to return
- * @param mode defines how to define the closest value
- * @returns closest exponent of two of the given value
- */
- public static GetExponentOfTwo(value: number, max: number, mode = Constants.SCALEMODE_NEAREST): number {
- let pot;
- switch (mode) {
- case Constants.SCALEMODE_FLOOR:
- pot = ThinEngine.FloorPOT(value);
- break;
- case Constants.SCALEMODE_NEAREST:
- pot = ThinEngine.NearestPOT(value);
- break;
- case Constants.SCALEMODE_CEILING:
- default:
- pot = ThinEngine.CeilingPOT(value);
- break;
- }
- return Math.min(pot, max);
- }
- /**
- * Queue a new function into the requested animation frame pool (ie. this function will be executed byt the browser for the next frame)
- * @param func - the function to be called
- * @param requester - the object that will request the next frame. Falls back to window.
- * @returns frame number
- */
- public static QueueNewFrame(func: () => void, requester?: any): number {
- if (!DomManagement.IsWindowObjectExist()) {
- return setTimeout(func, 16);
- }
- if (!requester) {
- requester = window;
- }
- if (requester.requestAnimationFrame) {
- return requester.requestAnimationFrame(func);
- }
- else if (requester.msRequestAnimationFrame) {
- return requester.msRequestAnimationFrame(func);
- }
- else if (requester.webkitRequestAnimationFrame) {
- return requester.webkitRequestAnimationFrame(func);
- }
- else if (requester.mozRequestAnimationFrame) {
- return requester.mozRequestAnimationFrame(func);
- }
- else if (requester.oRequestAnimationFrame) {
- return requester.oRequestAnimationFrame(func);
- }
- else {
- return window.setTimeout(func, 16);
- }
- }
- }
|