builtin-standard_rimlight.effect 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright (c) 2017-2019 Xiamen Yaji Software Co., Ltd.
  2. CCEffect %{
  3. techniques:
  4. - name: opaque
  5. passes:
  6. - vert: standard-vs:vert
  7. frag: standard-fs:frag
  8. properties: &props
  9. tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] }
  10. rimLightColor: { value: [1.0, 1.0, 1.0, 1.0], target: rimColor, editor: { displayName: Rim Color, type: color } }
  11. mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, editor: { displayName: Albedo, type: color } }
  12. albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz }
  13. alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } }
  14. occlusion: { value: 1.0, target: pbrParams.x }
  15. roughness: { value: 0.8, target: pbrParams.y }
  16. metallic: { value: 0.6, target: pbrParams.z }
  17. normalStrenth: { value: 1.0, target: pbrParams.w, editor: { parent: USE_NORMAL_MAP } }
  18. emissive: { value: [0.0, 0.0, 0.0, 1.0], editor: { type: color } }
  19. emissiveScale: { value: [1.0, 1.0, 1.0], target: emissiveScaleParam.xyz }
  20. mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } }
  21. normalMap: { value: normal }
  22. pbrMap: { value: grey }
  23. metallicRoughnessMap: { value: grey }
  24. occlusionMap: { value: white }
  25. emissiveMap: { value: grey }
  26. migrations: &migs # existing material data migration
  27. properties:
  28. mainColor: { formerlySerializedAs: albedo }
  29. alphaThreshold: { formerlySerializedAs: albedoScale.w }
  30. normalStrenth: { formerlySerializedAs: pbrScale.w }
  31. mainTexture: { formerlySerializedAs: albedoMap }
  32. - name: transparent
  33. passes:
  34. - vert: standard-vs:vert
  35. frag: standard-fs:frag
  36. depthStencilState:
  37. depthTest: true
  38. depthWrite: false
  39. blendState:
  40. targets:
  41. - blend: true
  42. blendSrc: src_alpha
  43. blendDst: one_minus_src_alpha
  44. blendDstAlpha: one_minus_src_alpha
  45. properties: *props
  46. migrations: *migs
  47. }%
  48. CCProgram shared-ubos %{
  49. uniform Constants {
  50. vec4 tilingOffset;
  51. vec4 rimColor;
  52. vec4 albedo;
  53. vec4 albedoScaleAndCutoff;
  54. vec4 pbrParams;
  55. vec4 emissive;
  56. vec4 emissiveScaleParam;
  57. vec4 fogParams;
  58. };
  59. }%
  60. CCProgram standard-vs %{
  61. precision highp float;
  62. #include <input-standard>
  63. #include <cc-global>
  64. #include <cc-local-batch>
  65. #include <shared-ubos>
  66. #if USE_VERTEX_COLOR
  67. in vec3 a_color;
  68. out vec3 v_color;
  69. #endif
  70. out vec3 v_position;
  71. out vec3 v_normal;
  72. out vec3 v_view_normal;
  73. out vec3 v_view_position;
  74. #if USE_NORMAL_MAP
  75. out vec3 v_tangent;
  76. out vec3 v_bitangent;
  77. #endif
  78. in vec2 a_texCoord;
  79. out vec2 v_uv;
  80. in vec2 a_texCoord1;
  81. out vec2 v_uv1;
  82. #if USE_LIGHTMAP && !USE_BATCHING && !USE_INSTANCING
  83. out vec2 v_luv;
  84. #endif
  85. vec4 vert () {
  86. StandardVertInput In;
  87. CCVertInput(In);
  88. mat4 matWorld, matWorldIT;
  89. CCGetWorldMatrixFull(matWorld, matWorldIT);
  90. vec4 pos = matWorld * In.position;
  91. v_position = pos.xyz;
  92. vec4 normal = vec4(In.normal,0.0);
  93. v_normal = normalize((matWorldIT * normal).xyz);
  94. v_view_normal = normalize(((cc_matView * matWorldIT) * normal).xyz);
  95. vec4 vpos = (cc_matView * matWorld) * In.position;
  96. v_view_position = vpos.xyz;
  97. #if USE_NORMAL_MAP
  98. v_tangent = normalize((matWorld * vec4(In.tangent.xyz, 0.0)).xyz);
  99. v_bitangent = cross(v_normal, v_tangent) * In.tangent.w; // note the cross order
  100. #endif
  101. v_uv = a_texCoord * tilingOffset.xy + tilingOffset.zw;
  102. #if HAS_SECOND_UV
  103. v_uv1 = a_texCoord1 * tilingOffset.xy + tilingOffset.zw;
  104. #endif
  105. #if USE_VERTEX_COLOR
  106. v_color = a_color;
  107. #endif
  108. #if USE_LIGHTMAP && HAS_SECOND_UV && !USE_BATCHING && !USE_INSTANCING
  109. v_luv = cc_lightingMapUVParam.xy + a_texCoord1 * cc_lightingMapUVParam.zw;
  110. #endif
  111. return cc_matProj * (cc_matView * matWorld) * In.position;
  112. }
  113. }%
  114. CCProgram standard-fs %{
  115. precision highp float;
  116. #include <cc-global>
  117. #include <shading-standard>
  118. #include <output-standard>
  119. #include <gamma>
  120. #include <shared-ubos>
  121. in vec3 v_position;
  122. in vec3 v_view_position;
  123. in vec2 v_uv;
  124. in vec2 v_uv1;
  125. in vec3 v_normal;
  126. in vec3 v_view_normal;
  127. #if USE_VERTEX_COLOR
  128. in vec3 v_color;
  129. #endif
  130. #if USE_ALBEDO_MAP
  131. uniform sampler2D albedoMap;
  132. #pragma define ALBEDO_UV options([v_uv, v_uv1])
  133. #endif
  134. #if USE_NORMAL_MAP
  135. in vec3 v_tangent;
  136. in vec3 v_bitangent;
  137. uniform sampler2D normalMap;
  138. #pragma define NORMAL_UV options([v_uv, v_uv1])
  139. #endif
  140. #if USE_PBR_MAP
  141. uniform sampler2D pbrMap;
  142. #pragma define PBR_UV options([v_uv, v_uv1])
  143. #endif
  144. #if USE_METALLIC_ROUGHNESS_MAP
  145. uniform sampler2D metallicRoughnessMap;
  146. #pragma define METALLIC_ROUGHNESS_UV options([v_uv, v_uv1])
  147. #endif
  148. #if USE_OCCLUSION_MAP
  149. uniform sampler2D occlusionMap;
  150. #pragma define OCCLUSION_UV options([v_uv, v_uv1])
  151. #endif
  152. #if USE_EMISSIVE_MAP
  153. uniform sampler2D emissiveMap;
  154. #pragma define EMISSIVE_UV options([v_uv, v_uv1])
  155. #endif
  156. #if USE_LIGHTMAP
  157. in vec2 v_luv;
  158. #pragma builtin(local)
  159. layout(binding = 30) uniform sampler2D cc_lightingMap;
  160. #endif
  161. #pragma define OCCLUSION_CHANNEL options([r, g, b])
  162. #pragma define ROUGHNESS_CHANNEL options([g, b, r])
  163. #pragma define METALLIC_CHANNEL options([b, r, g])
  164. #if USE_ALPHA_TEST
  165. #pragma define ALPHA_TEST_CHANNEL options([a, r])
  166. #endif
  167. void surf (out StandardSurface s) {
  168. vec4 baseColor = albedo;
  169. #if USE_VERTEX_COLOR
  170. baseColor.rgb *= v_color;
  171. #endif
  172. #if USE_ALBEDO_MAP
  173. vec4 texColor = texture(albedoMap, ALBEDO_UV);
  174. texColor.rgb = SRGBToLinear(texColor.rgb);
  175. baseColor *= texColor;
  176. #endif
  177. s.albedo = baseColor;
  178. s.albedo.rgb *= albedoScaleAndCutoff.xyz;
  179. #if USE_ALPHA_TEST
  180. if (s.albedo.ALPHA_TEST_CHANNEL < albedoScaleAndCutoff.w) discard;
  181. #endif
  182. //fog compute
  183. float density = 0.001;//fogParams.w;
  184. float dist2 = dot(v_view_position,v_view_position);
  185. float fogFactor = exp( -density*density * dist2);
  186. fogFactor = clamp(fogFactor, 0.0, 1.0);
  187. vec3 fogColor = vec3(1.0,1,1.0);//fogParams.xyz;
  188. s.albedo.rgb = mix(fogColor,s.albedo.rgb,fogFactor);
  189. s.normal = v_normal;
  190. #if USE_NORMAL_MAP
  191. vec3 nmmp = texture(normalMap, NORMAL_UV).xyz - vec3(0.5);
  192. s.normal =
  193. (nmmp.x * pbrParams.w) * normalize(v_tangent) +
  194. (nmmp.y * pbrParams.w) * normalize(v_bitangent) +
  195. nmmp.z * normalize(s.normal);
  196. #endif
  197. s.position = v_position;
  198. vec4 pbr = pbrParams;
  199. #if USE_PBR_MAP
  200. vec4 res = texture(pbrMap, PBR_UV);
  201. pbr.x *= res.OCCLUSION_CHANNEL;
  202. pbr.y *= res.ROUGHNESS_CHANNEL;
  203. pbr.z *= res.METALLIC_CHANNEL;
  204. #endif
  205. #if USE_METALLIC_ROUGHNESS_MAP
  206. vec4 metallicRoughness = texture(metallicRoughnessMap, METALLIC_ROUGHNESS_UV);
  207. pbr.z *= metallicRoughness.METALLIC_CHANNEL;
  208. pbr.y *= metallicRoughness.ROUGHNESS_CHANNEL;
  209. #endif
  210. #if USE_OCCLUSION_MAP
  211. pbr.x *= texture(occlusionMap, OCCLUSION_UV).OCCLUSION_CHANNEL;
  212. #endif
  213. s.occlusion = clamp(pbr.x, 0.0, 0.96);
  214. s.roughness = clamp(pbr.y, 0.04, 1.0);
  215. s.metallic = pbr.z;
  216. s.emissive = emissive.rgb * emissiveScaleParam.xyz;
  217. #if USE_EMISSIVE_MAP
  218. s.emissive *= SRGBToLinear(texture(emissiveMap, EMISSIVE_UV).rgb);
  219. #endif
  220. }
  221. vec4 frag () {
  222. StandardSurface s; surf(s);
  223. vec4 color = CCStandardShading(s);
  224. #if USE_LIGHTMAP && !USE_BATCHING && !USE_INSTANCING
  225. vec4 lighting = texture(cc_lightingMap, v_luv);
  226. float fAmb = 0.5 - s.normal.y * 0.5;
  227. vec3 ambDiff = mix(cc_ambientSky.rgb, cc_ambientGround.rgb, fAmb) * cc_ambientSky.w;
  228. vec3 finalColor = (ambDiff.rgb * s.albedo.rgb);
  229. finalColor += lighting.rgb * s.albedo.rgb;
  230. finalColor = finalColor * s.occlusion;
  231. finalColor += s.emissive;
  232. color.rgb = lighting.a * finalColor + (1.0 - lighting.a) * color.rgb;
  233. #endif
  234. float fRim = (1.0 - dot(normalize(v_view_normal),vec3(0,0,1.0))) * rimColor.w;
  235. color.rgb = mix(color.rgb,rimColor.rgb,fRim);
  236. //color.rgb = mix(vec3(0.0,0.0,0.0),rimColor.rgb,fRim);
  237. return CCFragOutput(color);
  238. }
  239. }%