imported-metallic-roughness.effect 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // Copyright (c) 2017-2022 Xiamen Yaji Software Co., Ltd.
  2. CCEffect %{
  3. techniques:
  4. - name: opaque
  5. passes:
  6. - vert: standard-vs
  7. frag: standard-fs
  8. properties: &props
  9. tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] }
  10. mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, linear: true, editor: { displayName: BaseColor, type: color } }
  11. mainTexture: { value: grey, target: albedoMap, editor: { displayName: BaseColorMap } }
  12. baseWeightMap: { value: grey }
  13. albedoScale: { value: 1.0, editor: { displayName: BaseWeight } }
  14. roughness: { value: 1.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  15. roughnessMap: { value: grey }
  16. metallic: { value: 1.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  17. metallicMap: { value: grey }
  18. occlusion: { value: 0.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  19. occlusionMap: { value: white }
  20. specularIntensity: { value: 0.5, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  21. emissiveScale: { value: 1.0 }
  22. emissiveScaleMap: { value: grey }
  23. emissive: { value: [0.0, 0.0, 0.0, 1.0], linear: true, editor: { displayName: EmissiveColor, type: color} }
  24. emissiveMap: { value: grey, editor: { displayName: EmissiveColorMap } }
  25. alphaSource: { value: 1.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  26. alphaSourceMap: { value: grey, editor: { parent: USE_OPACITY_MAP } }
  27. alphaThreshold: { value: 0.5, editor: { parent: USE_ALPHA_TEST, slide: true, range: [0, 1.0], step: 0.001 } }
  28. normalStrength: { value: 1.0, editor: { parent: USE_NORMAL_MAP, slide: true, range: [0, 1.0], step: 0.001 } }
  29. normalMap: { value: normal }
  30. - &forward-add
  31. vert: standard-vs
  32. frag: standard-fs
  33. phase: forward-add
  34. propertyIndex: 0
  35. embeddedMacros: { CC_FORWARD_ADD: true }
  36. depthStencilState:
  37. depthFunc: equal
  38. depthTest: true
  39. depthWrite: false
  40. blendState:
  41. targets:
  42. - blend: true
  43. blendSrc: one
  44. blendDst: one
  45. blendSrcAlpha: zero
  46. blendDstAlpha: one
  47. - &shadow-caster
  48. vert: shadow-caster-vs
  49. frag: shadow-caster-fs
  50. phase: shadow-caster
  51. propertyIndex: 0
  52. rasterizerState:
  53. cullMode: front
  54. properties:
  55. tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] }
  56. mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, editor: { displayName: Albedo, type: color } }
  57. albedoScale: { value: 1.0, editor: { displayName: BaseWeight } }
  58. alphaThreshold: { value: 0.5, editor: { parent: USE_ALPHA_TEST } }
  59. mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } }
  60. alphaSource: { value: 1.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  61. alphaSourceMap: { value: grey, editor: { parent: USE_OPACITY_MAP } }
  62. - &deferred-forward
  63. vert: standard-vs
  64. frag: standard-fs
  65. propertyIndex: 0
  66. phase: deferred-forward
  67. - name: transparent
  68. passes:
  69. - vert: standard-vs
  70. frag: standard-fs
  71. embeddedMacros: { CC_FORCE_FORWARD_SHADING: true }
  72. depthStencilState: &d1
  73. depthTest: true
  74. depthWrite: false
  75. blendState: &b1
  76. targets:
  77. - blend: true
  78. blendSrc: src_alpha
  79. blendDst: one_minus_src_alpha
  80. blendDstAlpha: one_minus_src_alpha
  81. properties: *props
  82. - *forward-add
  83. - *shadow-caster
  84. - &deferred-forward
  85. vert: standard-vs
  86. frag: standard-fs
  87. phase: deferred-forward
  88. embeddedMacros: { CC_PIPELINE_TYPE: 0 }
  89. depthStencilState: *d1
  90. blendState: *b1
  91. propertyIndex: 0
  92. }%
  93. CCProgram shared-ubos %{
  94. uniform Constants {
  95. vec4 tilingOffset;
  96. vec4 albedo;
  97. vec4 emissive;
  98. float emissiveScale;
  99. float occlusion;
  100. float roughness;
  101. float metallic;
  102. float normalStrength;
  103. float alphaSource;
  104. float albedoScale;
  105. float alphaThreshold;
  106. float specularIntensity;
  107. };
  108. }%
  109. CCProgram macro-remapping %{
  110. // ui displayed macros
  111. #pragma define-meta HAS_SECOND_UV
  112. #pragma define-meta USE_TWOSIDE
  113. #pragma define-meta USE_REFLECTION_DENOISE
  114. #pragma define-meta IS_ANISOTROPY
  115. #pragma define-meta USE_VERTEX_COLOR
  116. #define CC_SURFACES_USE_SECOND_UV HAS_SECOND_UV
  117. #define CC_SURFACES_USE_TWO_SIDED USE_TWOSIDE
  118. #define CC_SURFACES_USE_REFLECTION_DENOISE USE_REFLECTION_DENOISE
  119. #define CC_SURFACES_LIGHTING_ANISOTROPIC IS_ANISOTROPY
  120. #define CC_SURFACES_USE_VERTEX_COLOR USE_VERTEX_COLOR
  121. // if disabled, simulate convoluted IBL without convolution
  122. #pragma define-meta USE_COMPATIBLE_LIGHTING
  123. #define CC_SURFACES_USE_LEGACY_COMPATIBLE_LIGHTING USE_COMPATIBLE_LIGHTING
  124. // depend on UI macros
  125. #if IS_ANISOTROPY || USE_NORMAL_MAP
  126. #define CC_SURFACES_USE_TANGENT_SPACE 1
  127. #endif
  128. // functionality for each effect
  129. #define CC_SURFACES_LIGHTING_ANISOTROPIC_ENVCONVOLUTION_COUNT 31
  130. }%
  131. CCProgram surface-vertex %{
  132. #define CC_SURFACES_VERTEX_MODIFY_UV
  133. void SurfacesVertexModifyUV(inout SurfacesStandardVertexIntermediate In)
  134. {
  135. In.texCoord = In.texCoord * tilingOffset.xy + tilingOffset.zw;
  136. #if CC_SURFACES_USE_SECOND_UV
  137. In.texCoord1 = In.texCoord1 * tilingOffset.xy + tilingOffset.zw;
  138. #endif
  139. }
  140. }%
  141. CCProgram surface-fragment %{
  142. #pragma define-meta TEXTURE_UV options([v_uv, v_uv1])
  143. #if USE_ALBEDO_MAP
  144. uniform sampler2D albedoMap;
  145. #endif
  146. #if USE_WEIGHT_MAP
  147. uniform sampler2D baseWeightMap;
  148. #endif
  149. #if USE_METALLIC_MAP
  150. #pragma define-meta METALLIC_CHANNEL options([r, g, b, a])
  151. uniform sampler2D metallicMap;
  152. #endif
  153. #if USE_ROUGHNESS_MAP
  154. #pragma define-meta ROUGHNESS_CHANNEL options([r, g, b, a])
  155. uniform sampler2D roughnessMap;
  156. #endif
  157. #if USE_OCCLUSION_MAP
  158. #pragma define-meta OCCLUSION_CHANNEL options([r, g, b, a])
  159. uniform sampler2D occlusionMap;
  160. #endif
  161. #if USE_TRANSPARENCY_MAP
  162. uniform sampler2D transparencyMap;
  163. #endif
  164. #if USE_TRANSPARENCYCOLOR_MAP
  165. uniform sampler2D transparencyColorMap;
  166. #endif
  167. #if USE_EMISSIVE_MAP
  168. uniform sampler2D emissiveMap;
  169. #endif
  170. #if USE_EMISSIVESCALE_MAP
  171. uniform sampler2D emissiveScaleMap;
  172. #endif
  173. #if USE_OPACITY_MAP
  174. #pragma define-meta ALPHA_SOURCE_CHANNEL options([r, g, b, a])
  175. uniform sampler2D alphaSourceMap;
  176. #endif
  177. #if USE_NORMAL_MAP
  178. uniform sampler2D normalMap;
  179. #pragma define-meta NORMAL_UV options([v_uv, v_uv1])
  180. #endif
  181. float discolor(vec3 srcColor) {
  182. return dot(GRAY_VECTOR, srcColor);
  183. }
  184. #define CC_SURFACES_FRAGMENT_MODIFY_BASECOLOR_AND_TRANSPARENCY
  185. vec4 SurfacesFragmentModifyBaseColorAndTransparency()
  186. {
  187. vec4 baseColor = albedo;
  188. #if USE_VERTEX_COLOR
  189. baseColor.rgb *= SRGBToLinear(FSInput_vertexColor.rgb); // use linear
  190. baseColor.a *= FSInput_vertexColor.a;
  191. #endif
  192. #if USE_ALBEDO_MAP
  193. vec4 texColor = texture(albedoMap, TEXTURE_UV);
  194. texColor.rgb = SRGBToLinear(texColor.rgb);
  195. baseColor = texColor;
  196. #endif
  197. #if USE_WEIGHT_MAP
  198. vec4 weightColor = texture(baseWeightMap, TEXTURE_UV);
  199. weightColor.rgb = SRGBToLinear(weightColor.rgb);
  200. baseColor.rgb *= weightColor.rgb;
  201. #else
  202. baseColor.rgb *= albedoScale;
  203. #endif
  204. #if ALPHA_SOURCE_IS_OPACITY
  205. #if USE_OPACITY_MAP
  206. baseColor.a = 1.0 - texture(alphaSourceMap, TEXTURE_UV).ALPHA_SOURCE_CHANNEL;
  207. #else
  208. baseColor.a = 1.0 - alphaSource;
  209. #endif
  210. #else
  211. #if USE_OPACITY_MAP
  212. baseColor.a = texture(alphaSourceMap, TEXTURE_UV).ALPHA_SOURCE_CHANNEL;
  213. #else
  214. baseColor.a = alphaSource;
  215. #endif
  216. #endif
  217. #if USE_ALPHA_TEST
  218. if (baseColor.a < alphaThreshold) discard;
  219. #endif
  220. return baseColor;
  221. }
  222. #define CC_SURFACES_FRAGMENT_ALPHA_CLIP_ONLY
  223. void SurfacesFragmentAlphaClipOnly()
  224. {
  225. #if USE_ALPHA_TEST
  226. float alpha = albedo.a;
  227. #if USE_ALBEDO_MAP
  228. alpha = texture(albedoMap, TEXTURE_UV).a;
  229. #endif
  230. #if ALPHA_SOURCE_IS_OPACITY
  231. #if USE_OPACITY_MAP
  232. alpha = 1.0 - texture(alphaSourceMap, TEXTURE_UV).ALPHA_SOURCE_CHANNEL;
  233. #else
  234. alpha = 1.0 - alphaSource;
  235. #endif
  236. #else
  237. #if USE_OPACITY_MAP
  238. alpha = texture(alphaSourceMap, TEXTURE_UV).ALPHA_SOURCE_CHANNEL;
  239. #else
  240. alpha = alphaSource;
  241. #endif
  242. #endif
  243. if (alpha < alphaThreshold) discard;
  244. #endif
  245. }
  246. #define CC_SURFACES_FRAGMENT_MODIFY_WORLD_NORMAL
  247. vec3 SurfacesFragmentModifyWorldNormal()
  248. {
  249. vec3 normal = FSInput_worldNormal;
  250. #if USE_NORMAL_MAP
  251. vec3 nmmp = texture(normalMap, NORMAL_UV).xyz - vec3(0.5);
  252. normal = CalculateNormalFromTangentSpace(nmmp, normalStrength, normalize(normal.xyz), normalize(FSInput_worldTangent), FSInput_mirrorNormal);
  253. #endif
  254. return normalize(normal);
  255. }
  256. #define CC_SURFACES_FRAGMENT_MODIFY_EMISSIVE
  257. vec3 SurfacesFragmentModifyEmissive()
  258. {
  259. //emissive color
  260. vec3 emissiveColor = emissive.rgb;
  261. #if USE_EMISSIVE_MAP
  262. emissiveColor.rgb = SRGBToLinear(texture(emissiveMap, TEXTURE_UV).rgb);
  263. #endif
  264. //emissive color scale
  265. #if USE_EMISSIVESCALE_MAP
  266. vec4 emissiveScaleColor = texture(emissiveScaleMap, TEXTURE_UV);
  267. emissiveScaleColor.rgb = SRGBToLinear(emissiveScaleColor.rgb);
  268. emissiveColor.rgb *= emissiveScaleColor.rgb;
  269. #else
  270. emissiveColor.rgb *= emissiveScale;
  271. #endif
  272. return emissiveColor.rgb;
  273. }
  274. #define CC_SURFACES_FRAGMENT_MODIFY_PBRPARAMS
  275. vec4 SurfacesFragmentModifyPBRParams()
  276. {
  277. vec4 pbr = vec4(1.0,1.0,1.0,1.0);
  278. //ao
  279. float occlusionValue = 1.0;
  280. #if USE_OCCLUSION_MAP
  281. vec4 occlusionColor = texture(occlusionMap, TEXTURE_UV);
  282. float occlusionColorValue = discolor(occlusionColor.rgb);
  283. #if USE_OCCLUSION_CHANNEL
  284. occlusionColor.rgb = SRGBToLinear(occlusionColor.rgb);
  285. occlusionColorValue = occlusionColor.OCCLUSION_CHANNEL;
  286. #endif
  287. occlusionValue = mix(1.0, occlusionColorValue, occlusion);
  288. #endif
  289. pbr.x = occlusionValue;
  290. //roughness
  291. float roughnessValue = roughness;
  292. #if USE_ROUGHNESS_MAP
  293. vec4 roughnessColor = texture(roughnessMap, TEXTURE_UV);
  294. roughnessValue = discolor(roughnessColor.rgb);
  295. #if USE_ROUGHNESS_CHANNEL
  296. roughnessColor.rgb = SRGBToLinear(roughnessColor.rgb);
  297. roughnessValue = roughnessColor.ROUGHNESS_CHANNEL;
  298. #endif
  299. #endif
  300. //fit specular ior=1.5
  301. pbr.y = max(0.02, roughnessValue);
  302. //metallic
  303. float metallicValue = metallic;
  304. #if USE_METALLIC_MAP
  305. vec4 metallicColor = texture(metallicMap, TEXTURE_UV);
  306. metallicValue = discolor(metallicColor.rgb);
  307. #if USE_METALLIC_CHANNEL
  308. metallicColor.rgb = SRGBToLinear(metallicColor.rgb);
  309. metallicValue = metallicColor.METALLIC_CHANNEL;
  310. #endif
  311. #endif
  312. pbr.z = metallicValue;
  313. //specularIntensity
  314. pbr.w = specularIntensity;
  315. return pbr;
  316. }
  317. }%
  318. CCProgram standard-vs %{
  319. precision highp float;
  320. // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros
  321. #include <macro-remapping>
  322. #include <surfaces/effect-macros/common-macros>
  323. // 2. common include with corresponding shader stage, include before surface functions
  324. #include <surfaces/includes/common-vs>
  325. // 3. user surface functions that can use user (effect) parameters (ubo Constants)
  326. // see surfaces/default-functions/xxx.chunk
  327. #include <shared-ubos>
  328. #include <surface-vertex>
  329. // 4. surface include with corresponding shader stage and shading-model (optional)
  330. #include <surfaces/includes/standard-vs>
  331. // 5. shader entry with corresponding shader stage and technique usage/type
  332. #include <shading-entries/main-functions/render-to-scene/vs>
  333. }%
  334. CCProgram shadow-caster-vs %{
  335. precision highp float;
  336. #include <macro-remapping>
  337. #include <surfaces/effect-macros/render-to-shadowmap>
  338. #include <surfaces/includes/common-vs>
  339. #include <shared-ubos>
  340. #include <surface-vertex>
  341. #include <shading-entries/main-functions/render-to-shadowmap/vs>
  342. }%
  343. CCProgram standard-fs %{
  344. // shading-model : standard
  345. // lighting-model : standard (isotropy / anisotropy pbr)
  346. // shader stage : fs
  347. // technique usage/type : render-to-scene
  348. precision highp float;
  349. // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros
  350. #include <macro-remapping>
  351. #include <surfaces/effect-macros/common-macros>
  352. // 2. common include with corresponding shader stage, include before surface functions
  353. #include <surfaces/includes/common-fs>
  354. // 3. user surface functions that can use user (effect) parameters (ubo Constants)
  355. // see surfaces/default-functions/xxx.chunk
  356. #include <shared-ubos>
  357. #include <surface-fragment>
  358. // 4. lighting-model (optional)
  359. #include <lighting-models/includes/standard>
  360. // 5. surface include with corresponding shader stage and shading-model (optional)
  361. #include <surfaces/includes/standard-fs>
  362. // 6. shader entry with corresponding shader stage and technique usage/type
  363. #include <shading-entries/main-functions/render-to-scene/fs>
  364. }%
  365. CCProgram shadow-caster-fs %{
  366. precision highp float;
  367. #include <macro-remapping>
  368. #include <surfaces/effect-macros/render-to-shadowmap>
  369. #include <surfaces/includes/common-fs>
  370. #include <shared-ubos>
  371. #include <surface-fragment>
  372. #include <shading-entries/main-functions/render-to-shadowmap/fs>
  373. }%