imported-specular-glossiness.effect 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // Copyright (c) 2017-2020 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. mainTexture: { value: grey, target: albedoMap, editor: { displayName: DiffuseMap } }
  10. mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: diffuseColor, linear: true, editor: { displayName: DiffuseColor, type: color } }
  11. albedoScale: { value: 1.0, target: diffuseFactor, editor: { displayName: diffuseFactor } }
  12. alphaThreshold: { value: 0.5, editor: { parent: USE_ALPHA_TEST, slide: true, range: [0, 1.0], step: 0.001 } }
  13. emissive: { value: [0.0, 0.0, 0.0, 1.0], linear: true, editor: { type: color } }
  14. emissiveMap: { value: grey }
  15. emissiveScale: { value: 1.0 }
  16. emissiveScaleMap: { value: grey }
  17. shininessExponentMap: { value: grey }
  18. shininessExponent: { value: 100.0 }
  19. specularGlossinessMap: { value: grey }
  20. specularColor: { value: [0.0, 0.0, 0.0, 0.0], linear: true, editor: { displayName: SpecularColor, type: color } }
  21. specularMap: { value: grey }
  22. specularFactor: { value: 1.0 }
  23. transparencyMap: { value: grey, editor: { displayName: TransparencyMap } }
  24. transparencyFactor: { value: 1.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  25. tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] }
  26. normalStrength: { value: 1.0, target: normalScale, editor: { displayName: bumpFactor, parent: USE_NORMAL_MAP, slide: true, range: [0, 5.0], step: 0.001 } }
  27. normalMap: { value: normal }
  28. glossiness: { value: 0.0, editor: { parent: HAS_EXPORTED_GLOSSINESS, slide: true, range: [0, 1.0], step: 0.001 } }
  29. metallic: { value: 0.0, editor: { parent: HAS_EXPORTED_METALLIC, slide: true, range: [0, 1.0], step: 0.001 } }
  30. metallicMap: { value: grey, editor: { parent: HAS_EXPORTED_METALLIC } }
  31. occlusionMap: { value: white }
  32. - &forward-add
  33. vert: standard-vs
  34. frag: standard-fs
  35. phase: forward-add
  36. propertyIndex: 0
  37. embeddedMacros: { CC_FORWARD_ADD: true }
  38. depthStencilState:
  39. depthFunc: equal
  40. depthTest: true
  41. depthWrite: false
  42. blendState:
  43. targets:
  44. - blend: true
  45. blendSrc: one
  46. blendDst: one
  47. blendSrcAlpha: zero
  48. blendDstAlpha: one
  49. - &shadow-caster
  50. vert: shadow-caster-vs
  51. frag: shadow-caster-fs
  52. phase: shadow-caster
  53. propertyIndex: 0
  54. rasterizerState:
  55. cullMode: front
  56. properties:
  57. tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] }
  58. mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: diffuseColor, editor: { displayName: Albedo, type: color } }
  59. albedoScale: { value: 1.0, target: diffuseFactor, editor: { displayName: diffuseFactor } }
  60. alphaThreshold: { value: 0.5, editor: { parent: USE_ALPHA_TEST } }
  61. mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } }
  62. transparencyMap: { value: grey, editor: { displayName: TransparencyMap } }
  63. transparencyFactor: { value: 1.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  64. - &deferred-forward
  65. vert: standard-vs
  66. frag: standard-fs
  67. phase: deferred-forward
  68. propertyIndex: 0
  69. - name: transparent
  70. passes:
  71. - vert: standard-vs
  72. frag: standard-fs
  73. embeddedMacros: { CC_FORCE_FORWARD_SHADING: true }
  74. depthStencilState: &d1
  75. depthTest: true
  76. depthWrite: false
  77. blendState: &b1
  78. targets:
  79. - blend: true
  80. blendSrc: src_alpha
  81. blendDst: one_minus_src_alpha
  82. blendDstAlpha: one_minus_src_alpha
  83. properties: *props
  84. - *forward-add
  85. - *shadow-caster
  86. - &deferred-forward
  87. vert: standard-vs
  88. frag: standard-fs
  89. phase: deferred-forward
  90. embeddedMacros: { CC_PIPELINE_TYPE: 0 }
  91. depthStencilState: *d1
  92. blendState: *b1
  93. propertyIndex: 0
  94. }%
  95. CCProgram shared-ubos %{
  96. uniform Constants {
  97. vec4 tilingOffset;
  98. vec4 diffuseColor;
  99. vec4 specularColor;
  100. vec4 emissive;
  101. float emissiveScale;
  102. float alphaThreshold;
  103. float shininessExponent;
  104. float glossiness;
  105. float metallic;
  106. float normalScale;
  107. float transparencyFactor;
  108. float diffuseFactor;
  109. float specularFactor;
  110. };
  111. }%
  112. CCProgram macro-remapping %{
  113. // ui displayed macros
  114. #pragma define-meta HAS_SECOND_UV
  115. #pragma define-meta USE_TWOSIDE
  116. #pragma define-meta USE_REFLECTION_DENOISE
  117. #pragma define-meta IS_ANISOTROPY
  118. #pragma define-meta USE_VERTEX_COLOR
  119. #define CC_SURFACES_USE_SECOND_UV HAS_SECOND_UV
  120. #define CC_SURFACES_USE_TWO_SIDED USE_TWOSIDE
  121. #define CC_SURFACES_USE_REFLECTION_DENOISE USE_REFLECTION_DENOISE
  122. #define CC_SURFACES_LIGHTING_ANISOTROPIC IS_ANISOTROPY
  123. #define CC_SURFACES_USE_VERTEX_COLOR USE_VERTEX_COLOR
  124. // if disabled, simulate convoluted IBL without convolution
  125. #pragma define-meta USE_COMPATIBLE_LIGHTING
  126. #define CC_SURFACES_USE_LEGACY_COMPATIBLE_LIGHTING USE_COMPATIBLE_LIGHTING
  127. // depend on UI macros
  128. #if IS_ANISOTROPY || USE_NORMAL_MAP
  129. #define CC_SURFACES_USE_TANGENT_SPACE 1
  130. #endif
  131. // functionality for each effect
  132. #define CC_SURFACES_LIGHTING_ANISOTROPIC_ENVCONVOLUTION_COUNT 31
  133. }%
  134. CCProgram surface-vertex %{
  135. #define CC_SURFACES_VERTEX_MODIFY_UV
  136. void SurfacesVertexModifyUV(inout SurfacesStandardVertexIntermediate In)
  137. {
  138. In.texCoord = In.texCoord * tilingOffset.xy + tilingOffset.zw;
  139. #if CC_SURFACES_USE_SECOND_UV
  140. In.texCoord1 = In.texCoord1 * tilingOffset.xy + tilingOffset.zw;
  141. #endif
  142. }
  143. }%
  144. CCProgram surface-fragment %{
  145. #pragma define-meta TEXTURE_UV options([v_uv, v_uv1])
  146. #pragma define-meta DCC_APP_NAME range([0, 5])
  147. #define DCC_APP_OTHERS 0
  148. #define DCC_APP_MAX 1
  149. #define DCC_APP_BLENDER 2
  150. #define DCC_APP_CINEMA4D 3
  151. #define DCC_APP_GLTF 4
  152. #define DCC_APP_MAYA 5
  153. #if USE_SHININESS_MAP
  154. uniform sampler2D shininessExponentMap;
  155. #pragma define-meta GLOSSINESS_MAP_CHANNEL options([r,g,b,a])
  156. #endif
  157. #if USE_SPECULAR_GLOSSINESS_MAP
  158. uniform sampler2D specularGlossinessMap;
  159. #endif
  160. #if USE_SPECULAR_MAP
  161. uniform sampler2D specularMap;
  162. #endif
  163. #if USE_METALLIC_MAP
  164. uniform sampler2D metallicMap;
  165. #endif
  166. #if USE_ALBEDO_MAP
  167. uniform sampler2D albedoMap;
  168. #endif
  169. #if USE_TRANSPARENCY_MAP
  170. uniform sampler2D transparencyMap;
  171. #pragma define-meta TRANSPARENCY_MAP_CHANNEL options([a, r, g, b])
  172. #endif
  173. #if USE_EMISSIVE_MAP
  174. uniform sampler2D emissiveMap;
  175. #endif
  176. #if USE_EMISSIVESCALE_MAP
  177. uniform sampler2D emissiveScaleMap;
  178. #endif
  179. #if USE_NORMAL_MAP
  180. uniform sampler2D normalMap;
  181. #pragma define-meta NORMAL_UV options([v_uv, v_uv1])
  182. #endif
  183. #if USE_OCCLUSION_MAP
  184. uniform sampler2D occlusionMap;
  185. #pragma define-meta OCCLUSION_UV options([v_uv, v_uv1])
  186. #pragma define-meta OCCLUSION_CHANNEL options([r,g,b,a])
  187. #endif
  188. float discolor(vec3 srcColor)
  189. {
  190. return dot(GRAY_VECTOR, srcColor);
  191. }
  192. float convertShininessExponent(float shininessExp)
  193. {
  194. #if DCC_APP_NAME == DCC_APP_BLENDER
  195. // 2-100
  196. float glossiness = clamp(sqrt(shininessExp) * 0.1/*/10.0*/, 0.0, 0.95); // glossiness=1 may leads to specular disappear
  197. #elif DCC_APP_NAME == DCC_APP_MAX || DCC_APP_NAME == DCC_APP_MAYA
  198. // 2-1024
  199. float l2 = clamp(log(shininessExp + EPSILON) * 0.1442695 /*/log(2.0)/10.0*/, 0.0, 1.0);
  200. float glossiness = pow(l2, 0.5);
  201. #else // DCC_APP_NAME == DCC_APP_CINEMA4D / DCC_APP_GLTF / DCC_APP_OTHERS
  202. // 2-1024
  203. float glossiness = clamp(log(shininessExp + EPSILON) * 0.1442695 /*/log(2.0)/10.0*/, 0.0, 1.0);
  204. #endif
  205. return glossiness;
  206. }
  207. float getSpecularIntensityFromRoughness(float roughness)
  208. {
  209. #if DCC_APP_NAME == DCC_APP_BLENDER
  210. float specularIntensityMultiplier = mix(1.0, 5.0, roughness);
  211. #elif DCC_APP_NAME == DCC_APP_CINEMA4D
  212. float specularIntensityMultiplier = mix(1.0, 50.0, roughness);
  213. #elif DCC_APP_NAME == DCC_APP_MAX || DCC_APP_NAME == DCC_APP_MAYA
  214. float specularIntensityMultiplier = mix(1.0, 20.0, roughness);
  215. #else // DCC_APP_NAME == DCC_APP_GLTF / DCC_APP_OTHERS
  216. float specularIntensityMultiplier = 1.0;
  217. #endif
  218. return specularIntensityMultiplier;
  219. }
  220. vec4 getSpecularColorAndFactor()
  221. {
  222. vec3 inSpecular = specularColor.rgb * specularFactor;
  223. float inFactor = 1.0; //reserved
  224. #if USE_SPECULAR_GLOSSINESS_MAP
  225. inSpecular = SRGBToLinear(texture(specularGlossinessMap, TEXTURE_UV).rgb);
  226. #endif
  227. #if USE_SPECULAR_MAP
  228. vec4 specularTex = texture(specularMap, TEXTURE_UV);
  229. specularTex.rgb = SRGBToLinear(specularTex.rgb);
  230. inSpecular = specularTex.rgb;
  231. #endif
  232. return vec4(inSpecular, inFactor);
  233. }
  234. #define CC_SURFACES_FRAGMENT_ALPHA_CLIP_ONLY
  235. void SurfacesFragmentAlphaClipOnly()
  236. {
  237. #if USE_ALPHA_TEST
  238. float alpha = diffuseColor.a;
  239. #if USE_VERTEX_COLOR
  240. alpha *= FSInput_vertexColor.a;
  241. #endif
  242. #if USE_ALBEDO_MAP
  243. alpha *= texture(albedoMap, TEXTURE_UV).a * transparencyFactor;
  244. #endif
  245. #if USE_TRANSPARENCY_MAP
  246. alpha = texture(transparencyMap, TEXTURE_UV).TRANSPARENCY_MAP_CHANNEL;
  247. #if DCC_APP_NAME == DCC_APP_MAYA
  248. alpha = 1.0 - alpha;
  249. #endif
  250. #endif
  251. if (alpha < alphaThreshold) discard;
  252. #endif
  253. }
  254. #define CC_SURFACES_FRAGMENT_MODIFY_WORLD_NORMAL
  255. vec3 SurfacesFragmentModifyWorldNormal()
  256. {
  257. vec3 normal = FSInput_worldNormal;
  258. #if USE_NORMAL_MAP
  259. vec3 nmmp = texture(normalMap, NORMAL_UV).xyz - vec3(0.5);
  260. normal = CalculateNormalFromTangentSpace(nmmp, normalScale, normalize(normal.xyz), normalize(FSInput_worldTangent), FSInput_mirrorNormal);
  261. #endif
  262. return normalize(normal);
  263. }
  264. #define CC_SURFACES_FRAGMENT_MODIFY_EMISSIVE
  265. vec3 SurfacesFragmentModifyEmissive()
  266. {
  267. vec3 emissiveColor = emissive.rgb;
  268. #if USE_EMISSIVE_MAP
  269. emissiveColor.rgb = SRGBToLinear(texture(emissiveMap, TEXTURE_UV).rgb);
  270. #endif
  271. //emissive color scale
  272. #if USE_EMISSIVESCALE_MAP
  273. vec4 emissiveScaleColor = texture(emissiveScaleMap, TEXTURE_UV);
  274. emissiveScaleColor.rgb = SRGBToLinear(emissiveScaleColor.rgb);
  275. emissiveColor.rgb *= emissiveScaleColor.rgb;
  276. #else
  277. emissiveColor.rgb *= emissiveScale;
  278. #endif
  279. return emissiveColor;
  280. }
  281. #define CC_SURFACES_FRAGMENT_MODIFY_SHARED_DATA
  282. #include <surfaces/data-structures/standard>
  283. void SurfacesFragmentModifySharedData(inout SurfacesMaterialData surfaceData)
  284. {
  285. vec4 baseColor = vec4(1.0);
  286. #if USE_VERTEX_COLOR
  287. baseColor.rgb *= SRGBToLinear(FSInput_vertexColor.rgb); // use linear
  288. baseColor.a *= FSInput_vertexColor.a;
  289. #endif
  290. #if USE_ALBEDO_MAP
  291. vec4 texColor = texture(albedoMap, TEXTURE_UV);
  292. texColor.rgb = SRGBToLinear(texColor.rgb);
  293. texColor.a *= transparencyFactor;
  294. baseColor *= texColor;
  295. #else
  296. baseColor *= diffuseColor;
  297. #endif
  298. baseColor.rgb *= diffuseFactor;
  299. #if USE_TRANSPARENCY_MAP
  300. baseColor.a = texture(transparencyMap, TEXTURE_UV).TRANSPARENCY_MAP_CHANNEL;
  301. #if DCC_APP_NAME == DCC_APP_MAYA
  302. baseColor.a = 1.0 - baseColor.a;
  303. #endif
  304. #endif
  305. #if USE_ALPHA_TEST
  306. if (baseColor.a < alphaThreshold) discard;
  307. #endif
  308. vec4 specularColorAndFactor = getSpecularColorAndFactor();
  309. //glossiness
  310. float inGlossiness = 0.0, inSpecularIntensity = 1.0;
  311. #if HAS_EXPORTED_GLOSSINESS
  312. #if USE_SPECULAR_GLOSSINESS_MAP
  313. inGlossiness = 1.0 - texture(specularGlossinessMap, TEXTURE_UV).a;
  314. #else
  315. inGlossiness = glossiness;
  316. #endif
  317. #else
  318. #if USE_SHININESS_MAP
  319. #if GLOSSINESS_MAP_USE_SINGLE_CHANNEL
  320. inGlossiness = 1.0 - texture(shininessExponentMap, TEXTURE_UV).GLOSSINESS_MAP_CHANNEL;
  321. #else
  322. inGlossiness = 1.0 - discolor(texture(shininessExponentMap, TEXTURE_UV).rgb);
  323. #endif
  324. #else
  325. inGlossiness = convertShininessExponent(shininessExponent);
  326. #endif
  327. inSpecularIntensity *= getSpecularIntensityFromRoughness(1.0 - inGlossiness);
  328. #endif
  329. //metallic
  330. float inMetallic = 0.0;
  331. vec3 albedo = baseColor.rgb;
  332. #if HAS_EXPORTED_METALLIC
  333. inMetallic = metallic;
  334. float spec = specularFactor;
  335. #if USE_SPECULAR_MAP
  336. spec = dot(GRAY_VECTOR, texture(specularMap, TEXTURE_UV).rgb);
  337. #endif
  338. inSpecularIntensity *= spec * 0.5; //0.5 is suitable for blender
  339. #else
  340. GetMetallicAlbedoFromDiffuseSpecularWithoutColor(inMetallic, albedo.rgb, baseColor.rgb, specularColorAndFactor.rgb, 0.04);
  341. inSpecularIntensity *= inMetallic; //simulate specular color is black
  342. #endif
  343. baseColor.rgb = albedo;
  344. surfaceData.baseColor = baseColor;
  345. surfaceData.specularIntensity = inSpecularIntensity * 0.5;
  346. surfaceData.roughness = 1.0 - inGlossiness;
  347. surfaceData.metallic = inMetallic;
  348. surfaceData.ao = 1.0;
  349. #if USE_OCCLUSION_MAP
  350. surfaceData.ao = texture(occlusionMap, OCCLUSION_UV).OCCLUSION_CHANNEL;
  351. #endif
  352. }
  353. }%
  354. CCProgram standard-vs %{
  355. precision highp float;
  356. // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros
  357. #include <macro-remapping>
  358. #include <surfaces/effect-macros/common-macros>
  359. // 2. common include with corresponding shader stage, include before surface functions
  360. #include <surfaces/includes/common-vs>
  361. // 3. user surface functions that can use user (effect) parameters (ubo Constants)
  362. // see surfaces/default-functions/xxx.chunk
  363. #include <shared-ubos>
  364. #include <surface-vertex>
  365. // 4. surface include with corresponding shader stage and shading-model (optional)
  366. #include <surfaces/includes/standard-vs>
  367. // 5. shader entry with corresponding shader stage and technique usage/type
  368. #include <shading-entries/main-functions/render-to-scene/vs>
  369. }%
  370. CCProgram shadow-caster-vs %{
  371. precision highp float;
  372. #include <macro-remapping>
  373. #include <surfaces/effect-macros/render-to-shadowmap>
  374. #include <surfaces/includes/common-vs>
  375. #include <shared-ubos>
  376. #include <surface-vertex>
  377. #include <shading-entries/main-functions/render-to-shadowmap/vs>
  378. }%
  379. CCProgram standard-fs %{
  380. // shading-model : standard
  381. // lighting-model : standard (isotropy / anisotropy pbr)
  382. // shader stage : fs
  383. // technique usage/type : render-to-scene
  384. precision highp float;
  385. // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros
  386. #include <macro-remapping>
  387. #include <surfaces/effect-macros/common-macros>
  388. // 2. common include with corresponding shader stage, include before surface functions
  389. #include <surfaces/includes/common-fs>
  390. // 3. user surface functions that can use user (effect) parameters (ubo Constants)
  391. // see surfaces/default-functions/xxx.chunk
  392. #include <shared-ubos>
  393. #include <surface-fragment>
  394. // 4. lighting-model (optional)
  395. #include <lighting-models/includes/standard>
  396. // 5. surface include with corresponding shader stage and shading-model (optional)
  397. #include <surfaces/includes/standard-fs>
  398. // 6. shader entry with corresponding shader stage and technique usage/type
  399. #include <shading-entries/main-functions/render-to-scene/fs>
  400. }%
  401. CCProgram shadow-caster-fs %{
  402. precision highp float;
  403. #include <macro-remapping>
  404. #include <surfaces/effect-macros/render-to-shadowmap>
  405. #include <surfaces/includes/common-fs>
  406. #include <shared-ubos>
  407. #include <surface-fragment>
  408. #include <shading-entries/main-functions/render-to-shadowmap/fs>
  409. }%