terrain.effect 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
  2. CCEffect %{
  3. techniques:
  4. - name: opaque
  5. passes:
  6. - vert: terrain-vs
  7. frag: terrain-fs
  8. properties: &props
  9. UVScale: { value: [1, 1, 1, 1] }
  10. metallic: { value: [0, 0, 0, 0] }
  11. roughness: { value: [1, 1, 1, 1] }
  12. weightMap: { value: black }
  13. detailMap0: { value: grey }
  14. detailMap1: { value: grey }
  15. detailMap2: { value: grey }
  16. detailMap3: { value: grey }
  17. normalMap0: { value: normal }
  18. normalMap1: { value: normal }
  19. normalMap2: { value: normal }
  20. normalMap3: { value: normal }
  21. - vert: terrain-vs
  22. frag: terrain-fs
  23. phase: forward-add
  24. propertyIndex: 0
  25. embeddedMacros: { CC_FORWARD_ADD: true }
  26. depthStencilState:
  27. depthFunc: equal
  28. depthTest: true
  29. depthWrite: false
  30. blendState:
  31. targets:
  32. - blend: true
  33. blendSrc: one
  34. blendDst: one
  35. blendSrcAlpha: zero
  36. blendDstAlpha: one
  37. properties: *props
  38. - vert: shadow-caster-vs:vert
  39. frag: shadow-caster-fs:frag
  40. phase: shadow-add
  41. propertyIndex: 0
  42. rasterizerState:
  43. cullMode: back
  44. - vert: terrain-vs
  45. frag: terrain-fs
  46. phase: deferred-forward
  47. propertyIndex: 0
  48. }%
  49. CCProgram terrain-vs %{
  50. precision mediump float;
  51. #include <builtin/uniforms/cc-global>
  52. #include <builtin/uniforms/cc-local>
  53. #include <legacy/fog-vs>
  54. #include <legacy/shadow-map-vs>
  55. in vec3 a_position;
  56. in vec3 a_normal;
  57. in vec2 a_texCoord;
  58. #if CC_RECEIVE_SHADOW
  59. out vec2 v_shadowBias;
  60. #endif
  61. out highp vec3 v_position;
  62. out mediump vec3 v_normal;
  63. out mediump vec2 uvw;
  64. out mediump vec2 uv0;
  65. out mediump vec2 uv1;
  66. out mediump vec2 uv2;
  67. out mediump vec2 uv3;
  68. out mediump vec3 luv;
  69. out mediump vec3 diffuse;
  70. uniform TexCoords {
  71. vec4 UVScale;
  72. vec4 lightMapUVParam;
  73. };
  74. void main () {
  75. vec3 worldPos;
  76. worldPos.x = cc_matWorld[3][0] + a_position.x;
  77. worldPos.y = cc_matWorld[3][1] + a_position.y;
  78. worldPos.z = cc_matWorld[3][2] + a_position.z;
  79. vec4 pos = vec4(worldPos, 1.0);
  80. pos = cc_matViewProj * pos;
  81. uvw = a_texCoord;
  82. uv0 = a_position.xz * UVScale.x;
  83. uv1 = a_position.xz * UVScale.y;
  84. uv2 = a_position.xz * UVScale.z;
  85. uv3 = a_position.xz * UVScale.w;
  86. #if CC_USE_LIGHTMAP
  87. luv.xy = cc_lightingMapUVParam.xy + a_texCoord * cc_lightingMapUVParam.z;
  88. luv.z = cc_lightingMapUVParam.w;
  89. #endif
  90. v_position = worldPos;
  91. v_normal = a_normal;
  92. CC_TRANSFER_FOG(vec4(worldPos, 1.0));
  93. #if CC_RECEIVE_SHADOW
  94. v_shadowBias = vec2(0.0, 0.0);
  95. #endif
  96. CC_TRANSFER_SHADOW(vec4(worldPos, 1.0));
  97. gl_Position = pos;
  98. }
  99. }%
  100. CCProgram terrain-fs %{
  101. precision highp float;
  102. #include <builtin/uniforms/cc-global>
  103. #include <legacy/standard-surface-entry>
  104. #include <legacy/fog-fs>
  105. #if CC_USE_LIGHTMAP && !CC_FORWARD_ADD
  106. #include <legacy/lightingmap-fs>
  107. #endif
  108. #pragma define-meta LAYERS range([0, 4])
  109. in highp vec3 v_position;
  110. in mediump vec3 v_normal;
  111. #if CC_RECEIVE_SHADOW
  112. in vec2 v_shadowBias;
  113. #endif
  114. in mediump vec2 uvw;
  115. in mediump vec2 uv0;
  116. in mediump vec2 uv1;
  117. in mediump vec2 uv2;
  118. in mediump vec2 uv3;
  119. in mediump vec3 diffuse;
  120. in mediump vec3 luv;
  121. uniform PbrParams {
  122. vec4 metallic;
  123. vec4 roughness;
  124. };
  125. uniform sampler2D weightMap;
  126. uniform sampler2D detailMap0;
  127. uniform sampler2D detailMap1;
  128. uniform sampler2D detailMap2;
  129. uniform sampler2D detailMap3;
  130. uniform sampler2D normalMap0;
  131. uniform sampler2D normalMap1;
  132. uniform sampler2D normalMap2;
  133. uniform sampler2D normalMap3;
  134. void surf (out StandardSurface s) {
  135. #if LAYERS > 1
  136. vec4 w = texture(weightMap, uvw);
  137. #endif
  138. vec4 baseColor = vec4(0, 0, 0, 0);
  139. #if LAYERS == 1
  140. baseColor = texture(detailMap0, uv0);
  141. #elif LAYERS == 2
  142. baseColor += texture(detailMap0, uv0) * w.r;
  143. baseColor += texture(detailMap1, uv1) * w.g;
  144. #elif LAYERS == 3
  145. baseColor += texture(detailMap0, uv0) * w.r;
  146. baseColor += texture(detailMap1, uv1) * w.g;
  147. baseColor += texture(detailMap2, uv2) * w.b;
  148. #elif LAYERS == 4
  149. baseColor += texture(detailMap0, uv0) * w.r;
  150. baseColor += texture(detailMap1, uv1) * w.g;
  151. baseColor += texture(detailMap2, uv2) * w.b;
  152. baseColor += texture(detailMap3, uv3) * w.a;
  153. #else
  154. baseColor = texture(detailMap0, uv0);
  155. #endif
  156. HIGHP_VALUE_TO_STRUCT_DEFINED(v_position, s.position);
  157. #if USE_NORMALMAP
  158. vec4 baseNormal = vec4(0, 0, 0, 0);
  159. #if LAYERS == 1
  160. baseNormal = texture(normalMap0, uv0);
  161. #elif LAYERS == 2
  162. baseNormal += texture(normalMap0, uv0) * w.r;
  163. baseNormal += texture(normalMap1, uv1) * w.g;
  164. #elif LAYERS == 3
  165. baseNormal += texture(normalMap0, uv0) * w.r;
  166. baseNormal += texture(normalMap1, uv1) * w.g;
  167. baseNormal += texture(normalMap2, uv2) * w.b;
  168. #elif LAYERS == 4
  169. baseNormal += texture(normalMap0, uv0) * w.r;
  170. baseNormal += texture(normalMap1, uv1) * w.g;
  171. baseNormal += texture(normalMap2, uv2) * w.b;
  172. baseNormal += texture(normalMap3, uv3) * w.a;
  173. #else
  174. baseNormal = texture(normalMap0, uv0);
  175. #endif
  176. vec3 tangent = vec3(1.0, 0.0, 0.0);
  177. vec3 binormal = vec3(0.0, 0.0, 1.0);
  178. binormal = cross(tangent, v_normal);
  179. tangent = cross(v_normal, binormal);
  180. vec3 nmmp = baseNormal.xyz - vec3(0.5);
  181. s.normal =
  182. nmmp.x * normalize(tangent) +
  183. nmmp.y * normalize(binormal) +
  184. nmmp.z * normalize(v_normal);
  185. #else
  186. s.normal = v_normal;
  187. #endif
  188. #if CC_RECEIVE_SHADOW
  189. s.shadowBias = v_shadowBias;
  190. #endif
  191. s.albedo = vec4(SRGBToLinear(baseColor.rgb), 1.0);
  192. s.occlusion = 1.0;
  193. #if USE_PBR
  194. s.roughness = 0.0;
  195. #if LAYERS == 1
  196. s.roughness = roughness.x;
  197. #elif LAYERS == 2
  198. s.roughness += roughness.x * w.r;
  199. s.roughness += roughness.y * w.g;
  200. #elif LAYERS == 3
  201. s.roughness += roughness.x * w.r;
  202. s.roughness += roughness.y * w.g;
  203. s.roughness += roughness.z * w.b;
  204. #elif LAYERS == 4
  205. s.roughness += roughness.x * w.r;
  206. s.roughness += roughness.y * w.g;
  207. s.roughness += roughness.z * w.b;
  208. s.roughness += roughness.w * w.a;
  209. #else
  210. s.roughness = 1.0;
  211. #endif
  212. s.specularIntensity = 0.5;
  213. s.metallic = 0.0;
  214. #if LAYERS == 1
  215. s.specularIntensity = 0.5;
  216. s.metallic = metallic.x;
  217. #elif LAYERS == 2
  218. s.metallic += metallic.x * w.r;
  219. s.metallic += metallic.y * w.g;
  220. #elif LAYERS == 3
  221. s.metallic += metallic.x * w.r;
  222. s.metallic += metallic.y * w.g;
  223. s.metallic += metallic.z * w.b;
  224. #elif LAYERS == 4
  225. s.metallic += metallic.x * w.r;
  226. s.metallic += metallic.y * w.g;
  227. s.metallic += metallic.z * w.b;
  228. s.metallic += metallic.w * w.a;
  229. #else
  230. s.specularIntensity = 0.5;
  231. s.metallic = 0.0;
  232. #endif
  233. #else
  234. s.roughness = 1.0;
  235. s.specularIntensity = 0.5;
  236. s.metallic = 0.0;
  237. #endif
  238. s.emissive = vec3(0.0, 0.0, 0.0);
  239. #if CC_USE_LIGHTMAP && !CC_FORWARD_ADD
  240. SampleAndDecodeLightMapColor(s.lightmap.rgb, s.lightmap.a, s.lightmap_test, cc_lightingMap, luv.xy, luv.z, s.normal);
  241. #endif
  242. }
  243. CC_STANDARD_SURFACE_ENTRY()
  244. }%
  245. CCProgram shadow-caster-vs %{
  246. precision highp float;
  247. #include <builtin/uniforms/cc-global>
  248. #include <builtin/uniforms/cc-local>
  249. #include <builtin/uniforms/cc-shadow>
  250. in vec3 a_position;
  251. in vec3 a_normal;
  252. in vec2 a_texCoord;
  253. out highp vec2 v_clip_depth;
  254. vec4 vert () {
  255. vec4 worldPos;
  256. worldPos.x = cc_matWorld[3][0] + a_position.x;
  257. worldPos.y = cc_matWorld[3][1] + a_position.y;
  258. worldPos.z = cc_matWorld[3][2] + a_position.z;
  259. worldPos.w = 1.0;
  260. vec4 clipPos = cc_matLightViewProj * worldPos;
  261. v_clip_depth = clipPos.zw;
  262. return clipPos;
  263. }
  264. }%
  265. CCProgram shadow-caster-fs %{
  266. precision highp float;
  267. #include <common/data/packing>
  268. #pragma define SHADOWMAP_FORMAT_RGBA8 1
  269. #pragma define SHADOWMAP_FORMAT_FLOAT 0
  270. in highp vec2 v_clip_depth;
  271. vec4 frag () {
  272. highp float clipDepth = v_clip_depth.x / v_clip_depth.y * 0.5 + 0.5;
  273. //todo: do not support linear mode spot shadow
  274. #if CC_SHADOWMAP_FORMAT == SHADOWMAP_FORMAT_RGBA8
  275. return packDepthToRGBA(clipDepth);
  276. #else
  277. return vec4(clipDepth, 1.0, 1.0, 1.0);
  278. #endif
  279. }
  280. }%