WebGLShaders.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author mikael emtinger / http://gomo.se/
  5. */
  6. THREE.ShaderChunk = {
  7. // FOG
  8. fog_pars_fragment: [
  9. "#ifdef USE_FOG",
  10. "uniform vec3 fogColor;",
  11. "#ifdef FOG_EXP2",
  12. "uniform float fogDensity;",
  13. "#else",
  14. "uniform float fogNear;",
  15. "uniform float fogFar;",
  16. "#endif",
  17. "#endif"
  18. ].join("\n"),
  19. fog_fragment: [
  20. "#ifdef USE_FOG",
  21. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  22. "#ifdef FOG_EXP2",
  23. "const float LOG2 = 1.442695;",
  24. "float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );",
  25. "fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );",
  26. "#else",
  27. "float fogFactor = smoothstep( fogNear, fogFar, depth );",
  28. "#endif",
  29. "gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );",
  30. "#endif"
  31. ].join("\n"),
  32. // ENVIRONMENT MAP
  33. envmap_pars_fragment: [
  34. "#ifdef USE_ENVMAP",
  35. "varying vec3 vReflect;",
  36. "uniform float reflectivity;",
  37. "uniform samplerCube envMap;",
  38. "uniform float flipEnvMap;",
  39. "uniform int combine;",
  40. "#endif"
  41. ].join("\n"),
  42. envmap_fragment: [
  43. "#ifdef USE_ENVMAP",
  44. "#ifdef DOUBLE_SIDED",
  45. "float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  46. "vec4 cubeColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * vReflect.x, vReflect.yz ) );",
  47. "#else",
  48. "vec4 cubeColor = textureCube( envMap, vec3( flipEnvMap * vReflect.x, vReflect.yz ) );",
  49. "#endif",
  50. "#ifdef GAMMA_INPUT",
  51. "cubeColor.xyz *= cubeColor.xyz;",
  52. "#endif",
  53. "if ( combine == 1 ) {",
  54. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, reflectivity );",
  55. "} else {",
  56. "gl_FragColor.xyz = gl_FragColor.xyz * cubeColor.xyz;",
  57. "}",
  58. "#endif"
  59. ].join("\n"),
  60. envmap_pars_vertex: [
  61. "#ifdef USE_ENVMAP",
  62. "varying vec3 vReflect;",
  63. "uniform float refractionRatio;",
  64. "uniform bool useRefract;",
  65. "#endif"
  66. ].join("\n"),
  67. envmap_vertex : [
  68. "#ifdef USE_ENVMAP",
  69. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  70. "vec3 nWorld = mat3( objectMatrix[ 0 ].xyz, objectMatrix[ 1 ].xyz, objectMatrix[ 2 ].xyz ) * normal;",
  71. "if ( useRefract ) {",
  72. "vReflect = refract( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ), refractionRatio );",
  73. "} else {",
  74. "vReflect = reflect( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ) );",
  75. "}",
  76. "#endif"
  77. ].join("\n"),
  78. // COLOR MAP (particles)
  79. map_particle_pars_fragment: [
  80. "#ifdef USE_MAP",
  81. "uniform sampler2D map;",
  82. "#endif"
  83. ].join("\n"),
  84. map_particle_fragment: [
  85. "#ifdef USE_MAP",
  86. "gl_FragColor = gl_FragColor * texture2D( map, gl_PointCoord );",
  87. "#endif"
  88. ].join("\n"),
  89. // COLOR MAP (triangles)
  90. map_pars_vertex: [
  91. "#ifdef USE_MAP",
  92. "varying vec2 vUv;",
  93. "uniform vec4 offsetRepeat;",
  94. "#endif"
  95. ].join("\n"),
  96. map_pars_fragment: [
  97. "#ifdef USE_MAP",
  98. "varying vec2 vUv;",
  99. "uniform sampler2D map;",
  100. "#endif"
  101. ].join("\n"),
  102. map_vertex: [
  103. "#ifdef USE_MAP",
  104. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  105. "#endif"
  106. ].join("\n"),
  107. map_fragment: [
  108. "#ifdef USE_MAP",
  109. "#ifdef GAMMA_INPUT",
  110. "vec4 texelColor = texture2D( map, vUv );",
  111. "texelColor.xyz *= texelColor.xyz;",
  112. "gl_FragColor = gl_FragColor * texelColor;",
  113. "#else",
  114. "gl_FragColor = gl_FragColor * texture2D( map, vUv );",
  115. "#endif",
  116. "#endif"
  117. ].join("\n"),
  118. // LIGHT MAP
  119. lightmap_pars_fragment: [
  120. "#ifdef USE_LIGHTMAP",
  121. "varying vec2 vUv2;",
  122. "uniform sampler2D lightMap;",
  123. "#endif"
  124. ].join("\n"),
  125. lightmap_pars_vertex: [
  126. "#ifdef USE_LIGHTMAP",
  127. "varying vec2 vUv2;",
  128. "#endif"
  129. ].join("\n"),
  130. lightmap_fragment: [
  131. "#ifdef USE_LIGHTMAP",
  132. "gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );",
  133. "#endif"
  134. ].join("\n"),
  135. lightmap_vertex: [
  136. "#ifdef USE_LIGHTMAP",
  137. "vUv2 = uv2;",
  138. "#endif"
  139. ].join("\n"),
  140. // LIGHTS LAMBERT
  141. lights_lambert_pars_vertex: [
  142. "uniform vec3 ambient;",
  143. "uniform vec3 diffuse;",
  144. "uniform vec3 emissive;",
  145. "uniform vec3 ambientLightColor;",
  146. "#if MAX_DIR_LIGHTS > 0",
  147. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  148. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  149. "#endif",
  150. "#if MAX_POINT_LIGHTS > 0",
  151. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  152. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  153. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  154. "#endif",
  155. "#ifdef WRAP_AROUND",
  156. "uniform vec3 wrapRGB;",
  157. "#endif",
  158. ].join("\n"),
  159. lights_lambert_vertex: [
  160. "vLightFront = vec3( 0.0 );",
  161. "#ifdef DOUBLE_SIDED",
  162. "vLightBack = vec3( 0.0 );",
  163. "#endif",
  164. "transformedNormal = normalize( transformedNormal );",
  165. "#if MAX_DIR_LIGHTS > 0",
  166. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  167. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  168. "vec3 dirVector = normalize( lDirection.xyz );",
  169. "float dotProduct = dot( transformedNormal, dirVector );",
  170. "vec3 directionalLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  171. "#ifdef DOUBLE_SIDED",
  172. "vec3 directionalLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  173. "#ifdef WRAP_AROUND",
  174. "vec3 directionalLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  175. "#endif",
  176. "#endif",
  177. "#ifdef WRAP_AROUND",
  178. "vec3 directionalLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  179. "directionalLightWeighting = mix( directionalLightWeighting, directionalLightWeightingHalf, wrapRGB );",
  180. "#ifdef DOUBLE_SIDED",
  181. "directionalLightWeightingBack = mix( directionalLightWeightingBack, directionalLightWeightingHalfBack, wrapRGB );",
  182. "#endif",
  183. "#endif",
  184. "vLightFront += directionalLightColor[ i ] * directionalLightWeighting;",
  185. "#ifdef DOUBLE_SIDED",
  186. "vLightBack += directionalLightColor[ i ] * directionalLightWeightingBack;",
  187. "#endif",
  188. "}",
  189. "#endif",
  190. "#if MAX_POINT_LIGHTS > 0",
  191. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  192. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  193. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  194. "float lDistance = 1.0;",
  195. "if ( pointLightDistance[ i ] > 0.0 )",
  196. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  197. "lVector = normalize( lVector );",
  198. "float dotProduct = dot( transformedNormal, lVector );",
  199. "vec3 pointLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  200. "#ifdef DOUBLE_SIDED",
  201. "vec3 pointLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  202. "#ifdef WRAP_AROUND",
  203. "vec3 pointLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  204. "#endif",
  205. "#endif",
  206. "#ifdef WRAP_AROUND",
  207. "vec3 pointLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  208. "pointLightWeighting = mix( pointLightWeighting, pointLightWeightingHalf, wrapRGB );",
  209. "#ifdef DOUBLE_SIDED",
  210. "pointLightWeightingBack = mix( pointLightWeightingBack, pointLightWeightingHalfBack, wrapRGB );",
  211. "#endif",
  212. "#endif",
  213. "vLightFront += pointLightColor[ i ] * pointLightWeighting * lDistance;",
  214. "#ifdef DOUBLE_SIDED",
  215. "vLightBack += pointLightColor[ i ] * pointLightWeightingBack * lDistance;",
  216. "#endif",
  217. "}",
  218. "#endif",
  219. "vLightFront = vLightFront * diffuse + ambient * ambientLightColor + emissive;",
  220. "#ifdef DOUBLE_SIDED",
  221. "vLightBack = vLightBack * diffuse + ambient * ambientLightColor + emissive;",
  222. "#endif",
  223. ].join("\n"),
  224. // LIGHTS PHONG
  225. lights_phong_pars_vertex: [
  226. "#if MAX_POINT_LIGHTS > 0",
  227. "#ifndef PHONG_PER_PIXEL",
  228. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  229. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  230. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  231. "#endif",
  232. "#endif"
  233. ].join("\n"),
  234. lights_phong_vertex: [
  235. "#if MAX_POINT_LIGHTS > 0",
  236. "#ifndef PHONG_PER_PIXEL",
  237. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  238. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  239. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  240. "float lDistance = 1.0;",
  241. "if ( pointLightDistance[ i ] > 0.0 )",
  242. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  243. "vPointLight[ i ] = vec4( lVector, lDistance );",
  244. "}",
  245. "#endif",
  246. "#endif"
  247. ].join("\n"),
  248. lights_phong_pars_fragment: [
  249. "uniform vec3 ambientLightColor;",
  250. "#if MAX_DIR_LIGHTS > 0",
  251. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  252. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  253. "#endif",
  254. "#if MAX_POINT_LIGHTS > 0",
  255. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  256. "#ifdef PHONG_PER_PIXEL",
  257. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  258. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  259. "#else",
  260. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  261. "#endif",
  262. "#endif",
  263. "#ifdef WRAP_AROUND",
  264. "uniform vec3 wrapRGB;",
  265. "#endif",
  266. "varying vec3 vViewPosition;",
  267. "varying vec3 vNormal;"
  268. ].join("\n"),
  269. lights_phong_fragment: [
  270. "vec3 normal = normalize( vNormal );",
  271. "vec3 viewPosition = normalize( vViewPosition );",
  272. "#ifdef DOUBLE_SIDED",
  273. "normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  274. "#endif",
  275. "#if MAX_POINT_LIGHTS > 0",
  276. "vec3 pointDiffuse = vec3( 0.0 );",
  277. "vec3 pointSpecular = vec3( 0.0 );",
  278. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  279. "#ifdef PHONG_PER_PIXEL",
  280. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  281. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  282. "float lDistance = 1.0;",
  283. "if ( pointLightDistance[ i ] > 0.0 )",
  284. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  285. "lVector = normalize( lVector );",
  286. "#else",
  287. "vec3 lVector = normalize( vPointLight[ i ].xyz );",
  288. "float lDistance = vPointLight[ i ].w;",
  289. "#endif",
  290. // diffuse
  291. "float dotProduct = dot( normal, lVector );",
  292. "#ifdef WRAP_AROUND",
  293. "float pointDiffuseWeightFull = max( dotProduct, 0.0 );",
  294. "float pointDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  295. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  296. "#else",
  297. "float pointDiffuseWeight = max( dotProduct, 0.0 );",
  298. "#endif",
  299. "pointDiffuse += diffuse * pointLightColor[ i ] * pointDiffuseWeight * lDistance;",
  300. // specular
  301. "vec3 pointHalfVector = normalize( lVector + viewPosition );",
  302. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  303. "float pointSpecularWeight = max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  304. "#ifdef PHYSICALLY_BASED_SHADING",
  305. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( dot( lVector, pointHalfVector ), 5.0 );",
  306. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance;",
  307. "#else",
  308. "pointSpecular += specular * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance;",
  309. "#endif",
  310. "}",
  311. "#endif",
  312. "#if MAX_DIR_LIGHTS > 0",
  313. "vec3 dirDiffuse = vec3( 0.0 );",
  314. "vec3 dirSpecular = vec3( 0.0 );" ,
  315. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  316. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  317. "vec3 dirVector = normalize( lDirection.xyz );",
  318. // diffuse
  319. "float dotProduct = dot( normal, dirVector );",
  320. "#ifdef WRAP_AROUND",
  321. "float dirDiffuseWeightFull = max( dotProduct, 0.0 );",
  322. "float dirDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  323. "vec3 dirDiffuseWeight = mix( vec3( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), wrapRGB );",
  324. "#else",
  325. "float dirDiffuseWeight = max( dotProduct, 0.0 );",
  326. "#endif",
  327. "dirDiffuse += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
  328. // specular
  329. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  330. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  331. "float dirSpecularWeight = max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  332. "#ifdef PHYSICALLY_BASED_SHADING",
  333. /*
  334. // fresnel term from skin shader
  335. "const float F0 = 0.128;",
  336. "float base = 1.0 - dot( viewPosition, dirHalfVector );",
  337. "float exponential = pow( base, 5.0 );",
  338. "float fresnel = exponential + F0 * ( 1.0 - exponential );",
  339. */
  340. /*
  341. // fresnel term from fresnel shader
  342. "const float mFresnelBias = 0.08;",
  343. "const float mFresnelScale = 0.3;",
  344. "const float mFresnelPower = 5.0;",
  345. "float fresnel = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( -viewPosition ), normal ), mFresnelPower );",
  346. */
  347. // normalization factor
  348. //float specularNormalization = ( shininess + 2.0 ) / 8.0;
  349. //"dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;",
  350. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( dot( dirVector, dirHalfVector ), 5.0 );",
  351. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight;",
  352. "#else",
  353. "dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight;",
  354. "#endif",
  355. "}",
  356. "#endif",
  357. "vec3 totalDiffuse = vec3( 0.0 );",
  358. "vec3 totalSpecular = vec3( 0.0 );",
  359. "#if MAX_DIR_LIGHTS > 0",
  360. "totalDiffuse += dirDiffuse;",
  361. "totalSpecular += dirSpecular;",
  362. "#endif",
  363. "#if MAX_POINT_LIGHTS > 0",
  364. "totalDiffuse += pointDiffuse;",
  365. "totalSpecular += pointSpecular;",
  366. "#endif",
  367. "#ifdef METAL",
  368. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient + totalSpecular );",
  369. "#else",
  370. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient ) + totalSpecular;",
  371. "#endif"
  372. ].join("\n"),
  373. // VERTEX COLORS
  374. color_pars_fragment: [
  375. "#ifdef USE_COLOR",
  376. "varying vec3 vColor;",
  377. "#endif"
  378. ].join("\n"),
  379. color_fragment: [
  380. "#ifdef USE_COLOR",
  381. "gl_FragColor = gl_FragColor * vec4( vColor, opacity );",
  382. "#endif"
  383. ].join("\n"),
  384. color_pars_vertex: [
  385. "#ifdef USE_COLOR",
  386. "varying vec3 vColor;",
  387. "#endif"
  388. ].join("\n"),
  389. color_vertex: [
  390. "#ifdef USE_COLOR",
  391. "#ifdef GAMMA_INPUT",
  392. "vColor = color * color;",
  393. "#else",
  394. "vColor = color;",
  395. "#endif",
  396. "#endif"
  397. ].join("\n"),
  398. // SKINNING
  399. skinning_pars_vertex: [
  400. "#ifdef USE_SKINNING",
  401. "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
  402. "#endif"
  403. ].join("\n"),
  404. skinning_vertex: [
  405. "#ifdef USE_SKINNING",
  406. "gl_Position = ( boneGlobalMatrices[ int( skinIndex.x ) ] * skinVertexA ) * skinWeight.x;",
  407. "gl_Position += ( boneGlobalMatrices[ int( skinIndex.y ) ] * skinVertexB ) * skinWeight.y;",
  408. "gl_Position = projectionMatrix * modelViewMatrix * gl_Position;",
  409. "#endif"
  410. ].join("\n"),
  411. // MORPHING
  412. morphtarget_pars_vertex: [
  413. "#ifdef USE_MORPHTARGETS",
  414. "#ifndef USE_MORPHNORMALS",
  415. "uniform float morphTargetInfluences[ 8 ];",
  416. "#else",
  417. "uniform float morphTargetInfluences[ 4 ];",
  418. "#endif",
  419. "#endif"
  420. ].join("\n"),
  421. morphtarget_vertex: [
  422. "#ifdef USE_MORPHTARGETS",
  423. "vec3 morphed = vec3( 0.0 );",
  424. "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
  425. "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
  426. "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
  427. "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
  428. "#ifndef USE_MORPHNORMALS",
  429. "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
  430. "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
  431. "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
  432. "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
  433. "#endif",
  434. "morphed += position;",
  435. "gl_Position = projectionMatrix * modelViewMatrix * vec4( morphed, 1.0 );",
  436. "#endif"
  437. ].join("\n"),
  438. default_vertex : [
  439. "#ifndef USE_MORPHTARGETS",
  440. "#ifndef USE_SKINNING",
  441. "gl_Position = projectionMatrix * mvPosition;",
  442. "#endif",
  443. "#endif"
  444. ].join("\n"),
  445. morphnormal_vertex: [
  446. "#ifdef USE_MORPHNORMALS",
  447. "vec3 morphedNormal = vec3( 0.0 );",
  448. "morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];",
  449. "morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];",
  450. "morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];",
  451. "morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];",
  452. "morphedNormal += normal;",
  453. "vec3 transformedNormal = normalMatrix * morphedNormal;",
  454. "#else",
  455. "vec3 transformedNormal = normalMatrix * normal;",
  456. "#endif"
  457. ].join("\n"),
  458. // SHADOW MAP
  459. // based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples
  460. // http://spidergl.org/example.php?id=6
  461. // http://fabiensanglard.net/shadowmapping
  462. shadowmap_pars_fragment: [
  463. "#ifdef USE_SHADOWMAP",
  464. "uniform sampler2D shadowMap[ MAX_SHADOWS ];",
  465. "uniform vec2 shadowMapSize[ MAX_SHADOWS ];",
  466. "uniform float shadowDarkness[ MAX_SHADOWS ];",
  467. "uniform float shadowBias[ MAX_SHADOWS ];",
  468. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  469. "float unpackDepth( const in vec4 rgba_depth ) {",
  470. "const vec4 bit_shift = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );",
  471. "float depth = dot( rgba_depth, bit_shift );",
  472. "return depth;",
  473. "}",
  474. "#endif"
  475. ].join("\n"),
  476. shadowmap_fragment: [
  477. "#ifdef USE_SHADOWMAP",
  478. "#ifdef SHADOWMAP_DEBUG",
  479. "vec3 frustumColors[3];",
  480. "frustumColors[0] = vec3( 1.0, 0.5, 0.0 );",
  481. "frustumColors[1] = vec3( 0.0, 1.0, 0.8 );",
  482. "frustumColors[2] = vec3( 0.0, 0.5, 1.0 );",
  483. "#endif",
  484. "#ifdef SHADOWMAP_CASCADE",
  485. "int inFrustumCount = 0;",
  486. "#endif",
  487. "float fDepth;",
  488. "vec3 shadowColor = vec3( 1.0 );",
  489. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  490. "vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;",
  491. // "if ( something && something )" breaks ATI OpenGL shader compiler
  492. // "if ( all( something, something ) )" using this instead
  493. "bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );",
  494. "bool inFrustum = all( inFrustumVec );",
  495. // don't shadow pixels outside of light frustum
  496. // use just first frustum (for cascades)
  497. // don't shadow pixels behind far plane of light frustum
  498. "#ifdef SHADOWMAP_CASCADE",
  499. "inFrustumCount += int( inFrustum );",
  500. "bvec3 frustumTestVec = bvec3( inFrustum, inFrustumCount == 1, shadowCoord.z <= 1.0 );",
  501. "#else",
  502. "bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );",
  503. "#endif",
  504. "bool frustumTest = all( frustumTestVec );",
  505. "if ( frustumTest ) {",
  506. "shadowCoord.z += shadowBias[ i ];",
  507. "#ifdef SHADOWMAP_SOFT",
  508. // Percentage-close filtering
  509. // (9 pixel kernel)
  510. // http://fabiensanglard.net/shadowmappingPCF/
  511. "float shadow = 0.0;",
  512. /*
  513. // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
  514. // must enroll loop manually
  515. "for ( float y = -1.25; y <= 1.25; y += 1.25 )",
  516. "for ( float x = -1.25; x <= 1.25; x += 1.25 ) {",
  517. "vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );",
  518. // doesn't seem to produce any noticeable visual difference compared to simple "texture2D" lookup
  519. //"vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );",
  520. "float fDepth = unpackDepth( rgbaDepth );",
  521. "if ( fDepth < shadowCoord.z )",
  522. "shadow += 1.0;",
  523. "}",
  524. "shadow /= 9.0;",
  525. */
  526. "const float shadowDelta = 1.0 / 9.0;",
  527. "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
  528. "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
  529. "float dx0 = -1.25 * xPixelOffset;",
  530. "float dy0 = -1.25 * yPixelOffset;",
  531. "float dx1 = 1.25 * xPixelOffset;",
  532. "float dy1 = 1.25 * yPixelOffset;",
  533. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
  534. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  535. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
  536. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  537. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
  538. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  539. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
  540. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  541. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
  542. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  543. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
  544. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  545. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
  546. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  547. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
  548. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  549. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
  550. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  551. "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
  552. "#else",
  553. "vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );",
  554. "float fDepth = unpackDepth( rgbaDepth );",
  555. "if ( fDepth < shadowCoord.z )",
  556. // spot with multiple shadows is darker
  557. "shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );",
  558. // spot with multiple shadows has the same color as single shadow spot
  559. //"shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );",
  560. "#endif",
  561. "}",
  562. "#ifdef SHADOWMAP_DEBUG",
  563. "#ifdef SHADOWMAP_CASCADE",
  564. "if ( inFrustum && inFrustumCount == 1 ) gl_FragColor.xyz *= frustumColors[ i ];",
  565. "#else",
  566. "if ( inFrustum ) gl_FragColor.xyz *= frustumColors[ i ];",
  567. "#endif",
  568. "#endif",
  569. "}",
  570. "#ifdef GAMMA_OUTPUT",
  571. "shadowColor *= shadowColor;",
  572. "#endif",
  573. "gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;",
  574. "#endif"
  575. ].join("\n"),
  576. shadowmap_pars_vertex: [
  577. "#ifdef USE_SHADOWMAP",
  578. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  579. "uniform mat4 shadowMatrix[ MAX_SHADOWS ];",
  580. "#endif"
  581. ].join("\n"),
  582. shadowmap_vertex: [
  583. "#ifdef USE_SHADOWMAP",
  584. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  585. "#ifdef USE_MORPHTARGETS",
  586. "vShadowCoord[ i ] = shadowMatrix[ i ] * objectMatrix * vec4( morphed, 1.0 );",
  587. "#else",
  588. "vShadowCoord[ i ] = shadowMatrix[ i ] * objectMatrix * vec4( position, 1.0 );",
  589. "#endif",
  590. "}",
  591. "#endif"
  592. ].join("\n"),
  593. // ALPHATEST
  594. alphatest_fragment: [
  595. "#ifdef ALPHATEST",
  596. "if ( gl_FragColor.a < ALPHATEST ) discard;",
  597. "#endif"
  598. ].join("\n"),
  599. // LINEAR SPACE
  600. linear_to_gamma_fragment: [
  601. "#ifdef GAMMA_OUTPUT",
  602. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  603. "#endif"
  604. ].join("\n"),
  605. };
  606. THREE.UniformsUtils = {
  607. merge: function ( uniforms ) {
  608. var u, p, tmp, merged = {};
  609. for ( u = 0; u < uniforms.length; u++ ) {
  610. tmp = this.clone( uniforms[ u ] );
  611. for ( p in tmp ) {
  612. merged[ p ] = tmp[ p ];
  613. }
  614. }
  615. return merged;
  616. },
  617. clone: function ( uniforms_src ) {
  618. var u, p, parameter, parameter_src, uniforms_dst = {};
  619. for ( u in uniforms_src ) {
  620. uniforms_dst[ u ] = {};
  621. for ( p in uniforms_src[ u ] ) {
  622. parameter_src = uniforms_src[ u ][ p ];
  623. if ( parameter_src instanceof THREE.Color ||
  624. parameter_src instanceof THREE.Vector2 ||
  625. parameter_src instanceof THREE.Vector3 ||
  626. parameter_src instanceof THREE.Vector4 ||
  627. parameter_src instanceof THREE.Matrix4 ||
  628. parameter_src instanceof THREE.Texture ) {
  629. uniforms_dst[ u ][ p ] = parameter_src.clone();
  630. } else if ( parameter_src instanceof Array ) {
  631. uniforms_dst[ u ][ p ] = parameter_src.slice();
  632. } else {
  633. uniforms_dst[ u ][ p ] = parameter_src;
  634. }
  635. }
  636. }
  637. return uniforms_dst;
  638. }
  639. };
  640. THREE.UniformsLib = {
  641. common: {
  642. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  643. "opacity" : { type: "f", value: 1.0 },
  644. "map" : { type: "t", value: 0, texture: null },
  645. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  646. "lightMap" : { type: "t", value: 2, texture: null },
  647. "envMap" : { type: "t", value: 1, texture: null },
  648. "flipEnvMap" : { type: "f", value: -1 },
  649. "useRefract" : { type: "i", value: 0 },
  650. "reflectivity" : { type: "f", value: 1.0 },
  651. "refractionRatio" : { type: "f", value: 0.98 },
  652. "combine" : { type: "i", value: 0 },
  653. "morphTargetInfluences" : { type: "f", value: 0 }
  654. },
  655. fog : {
  656. "fogDensity" : { type: "f", value: 0.00025 },
  657. "fogNear" : { type: "f", value: 1 },
  658. "fogFar" : { type: "f", value: 2000 },
  659. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  660. },
  661. lights: {
  662. "ambientLightColor" : { type: "fv", value: [] },
  663. "directionalLightDirection" : { type: "fv", value: [] },
  664. "directionalLightColor" : { type: "fv", value: [] },
  665. "pointLightColor" : { type: "fv", value: [] },
  666. "pointLightPosition" : { type: "fv", value: [] },
  667. "pointLightDistance" : { type: "fv1", value: [] }
  668. },
  669. particle: {
  670. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  671. "opacity" : { type: "f", value: 1.0 },
  672. "size" : { type: "f", value: 1.0 },
  673. "scale" : { type: "f", value: 1.0 },
  674. "map" : { type: "t", value: 0, texture: null },
  675. "fogDensity" : { type: "f", value: 0.00025 },
  676. "fogNear" : { type: "f", value: 1 },
  677. "fogFar" : { type: "f", value: 2000 },
  678. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  679. },
  680. shadowmap: {
  681. "shadowMap": { type: "tv", value: 6, texture: [] },
  682. "shadowMapSize": { type: "v2v", value: [] },
  683. "shadowBias" : { type: "fv1", value: [] },
  684. "shadowDarkness": { type: "fv1", value: [] },
  685. "shadowMatrix" : { type: "m4v", value: [] },
  686. }
  687. };
  688. THREE.ShaderLib = {
  689. 'depth': {
  690. uniforms: {
  691. "mNear": { type: "f", value: 1.0 },
  692. "mFar" : { type: "f", value: 2000.0 },
  693. "opacity" : { type: "f", value: 1.0 }
  694. },
  695. vertexShader: [
  696. "void main() {",
  697. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  698. "}"
  699. ].join("\n"),
  700. fragmentShader: [
  701. "uniform float mNear;",
  702. "uniform float mFar;",
  703. "uniform float opacity;",
  704. "void main() {",
  705. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  706. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  707. "gl_FragColor = vec4( vec3( color ), opacity );",
  708. "}"
  709. ].join("\n")
  710. },
  711. 'normal': {
  712. uniforms: {
  713. "opacity" : { type: "f", value: 1.0 }
  714. },
  715. vertexShader: [
  716. "varying vec3 vNormal;",
  717. "void main() {",
  718. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  719. "vNormal = normalize( normalMatrix * normal );",
  720. "gl_Position = projectionMatrix * mvPosition;",
  721. "}"
  722. ].join("\n"),
  723. fragmentShader: [
  724. "uniform float opacity;",
  725. "varying vec3 vNormal;",
  726. "void main() {",
  727. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  728. "}"
  729. ].join("\n")
  730. },
  731. 'basic': {
  732. uniforms: THREE.UniformsUtils.merge( [
  733. THREE.UniformsLib[ "common" ],
  734. THREE.UniformsLib[ "fog" ],
  735. THREE.UniformsLib[ "shadowmap" ]
  736. ] ),
  737. vertexShader: [
  738. THREE.ShaderChunk[ "map_pars_vertex" ],
  739. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  740. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  741. THREE.ShaderChunk[ "color_pars_vertex" ],
  742. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  743. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  744. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  745. "void main() {",
  746. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  747. THREE.ShaderChunk[ "map_vertex" ],
  748. THREE.ShaderChunk[ "lightmap_vertex" ],
  749. THREE.ShaderChunk[ "envmap_vertex" ],
  750. THREE.ShaderChunk[ "color_vertex" ],
  751. THREE.ShaderChunk[ "skinning_vertex" ],
  752. THREE.ShaderChunk[ "morphtarget_vertex" ],
  753. THREE.ShaderChunk[ "default_vertex" ],
  754. THREE.ShaderChunk[ "shadowmap_vertex" ],
  755. "}"
  756. ].join("\n"),
  757. fragmentShader: [
  758. "uniform vec3 diffuse;",
  759. "uniform float opacity;",
  760. THREE.ShaderChunk[ "color_pars_fragment" ],
  761. THREE.ShaderChunk[ "map_pars_fragment" ],
  762. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  763. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  764. THREE.ShaderChunk[ "fog_pars_fragment" ],
  765. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  766. "void main() {",
  767. "gl_FragColor = vec4( diffuse, opacity );",
  768. THREE.ShaderChunk[ "map_fragment" ],
  769. THREE.ShaderChunk[ "alphatest_fragment" ],
  770. THREE.ShaderChunk[ "lightmap_fragment" ],
  771. THREE.ShaderChunk[ "color_fragment" ],
  772. THREE.ShaderChunk[ "envmap_fragment" ],
  773. THREE.ShaderChunk[ "shadowmap_fragment" ],
  774. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  775. THREE.ShaderChunk[ "fog_fragment" ],
  776. "}"
  777. ].join("\n")
  778. },
  779. 'lambert': {
  780. uniforms: THREE.UniformsUtils.merge( [
  781. THREE.UniformsLib[ "common" ],
  782. THREE.UniformsLib[ "fog" ],
  783. THREE.UniformsLib[ "lights" ],
  784. THREE.UniformsLib[ "shadowmap" ],
  785. {
  786. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  787. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  788. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  789. }
  790. ] ),
  791. vertexShader: [
  792. "varying vec3 vLightFront;",
  793. "#ifdef DOUBLE_SIDED",
  794. "varying vec3 vLightBack;",
  795. "#endif",
  796. THREE.ShaderChunk[ "map_pars_vertex" ],
  797. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  798. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  799. THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
  800. THREE.ShaderChunk[ "color_pars_vertex" ],
  801. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  802. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  803. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  804. "void main() {",
  805. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  806. THREE.ShaderChunk[ "map_vertex" ],
  807. THREE.ShaderChunk[ "lightmap_vertex" ],
  808. THREE.ShaderChunk[ "envmap_vertex" ],
  809. THREE.ShaderChunk[ "color_vertex" ],
  810. THREE.ShaderChunk[ "morphnormal_vertex" ],
  811. THREE.ShaderChunk[ "lights_lambert_vertex" ],
  812. THREE.ShaderChunk[ "skinning_vertex" ],
  813. THREE.ShaderChunk[ "morphtarget_vertex" ],
  814. THREE.ShaderChunk[ "default_vertex" ],
  815. THREE.ShaderChunk[ "shadowmap_vertex" ],
  816. "}"
  817. ].join("\n"),
  818. fragmentShader: [
  819. "uniform float opacity;",
  820. "varying vec3 vLightFront;",
  821. "#ifdef DOUBLE_SIDED",
  822. "varying vec3 vLightBack;",
  823. "#endif",
  824. THREE.ShaderChunk[ "color_pars_fragment" ],
  825. THREE.ShaderChunk[ "map_pars_fragment" ],
  826. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  827. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  828. THREE.ShaderChunk[ "fog_pars_fragment" ],
  829. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  830. "void main() {",
  831. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  832. THREE.ShaderChunk[ "map_fragment" ],
  833. THREE.ShaderChunk[ "alphatest_fragment" ],
  834. "#ifdef DOUBLE_SIDED",
  835. //"float isFront = float( gl_FrontFacing );",
  836. //"gl_FragColor.xyz *= isFront * vLightFront + ( 1.0 - isFront ) * vLightBack;",
  837. "if ( gl_FrontFacing )",
  838. "gl_FragColor.xyz *= vLightFront;",
  839. "else",
  840. "gl_FragColor.xyz *= vLightBack;",
  841. "#else",
  842. "gl_FragColor.xyz *= vLightFront;",
  843. "#endif",
  844. THREE.ShaderChunk[ "lightmap_fragment" ],
  845. THREE.ShaderChunk[ "color_fragment" ],
  846. THREE.ShaderChunk[ "envmap_fragment" ],
  847. THREE.ShaderChunk[ "shadowmap_fragment" ],
  848. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  849. THREE.ShaderChunk[ "fog_fragment" ],
  850. "}"
  851. ].join("\n")
  852. },
  853. 'phong': {
  854. uniforms: THREE.UniformsUtils.merge( [
  855. THREE.UniformsLib[ "common" ],
  856. THREE.UniformsLib[ "fog" ],
  857. THREE.UniformsLib[ "lights" ],
  858. THREE.UniformsLib[ "shadowmap" ],
  859. {
  860. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  861. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  862. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  863. "shininess": { type: "f", value: 30 },
  864. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  865. }
  866. ] ),
  867. vertexShader: [
  868. "varying vec3 vViewPosition;",
  869. "varying vec3 vNormal;",
  870. THREE.ShaderChunk[ "map_pars_vertex" ],
  871. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  872. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  873. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  874. THREE.ShaderChunk[ "color_pars_vertex" ],
  875. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  876. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  877. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  878. "void main() {",
  879. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  880. THREE.ShaderChunk[ "map_vertex" ],
  881. THREE.ShaderChunk[ "lightmap_vertex" ],
  882. THREE.ShaderChunk[ "envmap_vertex" ],
  883. THREE.ShaderChunk[ "color_vertex" ],
  884. "#ifndef USE_ENVMAP",
  885. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  886. "#endif",
  887. "vViewPosition = -mvPosition.xyz;",
  888. THREE.ShaderChunk[ "morphnormal_vertex" ],
  889. "vNormal = transformedNormal;",
  890. THREE.ShaderChunk[ "lights_phong_vertex" ],
  891. THREE.ShaderChunk[ "skinning_vertex" ],
  892. THREE.ShaderChunk[ "morphtarget_vertex" ],
  893. THREE.ShaderChunk[ "default_vertex" ],
  894. THREE.ShaderChunk[ "shadowmap_vertex" ],
  895. "}"
  896. ].join("\n"),
  897. fragmentShader: [
  898. "uniform vec3 diffuse;",
  899. "uniform float opacity;",
  900. "uniform vec3 ambient;",
  901. "uniform vec3 emissive;",
  902. "uniform vec3 specular;",
  903. "uniform float shininess;",
  904. THREE.ShaderChunk[ "color_pars_fragment" ],
  905. THREE.ShaderChunk[ "map_pars_fragment" ],
  906. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  907. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  908. THREE.ShaderChunk[ "fog_pars_fragment" ],
  909. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  910. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  911. "void main() {",
  912. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  913. THREE.ShaderChunk[ "map_fragment" ],
  914. THREE.ShaderChunk[ "alphatest_fragment" ],
  915. THREE.ShaderChunk[ "lights_phong_fragment" ],
  916. THREE.ShaderChunk[ "lightmap_fragment" ],
  917. THREE.ShaderChunk[ "color_fragment" ],
  918. THREE.ShaderChunk[ "envmap_fragment" ],
  919. THREE.ShaderChunk[ "shadowmap_fragment" ],
  920. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  921. THREE.ShaderChunk[ "fog_fragment" ],
  922. "}"
  923. ].join("\n")
  924. },
  925. 'particle_basic': {
  926. uniforms: THREE.UniformsUtils.merge( [
  927. THREE.UniformsLib[ "particle" ],
  928. THREE.UniformsLib[ "shadowmap" ]
  929. ] ),
  930. vertexShader: [
  931. "uniform float size;",
  932. "uniform float scale;",
  933. THREE.ShaderChunk[ "color_pars_vertex" ],
  934. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  935. "void main() {",
  936. THREE.ShaderChunk[ "color_vertex" ],
  937. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  938. "#ifdef USE_SIZEATTENUATION",
  939. "gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  940. "#else",
  941. "gl_PointSize = size;",
  942. "#endif",
  943. "gl_Position = projectionMatrix * mvPosition;",
  944. THREE.ShaderChunk[ "shadowmap_vertex" ],
  945. "}"
  946. ].join("\n"),
  947. fragmentShader: [
  948. "uniform vec3 psColor;",
  949. "uniform float opacity;",
  950. THREE.ShaderChunk[ "color_pars_fragment" ],
  951. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  952. THREE.ShaderChunk[ "fog_pars_fragment" ],
  953. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  954. "void main() {",
  955. "gl_FragColor = vec4( psColor, opacity );",
  956. THREE.ShaderChunk[ "map_particle_fragment" ],
  957. THREE.ShaderChunk[ "alphatest_fragment" ],
  958. THREE.ShaderChunk[ "color_fragment" ],
  959. THREE.ShaderChunk[ "shadowmap_fragment" ],
  960. THREE.ShaderChunk[ "fog_fragment" ],
  961. "}"
  962. ].join("\n")
  963. },
  964. // Depth encoding into RGBA texture
  965. // based on SpiderGL shadow map example
  966. // http://spidergl.org/example.php?id=6
  967. // originally from
  968. // http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  969. // see also here:
  970. // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  971. 'depthRGBA': {
  972. uniforms: {},
  973. vertexShader: [
  974. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  975. "void main() {",
  976. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  977. THREE.ShaderChunk[ "morphtarget_vertex" ],
  978. THREE.ShaderChunk[ "default_vertex" ],
  979. "}"
  980. ].join("\n"),
  981. fragmentShader: [
  982. "vec4 pack_depth( const in float depth ) {",
  983. "const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  984. "const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  985. "vec4 res = fract( depth * bit_shift );",
  986. "res -= res.xxyz * bit_mask;",
  987. "return res;",
  988. "}",
  989. "void main() {",
  990. "gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  991. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  992. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  993. //"gl_FragData[ 0 ] = pack_depth( z );",
  994. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  995. "}"
  996. ].join("\n")
  997. }
  998. };