您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

S3MTilesFS.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. export default `
  2. #ifdef GL_OES_standard_derivatives
  3. #extension GL_OES_standard_derivatives : enable
  4. #endif
  5. #ifdef GL_EXT_shader_texture_lod
  6. #extension GL_EXT_shader_texture_lod : enable
  7. #endif
  8. uniform vec4 uDiffuseColor;
  9. #ifdef TexCoord
  10. varying vec4 vTexCoord;
  11. #ifdef COMPUTE_TEXCOORD
  12. uniform sampler2D uTexture;
  13. uniform float uTexture0Width;
  14. varying vec4 vTexCoordTransform;
  15. varying vec4 vTexMatrix;
  16. varying vec2 vIsRGBA;
  17. #endif
  18. #endif
  19. varying vec4 vColor;
  20. varying vec4 vSecondColor;
  21. varying vec4 vPositionMC;
  22. varying vec3 vPositionEC;
  23. #ifdef VertexNormal
  24. varying vec3 vNormalEC;
  25. #endif
  26. #ifdef TexCoord2
  27. uniform sampler2D uTexture2;
  28. uniform float uTexture1Width;
  29. varying vec4 vTexMatrix2;
  30. #endif
  31. #ifdef COMPUTE_TEXCOORD
  32. void calculateMipLevel(in vec2 inTexCoord, in float vecTile, in float fMaxMip, inout float mipLevel)
  33. {
  34. vec2 dx = dFdx(inTexCoord * vecTile);
  35. vec2 dy = dFdy(inTexCoord * vecTile);
  36. float dotX = dot(dx, dx);
  37. float dotY = dot(dy, dy);
  38. float dMax = max(dotX, dotY);
  39. float dMin = min(dotX, dotY);
  40. float offset = (dMax - dMin) / (dMax + dMin);
  41. offset = clamp(offset, 0.0, 1.0);
  42. float d = dMax * (1.0 - offset) + dMin * offset;
  43. mipLevel = 0.5 * log2(d);
  44. mipLevel = clamp(mipLevel, 0.0, fMaxMip - 1.62);
  45. }
  46. void calculateTexCoord(in vec3 inTexCoord, in float scale, in float XTran, in float YTran, in float fTile, in float mipLevel, inout vec2 outTexCoord)
  47. {
  48. if(inTexCoord.z < -9000.0)
  49. {
  50. outTexCoord = inTexCoord.xy;
  51. }
  52. else
  53. {
  54. vec2 fTexCoord = fract(inTexCoord.xy);
  55. float offset = 1.0 * pow(2.0, mipLevel) / fTile;
  56. fTexCoord = clamp(fTexCoord, offset, 1.0 - offset);
  57. outTexCoord.x = (fTexCoord.x + XTran) * scale;
  58. outTexCoord.y = (fTexCoord.y + YTran) * scale;
  59. }
  60. }
  61. vec4 getTexColorForS3M(sampler2D curTexture, vec3 oriTexCoord, float texTileWidth, float fMaxMipLev, float fTexCoordScale, vec2 vecTexCoordTranslate, float isRGBA)
  62. {
  63. vec4 color = vec4(1.0);
  64. float mipLevel = 0.0;
  65. #ifdef GL_OES_standard_derivatives
  66. calculateMipLevel(oriTexCoord.xy, texTileWidth, fMaxMipLev, mipLevel);
  67. #endif
  68. vec2 realTexCoord;
  69. calculateTexCoord(oriTexCoord, fTexCoordScale, vecTexCoordTranslate.x, vecTexCoordTranslate.y, texTileWidth, mipLevel, realTexCoord);
  70. if(isRGBA > 0.5)
  71. {
  72. vec2 rgbTexCoord;
  73. rgbTexCoord.x = (realTexCoord.x + vecTexCoordTranslate.x * fTexCoordScale) * 0.5;
  74. rgbTexCoord.y = (realTexCoord.y + vecTexCoordTranslate.y * fTexCoordScale) * 0.5;
  75. color = texture2D(curTexture, rgbTexCoord.xy, -10.0);
  76. vec2 vecAlphaTexCoord;
  77. vecAlphaTexCoord.x = rgbTexCoord.x;
  78. vecAlphaTexCoord.y = rgbTexCoord.y + fTexCoordScale * 0.5;
  79. color.a = texture2D(curTexture, vecAlphaTexCoord.xy, -10.0).r;
  80. }
  81. else
  82. {
  83. if(oriTexCoord.z < -9000.0)
  84. {
  85. color = texture2D(curTexture, realTexCoord.xy);
  86. }
  87. else
  88. {
  89. #ifdef GL_EXT_shader_texture_lod
  90. color = texture2DLodEXT(curTexture, realTexCoord.xy, mipLevel);
  91. #else
  92. color = texture2D(curTexture, realTexCoord.xy, mipLevel);
  93. #endif
  94. }
  95. }
  96. return color;
  97. }
  98. vec4 getTextureColor()
  99. {
  100. if(vTexMatrix.z < 0.0)
  101. {
  102. return vec4(1.0);
  103. }
  104. float texTileWidth0 = vTexMatrix.z * uTexture0Width;
  105. vec3 realTexCoord = vec3(vTexCoord.xy, vTexCoordTransform.x);
  106. vec4 FColor = getTexColorForS3M(uTexture, realTexCoord, texTileWidth0, vTexMatrix.w, vTexMatrix.z, vTexMatrix.xy, vIsRGBA.x);
  107. #ifdef TexCoord2
  108. float texTileWidth1 = vTexMatrix2.z * uTexture1Width;
  109. realTexCoord = vec3(vTexCoord.zw, vTexCoordTransform.y);
  110. vec4 SColor = getTexColorForS3M(uTexture2, realTexCoord, texTileWidth1, vTexMatrix2.w, vTexMatrix2.z, vTexMatrix2.xy, vIsRGBA.y);
  111. SColor.r = clamp(SColor.r, 0.0, 1.0);
  112. SColor.g = clamp(SColor.g, 0.0, 1.0);
  113. SColor.b = clamp(SColor.b, 0.0, 1.0);
  114. return FColor * SColor;
  115. #else
  116. return FColor;
  117. #endif
  118. }
  119. #endif
  120. vec4 SRGBtoLINEAR4(vec4 srgbIn)
  121. {
  122. #ifndef HDR
  123. vec3 linearOut = pow(srgbIn.rgb, vec3(2.2));
  124. return vec4(linearOut, srgbIn.a);
  125. #else
  126. return srgbIn;
  127. #endif
  128. }
  129. vec3 LINEARtoSRGB(vec3 linearIn)
  130. {
  131. #ifndef HDR
  132. return pow(linearIn, vec3(1.0/2.2));
  133. #else
  134. return linearIn;
  135. #endif
  136. }
  137. vec3 applyTonemapping(vec3 linearIn)
  138. {
  139. #ifndef HDR
  140. return czm_acesTonemapping(linearIn);
  141. #else
  142. return linearIn;
  143. #endif
  144. }
  145. vec3 computeNormal(in vec3 oriVertex)
  146. {
  147. vec3 normal = cross(vec3(dFdx(oriVertex.x), dFdx(oriVertex.y), dFdx(oriVertex.z)), vec3(dFdy(oriVertex.x), dFdy(oriVertex.y), dFdy(oriVertex.z)));
  148. normal = normalize(normal);
  149. return normal;
  150. }
  151. void main()
  152. {
  153. if(vColor.a < 0.1)
  154. {
  155. discard;
  156. }
  157. vec4 baseColorWithAlpha = vColor;
  158. #ifdef COMPUTE_TEXCOORD
  159. baseColorWithAlpha *= SRGBtoLINEAR4(getTextureColor());
  160. #endif
  161. if(baseColorWithAlpha.a < 0.1)
  162. {
  163. discard;
  164. }
  165. vec3 normal = vec3(0.0);
  166. #ifdef VertexNormal
  167. normal = normalize(vNormalEC);
  168. #endif
  169. normal = length(normal) > 0.1 ? normal : computeNormal(vPositionMC.xyz);
  170. vec3 color = baseColorWithAlpha.rgb;
  171. vec3 dirVectorEC = normalize(czm_lightDirectionEC);
  172. float dotProduct = dot( normal, dirVectorEC );
  173. float dirDiffuseWeight = max( dotProduct, 0.0 );
  174. dirDiffuseWeight = dirDiffuseWeight * 0.5 + 0.5;
  175. color += color * uDiffuseColor.rgb * dirDiffuseWeight;
  176. #ifdef TexCoord
  177. color = LINEARtoSRGB(color);
  178. #endif
  179. gl_FragColor = vec4(color, baseColorWithAlpha.a);
  180. }
  181. `;