builtin-standard.effect 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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: Albedo, type: color } }
  11. albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz }
  12. alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST, slide: true, range: [0, 1.0], step: 0.001 } }
  13. occlusion: { value: 0.0, target: pbrParams.x, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  14. roughness: { value: 0.5, target: pbrParams.y, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  15. metallic: { value: 0.0, target: pbrParams.z, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  16. specularIntensity: { value: 0.5, target: pbrParams.w, editor: { slide: true, range: [0.0, 1.0], step: 0.001 } }
  17. emissive: { value: [0.0, 0.0, 0.0, 1.0], linear: true, editor: { type: color } }
  18. emissiveScale: { value: [1.0, 1.0, 1.0], target: emissiveScaleParam.xyz }
  19. normalStrength: { value: 1.0, target: emissiveScaleParam.w, editor: { parent: USE_NORMAL_MAP, slide: true, range: [0, 5.0], step: 0.001 } }
  20. anisotropyIntensity: { value: 1.0, target: anisotropyParam.x, editor: { parent: IS_ANISOTROPY, slide : true, range : [0.0, 1.0] , step : 0.0001 } }
  21. anisotropyRotation: { value: 0.0, target: anisotropyParam.y, editor: { parent: IS_ANISOTROPY, slide : true, range : [0, 1.0] , step : 0.0001 } }
  22. anisotropyMapResolutionHeight: { value: 0.0, target: anisotropyParam.w, editor: { parent: FIX_ANISOTROPIC_ROTATION_MAP } }
  23. addOnShadowBias: { value: 0.0, target: anisotropyParam.z }
  24. mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } }
  25. normalMap: { value: normal }
  26. pbrMap: { value: grey }
  27. occlusionMap: { value: white }
  28. emissiveMap: { value: grey }
  29. anisotropyMap: { value: black, editor : { parent: IS_ANISOTROPY } }
  30. anisotropyMapNearestFilter: { value: black, editor : { parent: FIX_ANISOTROPIC_ROTATION_MAP } }
  31. - &forward-add
  32. vert: standard-vs
  33. frag: standard-fs
  34. phase: forward-add
  35. propertyIndex: 0
  36. embeddedMacros: { CC_FORWARD_ADD: true }
  37. depthStencilState:
  38. depthFunc: equal
  39. depthTest: true
  40. depthWrite: false
  41. blendState:
  42. targets:
  43. - blend: true
  44. blendSrc: one
  45. blendDst: one
  46. blendSrcAlpha: zero
  47. blendDstAlpha: one
  48. - &shadow-caster
  49. vert: shadow-caster-vs
  50. frag: shadow-caster-fs
  51. phase: shadow-caster
  52. propertyIndex: 0
  53. rasterizerState:
  54. cullMode: front
  55. properties:
  56. tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] }
  57. mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, editor: { displayName: Albedo, type: color } }
  58. albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz }
  59. alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } }
  60. mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } }
  61. - &reflect-map
  62. vert: standard-vs
  63. frag: reflect-map-fs
  64. phase: reflect-map
  65. propertyIndex: 0
  66. - &planar-shadow
  67. vert: planar-shadow-vs
  68. frag: planar-shadow-fs
  69. phase: planar-shadow
  70. propertyIndex: 0
  71. depthStencilState:
  72. depthTest: true
  73. depthWrite: false
  74. stencilTestFront: true
  75. stencilFuncFront: not_equal
  76. stencilPassOpFront: replace
  77. stencilRef: 0x80 # only use the leftmost bit
  78. stencilReadMask: 0x80
  79. stencilWriteMask: 0x80
  80. blendState:
  81. targets:
  82. - blend: true
  83. blendSrc: src_alpha
  84. blendDst: one_minus_src_alpha
  85. blendDstAlpha: one_minus_src_alpha
  86. - &deferred
  87. vert: standard-vs
  88. frag: standard-fs
  89. pass: gbuffer
  90. phase: gbuffer
  91. embeddedMacros: { CC_PIPELINE_TYPE: 1 }
  92. propertyIndex: 0
  93. - name: transparent
  94. passes:
  95. - vert: standard-vs
  96. frag: standard-fs
  97. embeddedMacros: { CC_FORCE_FORWARD_SHADING: true }
  98. depthStencilState: &d1
  99. depthTest: true
  100. depthWrite: false
  101. blendState: &b1
  102. targets:
  103. - blend: true
  104. blendSrc: src_alpha
  105. blendDst: one_minus_src_alpha
  106. blendDstAlpha: one_minus_src_alpha
  107. properties: *props
  108. - *forward-add
  109. - *shadow-caster
  110. - *planar-shadow
  111. - &deferred-forward
  112. vert: standard-vs
  113. frag: standard-fs
  114. phase: deferred-forward
  115. embeddedMacros: { CC_PIPELINE_TYPE: 0 }
  116. propertyIndex: 0
  117. depthStencilState: *d1
  118. blendState: *b1
  119. }%
  120. CCProgram shared-ubos %{
  121. uniform Constants {
  122. vec4 tilingOffset;
  123. vec4 albedo;
  124. vec4 albedoScaleAndCutoff;
  125. vec4 pbrParams;
  126. vec4 emissive;
  127. vec4 emissiveScaleParam;
  128. vec4 anisotropyParam;
  129. };
  130. }%
  131. CCProgram macro-remapping %{
  132. // ui displayed macros
  133. #pragma define-meta HAS_SECOND_UV
  134. #pragma define-meta USE_TWOSIDE
  135. #pragma define-meta IS_ANISOTROPY
  136. #pragma define-meta USE_VERTEX_COLOR
  137. #pragma define-meta FIX_ANISOTROPIC_ROTATION_MAP
  138. #define CC_SURFACES_USE_SECOND_UV HAS_SECOND_UV
  139. #define CC_SURFACES_USE_TWO_SIDED USE_TWOSIDE
  140. #define CC_SURFACES_LIGHTING_ANISOTROPIC IS_ANISOTROPY
  141. #define CC_SURFACES_USE_VERTEX_COLOR USE_VERTEX_COLOR
  142. // depend on UI macros
  143. #if IS_ANISOTROPY || USE_NORMAL_MAP
  144. #define CC_SURFACES_USE_TANGENT_SPACE 1
  145. #endif
  146. // functionality for each effect
  147. #define CC_SURFACES_LIGHTING_ANISOTROPIC_ENVCONVOLUTION_COUNT 31
  148. }%
  149. CCProgram surface-vertex %{
  150. #define CC_SURFACES_VERTEX_MODIFY_SHADOW_BIAS
  151. vec2 SurfacesVertexModifyShadowBias(in SurfacesStandardVertexIntermediate In, vec2 originShadowBias)
  152. {
  153. return originShadowBias + vec2(anisotropyParam.z, 0.0);
  154. }
  155. /*#define CC_SURFACES_VERTEX_MODIFY_WORLD_POS
  156. vec3 SurfacesVertexModifyWorldPos(in SurfacesStandardVertexIntermediate In)
  157. {
  158. vec3 worldPos = In.worldPos;
  159. worldPos.x += sin(cc_time.x * worldPos.z);
  160. worldPos.y += cos(cc_time.x * worldPos.z);
  161. return worldPos;
  162. }
  163. #define CC_SURFACES_VERTEX_MODIFY_WORLD_NORMAL
  164. vec3 SurfacesVertexModifyWorldNormal(in SurfacesStandardVertexIntermediate In)
  165. {
  166. vec3 worldNormal = In.worldNormal.xyz;
  167. worldNormal.x += sin(cc_time.x * 3.0);
  168. worldNormal.y += cos(cc_time.x * 3.0);
  169. #if CC_SURFACES_USE_TWO_SIDED
  170. worldNormal.xyz *= In.worldNormal.w;
  171. #endif
  172. return normalize(worldNormal);
  173. }
  174. // see <surfaces/default-functions/common-vs> for more overrided functions
  175. #define CC_SURFACES_VERTEX_MODIFY_SHARED_DATA
  176. void SurfacesVertexModifySharedData(inout SurfacesStandardVertexIntermediate In)
  177. {
  178. }
  179. */
  180. #define CC_SURFACES_VERTEX_MODIFY_UV
  181. void SurfacesVertexModifyUV(inout SurfacesStandardVertexIntermediate In)
  182. {
  183. In.texCoord = In.texCoord * tilingOffset.xy + tilingOffset.zw;
  184. #if CC_SURFACES_USE_SECOND_UV
  185. In.texCoord1 = In.texCoord1 * tilingOffset.xy + tilingOffset.zw;
  186. #endif
  187. }
  188. }%
  189. CCProgram surface-fragment %{
  190. #if USE_ALBEDO_MAP
  191. uniform sampler2D albedoMap;
  192. #pragma define-meta ALBEDO_UV options([v_uv, v_uv1])
  193. #endif
  194. #if USE_NORMAL_MAP
  195. uniform sampler2D normalMap;
  196. #pragma define-meta NORMAL_UV options([v_uv, v_uv1])
  197. #endif
  198. #pragma define-meta DEFAULT_UV options([v_uv, v_uv1])
  199. #if USE_PBR_MAP
  200. uniform sampler2D pbrMap;
  201. #endif
  202. #if USE_OCCLUSION_MAP
  203. uniform sampler2D occlusionMap;
  204. #endif
  205. #if USE_EMISSIVE_MAP
  206. uniform sampler2D emissiveMap;
  207. #pragma define-meta EMISSIVE_UV options([v_uv, v_uv1])
  208. #endif
  209. #if IS_ANISOTROPY && USE_ANISOTROPY_MAP
  210. uniform sampler2D anisotropyMap;
  211. uniform sampler2D anisotropyMapNearestFilter;
  212. #endif
  213. #pragma define OCCLUSION_CHANNEL r
  214. #pragma define ROUGHNESS_CHANNEL g
  215. #pragma define METALLIC_CHANNEL b
  216. #pragma define SPECULAR_INTENSITY_CHANNEL a
  217. #if USE_ALPHA_TEST
  218. #pragma define-meta ALPHA_TEST_CHANNEL options([a, r])
  219. #endif
  220. #define CC_SURFACES_FRAGMENT_MODIFY_BASECOLOR_AND_TRANSPARENCY
  221. vec4 SurfacesFragmentModifyBaseColorAndTransparency()
  222. {
  223. vec4 baseColor = albedo;
  224. #if USE_VERTEX_COLOR
  225. baseColor.rgb *= SRGBToLinear(FSInput_vertexColor.rgb); // use linear
  226. baseColor.a *= FSInput_vertexColor.a;
  227. #endif
  228. #if USE_ALBEDO_MAP
  229. vec4 texColor = texture(albedoMap, ALBEDO_UV);
  230. texColor.rgb = SRGBToLinear(texColor.rgb);
  231. baseColor *= texColor;
  232. #endif
  233. #if USE_ALPHA_TEST
  234. if (baseColor.ALPHA_TEST_CHANNEL < albedoScaleAndCutoff.w) discard;
  235. #endif
  236. baseColor.rgb *= albedoScaleAndCutoff.xyz;
  237. return baseColor;
  238. }
  239. #define CC_SURFACES_FRAGMENT_ALPHA_CLIP_ONLY
  240. void SurfacesFragmentAlphaClipOnly()
  241. {
  242. #if USE_ALPHA_TEST
  243. float alpha = albedo.ALPHA_TEST_CHANNEL;
  244. #if USE_VERTEX_COLOR
  245. alpha *= FSInput_vertexColor.a;
  246. #endif
  247. #if USE_ALBEDO_MAP
  248. alpha = texture(albedoMap, ALBEDO_UV).ALPHA_TEST_CHANNEL;
  249. #endif
  250. if (alpha < albedoScaleAndCutoff.w) discard;
  251. #endif
  252. }
  253. #define CC_SURFACES_FRAGMENT_MODIFY_WORLD_NORMAL
  254. vec3 SurfacesFragmentModifyWorldNormal()
  255. {
  256. vec3 normal = FSInput_worldNormal;
  257. #if USE_NORMAL_MAP
  258. vec3 nmmp = texture(normalMap, NORMAL_UV).xyz - vec3(0.5);
  259. normal = CalculateNormalFromTangentSpace(nmmp, emissiveScaleParam.w, normalize(normal.xyz), normalize(FSInput_worldTangent), FSInput_mirrorNormal);
  260. #endif
  261. return normalize(normal);
  262. }
  263. #define CC_SURFACES_FRAGMENT_MODIFY_ANISOTROPY_PARAMS
  264. vec4 SurfacesFragmentModifyAnisotropyParams(out float isRotation)
  265. {
  266. float anisotropyRotation = anisotropyParam.y * PI;
  267. float anisotropyShape = anisotropyParam.x;
  268. #if IS_ANISOTROPY && USE_ANISOTROPY_MAP
  269. // Rotation angle should disable trilinear filtering
  270. vec4 tex = texture(anisotropyMap, DEFAULT_UV);
  271. anisotropyRotation = fract(anisotropyRotation * 0.5 + tex.y) * PI2;
  272. // less value is better for SP exported shape
  273. anisotropyShape *= tex.x;
  274. #endif
  275. // fix rotation map seam line of black and white
  276. #if FIX_ANISOTROPIC_ROTATION_MAP
  277. #if IS_ANISOTROPY && USE_ANISOTROPY_MAP
  278. vec4 reference = texture(anisotropyMapNearestFilter, DEFAULT_UV);
  279. vec2 oneTap = vec2(0.0, 1.0 / anisotropyParam.w);
  280. float threshold = 0.2;
  281. // scan more taps for stable result
  282. vec4 sample1 = texture(anisotropyMapNearestFilter, DEFAULT_UV + oneTap);
  283. vec4 sample2 = texture(anisotropyMapNearestFilter, DEFAULT_UV - oneTap);
  284. if (abs(sample1.y - reference.y) > threshold || abs(sample2.y - reference.y) > threshold) {
  285. tex.y = reference.y;
  286. }
  287. anisotropyRotation = fract(anisotropyParam.y * PI * 0.5 + tex.y) * PI2;
  288. #endif
  289. #endif
  290. isRotation = 1.0;
  291. return vec4(anisotropyShape, anisotropyRotation, 0.0, 0.0);
  292. }
  293. #define CC_SURFACES_FRAGMENT_MODIFY_EMISSIVE
  294. vec3 SurfacesFragmentModifyEmissive()
  295. {
  296. vec3 emissive = emissive.rgb;
  297. #if USE_EMISSIVE_MAP
  298. emissive = SRGBToLinear(texture(emissiveMap, EMISSIVE_UV).rgb);
  299. #endif
  300. return emissive * emissiveScaleParam.xyz;
  301. }
  302. #define CC_SURFACES_FRAGMENT_MODIFY_PBRPARAMS
  303. vec4 SurfacesFragmentModifyPBRParams()
  304. {
  305. vec4 pbr = pbrParams;
  306. pbr.x = 1.0;
  307. #if USE_PBR_MAP
  308. vec4 res = texture(pbrMap, DEFAULT_UV);
  309. pbr.x = mix(1.0, res.OCCLUSION_CHANNEL, pbrParams.x);
  310. pbr.y *= res.ROUGHNESS_CHANNEL;
  311. pbr.z *= res.METALLIC_CHANNEL;
  312. pbr.w *= res.SPECULAR_INTENSITY_CHANNEL;
  313. #endif
  314. #if USE_OCCLUSION_MAP
  315. pbr.x = mix(1.0, texture(occlusionMap, DEFAULT_UV).OCCLUSION_CHANNEL, pbrParams.x);
  316. #endif
  317. return pbr;
  318. }
  319. /*
  320. // definition of SurfacesMaterialData structure with corresponding shading-model
  321. #include <surfaces/data-structures/standard>
  322. //see <surfaces/default-functions/XXXX> for more overrided functions, XXXX is shading-model name
  323. #define CC_SURFACES_FRAGMENT_MODIFY_SHARED_DATA
  324. void SurfacesFragmentModifySharedData(inout SurfacesMaterialData surfaceData)
  325. {
  326. }
  327. // see <lighting-models/default-functions/XXXX> for more overrided functions, XXXX is lighting-model name
  328. #include <lighting-models/includes/common>
  329. #define CC_SURFACES_LIGHTING_MODIFY_FINAL_RESULT
  330. void SurfacesLightingModifyFinalResult(inout LightingResult result, in LightingIntermediateData lightingData, in SurfacesMaterialData surfaceData, in LightingMiscData miscData)
  331. {
  332. }*/
  333. }%
  334. CCProgram standard-vs %{
  335. precision highp float;
  336. // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros
  337. #include <macro-remapping>
  338. #include <surfaces/effect-macros/common-macros>
  339. // 2. common include with corresponding shader stage, include before surface functions
  340. #include <surfaces/includes/common-vs>
  341. // 3. user surface functions that can use user (effect) parameters (ubo Constants)
  342. // see surfaces/default-functions/xxx.chunk
  343. #include <shared-ubos>
  344. #include <surface-vertex>
  345. // 4. surface include with corresponding shader stage and shading-model (optional)
  346. #include <surfaces/includes/standard-vs>
  347. // 5. shader entry with corresponding shader stage and technique usage/type
  348. #include <shading-entries/main-functions/render-to-scene/vs>
  349. }%
  350. CCProgram shadow-caster-vs %{
  351. precision highp float;
  352. #include <macro-remapping>
  353. #include <surfaces/effect-macros/render-to-shadowmap>
  354. #include <surfaces/includes/common-vs>
  355. #include <shared-ubos>
  356. #include <surface-vertex>
  357. #include <shading-entries/main-functions/render-to-shadowmap/vs>
  358. }%
  359. CCProgram planar-shadow-vs %{
  360. precision highp float;
  361. #include <macro-remapping>
  362. #include <surfaces/effect-macros/render-planar-shadow>
  363. #include <surfaces/includes/common-vs>
  364. #include <shared-ubos>
  365. #include <surface-vertex>
  366. #include <shading-entries/main-functions/render-planar-shadow/vs>
  367. }%
  368. CCProgram standard-fs %{
  369. // shading-model : standard
  370. // lighting-model : standard (isotropy / anisotropy pbr)
  371. // shader stage : fs
  372. // technique usage/type : render-to-scene
  373. precision highp float;
  374. // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros
  375. #include <macro-remapping>
  376. #include <surfaces/effect-macros/common-macros>
  377. // 2. common include with corresponding shader stage, include before surface functions
  378. #include <surfaces/includes/common-fs>
  379. // 3. user surface functions that can use user (effect) parameters (ubo Constants)
  380. // see surfaces/default-functions/xxx.chunk
  381. #include <shared-ubos>
  382. #include <surface-fragment>
  383. // 4. lighting-model (optional)
  384. #include <lighting-models/includes/standard>
  385. // 5. surface include with corresponding shader stage and shading-model (optional)
  386. #include <surfaces/includes/standard-fs>
  387. // 6. shader entry with corresponding shader stage and technique usage/type
  388. #include <shading-entries/main-functions/render-to-scene/fs>
  389. }%
  390. CCProgram shadow-caster-fs %{
  391. precision highp float;
  392. #include <macro-remapping>
  393. #include <surfaces/effect-macros/render-to-shadowmap>
  394. #include <surfaces/includes/common-fs>
  395. #include <shared-ubos>
  396. #include <surface-fragment>
  397. #include <shading-entries/main-functions/render-to-shadowmap/fs>
  398. }%
  399. CCProgram planar-shadow-fs %{
  400. precision highp float;
  401. #include <macro-remapping>
  402. #include <surfaces/effect-macros/render-planar-shadow>
  403. #include <surfaces/includes/common-fs>
  404. #include <shared-ubos>
  405. #include <surface-fragment>
  406. #include <shading-entries/main-functions/render-planar-shadow/fs>
  407. }%
  408. CCProgram reflect-map-fs %{
  409. precision highp float;
  410. #include <macro-remapping>
  411. #include <surfaces/effect-macros/common-macros>
  412. #include <surfaces/includes/common-fs>
  413. #include <shared-ubos>
  414. #include <surface-fragment>
  415. #include <lighting-models/includes/standard>
  416. #include <surfaces/includes/standard-fs>
  417. #include <shading-entries/main-functions/render-to-reflectmap/fs>
  418. }%