glass.effect 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // Copyright (c) 2017-2022 Xiamen Yaji Software Co., Ltd.
  2. CCEffect %{
  3. techniques:
  4. - name: single-sided
  5. passes:
  6. - vert: standard-vs
  7. frag: standard-fs
  8. embeddedMacros: { CC_FORCE_FORWARD_SHADING: true }
  9. rasterizerState:
  10. cullMode: Back
  11. depthStencilState: &d1
  12. depthTest: true
  13. depthWrite: false
  14. blendState: &b1
  15. targets:
  16. - blend: true
  17. blendSrc: one
  18. blendDst: src_alpha
  19. blendSrcAlpha: zero
  20. blendDstAlpha: one
  21. properties: &props
  22. tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] }
  23. mainColor: { value: [0.05, 0.05, 0.05, 1.0], target: albedo, linear: true, editor: { displayName: Albedo, type: color } }
  24. albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz }
  25. roughness: { value: 0.0, target: pbrParams.y, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  26. specularIntensity: { value: 0.5, target: pbrParams.w, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } }
  27. F0: { value: 0.5, target: emissiveScaleParam.x, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  28. F90: { value: 0.9, target: emissiveScaleParam.y, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } }
  29. gradientColor: { value: [0.0, 0.05, 0.0, 1.0], linear: true, editor: { parent: USE_GRADIENT_COLOR, type: color } }
  30. gradientIntensity: { value: 0.2, target: emissiveScaleParam.z, editor: { parent: USE_GRADIENT_COLOR, slide: true, range: [0, 1.0], step: 0.001 } }
  31. normalStrength: { value: 1.0, target: emissiveScaleParam.w, editor: { parent: USE_NORMAL_MAP, slide: true, range: [0, 5.0], step: 0.001 } }
  32. mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } }
  33. normalMap: { value: normal }
  34. pbrMap: { value: grey }
  35. - &forward-add
  36. vert: standard-vs
  37. frag: standard-fs
  38. phase: forward-add
  39. propertyIndex: 0
  40. embeddedMacros: { CC_FORWARD_ADD: true }
  41. depthStencilState:
  42. depthFunc: equal
  43. depthTest: true
  44. depthWrite: false
  45. blendState:
  46. targets:
  47. - blend: true
  48. blendSrc: one
  49. blendDst: one
  50. blendSrcAlpha: zero
  51. blendDstAlpha: one
  52. - &shadow-caster
  53. vert: shadow-caster-vs
  54. frag: shadow-caster-fs
  55. phase: shadow-caster
  56. propertyIndex: 0
  57. rasterizerState:
  58. cullMode: front
  59. properties:
  60. tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] }
  61. mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, editor: { displayName: Albedo, type: color } }
  62. albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz }
  63. mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } }
  64. - &reflect-map
  65. vert: standard-vs
  66. frag: reflect-map-fs
  67. phase: reflect-map
  68. propertyIndex: 0
  69. - &planar-shadow
  70. vert: planar-shadow-vs
  71. frag: planar-shadow-fs
  72. phase: planar-shadow
  73. propertyIndex: 0
  74. depthStencilState:
  75. depthTest: true
  76. depthWrite: false
  77. stencilTestFront: true
  78. stencilFuncFront: not_equal
  79. stencilPassOpFront: replace
  80. stencilRef: 0x80 # only use the leftmost bit
  81. stencilReadMask: 0x80
  82. stencilWriteMask: 0x80
  83. blendState:
  84. targets:
  85. - blend: true
  86. blendSrc: src_alpha
  87. blendDst: one_minus_src_alpha
  88. blendDstAlpha: one_minus_src_alpha
  89. - &deferred
  90. vert: standard-vs
  91. frag: standard-fs
  92. pass: gbuffer
  93. phase: gbuffer
  94. embeddedMacros: { CC_PIPELINE_TYPE: 1 }
  95. propertyIndex: 0
  96. - name: two-sided
  97. passes:
  98. - vert: standard-vs
  99. frag: standard-fs
  100. embeddedMacros: { CC_FORCE_FORWARD_SHADING: true, TECHNIQUE_TWO_SIDE: true }
  101. rasterizerState:
  102. cullMode: Front
  103. properties: &props
  104. tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] }
  105. mainColor: { value: [0.05, 0.05, 0.05, 1.0], target: albedo, linear: true, editor: { displayName: Albedo, type: color } }
  106. albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz }
  107. roughness: { value: 0.0, target: pbrParams.y, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  108. specularIntensity: { value: 0.5, target: pbrParams.w, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } }
  109. F0: { value: 0.5, target: emissiveScaleParam.x, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  110. F90: { value: 0.9, target: emissiveScaleParam.y, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } }
  111. gradientColor: { value: [0.0, 0.05, 0.0, 1.0], linear: true, editor: { parent: USE_GRADIENT_COLOR, type: color } }
  112. gradientIntensity: { value: 0.2, target: emissiveScaleParam.z, editor: { parent: USE_GRADIENT_COLOR, slide: true, range: [0, 1.0], step: 0.001 } }
  113. normalStrength: { value: 1.0, target: emissiveScaleParam.w, editor: { parent: USE_NORMAL_MAP, slide: true, range: [0, 5.0], step: 0.001 } }
  114. mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } }
  115. normalMap: { value: normal }
  116. pbrMap: { value: grey }
  117. depthStencilState: *d1
  118. blendState: *b1
  119. - vert: standard-vs
  120. frag: standard-fs
  121. embeddedMacros: { CC_FORCE_FORWARD_SHADING: true, TECHNIQUE_TWO_SIDE: true }
  122. rasterizerState:
  123. cullMode: Back
  124. propertyIndex: 0
  125. depthStencilState: *d1
  126. blendState: *b1
  127. - *forward-add
  128. - *shadow-caster
  129. - *planar-shadow
  130. - &deferred-forward
  131. vert: standard-vs
  132. frag: standard-fs
  133. phase: deferred-forward
  134. embeddedMacros: { CC_PIPELINE_TYPE: 0 }
  135. propertyIndex: 0
  136. depthStencilState: *d1
  137. blendState: *b1
  138. }%
  139. CCProgram shared-ubos %{
  140. uniform Constants {
  141. vec4 tilingOffset;
  142. vec4 albedo;
  143. vec4 albedoScaleAndCutoff;
  144. vec4 pbrParams;
  145. vec4 emissiveScaleParam;
  146. vec4 gradientColor;
  147. };
  148. }%
  149. CCProgram macro-remapping %{
  150. // ui displayed macros
  151. #pragma define-meta USE_VERTEX_COLOR
  152. #pragma define-meta USE_PROBE_REFRACTION editor({ tooltip: 'Use transparency technique when disabled, otherwise use opaque technique' })
  153. #define CC_SURFACES_USE_VERTEX_COLOR USE_VERTEX_COLOR
  154. #define CC_SURFACES_LIGHTING_TRANSMIT_SPECULAR USE_PROBE_REFRACTION
  155. #define CC_SURFACES_LIGHTING_USE_FRESNEL USE_PROBE_REFRACTION
  156. // depend on UI macros
  157. #if USE_NORMAL_MAP
  158. #define CC_SURFACES_USE_TANGENT_SPACE 1
  159. #endif
  160. // depend on technique macros
  161. #if TECHNIQUE_TWO_SIDE
  162. #define CC_SURFACES_USE_TWO_SIDED 1
  163. #endif
  164. }%
  165. CCProgram surface-vertex %{
  166. #define CC_SURFACES_VERTEX_MODIFY_UV
  167. void SurfacesVertexModifyUV(inout SurfacesStandardVertexIntermediate In)
  168. {
  169. In.texCoord = In.texCoord * tilingOffset.xy + tilingOffset.zw;
  170. #if CC_SURFACES_USE_SECOND_UV
  171. In.texCoord1 = In.texCoord1 * tilingOffset.xy + tilingOffset.zw;
  172. #endif
  173. }
  174. }%
  175. CCProgram surface-fragment %{
  176. #if USE_ALBEDO_MAP
  177. uniform sampler2D albedoMap;
  178. #pragma define-meta ALBEDO_UV options([v_uv, v_uv1])
  179. #endif
  180. #if USE_NORMAL_MAP
  181. uniform sampler2D normalMap;
  182. #pragma define-meta NORMAL_UV options([v_uv, v_uv1])
  183. #endif
  184. #if USE_PBR_MAP
  185. uniform sampler2D pbrMap;
  186. #endif
  187. #pragma define-meta DEFAULT_UV options([v_uv, v_uv1])
  188. #define CC_SURFACES_FRAGMENT_MODIFY_BASECOLOR_AND_TRANSPARENCY
  189. vec4 SurfacesFragmentModifyBaseColorAndTransparency()
  190. {
  191. vec4 baseColor = albedo;
  192. #if USE_VERTEX_COLOR
  193. baseColor.rgb *= SRGBToLinear(FSInput_vertexColor.rgb); // use linear
  194. baseColor.a *= FSInput_vertexColor.a;
  195. #endif
  196. #if USE_ALBEDO_MAP
  197. vec4 texColor = texture(albedoMap, ALBEDO_UV);
  198. texColor.rgb = SRGBToLinear(texColor.rgb);
  199. baseColor *= texColor;
  200. #endif
  201. baseColor.rgb *= albedoScaleAndCutoff.xyz;
  202. return baseColor;
  203. }
  204. #define CC_SURFACES_FRAGMENT_MODIFY_WORLD_NORMAL
  205. vec3 SurfacesFragmentModifyWorldNormal()
  206. {
  207. vec3 normal = FSInput_worldNormal;
  208. #if USE_NORMAL_MAP
  209. vec3 nmmp = texture(normalMap, NORMAL_UV).xyz - vec3(0.5);
  210. normal = CalculateNormalFromTangentSpace(nmmp, emissiveScaleParam.w, normalize(normal.xyz), normalize(FSInput_worldTangent), FSInput_mirrorNormal);
  211. #endif
  212. return normalize(normal);
  213. }
  214. #define CC_SURFACES_FRAGMENT_MODIFY_PBRPARAMS
  215. vec4 SurfacesFragmentModifyPBRParams()
  216. {
  217. vec4 pbr = pbrParams;
  218. pbr.x = 1.0;
  219. pbr.z = 1.0;
  220. #if USE_PBR_MAP
  221. vec4 res = texture(pbrMap, DEFAULT_UV);
  222. pbr.yw *= res.yw;
  223. #endif
  224. return pbr;
  225. }
  226. #include <common/lighting/bxdf>
  227. #include <surfaces/data-structures/standard>
  228. #define CC_SURFACES_FRAGMENT_MODIFY_SHARED_DATA
  229. void SurfacesFragmentModifySharedData(inout SurfacesMaterialData surfaceData)
  230. {
  231. vec3 viewDir = normalize(cc_cameraPos.xyz - surfaceData.worldPos);
  232. float NoVSat = saturate(dot(surfaceData.worldNormal, viewDir));
  233. #if !USE_PROBE_REFRACTION
  234. // final color = srcColor * F + dstColor * (1-F) * glassColor
  235. // srcAlpha = (1-F) * glassColorBright (use bright instead for one channel)
  236. // srcBlend = 1, dstBlend = srcAlpha
  237. float F0 = emissiveScaleParam.x, F90 = emissiveScaleParam.y;
  238. float fresnel = CalculateFresnelCoefficient(min(F0, F90), F90, NoVSat);
  239. surfaceData.baseColor.a = (1.0 - fresnel) * length(surfaceData.baseColor.rgb);
  240. #endif
  241. #if USE_GRADIENT_COLOR
  242. surfaceData.baseColor.rgb = lerp(gradientColor * albedoScaleAndCutoff, surfaceData.baseColor, pow(NoVSat, emissiveScaleParam.z)).rgb;
  243. #endif
  244. }
  245. #include <lighting-models/includes/common>
  246. #define CC_SURFACES_LIGHTING_MODIFY_FINAL_RESULT
  247. void SurfacesLightingModifyFinalResult(inout LightingResult result, in LightingIntermediateData lightingData, in SurfacesMaterialData surfaceData, in LightingMiscData miscData)
  248. {
  249. vec3 viewDir = normalize(cc_cameraPos.xyz - surfaceData.worldPos);
  250. float NoVSat = saturate(dot(surfaceData.worldNormal, viewDir));
  251. float F0 = emissiveScaleParam.x, F90 = emissiveScaleParam.y;
  252. float fresnel = CalculateFresnelCoefficient(min(F0, F90), F90, NoVSat);
  253. result.fresnel = vec3(fresnel);
  254. #if !USE_PROBE_REFRACTION
  255. // simply fix single blend source issue for bright color alpha blending
  256. float threshold = 0.5, bright = dot(result.environmentSpecular, vec3(0.33333));
  257. if (bright > threshold)
  258. result.environmentSpecular = result.environmentSpecular / bright * threshold;
  259. #endif
  260. }
  261. #define CC_SURFACES_LIGHTING_MODIFY_FOG
  262. vec3 CCSurfacesLightingModifyFog(float fogFactor, vec3 color, in SurfacesMaterialData surfaceData, in LightingResult lightingResult)
  263. {
  264. // according to blend mode is one + src_alpha, fogColor should apply inv_src_alpha while glass is fully fogged, otherwise glass will be much more brighter.
  265. vec3 fogColor = cc_fogColor.rgb * (1.0 - surfaceData.baseColor.a);
  266. return mix(fogColor.rgb, color.rgb, fogFactor);
  267. }
  268. }%
  269. CCProgram standard-vs %{
  270. precision highp float;
  271. // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros
  272. #include <macro-remapping>
  273. #include <surfaces/effect-macros/common-macros>
  274. // 2. common include with corresponding shader stage, include before surface functions
  275. #include <surfaces/includes/common-vs>
  276. // 3. user surface functions that can use user (effect) parameters (ubo Constants)
  277. // see surfaces/default-functions/xxx.chunk
  278. #include <shared-ubos>
  279. #include <surface-vertex>
  280. // 4. surface include with corresponding shader stage and shading-model (optional)
  281. #include <surfaces/includes/standard-vs>
  282. // 5. shader entry with corresponding shader stage and technique usage/type
  283. #include <shading-entries/main-functions/render-to-scene/vs>
  284. }%
  285. CCProgram shadow-caster-vs %{
  286. precision highp float;
  287. #include <macro-remapping>
  288. #include <surfaces/effect-macros/render-to-shadowmap>
  289. #include <surfaces/includes/common-vs>
  290. #include <shared-ubos>
  291. #include <surface-vertex>
  292. #include <shading-entries/main-functions/render-to-shadowmap/vs>
  293. }%
  294. CCProgram planar-shadow-vs %{
  295. precision highp float;
  296. #include <macro-remapping>
  297. #include <surfaces/effect-macros/render-planar-shadow>
  298. #include <surfaces/includes/common-vs>
  299. #include <shared-ubos>
  300. #include <surface-vertex>
  301. #include <shading-entries/main-functions/render-planar-shadow/vs>
  302. }%
  303. CCProgram standard-fs %{
  304. // shading-model : standard
  305. // lighting-model : standard (isotropy / anisotropy pbr)
  306. // shader stage : fs
  307. // technique usage/type : render-to-scene
  308. precision highp float;
  309. // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros
  310. #include <macro-remapping>
  311. #include <surfaces/effect-macros/common-macros>
  312. // 2. common include with corresponding shader stage, include before surface functions
  313. #include <surfaces/includes/common-fs>
  314. // 3. user surface functions that can use user (effect) parameters (ubo Constants)
  315. // see surfaces/default-functions/xxx.chunk
  316. #include <shared-ubos>
  317. #include <surface-fragment>
  318. // 4. lighting-model (optional)
  319. #include <lighting-models/includes/standard>
  320. // 5. surface include with corresponding shader stage and shading-model (optional)
  321. #include <surfaces/includes/standard-fs>
  322. // 6. shader entry with corresponding shader stage and technique usage/type
  323. #include <shading-entries/main-functions/render-to-scene/fs>
  324. }%
  325. CCProgram shadow-caster-fs %{
  326. precision highp float;
  327. #include <macro-remapping>
  328. #include <surfaces/effect-macros/render-to-shadowmap>
  329. #include <surfaces/includes/common-fs>
  330. #include <shared-ubos>
  331. #include <surface-fragment>
  332. #include <shading-entries/main-functions/render-to-shadowmap/fs>
  333. }%
  334. CCProgram planar-shadow-fs %{
  335. precision highp float;
  336. #include <macro-remapping>
  337. #include <surfaces/effect-macros/render-planar-shadow>
  338. #include <surfaces/includes/common-fs>
  339. #include <shared-ubos>
  340. #include <surface-fragment>
  341. #include <shading-entries/main-functions/render-planar-shadow/fs>
  342. }%
  343. CCProgram reflect-map-fs %{
  344. precision highp float;
  345. #include <macro-remapping>
  346. #include <surfaces/effect-macros/common-macros>
  347. #include <surfaces/includes/common-fs>
  348. #include <shared-ubos>
  349. #include <surface-fragment>
  350. #include <lighting-models/includes/standard>
  351. #include <surfaces/includes/standard-fs>
  352. #include <shading-entries/main-functions/render-to-reflectmap/fs>
  353. }%