WebGLShaders.js 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  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 int combine;",
  39. "#endif"
  40. ].join("\n"),
  41. envmap_fragment: [
  42. "#ifdef USE_ENVMAP",
  43. "vec4 cubeColor = textureCube( envMap, vec3( -vReflect.x, vReflect.yz ) );",
  44. "if ( combine == 1 ) {",
  45. //"gl_FragColor = mix( gl_FragColor, cubeColor, reflectivity );",
  46. "gl_FragColor = vec4( mix( gl_FragColor.xyz, cubeColor.xyz, reflectivity ), opacity );",
  47. "} else {",
  48. "gl_FragColor = gl_FragColor * cubeColor;",
  49. "}",
  50. "#endif"
  51. ].join("\n"),
  52. envmap_pars_vertex: [
  53. "#ifdef USE_ENVMAP",
  54. "varying vec3 vReflect;",
  55. "uniform float refractionRatio;",
  56. "uniform bool useRefract;",
  57. "#endif"
  58. ].join("\n"),
  59. envmap_vertex : [
  60. "#ifdef USE_ENVMAP",
  61. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  62. "vec3 nWorld = mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal;",
  63. "if ( useRefract ) {",
  64. "vReflect = refract( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ), refractionRatio );",
  65. "} else {",
  66. "vReflect = reflect( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ) );",
  67. "}",
  68. "#endif"
  69. ].join("\n"),
  70. // COLOR MAP (particles)
  71. map_particle_pars_fragment: [
  72. "#ifdef USE_MAP",
  73. "uniform sampler2D map;",
  74. "#endif"
  75. ].join("\n"),
  76. map_particle_fragment: [
  77. "#ifdef USE_MAP",
  78. "gl_FragColor = gl_FragColor * texture2D( map, gl_PointCoord );",
  79. "#endif"
  80. ].join("\n"),
  81. // COLOR MAP (triangles)
  82. map_pars_vertex: [
  83. "#ifdef USE_MAP",
  84. "varying vec2 vUv;",
  85. "uniform vec4 offsetRepeat;",
  86. "#endif"
  87. ].join("\n"),
  88. map_pars_fragment: [
  89. "#ifdef USE_MAP",
  90. "varying vec2 vUv;",
  91. "uniform sampler2D map;",
  92. "#endif"
  93. ].join("\n"),
  94. map_vertex: [
  95. "#ifdef USE_MAP",
  96. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  97. "#endif"
  98. ].join("\n"),
  99. map_fragment: [
  100. "#ifdef USE_MAP",
  101. "gl_FragColor = gl_FragColor * texture2D( map, vUv );",
  102. "#endif"
  103. ].join("\n"),
  104. // LIGHT MAP
  105. lightmap_pars_fragment: [
  106. "#ifdef USE_LIGHTMAP",
  107. "varying vec2 vUv2;",
  108. "uniform sampler2D lightMap;",
  109. "#endif"
  110. ].join("\n"),
  111. lightmap_pars_vertex: [
  112. "#ifdef USE_LIGHTMAP",
  113. "varying vec2 vUv2;",
  114. "#endif"
  115. ].join("\n"),
  116. lightmap_fragment: [
  117. "#ifdef USE_LIGHTMAP",
  118. "gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );",
  119. "#endif"
  120. ].join("\n"),
  121. lightmap_vertex: [
  122. "#ifdef USE_LIGHTMAP",
  123. "vUv2 = uv2;",
  124. "#endif"
  125. ].join("\n"),
  126. lights_pars_vertex: [
  127. "uniform bool enableLighting;",
  128. "uniform vec3 ambientLightColor;",
  129. "#if MAX_DIR_LIGHTS > 0",
  130. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  131. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  132. "#endif",
  133. "#if MAX_POINT_LIGHTS > 0",
  134. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  135. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  136. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  137. "#ifdef PHONG",
  138. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  139. "#endif",
  140. "#endif"
  141. ].join("\n"),
  142. // LIGHTS
  143. lights_vertex: [
  144. "if ( !enableLighting ) {",
  145. "vLightWeighting = vec3( 1.0 );",
  146. "} else {",
  147. "vLightWeighting = ambientLightColor;",
  148. "#if MAX_DIR_LIGHTS > 0",
  149. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  150. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  151. "float directionalLightWeighting = max( dot( transformedNormal, normalize( lDirection.xyz ) ), 0.0 );",
  152. "vLightWeighting += directionalLightColor[ i ] * directionalLightWeighting;",
  153. "}",
  154. "#endif",
  155. "#if MAX_POINT_LIGHTS > 0",
  156. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  157. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  158. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  159. "float lDistance = 1.0;",
  160. "if ( pointLightDistance[ i ] > 0.0 )",
  161. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  162. "lVector = normalize( lVector );",
  163. "float pointLightWeighting = max( dot( transformedNormal, lVector ), 0.0 );",
  164. "vLightWeighting += pointLightColor[ i ] * pointLightWeighting * lDistance;",
  165. "#ifdef PHONG",
  166. "vPointLight[ i ] = vec4( lVector, lDistance );",
  167. "#endif",
  168. "}",
  169. "#endif",
  170. "}"
  171. ].join("\n"),
  172. lights_pars_fragment: [
  173. "#if MAX_DIR_LIGHTS > 0",
  174. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  175. "#endif",
  176. "#if MAX_POINT_LIGHTS > 0",
  177. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  178. "#endif",
  179. "varying vec3 vViewPosition;",
  180. "varying vec3 vNormal;"
  181. ].join("\n"),
  182. lights_fragment: [
  183. "vec3 normal = normalize( vNormal );",
  184. "vec3 viewPosition = normalize( vViewPosition );",
  185. "vec4 mColor = vec4( diffuse, opacity );",
  186. "vec4 mSpecular = vec4( specular, opacity );",
  187. "#if MAX_POINT_LIGHTS > 0",
  188. "vec4 pointDiffuse = vec4( 0.0 );",
  189. "vec4 pointSpecular = vec4( 0.0 );",
  190. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  191. "vec3 pointVector = normalize( vPointLight[ i ].xyz );",
  192. "vec3 pointHalfVector = normalize( vPointLight[ i ].xyz + viewPosition );",
  193. "float pointDistance = vPointLight[ i ].w;",
  194. "float pointDotNormalHalf = dot( normal, pointHalfVector );",
  195. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  196. "float pointSpecularWeight = 0.0;",
  197. "if ( pointDotNormalHalf >= 0.0 )",
  198. "pointSpecularWeight = pow( pointDotNormalHalf, shininess );",
  199. "pointDiffuse += mColor * pointDiffuseWeight * pointDistance;",
  200. "pointSpecular += mSpecular * pointSpecularWeight * pointDistance;",
  201. "}",
  202. "#endif",
  203. "#if MAX_DIR_LIGHTS > 0",
  204. "vec4 dirDiffuse = vec4( 0.0 );",
  205. "vec4 dirSpecular = vec4( 0.0 );" ,
  206. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  207. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  208. "vec3 dirVector = normalize( lDirection.xyz );",
  209. "vec3 dirHalfVector = normalize( lDirection.xyz + viewPosition );",
  210. "float dirDotNormalHalf = dot( normal, dirHalfVector );",
  211. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  212. "float dirSpecularWeight = 0.0;",
  213. "if ( dirDotNormalHalf >= 0.0 )",
  214. "dirSpecularWeight = pow( dirDotNormalHalf, shininess );",
  215. "dirDiffuse += mColor * dirDiffuseWeight;",
  216. "dirSpecular += mSpecular * dirSpecularWeight;",
  217. "}",
  218. "#endif",
  219. "vec4 totalLight = vec4( ambient, opacity );",
  220. "#if MAX_DIR_LIGHTS > 0",
  221. "totalLight += dirDiffuse + dirSpecular;",
  222. "#endif",
  223. "#if MAX_POINT_LIGHTS > 0",
  224. "totalLight += pointDiffuse + pointSpecular;",
  225. "#endif",
  226. "gl_FragColor = gl_FragColor * totalLight;"
  227. ].join("\n"),
  228. // VERTEX COLORS
  229. color_pars_fragment: [
  230. "#ifdef USE_COLOR",
  231. "varying vec3 vColor;",
  232. "#endif"
  233. ].join("\n"),
  234. color_fragment: [
  235. "#ifdef USE_COLOR",
  236. "gl_FragColor = gl_FragColor * vec4( vColor, opacity );",
  237. "#endif"
  238. ].join("\n"),
  239. color_pars_vertex: [
  240. "#ifdef USE_COLOR",
  241. "varying vec3 vColor;",
  242. "#endif"
  243. ].join("\n"),
  244. color_vertex: [
  245. "#ifdef USE_COLOR",
  246. "vColor = color;",
  247. "#endif"
  248. ].join("\n"),
  249. // skinning
  250. skinning_pars_vertex: [
  251. "#ifdef USE_SKINNING",
  252. "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
  253. "#endif"
  254. ].join("\n"),
  255. skinning_vertex: [
  256. "#ifdef USE_SKINNING",
  257. "gl_Position = ( boneGlobalMatrices[ int( skinIndex.x ) ] * skinVertexA ) * skinWeight.x;",
  258. "gl_Position += ( boneGlobalMatrices[ int( skinIndex.y ) ] * skinVertexB ) * skinWeight.y;",
  259. // this doesn't work, no idea why
  260. //"gl_Position = projectionMatrix * cameraInverseMatrix * objectMatrix * gl_Position;",
  261. "gl_Position = projectionMatrix * viewMatrix * objectMatrix * gl_Position;",
  262. "#endif"
  263. ].join("\n"),
  264. // morphing
  265. morphtarget_pars_vertex: [
  266. "#ifdef USE_MORPHTARGETS",
  267. "uniform float morphTargetInfluences[ 8 ];",
  268. "#endif"
  269. ].join("\n"),
  270. morphtarget_vertex: [
  271. "#ifdef USE_MORPHTARGETS",
  272. "vec3 morphed = vec3( 0.0, 0.0, 0.0 );",
  273. "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
  274. "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
  275. "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
  276. "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
  277. "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
  278. "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
  279. "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
  280. "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
  281. "morphed += position;",
  282. "gl_Position = projectionMatrix * modelViewMatrix * vec4( morphed, 1.0 );",
  283. "#endif"
  284. ].join("\n"),
  285. default_vertex : [
  286. "#ifndef USE_MORPHTARGETS",
  287. "#ifndef USE_SKINNING",
  288. "gl_Position = projectionMatrix * mvPosition;",
  289. "#endif",
  290. "#endif"
  291. ].join("\n")
  292. };
  293. THREE.UniformsUtils = {
  294. merge: function ( uniforms ) {
  295. var u, p, tmp, merged = {};
  296. for ( u = 0; u < uniforms.length; u++ ) {
  297. tmp = this.clone( uniforms[ u ] );
  298. for ( p in tmp ) {
  299. merged[ p ] = tmp[ p ];
  300. }
  301. }
  302. return merged;
  303. },
  304. clone: function ( uniforms_src ) {
  305. var u, p, parameter, parameter_src, uniforms_dst = {};
  306. for ( u in uniforms_src ) {
  307. uniforms_dst[ u ] = {};
  308. for ( p in uniforms_src[ u ] ) {
  309. parameter_src = uniforms_src[ u ][ p ];
  310. if ( parameter_src instanceof THREE.Color ||
  311. parameter_src instanceof THREE.Vector2 ||
  312. parameter_src instanceof THREE.Vector3 ||
  313. parameter_src instanceof THREE.Vector4 ||
  314. parameter_src instanceof THREE.Matrix4 ||
  315. parameter_src instanceof THREE.Texture ) {
  316. uniforms_dst[ u ][ p ] = parameter_src.clone();
  317. } else if ( parameter_src instanceof Array ) {
  318. uniforms_dst[ u ][ p ] = parameter_src.slice();
  319. } else {
  320. uniforms_dst[ u ][ p ] = parameter_src;
  321. }
  322. }
  323. }
  324. return uniforms_dst;
  325. }
  326. };
  327. THREE.UniformsLib = {
  328. common: {
  329. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  330. "opacity" : { type: "f", value: 1.0 },
  331. "map" : { type: "t", value: 0, texture: null },
  332. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  333. "lightMap" : { type: "t", value: 2, texture: null },
  334. "envMap" : { type: "t", value: 1, texture: null },
  335. "useRefract" : { type: "i", value: 0 },
  336. "reflectivity" : { type: "f", value: 1.0 },
  337. "refractionRatio" : { type: "f", value: 0.98 },
  338. "combine" : { type: "i", value: 0 },
  339. "morphTargetInfluences" : { type: "f", value: 0 }
  340. },
  341. fog : {
  342. "fogDensity" : { type: "f", value: 0.00025 },
  343. "fogNear" : { type: "f", value: 1 },
  344. "fogFar" : { type: "f", value: 2000 },
  345. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  346. },
  347. lights: {
  348. "enableLighting" : { type: "i", value: 1 },
  349. "ambientLightColor" : { type: "fv", value: [] },
  350. "directionalLightDirection" : { type: "fv", value: [] },
  351. "directionalLightColor" : { type: "fv", value: [] },
  352. "pointLightColor" : { type: "fv", value: [] },
  353. "pointLightPosition" : { type: "fv", value: [] },
  354. "pointLightDistance" : { type: "fv1", value: [] }
  355. },
  356. particle: {
  357. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  358. "opacity" : { type: "f", value: 1.0 },
  359. "size" : { type: "f", value: 1.0 },
  360. "scale" : { type: "f", value: 1.0 },
  361. "map" : { type: "t", value: 0, texture: null },
  362. "fogDensity" : { type: "f", value: 0.00025 },
  363. "fogNear" : { type: "f", value: 1 },
  364. "fogFar" : { type: "f", value: 2000 },
  365. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  366. }
  367. };
  368. THREE.ShaderLib = {
  369. 'lensFlareVertexTexture': {
  370. vertexShader: [
  371. "uniform vec3 screenPosition;",
  372. "uniform vec2 scale;",
  373. "uniform float rotation;",
  374. "uniform int renderType;",
  375. "uniform sampler2D occlusionMap;",
  376. "attribute vec2 position;",
  377. "attribute vec2 UV;",
  378. "varying vec2 vUV;",
  379. "varying float vVisibility;",
  380. "void main() {",
  381. "vUV = UV;",
  382. "vec2 pos = position;",
  383. "if( renderType == 2 ) {",
  384. "vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) ) +",
  385. "texture2D( occlusionMap, vec2( 0.5, 0.1 ) ) +",
  386. "texture2D( occlusionMap, vec2( 0.9, 0.1 ) ) +",
  387. "texture2D( occlusionMap, vec2( 0.9, 0.5 ) ) +",
  388. "texture2D( occlusionMap, vec2( 0.9, 0.9 ) ) +",
  389. "texture2D( occlusionMap, vec2( 0.5, 0.9 ) ) +",
  390. "texture2D( occlusionMap, vec2( 0.1, 0.9 ) ) +",
  391. "texture2D( occlusionMap, vec2( 0.1, 0.5 ) ) +",
  392. "texture2D( occlusionMap, vec2( 0.5, 0.5 ) );",
  393. "vVisibility = ( visibility.r / 9.0 ) *",
  394. "( 1.0 - visibility.g / 9.0 ) *",
  395. "( visibility.b / 9.0 ) *",
  396. "( 1.0 - visibility.a / 9.0 );",
  397. "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
  398. "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
  399. "}",
  400. "gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
  401. "}"
  402. ].join( "\n" ),
  403. fragmentShader: [
  404. "#ifdef GL_ES",
  405. "precision highp float;",
  406. "#endif",
  407. "uniform sampler2D map;",
  408. "uniform float opacity;",
  409. "uniform int renderType;",
  410. "varying vec2 vUV;",
  411. "varying float vVisibility;",
  412. "void main() {",
  413. // pink square
  414. "if( renderType == 0 ) {",
  415. "gl_FragColor = vec4( 1.0, 0.0, 1.0, 0.0 );",
  416. // restore
  417. "} else if( renderType == 1 ) {",
  418. "gl_FragColor = texture2D( map, vUV );",
  419. // flare
  420. "} else {",
  421. "vec4 color = texture2D( map, vUV );",
  422. "color.a *= opacity * vVisibility;",
  423. "gl_FragColor = color;",
  424. "}",
  425. "}"
  426. ].join( "\n" )
  427. },
  428. 'lensFlare': {
  429. vertexShader: [
  430. "uniform vec3 screenPosition;",
  431. "uniform vec2 scale;",
  432. "uniform float rotation;",
  433. "uniform int renderType;",
  434. "attribute vec2 position;",
  435. "attribute vec2 UV;",
  436. "varying vec2 vUV;",
  437. "void main() {",
  438. "vUV = UV;",
  439. "vec2 pos = position;",
  440. "if( renderType == 2 ) {",
  441. "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
  442. "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
  443. "}",
  444. "gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
  445. "}"
  446. ].join( "\n" ),
  447. fragmentShader: [
  448. "#ifdef GL_ES",
  449. "precision highp float;",
  450. "#endif",
  451. "uniform sampler2D map;",
  452. "uniform sampler2D occlusionMap;",
  453. "uniform float opacity;",
  454. "uniform int renderType;",
  455. "varying vec2 vUV;",
  456. "void main() {",
  457. // pink square
  458. "if( renderType == 0 ) {",
  459. "gl_FragColor = vec4( texture2D( map, vUV ).rgb, 0.0 );",
  460. // restore
  461. "} else if( renderType == 1 ) {",
  462. "gl_FragColor = texture2D( map, vUV );",
  463. // flare
  464. "} else {",
  465. "float visibility = texture2D( occlusionMap, vec2( 0.5, 0.1 ) ).a +",
  466. "texture2D( occlusionMap, vec2( 0.9, 0.5 ) ).a +",
  467. "texture2D( occlusionMap, vec2( 0.5, 0.9 ) ).a +",
  468. "texture2D( occlusionMap, vec2( 0.1, 0.5 ) ).a;",
  469. "visibility = ( 1.0 - visibility / 4.0 );",
  470. "vec4 color = texture2D( map, vUV );",
  471. "color.a *= opacity * visibility;",
  472. "gl_FragColor = color;",
  473. "}",
  474. "}"
  475. ].join( "\n" )
  476. },
  477. 'sprite': {
  478. vertexShader: [
  479. "uniform int useScreenCoordinates;",
  480. "uniform int affectedByDistance;",
  481. "uniform vec3 screenPosition;",
  482. "uniform mat4 modelViewMatrix;",
  483. "uniform mat4 projectionMatrix;",
  484. "uniform float rotation;",
  485. "uniform vec2 scale;",
  486. "uniform vec2 alignment;",
  487. "uniform vec2 uvOffset;",
  488. "uniform vec2 uvScale;",
  489. "attribute vec2 position;",
  490. "attribute vec2 uv;",
  491. "varying vec2 vUV;",
  492. "void main() {",
  493. "vUV = uvOffset + uv * uvScale;",
  494. "vec2 alignedPosition = position + alignment;",
  495. "vec2 rotatedPosition;",
  496. "rotatedPosition.x = ( cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y ) * scale.x;",
  497. "rotatedPosition.y = ( sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y ) * scale.y;",
  498. "vec4 finalPosition;",
  499. "if( useScreenCoordinates != 0 ) {",
  500. "finalPosition = vec4( screenPosition.xy + rotatedPosition, screenPosition.z, 1.0 );",
  501. "} else {",
  502. "finalPosition = projectionMatrix * modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );",
  503. "finalPosition.xy += rotatedPosition * ( affectedByDistance == 1 ? 1.0 : finalPosition.z );",
  504. "}",
  505. "gl_Position = finalPosition;",
  506. "}"
  507. ].join( "\n" ),
  508. fragmentShader: [
  509. "#ifdef GL_ES",
  510. "precision highp float;",
  511. "#endif",
  512. "uniform sampler2D map;",
  513. "uniform float opacity;",
  514. "varying vec2 vUV;",
  515. "void main() {",
  516. "vec4 color = texture2D( map, vUV );",
  517. "color.a *= opacity;",
  518. "gl_FragColor = color;",
  519. "}"
  520. ].join( "\n" )
  521. },
  522. 'shadowPost': {
  523. vertexShader: [
  524. "uniform mat4 projectionMatrix;",
  525. "attribute vec3 position;",
  526. "void main() {",
  527. "gl_Position = projectionMatrix * vec4( position, 1.0 );",
  528. "}"
  529. ].join( "\n" ),
  530. fragmentShader: [
  531. "#ifdef GL_ES",
  532. "precision highp float;",
  533. "#endif",
  534. "uniform float darkness;",
  535. "void main() {",
  536. "gl_FragColor = vec4( 0, 0, 0, darkness );",
  537. "}"
  538. ].join( "\n" )
  539. },
  540. 'shadowVolumeDynamic': {
  541. uniforms: { "directionalLightDirection": { type: "fv", value: [] }},
  542. vertexShader: [
  543. "uniform vec3 directionalLightDirection;",
  544. "void main() {",
  545. "vec4 pos = objectMatrix * vec4( position, 1.0 );",
  546. "vec3 norm = mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal;",
  547. "vec4 extruded = vec4( directionalLightDirection * 5000.0 * step( 0.0, dot( directionalLightDirection, norm ) ), 0.0 );",
  548. "gl_Position = projectionMatrix * viewMatrix * ( pos + extruded );",
  549. "}"
  550. ].join( "\n" ),
  551. fragmentShader: [
  552. "void main() {",
  553. "gl_FragColor = vec4( 1.0 );",
  554. "}"
  555. ].join( "\n" )
  556. },
  557. 'depth': {
  558. uniforms: { "mNear": { type: "f", value: 1.0 },
  559. "mFar" : { type: "f", value: 2000.0 },
  560. "opacity" : { type: "f", value: 1.0 }
  561. },
  562. fragmentShader: [
  563. "uniform float mNear;",
  564. "uniform float mFar;",
  565. "uniform float opacity;",
  566. "void main() {",
  567. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  568. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  569. "gl_FragColor = vec4( vec3( color ), opacity );",
  570. "}"
  571. ].join("\n"),
  572. vertexShader: [
  573. "void main() {",
  574. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  575. "}"
  576. ].join("\n")
  577. },
  578. 'normal': {
  579. uniforms: { "opacity" : { type: "f", value: 1.0 } },
  580. fragmentShader: [
  581. "uniform float opacity;",
  582. "varying vec3 vNormal;",
  583. "void main() {",
  584. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  585. "}"
  586. ].join("\n"),
  587. vertexShader: [
  588. "varying vec3 vNormal;",
  589. "void main() {",
  590. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  591. "vNormal = normalize( normalMatrix * normal );",
  592. "gl_Position = projectionMatrix * mvPosition;",
  593. "}"
  594. ].join("\n")
  595. },
  596. 'basic': {
  597. uniforms: THREE.UniformsUtils.merge( [
  598. THREE.UniformsLib[ "common" ],
  599. THREE.UniformsLib[ "fog" ]
  600. ] ),
  601. fragmentShader: [
  602. "uniform vec3 diffuse;",
  603. "uniform float opacity;",
  604. THREE.ShaderChunk[ "color_pars_fragment" ],
  605. THREE.ShaderChunk[ "map_pars_fragment" ],
  606. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  607. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  608. THREE.ShaderChunk[ "fog_pars_fragment" ],
  609. "void main() {",
  610. "gl_FragColor = vec4( diffuse, opacity );",
  611. THREE.ShaderChunk[ "map_fragment" ],
  612. THREE.ShaderChunk[ "lightmap_fragment" ],
  613. THREE.ShaderChunk[ "color_fragment" ],
  614. THREE.ShaderChunk[ "envmap_fragment" ],
  615. THREE.ShaderChunk[ "fog_fragment" ],
  616. "}"
  617. ].join("\n"),
  618. vertexShader: [
  619. THREE.ShaderChunk[ "map_pars_vertex" ],
  620. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  621. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  622. THREE.ShaderChunk[ "color_pars_vertex" ],
  623. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  624. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  625. "void main() {",
  626. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  627. THREE.ShaderChunk[ "map_vertex" ],
  628. THREE.ShaderChunk[ "lightmap_vertex" ],
  629. THREE.ShaderChunk[ "envmap_vertex" ],
  630. THREE.ShaderChunk[ "color_vertex" ],
  631. THREE.ShaderChunk[ "skinning_vertex" ],
  632. THREE.ShaderChunk[ "morphtarget_vertex" ],
  633. THREE.ShaderChunk[ "default_vertex" ],
  634. "}"
  635. ].join("\n")
  636. },
  637. 'lambert': {
  638. uniforms: THREE.UniformsUtils.merge( [
  639. THREE.UniformsLib[ "common" ],
  640. THREE.UniformsLib[ "fog" ],
  641. THREE.UniformsLib[ "lights" ]
  642. ] ),
  643. fragmentShader: [
  644. "uniform vec3 diffuse;",
  645. "uniform float opacity;",
  646. "varying vec3 vLightWeighting;",
  647. THREE.ShaderChunk[ "color_pars_fragment" ],
  648. THREE.ShaderChunk[ "map_pars_fragment" ],
  649. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  650. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  651. THREE.ShaderChunk[ "fog_pars_fragment" ],
  652. "void main() {",
  653. "gl_FragColor = vec4( diffuse, opacity );",
  654. "gl_FragColor = gl_FragColor * vec4( vLightWeighting, 1.0 );",
  655. THREE.ShaderChunk[ "map_fragment" ],
  656. THREE.ShaderChunk[ "lightmap_fragment" ],
  657. THREE.ShaderChunk[ "color_fragment" ],
  658. THREE.ShaderChunk[ "envmap_fragment" ],
  659. THREE.ShaderChunk[ "fog_fragment" ],
  660. "}"
  661. ].join("\n"),
  662. vertexShader: [
  663. "varying vec3 vLightWeighting;",
  664. THREE.ShaderChunk[ "map_pars_vertex" ],
  665. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  666. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  667. THREE.ShaderChunk[ "lights_pars_vertex" ],
  668. THREE.ShaderChunk[ "color_pars_vertex" ],
  669. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  670. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  671. "void main() {",
  672. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  673. THREE.ShaderChunk[ "map_vertex" ],
  674. THREE.ShaderChunk[ "lightmap_vertex" ],
  675. THREE.ShaderChunk[ "envmap_vertex" ],
  676. THREE.ShaderChunk[ "color_vertex" ],
  677. "vec3 transformedNormal = normalize( normalMatrix * normal );",
  678. THREE.ShaderChunk[ "lights_vertex" ],
  679. THREE.ShaderChunk[ "skinning_vertex" ],
  680. THREE.ShaderChunk[ "morphtarget_vertex" ],
  681. THREE.ShaderChunk[ "default_vertex" ],
  682. "}"
  683. ].join("\n")
  684. },
  685. 'phong': {
  686. uniforms: THREE.UniformsUtils.merge( [
  687. THREE.UniformsLib[ "common" ],
  688. THREE.UniformsLib[ "fog" ],
  689. THREE.UniformsLib[ "lights" ],
  690. {
  691. "ambient" : { type: "c", value: new THREE.Color( 0x050505 ) },
  692. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  693. "shininess": { type: "f", value: 30 }
  694. }
  695. ] ),
  696. fragmentShader: [
  697. "uniform vec3 diffuse;",
  698. "uniform float opacity;",
  699. "uniform vec3 ambient;",
  700. "uniform vec3 specular;",
  701. "uniform float shininess;",
  702. "varying vec3 vLightWeighting;",
  703. THREE.ShaderChunk[ "color_pars_fragment" ],
  704. THREE.ShaderChunk[ "map_pars_fragment" ],
  705. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  706. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  707. THREE.ShaderChunk[ "fog_pars_fragment" ],
  708. THREE.ShaderChunk[ "lights_pars_fragment" ],
  709. "void main() {",
  710. "gl_FragColor = vec4( vLightWeighting, 1.0 );",
  711. THREE.ShaderChunk[ "lights_fragment" ],
  712. THREE.ShaderChunk[ "map_fragment" ],
  713. THREE.ShaderChunk[ "lightmap_fragment" ],
  714. THREE.ShaderChunk[ "color_fragment" ],
  715. THREE.ShaderChunk[ "envmap_fragment" ],
  716. THREE.ShaderChunk[ "fog_fragment" ],
  717. "}"
  718. ].join("\n"),
  719. vertexShader: [
  720. "#define PHONG",
  721. "varying vec3 vLightWeighting;",
  722. "varying vec3 vViewPosition;",
  723. "varying vec3 vNormal;",
  724. THREE.ShaderChunk[ "map_pars_vertex" ],
  725. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  726. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  727. THREE.ShaderChunk[ "lights_pars_vertex" ],
  728. THREE.ShaderChunk[ "color_pars_vertex" ],
  729. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  730. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  731. "void main() {",
  732. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  733. THREE.ShaderChunk[ "map_vertex" ],
  734. THREE.ShaderChunk[ "lightmap_vertex" ],
  735. THREE.ShaderChunk[ "envmap_vertex" ],
  736. THREE.ShaderChunk[ "color_vertex" ],
  737. "#ifndef USE_ENVMAP",
  738. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  739. "#endif",
  740. "vViewPosition = cameraPosition - mPosition.xyz;",
  741. "vec3 transformedNormal = normalize( normalMatrix * normal );",
  742. "vNormal = transformedNormal;",
  743. THREE.ShaderChunk[ "lights_vertex" ],
  744. THREE.ShaderChunk[ "skinning_vertex" ],
  745. THREE.ShaderChunk[ "morphtarget_vertex" ],
  746. THREE.ShaderChunk[ "default_vertex" ],
  747. "}"
  748. ].join("\n")
  749. },
  750. 'particle_basic': {
  751. uniforms: THREE.UniformsLib[ "particle" ],
  752. fragmentShader: [
  753. "uniform vec3 psColor;",
  754. "uniform float opacity;",
  755. THREE.ShaderChunk[ "color_pars_fragment" ],
  756. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  757. THREE.ShaderChunk[ "fog_pars_fragment" ],
  758. "void main() {",
  759. "gl_FragColor = vec4( psColor, opacity );",
  760. THREE.ShaderChunk[ "map_particle_fragment" ],
  761. THREE.ShaderChunk[ "color_fragment" ],
  762. THREE.ShaderChunk[ "fog_fragment" ],
  763. "}"
  764. ].join("\n"),
  765. vertexShader: [
  766. "uniform float size;",
  767. "uniform float scale;",
  768. THREE.ShaderChunk[ "color_pars_vertex" ],
  769. "void main() {",
  770. THREE.ShaderChunk[ "color_vertex" ],
  771. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  772. "#ifdef USE_SIZEATTENUATION",
  773. "gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  774. "#else",
  775. "gl_PointSize = size;",
  776. "#endif",
  777. "gl_Position = projectionMatrix * mvPosition;",
  778. "}"
  779. ].join("\n")
  780. }
  781. };
粤ICP备19079148号