builtin-terrain.effect 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Copyright (c) 2017-2022 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
  39. frag: shadow-caster-fs
  40. phase: shadow-add
  41. propertyIndex: 0
  42. rasterizerState:
  43. cullMode: none
  44. - &reflect-map
  45. vert: terrain-vs
  46. frag: reflect-map-fs
  47. phase: reflect-map
  48. propertyIndex: 0
  49. - vert: terrain-vs
  50. frag: terrain-fs
  51. phase: deferred-forward
  52. propertyIndex: 0
  53. }%
  54. CCProgram shared-ubos %{
  55. uniform Constants {
  56. vec4 UVScale;
  57. vec4 metallic;
  58. vec4 roughness;
  59. };
  60. }%
  61. CCProgram macro-remapping %{
  62. #pragma define-meta USE_NORMALMAP
  63. #define CC_SURFACES_USE_TANGENT_SPACE USE_NORMALMAP
  64. }%
  65. CCProgram surface-vertex %{
  66. #define CC_SURFACES_VERTEX_MODIFY_WORLD_POS
  67. vec3 SurfacesVertexModifyWorldPos(in SurfacesStandardVertexIntermediate In)
  68. {
  69. vec3 worldPos;
  70. worldPos.x = cc_matWorld[3][0] + In.position.x;
  71. worldPos.y = cc_matWorld[3][1] + In.position.y;
  72. worldPos.z = cc_matWorld[3][2] + In.position.z;
  73. return worldPos;
  74. }
  75. #define CC_SURFACES_VERTEX_MODIFY_CLIP_POS
  76. vec4 SurfacesVertexModifyClipPos(in SurfacesStandardVertexIntermediate In)
  77. {
  78. vec4 pos = vec4(In.worldPos, 1.0);
  79. pos = cc_matViewProj * pos;
  80. return pos;
  81. }
  82. }%
  83. CCProgram surface-fragment %{
  84. #pragma define-meta LAYERS range([0, 4])
  85. uniform sampler2D weightMap;
  86. uniform sampler2D detailMap0;
  87. uniform sampler2D detailMap1;
  88. uniform sampler2D detailMap2;
  89. uniform sampler2D detailMap3;
  90. uniform sampler2D normalMap0;
  91. uniform sampler2D normalMap1;
  92. uniform sampler2D normalMap2;
  93. uniform sampler2D normalMap3;
  94. #define CC_SURFACES_FRAGMENT_ALPHA_CLIP_ONLY
  95. void SurfacesFragmentAlphaClipOnly(){}
  96. #define CC_SURFACES_FRAGMENT_MODIFY_SHARED_DATA
  97. #include <surfaces/data-structures/standard>
  98. void SurfacesFragmentModifySharedData(inout SurfacesMaterialData surfaceData)
  99. {
  100. vec2 uvw = FSInput_texcoord;
  101. vec2 uv0, uv1, uv2, uv3;
  102. #if CC_SURFACES_TRANSFER_LOCAL_POS
  103. uv0 = FSInput_localPos.xz * UVScale.x;
  104. uv1 = FSInput_localPos.xz * UVScale.y;
  105. uv2 = FSInput_localPos.xz * UVScale.z;
  106. uv3 = FSInput_localPos.xz * UVScale.w;
  107. #endif
  108. vec4 w = vec4(0.0);
  109. #if LAYERS > 1
  110. w = texture(weightMap, uvw);
  111. #endif
  112. vec4 baseColor = vec4(0, 0, 0, 0);
  113. #if LAYERS == 1
  114. baseColor = texture(detailMap0, uv0);
  115. #elif LAYERS == 2
  116. baseColor += texture(detailMap0, uv0) * w.r;
  117. baseColor += texture(detailMap1, uv1) * w.g;
  118. #elif LAYERS == 3
  119. baseColor += texture(detailMap0, uv0) * w.r;
  120. baseColor += texture(detailMap1, uv1) * w.g;
  121. baseColor += texture(detailMap2, uv2) * w.b;
  122. #elif LAYERS == 4
  123. baseColor += texture(detailMap0, uv0) * w.r;
  124. baseColor += texture(detailMap1, uv1) * w.g;
  125. baseColor += texture(detailMap2, uv2) * w.b;
  126. baseColor += texture(detailMap3, uv3) * w.a;
  127. #else
  128. baseColor = texture(detailMap0, uv0);
  129. #endif
  130. surfaceData.baseColor = vec4(SRGBToLinear(baseColor.rgb), 1.0);
  131. vec3 normal = FSInput_worldNormal;
  132. vec3 tangent = vec3(1.0, 0.0, 0.0);
  133. vec3 binormal = vec3(0.0, 0.0, 1.0);
  134. binormal = cross(tangent, normal);
  135. tangent = cross(normal, binormal);
  136. #if USE_NORMALMAP
  137. vec4 baseNormal = vec4(0, 0, 0, 0);
  138. #if LAYERS == 1
  139. baseNormal = texture(normalMap0, uv0);
  140. #elif LAYERS == 2
  141. baseNormal += texture(normalMap0, uv0) * w.r;
  142. baseNormal += texture(normalMap1, uv1) * w.g;
  143. #elif LAYERS == 3
  144. baseNormal += texture(normalMap0, uv0) * w.r;
  145. baseNormal += texture(normalMap1, uv1) * w.g;
  146. baseNormal += texture(normalMap2, uv2) * w.b;
  147. #elif LAYERS == 4
  148. baseNormal += texture(normalMap0, uv0) * w.r;
  149. baseNormal += texture(normalMap1, uv1) * w.g;
  150. baseNormal += texture(normalMap2, uv2) * w.b;
  151. baseNormal += texture(normalMap3, uv3) * w.a;
  152. #else
  153. baseNormal = texture(normalMap0, uv0);
  154. #endif
  155. vec3 nmmp = baseNormal.xyz - vec3(0.5);
  156. normal = CalculateNormalFromTangentSpace(nmmp, 1.0, normalize(normal.xyz), normalize(tangent), 1.0);
  157. #endif
  158. surfaceData.worldNormal = normalize(normal);
  159. surfaceData.worldTangent = normalize(tangent);
  160. surfaceData.worldBinormal = normalize(binormal);
  161. float roughnessValue = 1.0;
  162. float metallicValue = 0.0;
  163. #if USE_PBR
  164. #if LAYERS == 1
  165. roughnessValue = roughness.x;
  166. #elif LAYERS == 2
  167. roughnessValue += roughness.x * w.r;
  168. roughnessValue += roughness.y * w.g;
  169. #elif LAYERS == 3
  170. roughnessValue += roughness.x * w.r;
  171. roughnessValue += roughness.y * w.g;
  172. roughnessValue += roughness.z * w.b;
  173. #elif LAYERS == 4
  174. roughnessValue += roughness.x * w.r;
  175. roughnessValue += roughness.y * w.g;
  176. roughnessValue += roughness.z * w.b;
  177. roughnessValue += roughness.w * w.a;
  178. #endif
  179. #if LAYERS == 1
  180. metallicValue = metallic.x;
  181. #elif LAYERS == 2
  182. metallicValue += metallic.x * w.r;
  183. metallicValue += metallic.y * w.g;
  184. #elif LAYERS == 3
  185. metallicValue += metallic.x * w.r;
  186. metallicValue += metallic.y * w.g;
  187. metallicValue += metallic.z * w.b;
  188. #elif LAYERS == 4
  189. metallicValue += metallic.x * w.r;
  190. metallicValue += metallic.y * w.g;
  191. metallicValue += metallic.z * w.b;
  192. metallicValue += metallic.w * w.a;
  193. #endif
  194. #endif
  195. surfaceData.ao = 1.0;
  196. surfaceData.roughness = roughnessValue;
  197. surfaceData.metallic = metallicValue;
  198. surfaceData.specularIntensity = 0.5;
  199. surfaceData.emissive = vec3(0.0);
  200. }
  201. }%
  202. CCProgram terrain-vs %{
  203. precision highp float;
  204. #include <builtin/uniforms/cc-local>
  205. // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros
  206. #include <macro-remapping>
  207. #include <surfaces/effect-macros/terrain>
  208. // 2. common include with corresponding shader stage, include before surface functions
  209. #include <surfaces/includes/common-vs>
  210. // 3. user surface functions that can use user (effect) parameters (ubo Constants)
  211. // see surfaces/default-functions/xxx.chunk
  212. #include <shared-ubos>
  213. #include <surface-vertex>
  214. // 4. surface include with corresponding shader stage and shading-model (optional)
  215. #include <surfaces/includes/standard-vs>
  216. // 5. shader entry with corresponding shader stage and technique usage/type
  217. #include <shading-entries/main-functions/render-to-scene/vs>
  218. }%
  219. CCProgram shadow-caster-vs %{
  220. precision highp float;
  221. #include <builtin/uniforms/cc-local>
  222. #include <surfaces/effect-macros/render-to-shadowmap>
  223. #include <surfaces/includes/common-vs>
  224. #include <shared-ubos>
  225. #include <surface-vertex>
  226. #include <shading-entries/main-functions/render-to-shadowmap/vs>
  227. }%
  228. CCProgram terrain-fs %{
  229. precision highp float;
  230. // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros
  231. #include <macro-remapping>
  232. #include <surfaces/effect-macros/terrain>
  233. // 2. common include with corresponding shader stage, include before surface functions
  234. #include <surfaces/includes/common-fs>
  235. // 3. user surface functions that can use user (effect) parameters (ubo Constants)
  236. // see surfaces/default-functions/xxx.chunk
  237. #include <shared-ubos>
  238. #include <surface-fragment>
  239. // 4. lighting-model (optional)
  240. #include <lighting-models/includes/standard>
  241. // 5. surface include with corresponding shader stage and shading-model (optional)
  242. #include <surfaces/includes/standard-fs>
  243. // 6. shader entry with corresponding shader stage and technique usage/type
  244. #include <shading-entries/main-functions/render-to-scene/fs>
  245. }%
  246. CCProgram shadow-caster-fs %{
  247. precision highp float;
  248. #include <surfaces/effect-macros/render-to-shadowmap>
  249. #include <surfaces/includes/common-fs>
  250. #include <shared-ubos>
  251. #include <surface-fragment>
  252. #include <shading-entries/main-functions/render-to-shadowmap/fs>
  253. }%
  254. CCProgram reflect-map-fs %{
  255. precision highp float;
  256. #include <macro-remapping>
  257. #include <surfaces/effect-macros/terrain>
  258. #include <surfaces/includes/common-fs>
  259. #include <shared-ubos>
  260. #include <surface-fragment>
  261. #include <lighting-models/includes/standard>
  262. #include <surfaces/includes/standard-fs>
  263. #include <shading-entries/main-functions/render-to-reflectmap/fs>
  264. }%