WebGLRenderer.js 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791
  1. /**
  2. * @author supereggbert / http://www.paulbrunt.co.uk/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author szimek / https://github.com/szimek/
  6. */
  7. THREE.WebGLRenderer = function ( parameters ) {
  8. // Currently you can use just up to 4 directional / point lights total.
  9. // Chrome barfs on shader linking when there are more than 4 lights :(
  10. // The problem comes from shader using too many varying vectors.
  11. // This is not GPU limitation as the same shader works ok in Firefox
  12. // and Chrome with "--use-gl=desktop" flag.
  13. // Difference comes from Chrome on Windows using by default ANGLE,
  14. // thus going DirectX9 route (while FF uses OpenGL).
  15. // See http://code.google.com/p/chromium/issues/detail?id=63491
  16. var _canvas = document.createElement( 'canvas' ), _gl,
  17. _oldProgram = null,
  18. _oldFramebuffer = null,
  19. _this = this,
  20. // gl state cache
  21. _oldDoubleSided = null,
  22. _oldFlipSided = null,
  23. _oldBlending = null,
  24. _oldDepth = null,
  25. // camera matrices caches
  26. _frustum = [
  27. new THREE.Vector4(),
  28. new THREE.Vector4(),
  29. new THREE.Vector4(),
  30. new THREE.Vector4(),
  31. new THREE.Vector4(),
  32. new THREE.Vector4()
  33. ],
  34. _projScreenMatrix = new THREE.Matrix4(),
  35. _projectionMatrixArray = new Float32Array( 16 ),
  36. _cameraInverseMatrixArray = new Float32Array( 16 ),
  37. _viewMatrixArray = new Float32Array( 16 ),
  38. _vector3 = new THREE.Vector4(),
  39. // parameters defaults
  40. antialias = true,
  41. clearColor = new THREE.Color( 0x000000 ),
  42. clearAlpha = 0;
  43. if ( parameters ) {
  44. if ( parameters.antialias !== undefined ) antialias = parameters.antialias;
  45. if ( parameters.clearColor !== undefined ) clearColor.setHex( parameters.clearColor );
  46. if ( parameters.clearAlpha !== undefined ) clearAlpha = parameters.clearAlpha;
  47. }
  48. this.domElement = _canvas;
  49. this.autoClear = true;
  50. this.sortObjects = false;
  51. initGL( antialias, clearColor, clearAlpha );
  52. this.context = _gl;
  53. //alert( dumpObject( getGLParams() ) );
  54. this.lights = {
  55. ambient: [ 0, 0, 0 ],
  56. directional: { length: 0, colors: new Array(), positions: new Array() },
  57. point: { length: 0, colors: new Array(), positions: new Array() }
  58. };
  59. this.setSize = function ( width, height ) {
  60. _canvas.width = width;
  61. _canvas.height = height;
  62. _gl.viewport( 0, 0, _canvas.width, _canvas.height );
  63. };
  64. this.setClearColorHex = function ( hex, alpha ) {
  65. var color = new THREE.Color( hex );
  66. _gl.clearColor( color.r, color.g, color.b, alpha );
  67. };
  68. this.setClearColor = function ( color, alpha ) {
  69. _gl.clearColor( color.r, color.g, color.b, alpha );
  70. };
  71. this.clear = function () {
  72. _gl.clear( _gl.COLOR_BUFFER_BIT | _gl.DEPTH_BUFFER_BIT );
  73. };
  74. function setupLights ( program, lights ) {
  75. var l, ll, light, r = 0, g = 0, b = 0,
  76. color, position, intensity,
  77. zlights = _this.lights,
  78. dcolors = zlights.directional.colors,
  79. dpositions = zlights.directional.positions,
  80. pcolors = zlights.point.colors,
  81. ppositions = zlights.point.positions,
  82. dlength = 0,
  83. plength = 0,
  84. doffset = 0,
  85. poffset = 0;
  86. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  87. light = lights[ l ];
  88. color = light.color;
  89. position = light.position;
  90. intensity = light.intensity;
  91. if ( light instanceof THREE.AmbientLight ) {
  92. r += color.r;
  93. g += color.g;
  94. b += color.b;
  95. } else if ( light instanceof THREE.DirectionalLight ) {
  96. doffset = dlength * 3;
  97. dcolors[ doffset ] = color.r * intensity;
  98. dcolors[ doffset + 1 ] = color.g * intensity;
  99. dcolors[ doffset + 2 ] = color.b * intensity;
  100. dpositions[ doffset ] = position.x;
  101. dpositions[ doffset + 1 ] = position.y;
  102. dpositions[ doffset + 2 ] = position.z;
  103. dlength += 1;
  104. } else if( light instanceof THREE.PointLight ) {
  105. poffset = plength * 3;
  106. pcolors[ poffset ] = color.r * intensity;
  107. pcolors[ poffset + 1 ] = color.g * intensity;
  108. pcolors[ poffset + 2 ] = color.b * intensity;
  109. ppositions[ poffset ] = position.x;
  110. ppositions[ poffset + 1 ] = position.y;
  111. ppositions[ poffset + 2 ] = position.z;
  112. plength += 1;
  113. }
  114. }
  115. // null eventual remains from removed lights
  116. // (this is to avoid if in shader)
  117. for( l = dlength * 3; l < dcolors.length; l++ ) dcolors[ l ] = 0.0;
  118. for( l = plength * 3; l < pcolors.length; l++ ) pcolors[ l ] = 0.0;
  119. zlights.point.length = plength;
  120. zlights.directional.length = dlength;
  121. zlights.ambient[ 0 ] = r;
  122. zlights.ambient[ 1 ] = g;
  123. zlights.ambient[ 2 ] = b;
  124. };
  125. function createParticleBuffers ( geometry ) {
  126. geometry.__webGLVertexBuffer = _gl.createBuffer();
  127. geometry.__webGLColorBuffer = _gl.createBuffer();
  128. };
  129. function createLineBuffers ( geometry ) {
  130. geometry.__webGLVertexBuffer = _gl.createBuffer();
  131. geometry.__webGLColorBuffer = _gl.createBuffer();
  132. };
  133. function createRibbonBuffers ( geometry ) {
  134. geometry.__webGLVertexBuffer = _gl.createBuffer();
  135. geometry.__webGLColorBuffer = _gl.createBuffer();
  136. };
  137. function createMeshBuffers ( geometryChunk ) {
  138. geometryChunk.__webGLVertexBuffer = _gl.createBuffer();
  139. geometryChunk.__webGLNormalBuffer = _gl.createBuffer();
  140. geometryChunk.__webGLTangentBuffer = _gl.createBuffer();
  141. geometryChunk.__webGLColorBuffer = _gl.createBuffer();
  142. geometryChunk.__webGLUVBuffer = _gl.createBuffer();
  143. geometryChunk.__webGLUV2Buffer = _gl.createBuffer();
  144. geometryChunk.__webGLSkinVertexABuffer = _gl.createBuffer();
  145. geometryChunk.__webGLSkinVertexBBuffer = _gl.createBuffer();
  146. geometryChunk.__webGLSkinIndicesBuffer = _gl.createBuffer();
  147. geometryChunk.__webGLSkinWeightsBuffer = _gl.createBuffer();
  148. geometryChunk.__webGLFaceBuffer = _gl.createBuffer();
  149. geometryChunk.__webGLLineBuffer = _gl.createBuffer();
  150. };
  151. function initLineBuffers ( geometry ) {
  152. var nvertices = geometry.vertices.length;
  153. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  154. geometry.__colorArray = new Float32Array( nvertices * 3 );
  155. geometry.__webGLLineCount = nvertices;
  156. };
  157. function initRibbonBuffers ( geometry ) {
  158. var nvertices = geometry.vertices.length;
  159. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  160. geometry.__colorArray = new Float32Array( nvertices * 3 );
  161. geometry.__webGLVertexCount = nvertices;
  162. };
  163. function initParticleBuffers ( geometry ) {
  164. var nvertices = geometry.vertices.length;
  165. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  166. geometry.__colorArray = new Float32Array( nvertices * 3 );
  167. geometry.__sortArray = [];
  168. geometry.__webGLParticleCount = nvertices;
  169. };
  170. function initMeshBuffers ( geometryChunk, object ) {
  171. var f, fl, nvertices = 0, ntris = 0, nlines = 0,
  172. obj_faces = object.geometry.faces,
  173. chunk_faces = geometryChunk.faces;
  174. for ( f = 0, fl = chunk_faces.length; f < fl; f++ ) {
  175. fi = chunk_faces[ f ];
  176. face = obj_faces[ fi ];
  177. if ( face instanceof THREE.Face3 ) {
  178. nvertices += 3;
  179. ntris += 1;
  180. nlines += 3;
  181. } else if ( face instanceof THREE.Face4 ) {
  182. nvertices += 4;
  183. ntris += 2;
  184. nlines += 4;
  185. }
  186. }
  187. // TODO: only create arrays for attributes existing in the object
  188. geometryChunk.__vertexArray = new Float32Array( nvertices * 3 );
  189. geometryChunk.__normalArray = new Float32Array( nvertices * 3 );
  190. geometryChunk.__tangentArray = new Float32Array( nvertices * 4 );
  191. geometryChunk.__colorArray = new Float32Array( nvertices * 3 );
  192. geometryChunk.__uvArray = new Float32Array( nvertices * 2 );
  193. geometryChunk.__uv2Array = new Float32Array( nvertices * 2 );
  194. geometryChunk.__skinVertexAArray = new Float32Array( nvertices * 4 );
  195. geometryChunk.__skinVertexBArray = new Float32Array( nvertices * 4 );
  196. geometryChunk.__skinIndexArray = new Float32Array( nvertices * 4 );
  197. geometryChunk.__skinWeightArray = new Float32Array( nvertices * 4 );
  198. geometryChunk.__faceArray = new Uint16Array( ntris * 3 );
  199. geometryChunk.__lineArray = new Uint16Array( nlines * 2 );
  200. geometryChunk.__needsSmoothNormals = bufferNeedsSmoothNormals ( geometryChunk, object );
  201. geometryChunk.__webGLFaceCount = ntris * 3;
  202. geometryChunk.__webGLLineCount = nlines * 2;
  203. };
  204. function setMeshBuffers ( geometryChunk, object, hint ) {
  205. var f, fl, fi, face, vertexNormals, faceNormal, normal,
  206. uv, uv2, v1, v2, v3, v4, t1, t2, t3, t4,
  207. c1, c2, c3, c4,
  208. sw1, sw2, sw3, sw4,
  209. si1, si2, si3, si4,
  210. sa1, sa2, sa3, sa4,
  211. sb1, sb2, sb3, sb4,
  212. m, ml, i,
  213. vn, uvi, uv2i,
  214. vertexIndex = 0,
  215. offset = 0,
  216. offset_uv = 0,
  217. offset_uv2 = 0,
  218. offset_face = 0,
  219. offset_normal = 0,
  220. offset_tangent = 0,
  221. offset_line = 0,
  222. offset_color = 0,
  223. offset_skin = 0,
  224. vertexArray = geometryChunk.__vertexArray,
  225. uvArray = geometryChunk.__uvArray,
  226. uv2Array = geometryChunk.__uv2Array,
  227. normalArray = geometryChunk.__normalArray,
  228. tangentArray = geometryChunk.__tangentArray,
  229. colorArray = geometryChunk.__colorArray,
  230. skinVertexAArray = geometryChunk.__skinVertexAArray,
  231. skinVertexBArray = geometryChunk.__skinVertexBArray,
  232. skinIndexArray = geometryChunk.__skinIndexArray,
  233. skinWeightArray = geometryChunk.__skinWeightArray,
  234. faceArray = geometryChunk.__faceArray,
  235. lineArray = geometryChunk.__lineArray,
  236. needsSmoothNormals = geometryChunk.__needsSmoothNormals,
  237. geometry = object.geometry, // this is shared for all chunks
  238. dirtyVertices = geometry.__dirtyVertices,
  239. dirtyElements = geometry.__dirtyElements,
  240. dirtyUvs = geometry.__dirtyUvs,
  241. dirtyNormals = geometry.__dirtyNormals,
  242. dirtyTangents = geometry.__dirtyTangents,
  243. dirtyColors = geometry.__dirtyColors,
  244. vertices = geometry.vertices,
  245. chunk_faces = geometryChunk.faces,
  246. obj_faces = geometry.faces,
  247. obj_uvs = geometry.uvs,
  248. obj_uvs2 = geometry.uvs2,
  249. obj_colors = geometry.colors,
  250. obj_skinVerticesA = geometry.skinVerticesA,
  251. obj_skinVerticesB = geometry.skinVerticesB,
  252. obj_skinIndices = geometry.skinIndices,
  253. obj_skinWeights = geometry.skinWeights;
  254. for ( f = 0, fl = chunk_faces.length; f < fl; f++ ) {
  255. fi = chunk_faces[ f ];
  256. face = obj_faces[ fi ];
  257. uv = obj_uvs[ fi ];
  258. uv2 = obj_uvs2[ fi ];
  259. vertexNormals = face.vertexNormals;
  260. faceNormal = face.normal;
  261. if ( face instanceof THREE.Face3 ) {
  262. if ( dirtyVertices ) {
  263. v1 = vertices[ face.a ].position;
  264. v2 = vertices[ face.b ].position;
  265. v3 = vertices[ face.c ].position;
  266. vertexArray[ offset ] = v1.x;
  267. vertexArray[ offset + 1 ] = v1.y;
  268. vertexArray[ offset + 2 ] = v1.z;
  269. vertexArray[ offset + 3 ] = v2.x;
  270. vertexArray[ offset + 4 ] = v2.y;
  271. vertexArray[ offset + 5 ] = v2.z;
  272. vertexArray[ offset + 6 ] = v3.x;
  273. vertexArray[ offset + 7 ] = v3.y;
  274. vertexArray[ offset + 8 ] = v3.z;
  275. offset += 9;
  276. }
  277. if ( obj_skinWeights.length ) {
  278. // weights
  279. sw1 = obj_skinWeights[ face.a ];
  280. sw2 = obj_skinWeights[ face.b ];
  281. sw3 = obj_skinWeights[ face.c ];
  282. skinWeightArray[ offset_skin ] = sw1.x;
  283. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  284. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  285. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  286. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  287. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  288. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  289. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  290. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  291. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  292. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  293. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  294. // indices
  295. si1 = obj_skinIndices[ face.a ];
  296. si2 = obj_skinIndices[ face.b ];
  297. si3 = obj_skinIndices[ face.c ];
  298. skinIndexArray[ offset_skin ] = si1.x;
  299. skinIndexArray[ offset_skin + 1 ] = si1.y;
  300. skinIndexArray[ offset_skin + 2 ] = si1.z;
  301. skinIndexArray[ offset_skin + 3 ] = si1.w;
  302. skinIndexArray[ offset_skin + 4 ] = si2.x;
  303. skinIndexArray[ offset_skin + 5 ] = si2.y;
  304. skinIndexArray[ offset_skin + 6 ] = si2.z;
  305. skinIndexArray[ offset_skin + 7 ] = si2.w;
  306. skinIndexArray[ offset_skin + 8 ] = si3.x;
  307. skinIndexArray[ offset_skin + 9 ] = si3.y;
  308. skinIndexArray[ offset_skin + 10 ] = si3.z;
  309. skinIndexArray[ offset_skin + 11 ] = si3.w;
  310. // vertices A
  311. sa1 = obj_skinVerticesA[ face.a ];
  312. sa2 = obj_skinVerticesA[ face.b ];
  313. sa3 = obj_skinVerticesA[ face.c ];
  314. skinVertexAArray[ offset_skin ] = sa1.x;
  315. skinVertexAArray[ offset_skin + 1 ] = sa1.y;
  316. skinVertexAArray[ offset_skin + 2 ] = sa1.z;
  317. skinVertexAArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  318. skinVertexAArray[ offset_skin + 4 ] = sa2.x;
  319. skinVertexAArray[ offset_skin + 5 ] = sa2.y;
  320. skinVertexAArray[ offset_skin + 6 ] = sa2.z;
  321. skinVertexAArray[ offset_skin + 7 ] = 1;
  322. skinVertexAArray[ offset_skin + 8 ] = sa3.x;
  323. skinVertexAArray[ offset_skin + 9 ] = sa3.y;
  324. skinVertexAArray[ offset_skin + 10 ] = sa3.z;
  325. skinVertexAArray[ offset_skin + 11 ] = 1;
  326. // vertices B
  327. sb1 = obj_skinVerticesB[ face.a ];
  328. sb2 = obj_skinVerticesB[ face.b ];
  329. sb3 = obj_skinVerticesB[ face.c ];
  330. skinVertexBArray[ offset_skin ] = sb1.x;
  331. skinVertexBArray[ offset_skin + 1 ] = sb1.y;
  332. skinVertexBArray[ offset_skin + 2 ] = sb1.z;
  333. skinVertexBArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  334. skinVertexBArray[ offset_skin + 4 ] = sb2.x;
  335. skinVertexBArray[ offset_skin + 5 ] = sb2.y;
  336. skinVertexBArray[ offset_skin + 6 ] = sb2.z;
  337. skinVertexBArray[ offset_skin + 7 ] = 1;
  338. skinVertexBArray[ offset_skin + 8 ] = sb3.x;
  339. skinVertexBArray[ offset_skin + 9 ] = sb3.y;
  340. skinVertexBArray[ offset_skin + 10 ] = sb3.z;
  341. skinVertexBArray[ offset_skin + 11 ] = 1;
  342. offset_skin += 12;
  343. }
  344. if ( dirtyColors && obj_colors.length ) {
  345. c1 = obj_colors[ face.a ];
  346. c2 = obj_colors[ face.b ];
  347. c3 = obj_colors[ face.c ];
  348. colorArray[ offset_color ] = c1.r;
  349. colorArray[ offset_color + 1 ] = c1.g;
  350. colorArray[ offset_color + 2 ] = c1.b;
  351. colorArray[ offset_color + 3 ] = c2.r;
  352. colorArray[ offset_color + 4 ] = c2.g;
  353. colorArray[ offset_color + 5 ] = c2.b;
  354. colorArray[ offset_color + 6 ] = c3.r;
  355. colorArray[ offset_color + 7 ] = c3.g;
  356. colorArray[ offset_color + 8 ] = c3.b;
  357. offset_color += 9;
  358. }
  359. if ( dirtyTangents && geometry.hasTangents ) {
  360. t1 = vertices[ face.a ].tangent;
  361. t2 = vertices[ face.b ].tangent;
  362. t3 = vertices[ face.c ].tangent;
  363. tangentArray[ offset_tangent ] = t1.x;
  364. tangentArray[ offset_tangent + 1 ] = t1.y;
  365. tangentArray[ offset_tangent + 2 ] = t1.z;
  366. tangentArray[ offset_tangent + 3 ] = t1.w;
  367. tangentArray[ offset_tangent + 4 ] = t2.x;
  368. tangentArray[ offset_tangent + 5 ] = t2.y;
  369. tangentArray[ offset_tangent + 6 ] = t2.z;
  370. tangentArray[ offset_tangent + 7 ] = t2.w;
  371. tangentArray[ offset_tangent + 8 ] = t3.x;
  372. tangentArray[ offset_tangent + 9 ] = t3.y;
  373. tangentArray[ offset_tangent + 10 ] = t3.z;
  374. tangentArray[ offset_tangent + 11 ] = t3.w;
  375. offset_tangent += 12;
  376. }
  377. if( dirtyNormals ) {
  378. if ( vertexNormals.length == 3 && needsSmoothNormals ) {
  379. for ( i = 0; i < 3; i ++ ) {
  380. vn = vertexNormals[ i ];
  381. normalArray[ offset_normal ] = vn.x;
  382. normalArray[ offset_normal + 1 ] = vn.y;
  383. normalArray[ offset_normal + 2 ] = vn.z;
  384. offset_normal += 3;
  385. }
  386. } else {
  387. for ( i = 0; i < 3; i ++ ) {
  388. normalArray[ offset_normal ] = faceNormal.x;
  389. normalArray[ offset_normal + 1 ] = faceNormal.y;
  390. normalArray[ offset_normal + 2 ] = faceNormal.z;
  391. offset_normal += 3;
  392. }
  393. }
  394. }
  395. if ( dirtyUvs && uv ) {
  396. for ( i = 0; i < 3; i ++ ) {
  397. uvi = uv[ i ];
  398. uvArray[ offset_uv ] = uvi.u;
  399. uvArray[ offset_uv + 1 ] = uvi.v;
  400. offset_uv += 2;
  401. }
  402. }
  403. if ( dirtyUvs && uv2 ) {
  404. for ( i = 0; i < 3; i ++ ) {
  405. uv2i = uv2[ i ];
  406. uv2Array[ offset_uv2 ] = uv2i.u;
  407. uv2Array[ offset_uv2 + 1 ] = uv2i.v;
  408. offset_uv2 += 2;
  409. }
  410. }
  411. if( dirtyElements ) {
  412. faceArray[ offset_face ] = vertexIndex;
  413. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  414. faceArray[ offset_face + 2 ] = vertexIndex + 2;
  415. offset_face += 3;
  416. lineArray[ offset_line ] = vertexIndex;
  417. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  418. lineArray[ offset_line + 2 ] = vertexIndex;
  419. lineArray[ offset_line + 3 ] = vertexIndex + 2;
  420. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  421. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  422. offset_line += 6;
  423. vertexIndex += 3;
  424. }
  425. } else if ( face instanceof THREE.Face4 ) {
  426. if ( dirtyVertices ) {
  427. v1 = vertices[ face.a ].position;
  428. v2 = vertices[ face.b ].position;
  429. v3 = vertices[ face.c ].position;
  430. v4 = vertices[ face.d ].position;
  431. vertexArray[ offset ] = v1.x;
  432. vertexArray[ offset + 1 ] = v1.y;
  433. vertexArray[ offset + 2 ] = v1.z;
  434. vertexArray[ offset + 3 ] = v2.x;
  435. vertexArray[ offset + 4 ] = v2.y;
  436. vertexArray[ offset + 5 ] = v2.z;
  437. vertexArray[ offset + 6 ] = v3.x;
  438. vertexArray[ offset + 7 ] = v3.y;
  439. vertexArray[ offset + 8 ] = v3.z;
  440. vertexArray[ offset + 9 ] = v4.x;
  441. vertexArray[ offset + 10 ] = v4.y;
  442. vertexArray[ offset + 11 ] = v4.z;
  443. offset += 12;
  444. }
  445. if ( obj_skinWeights.length ) {
  446. // weights
  447. sw1 = obj_skinWeights[ face.a ];
  448. sw2 = obj_skinWeights[ face.b ];
  449. sw3 = obj_skinWeights[ face.c ];
  450. sw4 = obj_skinWeights[ face.d ];
  451. skinWeightArray[ offset_skin ] = sw1.x;
  452. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  453. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  454. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  455. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  456. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  457. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  458. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  459. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  460. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  461. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  462. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  463. skinWeightArray[ offset_skin + 12 ] = sw4.x;
  464. skinWeightArray[ offset_skin + 13 ] = sw4.y;
  465. skinWeightArray[ offset_skin + 14 ] = sw4.z;
  466. skinWeightArray[ offset_skin + 15 ] = sw4.w;
  467. // indices
  468. si1 = obj_skinIndices[ face.a ];
  469. si2 = obj_skinIndices[ face.b ];
  470. si3 = obj_skinIndices[ face.c ];
  471. si4 = obj_skinIndices[ face.d ];
  472. skinIndexArray[ offset_skin ] = si1.x;
  473. skinIndexArray[ offset_skin + 1 ] = si1.y;
  474. skinIndexArray[ offset_skin + 2 ] = si1.z;
  475. skinIndexArray[ offset_skin + 3 ] = si1.w;
  476. skinIndexArray[ offset_skin + 4 ] = si2.x;
  477. skinIndexArray[ offset_skin + 5 ] = si2.y;
  478. skinIndexArray[ offset_skin + 6 ] = si2.z;
  479. skinIndexArray[ offset_skin + 7 ] = si2.w;
  480. skinIndexArray[ offset_skin + 8 ] = si3.x;
  481. skinIndexArray[ offset_skin + 9 ] = si3.y;
  482. skinIndexArray[ offset_skin + 10 ] = si3.z;
  483. skinIndexArray[ offset_skin + 11 ] = si3.w;
  484. skinIndexArray[ offset_skin + 12 ] = si4.x;
  485. skinIndexArray[ offset_skin + 13 ] = si4.y;
  486. skinIndexArray[ offset_skin + 14 ] = si4.z;
  487. skinIndexArray[ offset_skin + 15 ] = si4.w;
  488. // vertices A
  489. sa1 = obj_skinVerticesA[ face.a ];
  490. sa2 = obj_skinVerticesA[ face.b ];
  491. sa3 = obj_skinVerticesA[ face.c ];
  492. sa4 = obj_skinVerticesA[ face.d ];
  493. skinVertexAArray[ offset_skin ] = sa1.x;
  494. skinVertexAArray[ offset_skin + 1 ] = sa1.y;
  495. skinVertexAArray[ offset_skin + 2 ] = sa1.z;
  496. skinVertexAArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  497. skinVertexAArray[ offset_skin + 4 ] = sa2.x;
  498. skinVertexAArray[ offset_skin + 5 ] = sa2.y;
  499. skinVertexAArray[ offset_skin + 6 ] = sa2.z;
  500. skinVertexAArray[ offset_skin + 7 ] = 1;
  501. skinVertexAArray[ offset_skin + 8 ] = sa3.x;
  502. skinVertexAArray[ offset_skin + 9 ] = sa3.y;
  503. skinVertexAArray[ offset_skin + 10 ] = sa3.z;
  504. skinVertexAArray[ offset_skin + 11 ] = 1;
  505. skinVertexAArray[ offset_skin + 12 ] = sa4.x;
  506. skinVertexAArray[ offset_skin + 13 ] = sa4.y;
  507. skinVertexAArray[ offset_skin + 14 ] = sa4.z;
  508. skinVertexAArray[ offset_skin + 15 ] = 1;
  509. // vertices B
  510. sb1 = obj_skinVerticesB[ face.a ];
  511. sb2 = obj_skinVerticesB[ face.b ];
  512. sb3 = obj_skinVerticesB[ face.c ];
  513. sb4 = obj_skinVerticesB[ face.d ];
  514. skinVertexBArray[ offset_skin ] = sb1.x;
  515. skinVertexBArray[ offset_skin + 1 ] = sb1.y;
  516. skinVertexBArray[ offset_skin + 2 ] = sb1.z;
  517. skinVertexBArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  518. skinVertexBArray[ offset_skin + 4 ] = sb2.x;
  519. skinVertexBArray[ offset_skin + 5 ] = sb2.y;
  520. skinVertexBArray[ offset_skin + 6 ] = sb2.z;
  521. skinVertexBArray[ offset_skin + 7 ] = 1;
  522. skinVertexBArray[ offset_skin + 8 ] = sb3.x;
  523. skinVertexBArray[ offset_skin + 9 ] = sb3.y;
  524. skinVertexBArray[ offset_skin + 10 ] = sb3.z;
  525. skinVertexBArray[ offset_skin + 11 ] = 1;
  526. skinVertexBArray[ offset_skin + 12 ] = sb4.x;
  527. skinVertexBArray[ offset_skin + 13 ] = sb4.y;
  528. skinVertexBArray[ offset_skin + 14 ] = sb4.z;
  529. skinVertexBArray[ offset_skin + 15 ] = 1;
  530. offset_skin += 16;
  531. }
  532. if ( dirtyColors && obj_colors.length ) {
  533. c1 = obj_colors[ face.a ];
  534. c2 = obj_colors[ face.b ];
  535. c3 = obj_colors[ face.c ];
  536. c4 = obj_colors[ face.d ];
  537. colorArray[ offset_color ] = c1.r;
  538. colorArray[ offset_color + 1 ] = c1.g;
  539. colorArray[ offset_color + 2 ] = c1.b;
  540. colorArray[ offset_color + 3 ] = c2.r;
  541. colorArray[ offset_color + 4 ] = c2.g;
  542. colorArray[ offset_color + 5 ] = c2.b;
  543. colorArray[ offset_color + 6 ] = c3.r;
  544. colorArray[ offset_color + 7 ] = c3.g;
  545. colorArray[ offset_color + 8 ] = c3.b;
  546. colorArray[ offset_color + 9 ] = c4.r;
  547. colorArray[ offset_color + 10 ] = c4.g;
  548. colorArray[ offset_color + 11 ] = c4.b;
  549. offset_color += 12;
  550. }
  551. if ( dirtyTangents && geometry.hasTangents ) {
  552. t1 = vertices[ face.a ].tangent;
  553. t2 = vertices[ face.b ].tangent;
  554. t3 = vertices[ face.c ].tangent;
  555. t4 = vertices[ face.d ].tangent;
  556. tangentArray[ offset_tangent ] = t1.x;
  557. tangentArray[ offset_tangent + 1 ] = t1.y;
  558. tangentArray[ offset_tangent + 2 ] = t1.z;
  559. tangentArray[ offset_tangent + 3 ] = t1.w;
  560. tangentArray[ offset_tangent + 4 ] = t2.x;
  561. tangentArray[ offset_tangent + 5 ] = t2.y;
  562. tangentArray[ offset_tangent + 6 ] = t2.z;
  563. tangentArray[ offset_tangent + 7 ] = t2.w;
  564. tangentArray[ offset_tangent + 8 ] = t3.x;
  565. tangentArray[ offset_tangent + 9 ] = t3.y;
  566. tangentArray[ offset_tangent + 10 ] = t3.z;
  567. tangentArray[ offset_tangent + 11 ] = t3.w;
  568. tangentArray[ offset_tangent + 12 ] = t4.x;
  569. tangentArray[ offset_tangent + 13 ] = t4.y;
  570. tangentArray[ offset_tangent + 14 ] = t4.z;
  571. tangentArray[ offset_tangent + 15 ] = t4.w;
  572. offset_tangent += 16;
  573. }
  574. if( dirtyNormals ) {
  575. if ( vertexNormals.length == 4 && needsSmoothNormals ) {
  576. for ( i = 0; i < 4; i ++ ) {
  577. vn = vertexNormals[ i ];
  578. normalArray[ offset_normal ] = vn.x;
  579. normalArray[ offset_normal + 1 ] = vn.y;
  580. normalArray[ offset_normal + 2 ] = vn.z;
  581. offset_normal += 3;
  582. }
  583. } else {
  584. for ( i = 0; i < 4; i ++ ) {
  585. normalArray[ offset_normal ] = faceNormal.x;
  586. normalArray[ offset_normal + 1 ] = faceNormal.y;
  587. normalArray[ offset_normal + 2 ] = faceNormal.z;
  588. offset_normal += 3;
  589. }
  590. }
  591. }
  592. if ( dirtyUvs && uv ) {
  593. for ( i = 0; i < 4; i ++ ) {
  594. uvi = uv[ i ];
  595. uvArray[ offset_uv ] = uvi.u;
  596. uvArray[ offset_uv + 1 ] = uvi.v;
  597. offset_uv += 2;
  598. }
  599. }
  600. if ( dirtyUvs && uv2 ) {
  601. for ( i = 0; i < 4; i ++ ) {
  602. uv2i = uv2[ i ];
  603. uv2Array[ offset_uv2 ] = uv2i.u;
  604. uv2Array[ offset_uv2 + 1 ] = uv2i.v;
  605. offset_uv2 += 2;
  606. }
  607. }
  608. if( dirtyElements ) {
  609. faceArray[ offset_face ] = vertexIndex;
  610. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  611. faceArray[ offset_face + 2 ] = vertexIndex + 2;
  612. faceArray[ offset_face + 3 ] = vertexIndex;
  613. faceArray[ offset_face + 4 ] = vertexIndex + 2;
  614. faceArray[ offset_face + 5 ] = vertexIndex + 3;
  615. offset_face += 6;
  616. lineArray[ offset_line ] = vertexIndex;
  617. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  618. lineArray[ offset_line + 2 ] = vertexIndex;
  619. lineArray[ offset_line + 3 ] = vertexIndex + 3;
  620. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  621. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  622. lineArray[ offset_line + 6 ] = vertexIndex + 2;
  623. lineArray[ offset_line + 7 ] = vertexIndex + 3;
  624. offset_line += 8;
  625. vertexIndex += 4;
  626. }
  627. }
  628. }
  629. if ( dirtyVertices ) {
  630. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLVertexBuffer );
  631. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  632. }
  633. if ( dirtyColors && obj_colors.length ) {
  634. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLColorBuffer );
  635. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  636. }
  637. if ( dirtyNormals ) {
  638. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLNormalBuffer );
  639. _gl.bufferData( _gl.ARRAY_BUFFER, normalArray, hint );
  640. }
  641. if ( dirtyTangents && geometry.hasTangents ) {
  642. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLTangentBuffer );
  643. _gl.bufferData( _gl.ARRAY_BUFFER, tangentArray, hint );
  644. }
  645. if ( dirtyUvs && offset_uv > 0 ) {
  646. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLUVBuffer );
  647. _gl.bufferData( _gl.ARRAY_BUFFER, uvArray, hint );
  648. }
  649. if ( dirtyUvs && offset_uv2 > 0 ) {
  650. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLUV2Buffer );
  651. _gl.bufferData( _gl.ARRAY_BUFFER, uv2Array, hint );
  652. }
  653. if( dirtyElements ) {
  654. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryChunk.__webGLFaceBuffer );
  655. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faceArray, hint );
  656. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryChunk.__webGLLineBuffer );
  657. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, lineArray, hint );
  658. }
  659. if( offset_skin > 0 ) {
  660. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLSkinVertexABuffer );
  661. _gl.bufferData( _gl.ARRAY_BUFFER, skinVertexAArray, hint );
  662. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLSkinVertexBBuffer );
  663. _gl.bufferData( _gl.ARRAY_BUFFER, skinVertexBArray, hint );
  664. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLSkinIndicesBuffer );
  665. _gl.bufferData( _gl.ARRAY_BUFFER, skinIndexArray, hint );
  666. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLSkinWeightsBuffer );
  667. _gl.bufferData( _gl.ARRAY_BUFFER, skinWeightArray, hint );
  668. }
  669. };
  670. function setLineBuffers ( geometry, hint ) {
  671. var v, c, vertex, offset,
  672. vertices = geometry.vertices,
  673. colors = geometry.colors,
  674. vl = vertices.length,
  675. cl = colors.length,
  676. vertexArray = geometry.__vertexArray,
  677. colorArray = geometry.__colorArray,
  678. dirtyVertices = geometry.__dirtyVertices,
  679. dirtyColors = geometry.__dirtyColors;
  680. if ( dirtyVertices ) {
  681. for ( v = 0; v < vl; v++ ) {
  682. vertex = vertices[ v ].position;
  683. offset = v * 3;
  684. vertexArray[ offset ] = vertex.x;
  685. vertexArray[ offset + 1 ] = vertex.y;
  686. vertexArray[ offset + 2 ] = vertex.z;
  687. }
  688. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webGLVertexBuffer );
  689. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  690. }
  691. if ( dirtyColors ) {
  692. for ( c = 0; c < cl; c++ ) {
  693. color = colors[ c ];
  694. offset = c * 3;
  695. colorArray[ offset ] = color.r;
  696. colorArray[ offset + 1 ] = color.g;
  697. colorArray[ offset + 2 ] = color.b;
  698. }
  699. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webGLColorBuffer );
  700. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  701. }
  702. };
  703. function setRibbonBuffers ( geometry, hint ) {
  704. var v, c, vertex, offset,
  705. vertices = geometry.vertices,
  706. colors = geometry.colors,
  707. vl = vertices.length,
  708. cl = colors.length,
  709. vertexArray = geometry.__vertexArray,
  710. colorArray = geometry.__colorArray,
  711. dirtyVertices = geometry.__dirtyVertices,
  712. dirtyColors = geometry.__dirtyColors;
  713. if ( dirtyVertices ) {
  714. for ( v = 0; v < vl; v++ ) {
  715. vertex = vertices[ v ].position;
  716. offset = v * 3;
  717. vertexArray[ offset ] = vertex.x;
  718. vertexArray[ offset + 1 ] = vertex.y;
  719. vertexArray[ offset + 2 ] = vertex.z;
  720. }
  721. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webGLVertexBuffer );
  722. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  723. }
  724. if ( dirtyColors ) {
  725. for ( c = 0; c < cl; c++ ) {
  726. color = colors[ c ];
  727. offset = c * 3;
  728. colorArray[ offset ] = color.r;
  729. colorArray[ offset + 1 ] = color.g;
  730. colorArray[ offset + 2 ] = color.b;
  731. }
  732. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webGLColorBuffer );
  733. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  734. }
  735. };
  736. function setParticleBuffers ( geometry, hint, object, camera ) {
  737. var v, c, vertex, offset,
  738. vertices = geometry.vertices,
  739. vl = vertices.length,
  740. colors = geometry.colors,
  741. cl = colors.length,
  742. vertexArray = geometry.__vertexArray,
  743. colorArray = geometry.__colorArray,
  744. sortArray = geometry.__sortArray,
  745. dirtyVertices = geometry.__dirtyVertices,
  746. dirtyElements = geometry.__dirtyElements,
  747. dirtyColors = geometry.__dirtyColors;
  748. if ( object.sortParticles ) {
  749. _projScreenMatrix.multiplySelf( object.matrixWorld );
  750. for ( v = 0; v < vl; v++ ) {
  751. vertex = vertices[ v ].position;
  752. _vector3.copy( vertex );
  753. _projScreenMatrix.multiplyVector3( _vector3 );
  754. sortArray[ v ] = [ _vector3.z, v ];
  755. }
  756. sortArray.sort( function(a,b) { return b[0] - a[0]; } );
  757. for ( v = 0; v < vl; v++ ) {
  758. vertex = vertices[ sortArray[v][1] ].position;
  759. offset = v * 3;
  760. vertexArray[ offset ] = vertex.x;
  761. vertexArray[ offset + 1 ] = vertex.y;
  762. vertexArray[ offset + 2 ] = vertex.z;
  763. }
  764. for ( c = 0; c < cl; c++ ) {
  765. offset = c * 3;
  766. color = colors[ sortArray[c][1] ];
  767. colorArray[ offset ] = color.r;
  768. colorArray[ offset + 1 ] = color.g;
  769. colorArray[ offset + 2 ] = color.b;
  770. }
  771. } else {
  772. if ( dirtyVertices ) {
  773. for ( v = 0; v < vl; v++ ) {
  774. vertex = vertices[ v ].position;
  775. offset = v * 3;
  776. vertexArray[ offset ] = vertex.x;
  777. vertexArray[ offset + 1 ] = vertex.y;
  778. vertexArray[ offset + 2 ] = vertex.z;
  779. }
  780. }
  781. if ( dirtyColors ) {
  782. for ( c = 0; c < cl; c++ ) {
  783. color = colors[ c ];
  784. offset = c * 3;
  785. colorArray[ offset ] = color.r;
  786. colorArray[ offset + 1 ] = color.g;
  787. colorArray[ offset + 2 ] = color.b;
  788. }
  789. }
  790. }
  791. if ( dirtyVertices || object.sortParticles ) {
  792. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webGLVertexBuffer );
  793. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  794. }
  795. if ( dirtyColors || object.sortParticles ) {
  796. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webGLColorBuffer );
  797. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  798. }
  799. };
  800. function setMaterialShaders( material, shaders ) {
  801. material.fragment_shader = shaders.fragment_shader;
  802. material.vertex_shader = shaders.vertex_shader;
  803. material.uniforms = Uniforms.clone( shaders.uniforms );
  804. };
  805. function refreshUniformsCommon( uniforms, material ) {
  806. // premultiply alpha
  807. uniforms.diffuse.value.setRGB( material.color.r * material.opacity, material.color.g * material.opacity, material.color.b * material.opacity );
  808. // pure color
  809. //uniforms.color.value.setHex( material.color.hex );
  810. uniforms.opacity.value = material.opacity;
  811. uniforms.map.texture = material.map;
  812. uniforms.light_map.texture = material.light_map;
  813. uniforms.env_map.texture = material.env_map;
  814. uniforms.reflectivity.value = material.reflectivity;
  815. uniforms.refraction_ratio.value = material.refraction_ratio;
  816. uniforms.combine.value = material.combine;
  817. uniforms.useRefract.value = material.env_map && material.env_map.mapping instanceof THREE.CubeRefractionMapping;
  818. };
  819. function refreshUniformsLine( uniforms, material ) {
  820. uniforms.diffuse.value.setRGB( material.color.r * material.opacity, material.color.g * material.opacity, material.color.b * material.opacity );
  821. uniforms.opacity.value = material.opacity;
  822. };
  823. function refreshUniformsParticle( uniforms, material ) {
  824. uniforms.psColor.value.setRGB( material.color.r * material.opacity, material.color.g * material.opacity, material.color.b * material.opacity );
  825. uniforms.opacity.value = material.opacity;
  826. uniforms.size.value = material.size;
  827. uniforms.map.texture = material.map;
  828. };
  829. function refreshUniformsFog( uniforms, fog ) {
  830. uniforms.fogColor.value.setHex( fog.color.hex );
  831. if ( fog instanceof THREE.Fog ) {
  832. uniforms.fogNear.value = fog.near;
  833. uniforms.fogFar.value = fog.far;
  834. } else if ( fog instanceof THREE.FogExp2 ) {
  835. uniforms.fogDensity.value = fog.density;
  836. }
  837. };
  838. function refreshUniformsPhong( uniforms, material ) {
  839. //uniforms.ambient.value.setHex( material.ambient.hex );
  840. //uniforms.specular.value.setHex( material.specular.hex );
  841. uniforms.ambient.value.setRGB( material.ambient.r, material.ambient.g, material.ambient.b );
  842. uniforms.specular.value.setRGB( material.specular.r, material.specular.g, material.specular.b );
  843. uniforms.shininess.value = material.shininess;
  844. };
  845. function refreshUniformsLights( uniforms, lights ) {
  846. uniforms.enableLighting.value = lights.directional.length + lights.point.length;
  847. uniforms.ambientLightColor.value = lights.ambient;
  848. uniforms.directionalLightColor.value = lights.directional.colors;
  849. uniforms.directionalLightDirection.value = lights.directional.positions;
  850. uniforms.pointLightColor.value = lights.point.colors;
  851. uniforms.pointLightPosition.value = lights.point.positions;
  852. };
  853. this.initMaterial = function( material, lights, fog ) {
  854. var u, identifiers, parameters, maxLightCount;
  855. if ( material instanceof THREE.MeshDepthMaterial ) {
  856. setMaterialShaders( material, THREE.ShaderLib[ 'depth' ] );
  857. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  858. setMaterialShaders( material, THREE.ShaderLib[ 'normal' ] );
  859. } else if ( material instanceof THREE.MeshBasicMaterial ) {
  860. setMaterialShaders( material, THREE.ShaderLib[ 'basic' ] );
  861. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  862. setMaterialShaders( material, THREE.ShaderLib[ 'lambert' ] );
  863. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  864. setMaterialShaders( material, THREE.ShaderLib[ 'phong' ] );
  865. } else if ( material instanceof THREE.LineBasicMaterial ) {
  866. setMaterialShaders( material, THREE.ShaderLib[ 'basic' ] );
  867. } else if ( material instanceof THREE.ParticleBasicMaterial ) {
  868. setMaterialShaders( material, THREE.ShaderLib[ 'particle_basic' ] );
  869. }
  870. // heuristics to create shader parameters according to lights in the scene
  871. // (not to blow over maxLights budget)
  872. maxLightCount = allocateLights( lights, 4 );
  873. parameters = { fog: fog, map: material.map, env_map: material.env_map, light_map: material.light_map, vertex_colors: material.vertex_colors,
  874. skinning: material.skinning,
  875. maxDirLights: maxLightCount.directional, maxPointLights: maxLightCount.point };
  876. material.program = buildProgram( material.fragment_shader, material.vertex_shader, parameters );
  877. identifiers = [ 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition',
  878. 'cameraInverseMatrix', 'boneGlobalMatrices'
  879. ];
  880. for( u in material.uniforms ) {
  881. identifiers.push(u);
  882. }
  883. cacheUniformLocations( material.program, identifiers );
  884. cacheAttributeLocations( material.program, [ "position", "normal", "uv", "uv2", "tangent", "color",
  885. "skinVertexA", "skinVertexB", "skinIndex", "skinWeight" ] );
  886. var attributes = material.program.attributes;
  887. _gl.enableVertexAttribArray( attributes.position );
  888. if ( attributes.color >= 0 ) _gl.enableVertexAttribArray( attributes.color );
  889. if ( attributes.normal >= 0 ) _gl.enableVertexAttribArray( attributes.normal );
  890. if ( attributes.tangent >= 0 ) _gl.enableVertexAttribArray( attributes.tangent );
  891. if ( material.skinning &&
  892. attributes.skinVertexA >=0 && attributes.skinVertexB >= 0 &&
  893. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  894. _gl.enableVertexAttribArray( attributes.skinVertexA );
  895. _gl.enableVertexAttribArray( attributes.skinVertexB );
  896. _gl.enableVertexAttribArray( attributes.skinIndex );
  897. _gl.enableVertexAttribArray( attributes.skinWeight );
  898. }
  899. };
  900. function setProgram ( camera, lights, fog, material, object ) {
  901. if ( !material.program ) _this.initMaterial( material, lights, fog );
  902. var program = material.program,
  903. p_uniforms = program.uniforms,
  904. m_uniforms = material.uniforms;
  905. if( program != _oldProgram ) {
  906. _gl.useProgram( program );
  907. _oldProgram = program;
  908. _gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, _projectionMatrixArray );
  909. }
  910. // refresh uniforms common to several materials
  911. if ( fog && (
  912. material instanceof THREE.MeshBasicMaterial ||
  913. material instanceof THREE.MeshLambertMaterial ||
  914. material instanceof THREE.MeshPhongMaterial ||
  915. material instanceof THREE.LineBasicMaterial ||
  916. material instanceof THREE.ParticleBasicMaterial )
  917. ) {
  918. refreshUniformsFog( m_uniforms, fog );
  919. }
  920. if ( material instanceof THREE.MeshPhongMaterial ||
  921. material instanceof THREE.MeshLambertMaterial ) {
  922. setupLights( program, lights );
  923. refreshUniformsLights( m_uniforms, _this.lights );
  924. }
  925. if ( material instanceof THREE.MeshBasicMaterial ||
  926. material instanceof THREE.MeshLambertMaterial ||
  927. material instanceof THREE.MeshPhongMaterial ) {
  928. refreshUniformsCommon( m_uniforms, material );
  929. }
  930. // refresh single material specific uniforms
  931. if ( material instanceof THREE.LineBasicMaterial ) {
  932. refreshUniformsLine( m_uniforms, material );
  933. } else if ( material instanceof THREE.ParticleBasicMaterial ) {
  934. refreshUniformsParticle( m_uniforms, material );
  935. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  936. refreshUniformsPhong( m_uniforms, material );
  937. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  938. m_uniforms.mNear.value = camera.near;
  939. m_uniforms.mFar.value = camera.far;
  940. m_uniforms.opacity.value = material.opacity;
  941. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  942. m_uniforms.opacity.value = material.opacity;
  943. }
  944. // load common uniforms
  945. loadUniformsGeneric( program, m_uniforms );
  946. loadUniformsMatrices( p_uniforms, object );
  947. // load material specific uniforms
  948. // (shader material also gets them for the sake of genericity)
  949. if ( material instanceof THREE.MeshShaderMaterial ||
  950. material instanceof THREE.MeshPhongMaterial ||
  951. material.env_map ) {
  952. _gl.uniform3f( p_uniforms.cameraPosition, camera.position.x, camera.position.y, camera.position.z );
  953. }
  954. if ( material instanceof THREE.MeshShaderMaterial ||
  955. material.env_map ||
  956. material.skinning ) {
  957. _gl.uniformMatrix4fv( p_uniforms.objectMatrix, false, object._objectMatrixArray );
  958. }
  959. if ( material instanceof THREE.MeshPhongMaterial ||
  960. material instanceof THREE.MeshLambertMaterial ||
  961. material instanceof THREE.MeshShaderMaterial ||
  962. material.skinning ) {
  963. _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, _viewMatrixArray );
  964. }
  965. if ( material.skinning ) {
  966. loadUniformsSkinning( p_uniforms, object );
  967. }
  968. return program;
  969. };
  970. function renderBuffer( camera, lights, fog, material, geometryChunk, object ) {
  971. var program, attributes, linewidth, primitives;
  972. program = setProgram( camera, lights, fog, material, object );
  973. attributes = program.attributes;
  974. // vertices
  975. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLVertexBuffer );
  976. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  977. // colors
  978. if ( attributes.color >= 0 ) {
  979. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLColorBuffer );
  980. _gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  981. }
  982. // normals
  983. if ( attributes.normal >= 0 ) {
  984. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLNormalBuffer );
  985. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  986. }
  987. // tangents
  988. if ( attributes.tangent >= 0 ) {
  989. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLTangentBuffer );
  990. _gl.vertexAttribPointer( attributes.tangent, 4, _gl.FLOAT, false, 0, 0 );
  991. }
  992. // uvs
  993. if ( attributes.uv >= 0 ) {
  994. if ( geometryChunk.__webGLUVBuffer ) {
  995. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLUVBuffer );
  996. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  997. _gl.enableVertexAttribArray( attributes.uv );
  998. } else {
  999. _gl.disableVertexAttribArray( attributes.uv );
  1000. }
  1001. }
  1002. if ( attributes.uv2 >= 0 ) {
  1003. if ( geometryChunk.__webGLUV2Buffer ) {
  1004. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLUV2Buffer );
  1005. _gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 );
  1006. _gl.enableVertexAttribArray( attributes.uv2 );
  1007. } else {
  1008. _gl.disableVertexAttribArray( attributes.uv2 );
  1009. }
  1010. }
  1011. if ( material.skinning &&
  1012. attributes.skinVertexA >=0 && attributes.skinVertexB >= 0 &&
  1013. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  1014. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLSkinVertexABuffer );
  1015. _gl.vertexAttribPointer( attributes.skinVertexA, 4, _gl.FLOAT, false, 0, 0 );
  1016. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLSkinVertexBBuffer );
  1017. _gl.vertexAttribPointer( attributes.skinVertexB, 4, _gl.FLOAT, false, 0, 0 );
  1018. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLSkinIndicesBuffer );
  1019. _gl.vertexAttribPointer( attributes.skinIndex, 4, _gl.FLOAT, false, 0, 0 );
  1020. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryChunk.__webGLSkinWeightsBuffer );
  1021. _gl.vertexAttribPointer( attributes.skinWeight, 4, _gl.FLOAT, false, 0, 0 );
  1022. }
  1023. // render mesh
  1024. if ( object instanceof THREE.Mesh ) {
  1025. // wireframe
  1026. if ( material.wireframe ) {
  1027. _gl.lineWidth( material.wireframe_linewidth );
  1028. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryChunk.__webGLLineBuffer );
  1029. _gl.drawElements( _gl.LINES, geometryChunk.__webGLLineCount, _gl.UNSIGNED_SHORT, 0 );
  1030. // triangles
  1031. } else {
  1032. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryChunk.__webGLFaceBuffer );
  1033. _gl.drawElements( _gl.TRIANGLES, geometryChunk.__webGLFaceCount, _gl.UNSIGNED_SHORT, 0 );
  1034. }
  1035. // render lines
  1036. } else if ( object instanceof THREE.Line ) {
  1037. primitives = ( object.type == THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
  1038. _gl.lineWidth( material.linewidth );
  1039. _gl.drawArrays( primitives, 0, geometryChunk.__webGLLineCount );
  1040. // render particles
  1041. } else if ( object instanceof THREE.ParticleSystem ) {
  1042. _gl.drawArrays( _gl.POINTS, 0, geometryChunk.__webGLParticleCount );
  1043. // render ribbon
  1044. } else if ( object instanceof THREE.Ribbon ) {
  1045. _gl.drawArrays( _gl.TRIANGLE_STRIP, 0, geometryChunk.__webGLVertexCount );
  1046. }
  1047. };
  1048. function renderBufferImmediate( object, program ) {
  1049. if ( ! object.__webGLVertexBuffer ) object.__webGLVertexBuffer = _gl.createBuffer();
  1050. if ( ! object.__webGLNormalBuffer ) object.__webGLNormalBuffer = _gl.createBuffer();
  1051. if ( object.hasPos ) {
  1052. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLVertexBuffer );
  1053. _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
  1054. _gl.enableVertexAttribArray( program.attributes.position );
  1055. _gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1056. }
  1057. if ( object.hasNormal ) {
  1058. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLNormalBuffer );
  1059. _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
  1060. _gl.enableVertexAttribArray( program.attributes.normal );
  1061. _gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  1062. }
  1063. _gl.drawArrays( _gl.TRIANGLES, 0, object.count );
  1064. object.count = 0;
  1065. };
  1066. function setObjectFaces( object ) {
  1067. if ( _oldDoubleSided != object.doubleSided ) {
  1068. if( object.doubleSided ) {
  1069. _gl.disable( _gl.CULL_FACE );
  1070. } else {
  1071. _gl.enable( _gl.CULL_FACE );
  1072. }
  1073. _oldDoubleSided = object.doubleSided;
  1074. }
  1075. if ( _oldFlipSided != object.flipSided ) {
  1076. if( object.flipSided ) {
  1077. _gl.frontFace( _gl.CW );
  1078. } else {
  1079. _gl.frontFace( _gl.CCW );
  1080. }
  1081. _oldFlipSided = object.flipSided;
  1082. }
  1083. };
  1084. function setDepthTest( test ) {
  1085. if ( _oldDepth != test ) {
  1086. if( test ) {
  1087. _gl.enable( _gl.DEPTH_TEST );
  1088. } else {
  1089. _gl.disable( _gl.DEPTH_TEST );
  1090. }
  1091. _oldDepth = test;
  1092. }
  1093. };
  1094. function computeFrustum( m ) {
  1095. _frustum[ 0 ].set( m.n41 - m.n11, m.n42 - m.n12, m.n43 - m.n13, m.n44 - m.n14 );
  1096. _frustum[ 1 ].set( m.n41 + m.n11, m.n42 + m.n12, m.n43 + m.n13, m.n44 + m.n14 );
  1097. _frustum[ 2 ].set( m.n41 + m.n21, m.n42 + m.n22, m.n43 + m.n23, m.n44 + m.n24 );
  1098. _frustum[ 3 ].set( m.n41 - m.n21, m.n42 - m.n22, m.n43 - m.n23, m.n44 - m.n24 );
  1099. _frustum[ 4 ].set( m.n41 - m.n31, m.n42 - m.n32, m.n43 - m.n33, m.n44 - m.n34 );
  1100. _frustum[ 5 ].set( m.n41 + m.n31, m.n42 + m.n32, m.n43 + m.n33, m.n44 + m.n34 );
  1101. var i, plane;
  1102. for ( i = 0; i < 6; i ++ ) {
  1103. plane = _frustum[ i ];
  1104. plane.divideScalar( Math.sqrt( plane.x * plane.x + plane.y * plane.y + plane.z * plane.z ) );
  1105. }
  1106. };
  1107. function isInFrustum( object ) {
  1108. var distance, matrix = object.matrixWorld,
  1109. radius = - object.geometry.boundingSphere.radius * Math.max( object.scale.x, Math.max( object.scale.y, object.scale.z ) );
  1110. for ( var i = 0; i < 6; i ++ ) {
  1111. distance = _frustum[ i ].x * matrix.n14 + _frustum[ i ].y * matrix.n24 + _frustum[ i ].z * matrix.n34 + _frustum[ i ].w;
  1112. if ( distance <= radius ) return false;
  1113. }
  1114. return true;
  1115. };
  1116. function addToFixedArray( where, what ) {
  1117. where.list[ where.count ] = what;
  1118. where.count += 1;
  1119. };
  1120. function unrollImmediateBufferMaterials( globject ) {
  1121. var i, l, m, ml, material,
  1122. object = globject.object,
  1123. opaque = globject.opaque,
  1124. transparent = globject.transparent;
  1125. transparent.count = 0;
  1126. opaque.count = 0;
  1127. for ( m = 0, ml = object.materials.length; m < ml; m++ ) {
  1128. material = object.materials[ m ];
  1129. if ( ( material.opacity && material.opacity < 1.0 ) || material.blending != THREE.NormalBlending )
  1130. addToFixedArray( transparent, material );
  1131. else
  1132. addToFixedArray( opaque, material );
  1133. }
  1134. };
  1135. function unrollBufferMaterials( globject ) {
  1136. var i, l, m, ml, material, meshMaterial,
  1137. object = globject.object,
  1138. buffer = globject.buffer,
  1139. opaque = globject.opaque,
  1140. transparent = globject.transparent;
  1141. transparent.count = 0;
  1142. opaque.count = 0;
  1143. for ( m = 0, ml = object.materials.length; m < ml; m++ ) {
  1144. meshMaterial = object.materials[ m ];
  1145. if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
  1146. for ( i = 0, l = buffer.materials.length; i < l; i++ ) {
  1147. material = buffer.materials[ i ];
  1148. if ( material ) {
  1149. if ( ( material.opacity && material.opacity < 1.0 ) || material.blending != THREE.NormalBlending )
  1150. addToFixedArray( transparent, material );
  1151. else
  1152. addToFixedArray( opaque, material );
  1153. }
  1154. }
  1155. } else {
  1156. material = meshMaterial;
  1157. if ( ( material.opacity && material.opacity < 1.0 ) || material.blending != THREE.NormalBlending )
  1158. addToFixedArray( transparent, material );
  1159. else
  1160. addToFixedArray( opaque, material );
  1161. }
  1162. }
  1163. };
  1164. function painterSort( a, b ) {
  1165. return b.z - a.z;
  1166. };
  1167. this.render = function( scene, camera, renderTarget, clear ) {
  1168. var i, program, opaque, transparent, material,
  1169. o, ol, oil, webGLObject, object, buffer,
  1170. lights = scene.lights,
  1171. fog = scene.fog,
  1172. ol;
  1173. camera.matrixAutoUpdate && camera.update();
  1174. camera.matrixWorld.flattenToArray( _viewMatrixArray );
  1175. camera.projectionMatrix.flattenToArray( _projectionMatrixArray );
  1176. camera.inverseMatrix.flattenToArray( _cameraInverseMatrixArray );
  1177. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorld );
  1178. computeFrustum( _projScreenMatrix );
  1179. if( THREE.AnimationHandler ) THREE.AnimationHandler.update();
  1180. scene.update( undefined, false, camera );
  1181. this.initWebGLObjects( scene, camera );
  1182. setRenderTarget( renderTarget, clear !== undefined ? clear : true );
  1183. if ( this.autoClear ) {
  1184. this.clear();
  1185. }
  1186. // set matrices
  1187. ol = scene.__webGLObjects.length;
  1188. for ( o = 0; o < ol; o++ ) {
  1189. webGLObject = scene.__webGLObjects[ o ];
  1190. object = webGLObject.object;
  1191. if ( object.visible ) {
  1192. if ( ! ( object instanceof THREE.Mesh ) || isInFrustum( object ) ) {
  1193. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  1194. setupMatrices( object, camera );
  1195. unrollBufferMaterials( webGLObject );
  1196. webGLObject.render = true;
  1197. if ( this.sortObjects ) {
  1198. _vector3.copy( object.position );
  1199. _projScreenMatrix.multiplyVector3( _vector3 );
  1200. webGLObject.z = _vector3.z;
  1201. }
  1202. } else {
  1203. webGLObject.render = false;
  1204. }
  1205. } else {
  1206. webGLObject.render = false;
  1207. }
  1208. }
  1209. if ( this.sortObjects ) {
  1210. scene.__webGLObjects.sort( painterSort );
  1211. }
  1212. oil = scene.__webGLObjectsImmediate.length;
  1213. for ( o = 0; o < oil; o++ ) {
  1214. webGLObject = scene.__webGLObjectsImmediate[ o ];
  1215. object = webGLObject.object;
  1216. if ( object.visible ) {
  1217. if( object.matrixAutoUpdate ) {
  1218. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  1219. }
  1220. setupMatrices( object, camera );
  1221. unrollImmediateBufferMaterials( webGLObject );
  1222. }
  1223. }
  1224. // opaque pass
  1225. setBlending( THREE.NormalBlending );
  1226. for ( o = 0; o < ol; o++ ) {
  1227. webGLObject = scene.__webGLObjects[ o ];
  1228. if ( webGLObject.render ) {
  1229. object = webGLObject.object;
  1230. buffer = webGLObject.buffer;
  1231. opaque = webGLObject.opaque;
  1232. setObjectFaces( object );
  1233. for( i = 0; i < opaque.count; i++ ) {
  1234. material = opaque.list[ i ];
  1235. setDepthTest( material.depth_test );
  1236. renderBuffer( camera, lights, fog, material, buffer, object );
  1237. }
  1238. }
  1239. }
  1240. // opaque pass (immediate simulator)
  1241. for ( o = 0; o < oil; o++ ) {
  1242. webGLObject = scene.__webGLObjectsImmediate[ o ];
  1243. object = webGLObject.object;
  1244. if ( object.visible ) {
  1245. opaque = webGLObject.opaque;
  1246. setObjectFaces( object );
  1247. for( i = 0; i < opaque.count; i++ ) {
  1248. material = opaque.list[ i ];
  1249. setDepthTest( material.depth_test );
  1250. program = setProgram( camera, lights, fog, material, object );
  1251. object.render( function( object ) { renderBufferImmediate( object, program ); } );
  1252. }
  1253. }
  1254. }
  1255. // transparent pass
  1256. for ( o = 0; o < ol; o++ ) {
  1257. webGLObject = scene.__webGLObjects[ o ];
  1258. if ( webGLObject.render ) {
  1259. object = webGLObject.object;
  1260. buffer = webGLObject.buffer;
  1261. transparent = webGLObject.transparent;
  1262. setObjectFaces( object );
  1263. for( i = 0; i < transparent.count; i++ ) {
  1264. material = transparent.list[ i ];
  1265. setBlending( material.blending );
  1266. setDepthTest( material.depth_test );
  1267. renderBuffer( camera, lights, fog, material, buffer, object );
  1268. }
  1269. }
  1270. }
  1271. // transparent pass (immediate simulator)
  1272. for ( o = 0; o < oil; o++ ) {
  1273. webGLObject = scene.__webGLObjectsImmediate[ o ];
  1274. object = webGLObject.object;
  1275. if ( object.visible ) {
  1276. transparent = webGLObject.transparent;
  1277. setObjectFaces( object );
  1278. for( i = 0; i < transparent.count; i++ ) {
  1279. material = transparent.list[ i ];
  1280. setBlending( material.blending );
  1281. setDepthTest( material.depth_test );
  1282. program = setProgram( camera, lights, fog, material, object );
  1283. object.render( function( object ) { renderBufferImmediate( object, program ); } );
  1284. }
  1285. }
  1286. }
  1287. // Generate mipmap if we're using any kind of mipmap filtering
  1288. if ( renderTarget && renderTarget.min_filter !== THREE.NearestFilter && renderTarget.min_filter !== THREE.LinearFilter ) {
  1289. updateRenderTargetMipmap( renderTarget );
  1290. }
  1291. };
  1292. function addObject( scene, object, camera ) {
  1293. var g, geometry, geometryChunk, objmap;
  1294. geometry = object.geometry;
  1295. if ( scene.__webGLObjectsMap[ object.id ] == undefined ) {
  1296. scene.__webGLObjectsMap[ object.id ] = {};
  1297. object._modelViewMatrix = new THREE.Matrix4();
  1298. object._normalMatrixArray = new Float32Array( 9 );
  1299. object._modelViewMatrixArray = new Float32Array( 16 );
  1300. object._objectMatrixArray = new Float32Array( 16 );
  1301. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  1302. }
  1303. objmap = scene.__webGLObjectsMap[ object.id ];
  1304. objlist = scene.__webGLObjects;
  1305. if ( object instanceof THREE.Mesh ) {
  1306. // create separate VBOs per geometry chunk
  1307. for ( g in geometry.geometryChunks ) {
  1308. geometryChunk = geometry.geometryChunks[ g ];
  1309. // initialise VBO on the first access
  1310. if( ! geometryChunk.__webGLVertexBuffer ) {
  1311. createMeshBuffers( geometryChunk );
  1312. initMeshBuffers( geometryChunk, object );
  1313. geometry.__dirtyVertices = true;
  1314. geometry.__dirtyElements = true;
  1315. geometry.__dirtyUvs = true;
  1316. geometry.__dirtyNormals = true;
  1317. geometry.__dirtyTangents = true;
  1318. geometry.__dirtyColors = true;
  1319. }
  1320. if( geometry.__dirtyVertices || geometry.__dirtyElements ||
  1321. geometry.__dirtyUvs || geometry.__dirtyNormals ||
  1322. geometry.__dirtyColors || geometry.__dirtyTangents ) {
  1323. setMeshBuffers( geometryChunk, object, _gl.DYNAMIC_DRAW );
  1324. }
  1325. // create separate wrapper per each use of VBO
  1326. add_buffer( objlist, objmap, g, geometryChunk, object );
  1327. }
  1328. geometry.__dirtyVertices = false;
  1329. geometry.__dirtyElements = false;
  1330. geometry.__dirtyUvs = false;
  1331. geometry.__dirtyNormals = false;
  1332. geometry.__dirtyTangents = false;
  1333. geometry.__dirtyColors = false;
  1334. } else if ( object instanceof THREE.Ribbon ) {
  1335. if( ! geometry.__webGLVertexBuffer ) {
  1336. createRibbonBuffers( geometry );
  1337. initRibbonBuffers( geometry );
  1338. geometry.__dirtyVertices = true;
  1339. geometry.__dirtyColors = true;
  1340. }
  1341. if( geometry.__dirtyVertices || geometry.__dirtyColors ) {
  1342. setRibbonBuffers( geometry, _gl.DYNAMIC_DRAW );
  1343. }
  1344. add_buffer( objlist, objmap, 0, geometry, object );
  1345. geometry.__dirtyVertices = false;
  1346. geometry.__dirtyColors = false;
  1347. } else if ( object instanceof THREE.Line ) {
  1348. if( ! geometry.__webGLVertexBuffer ) {
  1349. createLineBuffers( geometry );
  1350. initLineBuffers( geometry );
  1351. geometry.__dirtyVertices = true;
  1352. geometry.__dirtyColors = true;
  1353. }
  1354. if( geometry.__dirtyVertices || geometry.__dirtyColors ) {
  1355. setLineBuffers( geometry, _gl.DYNAMIC_DRAW );
  1356. }
  1357. add_buffer( objlist, objmap, 0, geometry, object );
  1358. geometry.__dirtyVertices = false;
  1359. geometry.__dirtyColors = false;
  1360. } else if ( object instanceof THREE.ParticleSystem ) {
  1361. if( ! geometry.__webGLVertexBuffer ) {
  1362. createParticleBuffers( geometry );
  1363. initParticleBuffers( geometry );
  1364. geometry.__dirtyVertices = true;
  1365. geometry.__dirtyColors = true;
  1366. }
  1367. if( geometry.__dirtyVertices || geometry.__dirtyColors || object.sortParticles ) {
  1368. setParticleBuffers( geometry, _gl.DYNAMIC_DRAW, object, camera );
  1369. }
  1370. add_buffer( objlist, objmap, 0, geometry, object );
  1371. geometry.__dirtyVertices = false;
  1372. geometry.__dirtyColors = false;
  1373. } else if ( THREE.MarchingCubes !== undefined && object instanceof THREE.MarchingCubes ) {
  1374. add_buffer_immediate( scene.__webGLObjectsImmediate, objmap, 0, object );
  1375. }/*else if ( object instanceof THREE.Particle ) {
  1376. }*/
  1377. };
  1378. function add_buffer( objlist, objmap, id, buffer, object ) {
  1379. if ( objmap[ id ] == undefined ) {
  1380. objlist.push( { buffer: buffer, object: object,
  1381. opaque: { list: [], count: 0 },
  1382. transparent: { list: [], count: 0 }
  1383. } );
  1384. objmap[ id ] = 1;
  1385. }
  1386. };
  1387. function add_buffer_immediate( objlist, objmap, id, object ) {
  1388. if ( objmap[ id ] == undefined ) {
  1389. objlist.push( { object: object,
  1390. opaque: { list: [], count: 0 },
  1391. transparent: { list: [], count: 0 }
  1392. } );
  1393. objmap[ id ] = 1;
  1394. }
  1395. };
  1396. this.initWebGLObjects = function( scene, camera ) {
  1397. var o, ol, object;
  1398. if ( !scene.__webGLObjects ) {
  1399. scene.__webGLObjects = [];
  1400. scene.__webGLObjectsMap = {};
  1401. scene.__webGLObjectsImmediate = [];
  1402. }
  1403. for ( o = 0, ol = scene.objects.length; o < ol; o++ ) {
  1404. object = scene.objects[ o ];
  1405. addObject( scene, object, camera );
  1406. }
  1407. };
  1408. this.removeObject = function ( scene, object ) {
  1409. var o, ol, zobject;
  1410. for ( o = scene.__webGLObjects.length - 1; o >= 0; o-- ) {
  1411. zobject = scene.__webGLObjects[ o ].object;
  1412. if ( object == zobject ) {
  1413. scene.__webGLObjects.splice( o, 1 );
  1414. }
  1415. }
  1416. };
  1417. function setupMatrices ( object, camera ) {
  1418. object._modelViewMatrix.multiplyToArray( camera.matrixWorld, object.matrixWorld, object._modelViewMatrixArray );
  1419. THREE.Matrix4.makeInvert3x3( object._modelViewMatrix ).transposeIntoArray( object._normalMatrixArray );
  1420. };
  1421. this.setFaceCulling = function ( cullFace, frontFace ) {
  1422. if ( cullFace ) {
  1423. if ( !frontFace || frontFace == "ccw" ) {
  1424. _gl.frontFace( _gl.CCW );
  1425. } else {
  1426. _gl.frontFace( _gl.CW );
  1427. }
  1428. if( cullFace == "back" ) {
  1429. _gl.cullFace( _gl.BACK );
  1430. } else if( cullFace == "front" ) {
  1431. _gl.cullFace( _gl.FRONT );
  1432. } else {
  1433. _gl.cullFace( _gl.FRONT_AND_BACK );
  1434. }
  1435. _gl.enable( _gl.CULL_FACE );
  1436. } else {
  1437. _gl.disable( _gl.CULL_FACE );
  1438. }
  1439. };
  1440. this.supportsVertexTextures = function() {
  1441. return maxVertexTextures() > 0;
  1442. };
  1443. function maxVertexTextures() {
  1444. return _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
  1445. };
  1446. function initGL( antialias, clearColor, clearAlpha ) {
  1447. try {
  1448. _gl = _canvas.getContext( 'experimental-webgl', { antialias: antialias } );
  1449. } catch(e) { console.log(e) }
  1450. if (!_gl) {
  1451. // alert("WebGL not supported");
  1452. throw "cannot create webgl context";
  1453. }
  1454. _gl.clearColor( 0, 0, 0, 1 );
  1455. _gl.clearDepth( 1 );
  1456. _gl.enable( _gl.DEPTH_TEST );
  1457. _gl.depthFunc( _gl.LEQUAL );
  1458. _gl.frontFace( _gl.CCW );
  1459. _gl.cullFace( _gl.BACK );
  1460. _gl.enable( _gl.CULL_FACE );
  1461. _gl.enable( _gl.BLEND );
  1462. _gl.blendFunc( _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  1463. _gl.clearColor( clearColor.r, clearColor.g, clearColor.b, clearAlpha );
  1464. _cullEnabled = true;
  1465. };
  1466. function buildProgram( fragment_shader, vertex_shader, parameters ) {
  1467. var program = _gl.createProgram(),
  1468. prefix_fragment = [
  1469. "#ifdef GL_ES",
  1470. "precision highp float;",
  1471. "#endif",
  1472. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  1473. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  1474. parameters.fog ? "#define USE_FOG" : "",
  1475. parameters.fog instanceof THREE.FogExp2 ? "#define FOG_EXP2" : "",
  1476. parameters.map ? "#define USE_MAP" : "",
  1477. parameters.env_map ? "#define USE_ENVMAP" : "",
  1478. parameters.light_map ? "#define USE_LIGHTMAP" : "",
  1479. parameters.vertex_colors ? "#define USE_COLOR" : "",
  1480. "uniform mat4 viewMatrix;",
  1481. "uniform vec3 cameraPosition;",
  1482. ""
  1483. ].join("\n"),
  1484. prefix_vertex = [
  1485. maxVertexTextures() > 0 ? "#define VERTEX_TEXTURES" : "",
  1486. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  1487. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  1488. parameters.map ? "#define USE_MAP" : "",
  1489. parameters.env_map ? "#define USE_ENVMAP" : "",
  1490. parameters.light_map ? "#define USE_LIGHTMAP" : "",
  1491. parameters.vertex_colors ? "#define USE_COLOR" : "",
  1492. parameters.skinning ? "#define USE_SKINNING" : "",
  1493. "uniform mat4 objectMatrix;",
  1494. "uniform mat4 modelViewMatrix;",
  1495. "uniform mat4 projectionMatrix;",
  1496. "uniform mat4 viewMatrix;",
  1497. "uniform mat3 normalMatrix;",
  1498. "uniform vec3 cameraPosition;",
  1499. "uniform mat4 cameraInverseMatrix;",
  1500. "attribute vec3 position;",
  1501. "attribute vec3 normal;",
  1502. "attribute vec3 color;",
  1503. "attribute vec2 uv;",
  1504. "attribute vec2 uv2;",
  1505. "attribute vec4 skinVertexA;",
  1506. "attribute vec4 skinVertexB;",
  1507. "attribute vec4 skinIndex;",
  1508. "attribute vec4 skinWeight;",
  1509. ""
  1510. ].join("\n");
  1511. _gl.attachShader( program, getShader( "fragment", prefix_fragment + fragment_shader ) );
  1512. _gl.attachShader( program, getShader( "vertex", prefix_vertex + vertex_shader ) );
  1513. _gl.linkProgram( program );
  1514. if ( !_gl.getProgramParameter( program, _gl.LINK_STATUS ) ) {
  1515. alert( "Could not initialise shaders\n"+
  1516. "VALIDATE_STATUS: " + _gl.getProgramParameter( program, _gl.VALIDATE_STATUS ) + ", gl error [" + _gl.getError() + "]" );
  1517. //console.log( prefix_fragment + fragment_shader );
  1518. //console.log( prefix_vertex + vertex_shader );
  1519. }
  1520. //console.log( prefix_fragment + fragment_shader );
  1521. //console.log( prefix_vertex + vertex_shader );
  1522. program.uniforms = {};
  1523. program.attributes = {};
  1524. return program;
  1525. };
  1526. function loadUniformsSkinning( uniforms, object ) {
  1527. _gl.uniformMatrix4fv( uniforms.cameraInverseMatrix, false, _cameraInverseMatrixArray );
  1528. _gl.uniformMatrix4fv( uniforms.boneGlobalMatrices, false, object.boneMatrices );
  1529. };
  1530. function loadUniformsMatrices( uniforms, object ) {
  1531. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object._modelViewMatrixArray );
  1532. _gl.uniformMatrix3fv( uniforms.normalMatrix, false, object._normalMatrixArray );
  1533. };
  1534. function loadUniformsGeneric( program, uniforms ) {
  1535. var u, uniform, value, type, location, texture;
  1536. for( u in uniforms ) {
  1537. location = program.uniforms[u];
  1538. if ( !location ) continue;
  1539. uniform = uniforms[u];
  1540. type = uniform.type;
  1541. value = uniform.value;
  1542. if( type == "i" ) {
  1543. _gl.uniform1i( location, value );
  1544. } else if( type == "f" ) {
  1545. _gl.uniform1f( location, value );
  1546. } else if( type == "fv1" ) {
  1547. _gl.uniform1fv( location, value );
  1548. } else if( type == "fv" ) {
  1549. _gl.uniform3fv( location, value );
  1550. } else if( type == "v2" ) {
  1551. _gl.uniform2f( location, value.x, value.y );
  1552. } else if( type == "v3" ) {
  1553. _gl.uniform3f( location, value.x, value.y, value.z );
  1554. } else if( type == "c" ) {
  1555. _gl.uniform3f( location, value.r, value.g, value.b );
  1556. } else if( type == "t" ) {
  1557. _gl.uniform1i( location, value );
  1558. texture = uniform.texture;
  1559. if ( !texture ) continue;
  1560. if ( texture.image instanceof Array && texture.image.length == 6 ) {
  1561. setCubeTexture( texture, value );
  1562. } else {
  1563. setTexture( texture, value );
  1564. }
  1565. }
  1566. }
  1567. };
  1568. function setBlending( blending ) {
  1569. if ( blending != _oldBlending ) {
  1570. switch ( blending ) {
  1571. case THREE.AdditiveBlending:
  1572. _gl.blendEquation( _gl.FUNC_ADD );
  1573. _gl.blendFunc( _gl.ONE, _gl.ONE );
  1574. break;
  1575. case THREE.SubtractiveBlending:
  1576. //_gl.blendEquation( _gl.FUNC_SUBTRACT );
  1577. _gl.blendFunc( _gl.DST_COLOR, _gl.ZERO );
  1578. break;
  1579. case THREE.BillboardBlending:
  1580. _gl.blendEquation( _gl.FUNC_ADD );
  1581. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA);
  1582. break;
  1583. case THREE.ReverseSubtractiveBlending:
  1584. _gl.blendEquation( _gl.FUNC_REVERSE_SUBTRACT );
  1585. _gl.blendFunc( _gl.ONE, _gl.ONE );
  1586. break;
  1587. default:
  1588. _gl.blendEquation( _gl.FUNC_ADD );
  1589. _gl.blendFunc( _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  1590. break;
  1591. }
  1592. _oldBlending = blending;
  1593. }
  1594. };
  1595. function setCubeTexture( texture, slot ) {
  1596. if ( texture.image.length == 6 ) {
  1597. if ( texture.needsUpdate ) {
  1598. if ( !texture.image.__webGLTextureCube ) {
  1599. texture.image.__webGLTextureCube = _gl.createTexture();
  1600. }
  1601. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webGLTextureCube );
  1602. _gl.texParameteri( _gl.TEXTURE_CUBE_MAP, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  1603. _gl.texParameteri( _gl.TEXTURE_CUBE_MAP, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  1604. _gl.texParameteri( _gl.TEXTURE_CUBE_MAP, _gl.TEXTURE_MAG_FILTER, _gl.LINEAR );
  1605. _gl.texParameteri( _gl.TEXTURE_CUBE_MAP, _gl.TEXTURE_MIN_FILTER, _gl.LINEAR_MIPMAP_LINEAR );
  1606. for ( var i = 0; i < 6; ++i ) {
  1607. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, texture.image[ i ] );
  1608. }
  1609. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  1610. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  1611. texture.needsUpdate = false;
  1612. }
  1613. _gl.activeTexture( _gl.TEXTURE0 + slot );
  1614. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webGLTextureCube );
  1615. }
  1616. };
  1617. function setTexture( texture, slot ) {
  1618. if ( texture.needsUpdate ) {
  1619. if ( !texture.__wasSetOnce ) {
  1620. texture.__webGLTexture = _gl.createTexture();
  1621. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webGLTexture );
  1622. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, texture.image );
  1623. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrap_s ) );
  1624. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrap_t ) );
  1625. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.mag_filter ) );
  1626. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.min_filter ) );
  1627. _gl.generateMipmap( _gl.TEXTURE_2D );
  1628. _gl.bindTexture( _gl.TEXTURE_2D, null );
  1629. texture.__wasSetOnce = true;
  1630. } else {
  1631. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webGLTexture );
  1632. _gl.texSubImage2D( _gl.TEXTURE_2D, 0, 0, 0, _gl.RGBA, _gl.UNSIGNED_BYTE, texture.image );
  1633. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrap_s ) );
  1634. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrap_t ) );
  1635. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.mag_filter ) );
  1636. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.min_filter ) );
  1637. _gl.generateMipmap( _gl.TEXTURE_2D );
  1638. _gl.bindTexture( _gl.TEXTURE_2D, null );
  1639. }
  1640. texture.needsUpdate = false;
  1641. }
  1642. _gl.activeTexture( _gl.TEXTURE0 + slot );
  1643. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webGLTexture );
  1644. };
  1645. function setRenderTarget( renderTexture, clear ) {
  1646. if ( renderTexture && !renderTexture.__webGLFramebuffer ) {
  1647. renderTexture.__webGLFramebuffer = _gl.createFramebuffer();
  1648. renderTexture.__webGLRenderbuffer = _gl.createRenderbuffer();
  1649. renderTexture.__webGLTexture = _gl.createTexture();
  1650. // Setup renderbuffer
  1651. _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderTexture.__webGLRenderbuffer );
  1652. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTexture.width, renderTexture.height );
  1653. // Setup texture
  1654. _gl.bindTexture( _gl.TEXTURE_2D, renderTexture.__webGLTexture );
  1655. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, paramThreeToGL( renderTexture.wrap_s ) );
  1656. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, paramThreeToGL( renderTexture.wrap_t ) );
  1657. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( renderTexture.mag_filter ) );
  1658. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( renderTexture.min_filter ) );
  1659. _gl.texImage2D( _gl.TEXTURE_2D, 0, paramThreeToGL( renderTexture.format ), renderTexture.width, renderTexture.height, 0, paramThreeToGL( renderTexture.format ), paramThreeToGL( renderTexture.type ), null );
  1660. // Setup framebuffer
  1661. _gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTexture.__webGLFramebuffer );
  1662. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D, renderTexture.__webGLTexture, 0 );
  1663. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderTexture.__webGLRenderbuffer );
  1664. // Release everything
  1665. _gl.bindTexture( _gl.TEXTURE_2D, null );
  1666. _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
  1667. _gl.bindFramebuffer( _gl.FRAMEBUFFER, null);
  1668. }
  1669. var framebuffer, width, height;
  1670. if ( renderTexture ) {
  1671. framebuffer = renderTexture.__webGLFramebuffer;
  1672. width = renderTexture.width;
  1673. height = renderTexture.height;
  1674. } else {
  1675. framebuffer = null;
  1676. width = _canvas.width;
  1677. height = _canvas.height;
  1678. }
  1679. if( framebuffer != _oldFramebuffer ) {
  1680. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  1681. _gl.viewport( 0, 0, width, height );
  1682. if ( clear ) {
  1683. _gl.clear( _gl.COLOR_BUFFER_BIT | _gl.DEPTH_BUFFER_BIT );
  1684. }
  1685. _oldFramebuffer = framebuffer;
  1686. }
  1687. };
  1688. function updateRenderTargetMipmap( renderTarget ) {
  1689. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webGLTexture );
  1690. _gl.generateMipmap( _gl.TEXTURE_2D );
  1691. _gl.bindTexture( _gl.TEXTURE_2D, null );
  1692. };
  1693. function cacheUniformLocations( program, identifiers ) {
  1694. var i, l, id;
  1695. for( i = 0, l = identifiers.length; i < l; i++ ) {
  1696. id = identifiers[ i ];
  1697. program.uniforms[ id ] = _gl.getUniformLocation( program, id );
  1698. }
  1699. };
  1700. function cacheAttributeLocations( program, identifiers ) {
  1701. var i, l, id;
  1702. for( i = 0, l = identifiers.length; i < l; i++ ) {
  1703. id = identifiers[ i ];
  1704. program.attributes[ id ] = _gl.getAttribLocation( program, id );
  1705. }
  1706. };
  1707. function getShader( type, string ) {
  1708. var shader;
  1709. if ( type == "fragment" ) {
  1710. shader = _gl.createShader( _gl.FRAGMENT_SHADER );
  1711. } else if ( type == "vertex" ) {
  1712. shader = _gl.createShader( _gl.VERTEX_SHADER );
  1713. }
  1714. _gl.shaderSource( shader, string );
  1715. _gl.compileShader( shader );
  1716. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  1717. alert( _gl.getShaderInfoLog( shader ) );
  1718. return null;
  1719. }
  1720. return shader;
  1721. };
  1722. function paramThreeToGL( p ) {
  1723. switch ( p ) {
  1724. case THREE.RepeatWrapping: return _gl.REPEAT; break;
  1725. case THREE.ClampToEdgeWrapping: return _gl.CLAMP_TO_EDGE; break;
  1726. case THREE.MirroredRepeatWrapping: return _gl.MIRRORED_REPEAT; break;
  1727. case THREE.NearestFilter: return _gl.NEAREST; break;
  1728. case THREE.NearestMipMapNearestFilter: return _gl.NEAREST_MIPMAP_NEAREST; break;
  1729. case THREE.NearestMipMapLinearFilter: return _gl.NEAREST_MIPMAP_LINEAR; break;
  1730. case THREE.LinearFilter: return _gl.LINEAR; break;
  1731. case THREE.LinearMipMapNearestFilter: return _gl.LINEAR_MIPMAP_NEAREST; break;
  1732. case THREE.LinearMipMapLinearFilter: return _gl.LINEAR_MIPMAP_LINEAR; break;
  1733. case THREE.ByteType: return _gl.BYTE; break;
  1734. case THREE.UnsignedByteType: return _gl.UNSIGNED_BYTE; break;
  1735. case THREE.ShortType: return _gl.SHORT; break;
  1736. case THREE.UnsignedShortType: return _gl.UNSIGNED_SHORT; break;
  1737. case THREE.IntType: return _gl.INT; break;
  1738. case THREE.UnsignedShortType: return _gl.UNSIGNED_INT; break;
  1739. case THREE.FloatType: return _gl.FLOAT; break;
  1740. case THREE.AlphaFormat: return _gl.ALPHA; break;
  1741. case THREE.RGBFormat: return _gl.RGB; break;
  1742. case THREE.RGBAFormat: return _gl.RGBA; break;
  1743. case THREE.LuminanceFormat: return _gl.LUMINANCE; break;
  1744. case THREE.LuminanceAlphaFormat: return _gl.LUMINANCE_ALPHA; break;
  1745. }
  1746. return 0;
  1747. };
  1748. function materialNeedsSmoothNormals( material ) {
  1749. return material && material.shading != undefined && material.shading == THREE.SmoothShading;
  1750. };
  1751. function bufferNeedsSmoothNormals( geometryChunk, object ) {
  1752. var m, ml, i, l, meshMaterial, needsSmoothNormals = false;
  1753. for ( m = 0, ml = object.materials.length; m < ml; m++ ) {
  1754. meshMaterial = object.materials[ m ];
  1755. if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
  1756. for ( i = 0, l = geometryChunk.materials.length; i < l; i++ ) {
  1757. if ( materialNeedsSmoothNormals( geometryChunk.materials[ i ] ) ) {
  1758. needsSmoothNormals = true;
  1759. break;
  1760. }
  1761. }
  1762. } else {
  1763. if ( materialNeedsSmoothNormals( meshMaterial ) ) {
  1764. needsSmoothNormals = true;
  1765. break;
  1766. }
  1767. }
  1768. if ( needsSmoothNormals ) break;
  1769. }
  1770. return needsSmoothNormals;
  1771. };
  1772. function allocateLights( lights, maxLights ) {
  1773. var l, ll, light, dirLights, pointLights, maxDirLights, maxPointLights;
  1774. dirLights = pointLights = maxDirLights = maxPointLights = 0;
  1775. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  1776. light = lights[ l ];
  1777. if ( light instanceof THREE.DirectionalLight ) dirLights++;
  1778. if ( light instanceof THREE.PointLight ) pointLights++;
  1779. }
  1780. if ( ( pointLights + dirLights ) <= maxLights ) {
  1781. maxDirLights = dirLights;
  1782. maxPointLights = pointLights;
  1783. } else {
  1784. maxDirLights = Math.ceil( maxLights * dirLights / ( pointLights + dirLights ) );
  1785. maxPointLights = maxLights - maxDirLights;
  1786. }
  1787. return { 'directional' : maxDirLights, 'point' : maxPointLights };
  1788. };
  1789. /* DEBUG
  1790. function getGLParams() {
  1791. var params = {
  1792. 'MAX_VARYING_VECTORS': _gl.getParameter( _gl.MAX_VARYING_VECTORS ),
  1793. 'MAX_VERTEX_ATTRIBS': _gl.getParameter( _gl.MAX_VERTEX_ATTRIBS ),
  1794. 'MAX_TEXTURE_IMAGE_UNITS': _gl.getParameter( _gl.MAX_TEXTURE_IMAGE_UNITS ),
  1795. 'MAX_VERTEX_TEXTURE_IMAGE_UNITS': _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ),
  1796. 'MAX_COMBINED_TEXTURE_IMAGE_UNITS' : _gl.getParameter( _gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS ),
  1797. 'MAX_VERTEX_UNIFORM_VECTORS': _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS ),
  1798. 'MAX_FRAGMENT_UNIFORM_VECTORS': _gl.getParameter( _gl.MAX_FRAGMENT_UNIFORM_VECTORS )
  1799. }
  1800. return params;
  1801. };
  1802. function dumpObject( obj ) {
  1803. var p, str = "";
  1804. for ( p in obj ) {
  1805. str += p + ": " + obj[p] + "\n";
  1806. }
  1807. return str;
  1808. }
  1809. */
  1810. };
  1811. THREE.Snippets = {
  1812. // FOG
  1813. fog_pars_fragment: [
  1814. "#ifdef USE_FOG",
  1815. "uniform vec3 fogColor;",
  1816. "#ifdef FOG_EXP2",
  1817. "uniform float fogDensity;",
  1818. "#else",
  1819. "uniform float fogNear;",
  1820. "uniform float fogFar;",
  1821. "#endif",
  1822. "#endif"
  1823. ].join("\n"),
  1824. fog_fragment: [
  1825. "#ifdef USE_FOG",
  1826. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  1827. "#ifdef FOG_EXP2",
  1828. "const float LOG2 = 1.442695;",
  1829. "float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );",
  1830. "fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );",
  1831. "#else",
  1832. "float fogFactor = smoothstep( fogNear, fogFar, depth );",
  1833. "#endif",
  1834. "gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );",
  1835. "#endif"
  1836. ].join("\n"),
  1837. // ENVIRONMENT MAP
  1838. envmap_pars_fragment: [
  1839. "#ifdef USE_ENVMAP",
  1840. "varying vec3 vReflect;",
  1841. "uniform float reflectivity;",
  1842. "uniform samplerCube env_map;",
  1843. "uniform int combine;",
  1844. "#endif"
  1845. ].join("\n"),
  1846. envmap_fragment: [
  1847. "#ifdef USE_ENVMAP",
  1848. "vec4 cubeColor = textureCube( env_map, vec3( -vReflect.x, vReflect.yz ) );",
  1849. "if ( combine == 1 ) {",
  1850. //"gl_FragColor = mix( gl_FragColor, cubeColor, reflectivity );",
  1851. "gl_FragColor = vec4( mix( gl_FragColor.xyz, cubeColor.xyz, reflectivity ), opacity );",
  1852. "} else {",
  1853. "gl_FragColor = gl_FragColor * cubeColor;",
  1854. "}",
  1855. "#endif"
  1856. ].join("\n"),
  1857. envmap_pars_vertex: [
  1858. "#ifdef USE_ENVMAP",
  1859. "varying vec3 vReflect;",
  1860. "uniform float refraction_ratio;",
  1861. "uniform bool useRefract;",
  1862. "#endif"
  1863. ].join("\n"),
  1864. envmap_vertex : [
  1865. "#ifdef USE_ENVMAP",
  1866. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  1867. "vec3 nWorld = mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal;",
  1868. "if ( useRefract ) {",
  1869. "vReflect = refract( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ), refraction_ratio );",
  1870. "} else {",
  1871. "vReflect = reflect( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ) );",
  1872. "}",
  1873. "#endif"
  1874. ].join("\n"),
  1875. // COLOR MAP (particles)
  1876. map_particle_pars_fragment: [
  1877. "#ifdef USE_MAP",
  1878. "uniform sampler2D map;",
  1879. "#endif"
  1880. ].join("\n"),
  1881. map_particle_fragment: [
  1882. "#ifdef USE_MAP",
  1883. "gl_FragColor = gl_FragColor * texture2D( map, gl_PointCoord );",
  1884. "#endif"
  1885. ].join("\n"),
  1886. // COLOR MAP (triangles)
  1887. map_pars_fragment: [
  1888. "#ifdef USE_MAP",
  1889. "varying vec2 vUv;",
  1890. "uniform sampler2D map;",
  1891. "#endif"
  1892. ].join("\n"),
  1893. map_pars_vertex: [
  1894. "#ifdef USE_MAP",
  1895. "varying vec2 vUv;",
  1896. "#endif"
  1897. ].join("\n"),
  1898. map_fragment: [
  1899. "#ifdef USE_MAP",
  1900. "gl_FragColor = gl_FragColor * texture2D( map, vUv );",
  1901. "#endif"
  1902. ].join("\n"),
  1903. map_vertex: [
  1904. "#ifdef USE_MAP",
  1905. "vUv = uv;",
  1906. "#endif"
  1907. ].join("\n"),
  1908. // LIGHT MAP
  1909. lightmap_pars_fragment: [
  1910. "#ifdef USE_LIGHTMAP",
  1911. "varying vec2 vUv2;",
  1912. "uniform sampler2D light_map;",
  1913. "#endif"
  1914. ].join("\n"),
  1915. lightmap_pars_vertex: [
  1916. "#ifdef USE_LIGHTMAP",
  1917. "varying vec2 vUv2;",
  1918. "#endif"
  1919. ].join("\n"),
  1920. lightmap_fragment: [
  1921. "#ifdef USE_LIGHTMAP",
  1922. "gl_FragColor = gl_FragColor * texture2D( light_map, vUv2 );",
  1923. "#endif"
  1924. ].join("\n"),
  1925. lightmap_vertex: [
  1926. "#ifdef USE_LIGHTMAP",
  1927. "vUv2 = uv2;",
  1928. "#endif"
  1929. ].join("\n"),
  1930. lights_pars_vertex: [
  1931. "uniform bool enableLighting;",
  1932. "uniform vec3 ambientLightColor;",
  1933. "#if MAX_DIR_LIGHTS > 0",
  1934. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  1935. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  1936. "#endif",
  1937. "#if MAX_POINT_LIGHTS > 0",
  1938. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  1939. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  1940. "#ifdef PHONG",
  1941. "varying vec3 vPointLightVector[ MAX_POINT_LIGHTS ];",
  1942. "#endif",
  1943. "#endif"
  1944. ].join("\n"),
  1945. // LIGHTS
  1946. lights_vertex: [
  1947. "if ( !enableLighting ) {",
  1948. "vLightWeighting = vec3( 1.0 );",
  1949. "} else {",
  1950. "vLightWeighting = ambientLightColor;",
  1951. "#if MAX_DIR_LIGHTS > 0",
  1952. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  1953. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  1954. "float directionalLightWeighting = max( dot( transformedNormal, normalize( lDirection.xyz ) ), 0.0 );",
  1955. "vLightWeighting += directionalLightColor[ i ] * directionalLightWeighting;",
  1956. "}",
  1957. "#endif",
  1958. "#if MAX_POINT_LIGHTS > 0",
  1959. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  1960. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  1961. "vec3 pointLightVector = normalize( lPosition.xyz - mvPosition.xyz );",
  1962. "float pointLightWeighting = max( dot( transformedNormal, pointLightVector ), 0.0 );",
  1963. "vLightWeighting += pointLightColor[ i ] * pointLightWeighting;",
  1964. "#ifdef PHONG",
  1965. "vPointLightVector[ i ] = pointLightVector;",
  1966. "#endif",
  1967. "}",
  1968. "#endif",
  1969. "}"
  1970. ].join("\n"),
  1971. lights_pars_fragment: [
  1972. "#if MAX_DIR_LIGHTS > 0",
  1973. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  1974. "#endif",
  1975. "#if MAX_POINT_LIGHTS > 0",
  1976. "varying vec3 vPointLightVector[ MAX_POINT_LIGHTS ];",
  1977. "#endif",
  1978. "varying vec3 vViewPosition;",
  1979. "varying vec3 vNormal;"
  1980. ].join("\n"),
  1981. lights_fragment: [
  1982. "vec3 normal = normalize( vNormal );",
  1983. "vec3 viewPosition = normalize( vViewPosition );",
  1984. "vec4 mColor = vec4( diffuse, opacity );",
  1985. "vec4 mSpecular = vec4( specular, opacity );",
  1986. "#if MAX_POINT_LIGHTS > 0",
  1987. "vec4 pointDiffuse = vec4( 0.0 );",
  1988. "vec4 pointSpecular = vec4( 0.0 );",
  1989. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  1990. "vec3 pointVector = normalize( vPointLightVector[ i ] );",
  1991. "vec3 pointHalfVector = normalize( vPointLightVector[ i ] + vViewPosition );",
  1992. "float pointDotNormalHalf = dot( normal, pointHalfVector );",
  1993. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  1994. "float pointSpecularWeight = 0.0;",
  1995. "if ( pointDotNormalHalf >= 0.0 )",
  1996. "pointSpecularWeight = pow( pointDotNormalHalf, shininess );",
  1997. "pointDiffuse += mColor * pointDiffuseWeight;",
  1998. "pointSpecular += mSpecular * pointSpecularWeight;",
  1999. "}",
  2000. "#endif",
  2001. "#if MAX_DIR_LIGHTS > 0",
  2002. "vec4 dirDiffuse = vec4( 0.0 );",
  2003. "vec4 dirSpecular = vec4( 0.0 );" ,
  2004. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  2005. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  2006. "vec3 dirVector = normalize( lDirection.xyz );",
  2007. "vec3 dirHalfVector = normalize( lDirection.xyz + vViewPosition );",
  2008. "float dirDotNormalHalf = dot( normal, dirHalfVector );",
  2009. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  2010. "float dirSpecularWeight = 0.0;",
  2011. "if ( dirDotNormalHalf >= 0.0 )",
  2012. "dirSpecularWeight = pow( dirDotNormalHalf, shininess );",
  2013. "dirDiffuse += mColor * dirDiffuseWeight;",
  2014. "dirSpecular += mSpecular * dirSpecularWeight;",
  2015. "}",
  2016. "#endif",
  2017. "vec4 totalLight = vec4( ambient, opacity );",
  2018. "#if MAX_DIR_LIGHTS > 0",
  2019. "totalLight += dirDiffuse + dirSpecular;",
  2020. "#endif",
  2021. "#if MAX_POINT_LIGHTS > 0",
  2022. "totalLight += pointDiffuse + pointSpecular;",
  2023. "#endif",
  2024. "gl_FragColor = gl_FragColor * totalLight;"
  2025. ].join("\n"),
  2026. // VERTEX COLORS
  2027. color_pars_fragment: [
  2028. "#ifdef USE_COLOR",
  2029. "varying vec3 vColor;",
  2030. "#endif"
  2031. ].join("\n"),
  2032. color_fragment: [
  2033. "#ifdef USE_COLOR",
  2034. "gl_FragColor = gl_FragColor * vec4( vColor, opacity );",
  2035. "#endif"
  2036. ].join("\n"),
  2037. color_pars_vertex: [
  2038. "#ifdef USE_COLOR",
  2039. "varying vec3 vColor;",
  2040. "#endif"
  2041. ].join("\n"),
  2042. color_vertex: [
  2043. "#ifdef USE_COLOR",
  2044. "vColor = color;",
  2045. "#endif"
  2046. ].join("\n"),
  2047. // skinning
  2048. skinning_pars_vertex: [
  2049. "#ifdef USE_SKINNING",
  2050. "uniform mat4 boneGlobalMatrices[20];",
  2051. "#endif"
  2052. ].join("\n"),
  2053. skinning_vertex: [
  2054. "#ifdef USE_SKINNING",
  2055. "gl_Position = ( boneGlobalMatrices[ int( skinIndex.x ) ] * skinVertexA ) * skinWeight.x;",
  2056. "gl_Position += ( boneGlobalMatrices[ int( skinIndex.y ) ] * skinVertexB ) * skinWeight.y;",
  2057. // this doesn't work, no idea why
  2058. //"gl_Position = projectionMatrix * cameraInverseMatrix * objectMatrix * gl_Position;",
  2059. "gl_Position = projectionMatrix * viewMatrix * objectMatrix * gl_Position;",
  2060. "#else",
  2061. "gl_Position = projectionMatrix * mvPosition;",
  2062. "#endif"
  2063. ].join("\n")
  2064. };
  2065. THREE.UniformsLib = {
  2066. common: {
  2067. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  2068. "opacity" : { type: "f", value: 1.0 },
  2069. "map" : { type: "t", value: 0, texture: null },
  2070. "light_map" : { type: "t", value: 2, texture: null },
  2071. "env_map" : { type: "t", value: 1, texture: null },
  2072. "useRefract" : { type: "i", value: 0 },
  2073. "reflectivity" : { type: "f", value: 1.0 },
  2074. "refraction_ratio": { type: "f", value: 0.98 },
  2075. "combine" : { type: "i", value: 0 },
  2076. "fogDensity": { type: "f", value: 0.00025 },
  2077. "fogNear" : { type: "f", value: 1 },
  2078. "fogFar" : { type: "f", value: 2000 },
  2079. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  2080. },
  2081. lights: {
  2082. "enableLighting" : { type: "i", value: 1 },
  2083. "ambientLightColor" : { type: "fv", value: [] },
  2084. "directionalLightDirection" : { type: "fv", value: [] },
  2085. "directionalLightColor" : { type: "fv", value: [] },
  2086. "pointLightPosition" : { type: "fv", value: [] },
  2087. "pointLightColor" : { type: "fv", value: [] }
  2088. },
  2089. particle: {
  2090. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  2091. "opacity" : { type: "f", value: 1.0 },
  2092. "size" : { type: "f", value: 1.0 },
  2093. "map" : { type: "t", value: 0, texture: null },
  2094. "fogDensity": { type: "f", value: 0.00025 },
  2095. "fogNear" : { type: "f", value: 1 },
  2096. "fogFar" : { type: "f", value: 2000 },
  2097. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  2098. }
  2099. };
  2100. THREE.ShaderLib = {
  2101. 'depth': {
  2102. uniforms: { "mNear": { type: "f", value: 1.0 },
  2103. "mFar" : { type: "f", value: 2000.0 },
  2104. "opacity" : { type: "f", value: 1.0 }
  2105. },
  2106. fragment_shader: [
  2107. "uniform float mNear;",
  2108. "uniform float mFar;",
  2109. "uniform float opacity;",
  2110. "void main() {",
  2111. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  2112. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  2113. "gl_FragColor = vec4( vec3( color ), opacity );",
  2114. "}"
  2115. ].join("\n"),
  2116. vertex_shader: [
  2117. "void main() {",
  2118. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  2119. "}"
  2120. ].join("\n")
  2121. },
  2122. 'normal': {
  2123. uniforms: { "opacity" : { type: "f", value: 1.0 } },
  2124. fragment_shader: [
  2125. "uniform float opacity;",
  2126. "varying vec3 vNormal;",
  2127. "void main() {",
  2128. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  2129. "}"
  2130. ].join("\n"),
  2131. vertex_shader: [
  2132. "varying vec3 vNormal;",
  2133. "void main() {",
  2134. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  2135. "vNormal = normalize( normalMatrix * normal );",
  2136. "gl_Position = projectionMatrix * mvPosition;",
  2137. "}"
  2138. ].join("\n")
  2139. },
  2140. 'basic': {
  2141. uniforms: THREE.UniformsLib[ "common" ],
  2142. fragment_shader: [
  2143. "uniform vec3 diffuse;",
  2144. "uniform float opacity;",
  2145. THREE.Snippets[ "color_pars_fragment" ],
  2146. THREE.Snippets[ "map_pars_fragment" ],
  2147. THREE.Snippets[ "lightmap_pars_fragment" ],
  2148. THREE.Snippets[ "envmap_pars_fragment" ],
  2149. THREE.Snippets[ "fog_pars_fragment" ],
  2150. "void main() {",
  2151. "gl_FragColor = vec4( diffuse, opacity );",
  2152. THREE.Snippets[ "map_fragment" ],
  2153. THREE.Snippets[ "lightmap_fragment" ],
  2154. THREE.Snippets[ "color_fragment" ],
  2155. THREE.Snippets[ "envmap_fragment" ],
  2156. THREE.Snippets[ "fog_fragment" ],
  2157. "}"
  2158. ].join("\n"),
  2159. vertex_shader: [
  2160. THREE.Snippets[ "map_pars_vertex" ],
  2161. THREE.Snippets[ "lightmap_pars_vertex" ],
  2162. THREE.Snippets[ "envmap_pars_vertex" ],
  2163. THREE.Snippets[ "color_pars_vertex" ],
  2164. THREE.Snippets[ "skinning_pars_vertex" ],
  2165. "void main() {",
  2166. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  2167. THREE.Snippets[ "map_vertex" ],
  2168. THREE.Snippets[ "lightmap_vertex" ],
  2169. THREE.Snippets[ "envmap_vertex" ],
  2170. THREE.Snippets[ "color_vertex" ],
  2171. THREE.Snippets[ "skinning_vertex" ],
  2172. "}"
  2173. ].join("\n")
  2174. },
  2175. 'lambert': {
  2176. uniforms: Uniforms.merge( [ THREE.UniformsLib[ "common" ],
  2177. THREE.UniformsLib[ "lights" ] ] ),
  2178. fragment_shader: [
  2179. "uniform vec3 diffuse;",
  2180. "uniform float opacity;",
  2181. "varying vec3 vLightWeighting;",
  2182. THREE.Snippets[ "color_pars_fragment" ],
  2183. THREE.Snippets[ "map_pars_fragment" ],
  2184. THREE.Snippets[ "lightmap_pars_fragment" ],
  2185. THREE.Snippets[ "envmap_pars_fragment" ],
  2186. THREE.Snippets[ "fog_pars_fragment" ],
  2187. "void main() {",
  2188. "gl_FragColor = vec4( diffuse, opacity );",
  2189. "gl_FragColor = gl_FragColor * vec4( vLightWeighting, 1.0 );",
  2190. THREE.Snippets[ "map_fragment" ],
  2191. THREE.Snippets[ "lightmap_fragment" ],
  2192. THREE.Snippets[ "color_fragment" ],
  2193. THREE.Snippets[ "envmap_fragment" ],
  2194. THREE.Snippets[ "fog_fragment" ],
  2195. "}"
  2196. ].join("\n"),
  2197. vertex_shader: [
  2198. "varying vec3 vLightWeighting;",
  2199. THREE.Snippets[ "map_pars_vertex" ],
  2200. THREE.Snippets[ "lightmap_pars_vertex" ],
  2201. THREE.Snippets[ "envmap_pars_vertex" ],
  2202. THREE.Snippets[ "lights_pars_vertex" ],
  2203. THREE.Snippets[ "color_pars_vertex" ],
  2204. THREE.Snippets[ "skinning_pars_vertex" ],
  2205. "void main() {",
  2206. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  2207. THREE.Snippets[ "map_vertex" ],
  2208. THREE.Snippets[ "lightmap_vertex" ],
  2209. THREE.Snippets[ "envmap_vertex" ],
  2210. THREE.Snippets[ "color_vertex" ],
  2211. "vec3 transformedNormal = normalize( normalMatrix * normal );",
  2212. THREE.Snippets[ "lights_vertex" ],
  2213. THREE.Snippets[ "skinning_vertex" ],
  2214. "}"
  2215. ].join("\n")
  2216. },
  2217. 'phong': {
  2218. uniforms: Uniforms.merge( [ THREE.UniformsLib[ "common" ],
  2219. THREE.UniformsLib[ "lights" ],
  2220. { "ambient" : { type: "c", value: new THREE.Color( 0x050505 ) },
  2221. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  2222. "shininess": { type: "f", value: 30 }
  2223. }
  2224. ] ),
  2225. fragment_shader: [
  2226. "uniform vec3 diffuse;",
  2227. "uniform float opacity;",
  2228. "uniform vec3 ambient;",
  2229. "uniform vec3 specular;",
  2230. "uniform float shininess;",
  2231. "varying vec3 vLightWeighting;",
  2232. THREE.Snippets[ "color_pars_fragment" ],
  2233. THREE.Snippets[ "map_pars_fragment" ],
  2234. THREE.Snippets[ "lightmap_pars_fragment" ],
  2235. THREE.Snippets[ "envmap_pars_fragment" ],
  2236. THREE.Snippets[ "fog_pars_fragment" ],
  2237. THREE.Snippets[ "lights_pars_fragment" ],
  2238. "void main() {",
  2239. "gl_FragColor = vec4( vLightWeighting, 1.0 );",
  2240. THREE.Snippets[ "lights_fragment" ],
  2241. THREE.Snippets[ "map_fragment" ],
  2242. THREE.Snippets[ "lightmap_fragment" ],
  2243. THREE.Snippets[ "color_fragment" ],
  2244. THREE.Snippets[ "envmap_fragment" ],
  2245. THREE.Snippets[ "fog_fragment" ],
  2246. "}"
  2247. ].join("\n"),
  2248. vertex_shader: [
  2249. "#define PHONG",
  2250. "varying vec3 vLightWeighting;",
  2251. "varying vec3 vViewPosition;",
  2252. "varying vec3 vNormal;",
  2253. THREE.Snippets[ "map_pars_vertex" ],
  2254. THREE.Snippets[ "lightmap_pars_vertex" ],
  2255. THREE.Snippets[ "envmap_pars_vertex" ],
  2256. THREE.Snippets[ "lights_pars_vertex" ],
  2257. THREE.Snippets[ "color_pars_vertex" ],
  2258. THREE.Snippets[ "skinning_pars_vertex" ],
  2259. "void main() {",
  2260. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  2261. THREE.Snippets[ "map_vertex" ],
  2262. THREE.Snippets[ "lightmap_vertex" ],
  2263. THREE.Snippets[ "envmap_vertex" ],
  2264. THREE.Snippets[ "color_vertex" ],
  2265. "#ifndef USE_ENVMAP",
  2266. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  2267. "#endif",
  2268. "vViewPosition = cameraPosition - mPosition.xyz;",
  2269. "vec3 transformedNormal = normalize( normalMatrix * normal );",
  2270. "vNormal = transformedNormal;",
  2271. THREE.Snippets[ "lights_vertex" ],
  2272. THREE.Snippets[ "skinning_vertex" ],
  2273. "}"
  2274. ].join("\n")
  2275. },
  2276. 'particle_basic': {
  2277. uniforms: THREE.UniformsLib[ "particle" ],
  2278. fragment_shader: [
  2279. "uniform vec3 psColor;",
  2280. "uniform float opacity;",
  2281. THREE.Snippets[ "color_pars_fragment" ],
  2282. THREE.Snippets[ "map_particle_pars_fragment" ],
  2283. THREE.Snippets[ "fog_pars_fragment" ],
  2284. "void main() {",
  2285. "gl_FragColor = vec4( psColor, opacity );",
  2286. THREE.Snippets[ "map_particle_fragment" ],
  2287. THREE.Snippets[ "color_fragment" ],
  2288. THREE.Snippets[ "fog_fragment" ],
  2289. "}"
  2290. ].join("\n"),
  2291. vertex_shader: [
  2292. "uniform float size;",
  2293. THREE.Snippets[ "color_pars_vertex" ],
  2294. "void main() {",
  2295. THREE.Snippets[ "color_vertex" ],
  2296. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  2297. "gl_Position = projectionMatrix * mvPosition;",
  2298. "gl_PointSize = size;",
  2299. //"gl_PointSize = 10.0 + 6.0 * mvPosition.z;";
  2300. "}"
  2301. ].join("\n")
  2302. }
  2303. };