WebGLShaders.js 35 KB

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