builtin-toon.effect 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // Copyright (c) 2017-2022 Xiamen Yaji Software Co., Ltd.
  2. CCEffect %{
  3. techniques:
  4. - passes:
  5. - switch: USE_OUTLINE_PASS
  6. vert: silhouette-edge-vs
  7. frag: silhouette-edge-fs
  8. rasterizerState: &r1
  9. cullMode: front
  10. depthStencilState: &d1
  11. depthFunc: less_equal
  12. depthTest: true
  13. depthWrite: true
  14. properties:
  15. lineWidth: { value: 10, target: outlineParams.x }
  16. depthBias: { value: 0, target: outlineParams.y }
  17. baseColor: { editor: { type: color } }
  18. baseColorMap: { value: grey }
  19. - vert: toon-vs
  20. frag: toon-fs
  21. properties: &props
  22. tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] }
  23. mainColor: { value: [0.6, 0.6, 0.6, 1.0], target: baseColor, linear: true, editor: { displayName: BaseColor, type: color } }
  24. colorScale: { value: [1.0, 1.0, 1.0], target: colorScaleAndCutoff.xyz }
  25. alphaThreshold: { value: 0.5, target: colorScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } }
  26. shadeColor1: { value: [0.4, 0.4, 0.4, 1.0], linear: true, editor: { type: color } }
  27. shadeColor2: { value: [0.2, 0.2, 0.2, 1.0], linear: true, editor: { type: color } }
  28. specular: { value: [1.0, 1.0, 1.0, 0.3], linear: true, editor: { type: color } }
  29. baseStep: { value: 0.8, target: shadeParams.x }
  30. baseFeather: { value: 0.001, target: shadeParams.y }
  31. shadeStep: { value: 0.5, target: shadeParams.z }
  32. shadeFeather: { value: 0.001, target: shadeParams.w }
  33. shadowCover: { value: 0.5, target: miscParams.x, editor: { slide: true, range: [0, 1.0], step: 0.001 } }
  34. emissive: { value: [0.0, 0.0, 0.0, 1.0], linear: true, editor: { type: color } }
  35. emissiveScale: { value: [1.0, 1.0, 1.0], target: emissiveScaleAndStrenth.xyz }
  36. normalStrength: { value: 1.0, target: emissiveScaleAndStrenth.w, editor: { parent: USE_NORMAL_MAP }, slide: true, range: [0, 5.0], step: 0.001 }
  37. normalMap: { value: normal }
  38. mainTexture: { value: white, target: baseColorMap, editor: { displayName: BaseColorMap } }
  39. shadeMap1: { value: white }
  40. shadeMap2: { value: white }
  41. specularMap: { value: white }
  42. emissiveMap: { value: grey }
  43. - vert: toon-vs
  44. frag: toon-fs
  45. phase: forward-add
  46. propertyIndex: 1
  47. embeddedMacros: { CC_FORWARD_ADD: true }
  48. depthStencilState:
  49. depthFunc: equal
  50. depthTest: true
  51. depthWrite: false
  52. blendState:
  53. targets:
  54. - blend: true
  55. blendSrc: one
  56. blendDst: one
  57. blendSrcAlpha: zero
  58. blendDstAlpha: one
  59. properties: *props
  60. - vert: shadow-caster-vs
  61. frag: shadow-caster-fs
  62. phase: shadow-caster
  63. propertyIndex: 1
  64. rasterizerState:
  65. cullMode: front
  66. properties:
  67. tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] }
  68. mainColor: { value: [0.6, 0.6, 0.6, 1.0], target: baseColor, editor: { displayName: BaseColor, type: color } }
  69. colorScale: { value: [1.0, 1.0, 1.0], target: colorScaleAndCutoff.xyz }
  70. alphaThreshold: { value: 0.5, target: colorScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } }
  71. shadeColor1: { value: [0.4, 0.4, 0.4, 1.0], editor: { type: color } }
  72. shadeColor2: { value: [0.2, 0.2, 0.2, 1.0], editor: { type: color } }
  73. specular: { value: [1.0, 1.0, 1.0, 0.3], editor: { type: color } }
  74. baseStep: { value: 0.8, target: shadeParams.x }
  75. baseFeather: { value: 0.001, target: shadeParams.y }
  76. shadeStep: { value: 0.5, target: shadeParams.z }
  77. shadeFeather: { value: 0.001, target: shadeParams.w }
  78. emissive: { value: [0.0, 0.0, 0.0, 1.0], editor: { type: color } }
  79. emissiveScale: { value: [1.0, 1.0, 1.0], target: emissiveScaleAndStrenth.xyz }
  80. normalStrenth: { value: 1.0, target: emissiveScaleAndStrenth.w, editor: { parent: USE_NORMAL_MAP } }
  81. mainTexture: { value: white, target: baseColorMap, editor: { displayName: BaseColorMap } }
  82. - &planar-shadow
  83. vert: planar-shadow-vs
  84. frag: planar-shadow-fs
  85. phase: planar-shadow
  86. propertyIndex: 0
  87. depthStencilState:
  88. depthTest: true
  89. depthWrite: false
  90. stencilTestFront: true
  91. stencilFuncFront: not_equal
  92. stencilPassOpFront: replace
  93. stencilRef: 0x80 # only use the leftmost bit
  94. stencilReadMask: 0x80
  95. stencilWriteMask: 0x80
  96. blendState:
  97. targets:
  98. - blend: true
  99. blendSrc: src_alpha
  100. blendDst: one_minus_src_alpha
  101. blendDstAlpha: one_minus_src_alpha
  102. - switch: USE_OUTLINE_PASS
  103. vert: silhouette-edge-vs
  104. frag: silhouette-edge-fs
  105. phase: deferred-forward
  106. rasterizerState: *r1
  107. depthStencilState: *d1
  108. propertyIndex: 0
  109. - vert: toon-vs
  110. frag: toon-fs
  111. phase: deferred-forward
  112. propertyIndex: 1
  113. }%
  114. CCProgram shared-ubos %{
  115. uniform Constants {
  116. vec4 tilingOffset;
  117. vec4 baseColor;
  118. vec4 colorScaleAndCutoff;
  119. vec4 shadeColor1;
  120. vec4 shadeColor2;
  121. vec4 specular; // xyz: specular color, w: power
  122. vec4 shadeParams;
  123. vec4 miscParams;
  124. vec4 emissive;
  125. vec4 emissiveScaleAndStrenth;
  126. };}%
  127. CCProgram macro-remapping %{
  128. // if enabled, shadowed area is very dark
  129. #pragma define-meta USE_COMPATIBLE_LIGHTING
  130. #define CC_SURFACES_USE_LEGACY_COMPATIBLE_LIGHTING USE_COMPATIBLE_LIGHTING
  131. #pragma define-meta USE_NORMAL_MAP
  132. #define CC_SURFACES_USE_TANGENT_SPACE USE_NORMAL_MAP
  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. }
  140. }%
  141. CCProgram surface-fragment %{
  142. #if USE_BASE_COLOR_MAP
  143. uniform sampler2D baseColorMap;
  144. #endif
  145. #if USE_NORMAL_MAP
  146. uniform sampler2D normalMap;
  147. #endif
  148. #if USE_1ST_SHADE_MAP
  149. uniform sampler2D shadeMap1;
  150. #endif
  151. #if USE_2ND_SHADE_MAP
  152. uniform sampler2D shadeMap2;
  153. #endif
  154. #if USE_SPECULAR_MAP
  155. uniform sampler2D specularMap;
  156. #endif
  157. #if USE_EMISSIVE_MAP
  158. uniform sampler2D emissiveMap;
  159. #endif
  160. #if USE_ALPHA_TEST
  161. #pragma define-meta ALPHA_TEST_CHANNEL options([a, r, g, b])
  162. #endif
  163. #define CC_SURFACES_FRAGMENT_MODIFY_BASECOLOR_AND_TOONSHADE
  164. void SurfacesFragmentModifyBaseColorAndToonShade(out vec4 baseColorAndTransparency, out vec3 shade1, out vec3 shade2)
  165. {
  166. shade2 = shadeColor2.rgb * colorScaleAndCutoff.rgb;
  167. #if USE_2ND_SHADE_MAP
  168. shade2 *= SRGBToLinear(texture(shadeMap2, FSInput_texcoord).rgb);
  169. #endif
  170. shade1 = shadeColor1.rgb * colorScaleAndCutoff.rgb;
  171. #if USE_1ST_SHADE_MAP
  172. shade1 *= SRGBToLinear(texture(shadeMap1, FSInput_texcoord).rgb);
  173. #if SHADE_MAP_1_AS_SHADE_MAP_2
  174. shade2 *= shade1.rgb;
  175. #endif
  176. #endif
  177. vec4 color = baseColor;
  178. #if USE_BASE_COLOR_MAP
  179. vec4 texColor = texture(baseColorMap, FSInput_texcoord);
  180. texColor.rgb = SRGBToLinear(texColor.rgb);
  181. color *= texColor;
  182. #if BASE_COLOR_MAP_AS_SHADE_MAP_1
  183. shade1 *= texColor.rgb;
  184. #endif
  185. #if BASE_COLOR_MAP_AS_SHADE_MAP_2
  186. shade2 *= texColor.rgb;
  187. #endif
  188. #endif
  189. baseColorAndTransparency = color;
  190. baseColorAndTransparency.rgb *= colorScaleAndCutoff.xyz;
  191. #if USE_ALPHA_TEST
  192. if (baseColorAndTransparency.ALPHA_TEST_CHANNEL < colorScaleAndCutoff.w) discard;
  193. #endif
  194. }
  195. #define CC_SURFACES_FRAGMENT_ALPHA_CLIP_ONLY
  196. void SurfacesFragmentAlphaClipOnly()
  197. {
  198. #if USE_ALPHA_TEST
  199. float alpha = baseColor.ALPHA_TEST_CHANNEL;
  200. #if USE_BASE_COLOR_MAP
  201. alpha = texture(baseColorMap, FSInput_texcoord).ALPHA_TEST_CHANNEL;
  202. #endif
  203. if (alpha < colorScaleAndCutoff.w) discard;
  204. #endif
  205. }
  206. #define CC_SURFACES_FRAGMENT_MODIFY_WORLD_NORMAL
  207. vec3 SurfacesFragmentModifyWorldNormal()
  208. {
  209. vec3 normal = FSInput_worldNormal;
  210. #if USE_NORMAL_MAP
  211. vec3 nmmp = texture(normalMap, FSInput_texcoord).xyz - vec3(0.5);
  212. normal = CalculateNormalFromTangentSpace(nmmp, emissiveScaleAndStrenth.w, normalize(normal.xyz), normalize(FSInput_worldTangent), FSInput_mirrorNormal);
  213. #endif
  214. return normalize(normal);
  215. }
  216. #define CC_SURFACES_FRAGMENT_MODIFY_EMISSIVE
  217. vec3 SurfacesFragmentModifyEmissive()
  218. {
  219. vec3 emissive = emissive.rgb * emissiveScaleAndStrenth.xyz;
  220. #if USE_EMISSIVE_MAP
  221. emissive *= SRGBToLinear(texture(emissiveMap, FSInput_texcoord).rgb);
  222. #endif
  223. return emissive;
  224. }
  225. #define CC_SURFACES_FRAGMENT_MODIFY_TOON_STEP_AND_FEATHER
  226. vec4 SurfacesFragmentModifyToonStepAndFeather()
  227. {
  228. return shadeParams;
  229. }
  230. #define CC_SURFACES_FRAGMENT_MODIFY_TOON_SHADOW_COVER
  231. float SurfacesFragmentModifyToonShadowCover()
  232. {
  233. return miscParams.x;
  234. }
  235. #define CC_SURFACES_FRAGMENT_MODIFY_TOON_SPECULAR
  236. vec4 SurfacesFragmentModifyToonSpecular()
  237. {
  238. vec4 specularParam = specular;
  239. #if USE_SPECULAR_MAP
  240. specularParam.rgb *= SRGBToLinear(texture(specularMap, FSInput_texcoord).rgb);
  241. #endif
  242. return specularParam;
  243. }
  244. }%
  245. CCProgram toon-vs %{
  246. precision highp float;
  247. // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros
  248. #include <macro-remapping>
  249. #include <surfaces/effect-macros/common-macros>
  250. // 2. common include with corresponding shader stage
  251. #include <surfaces/includes/common-vs>
  252. // 3. user surface functions that can use user (effect) parameters (ubo Constants)
  253. // see surfaces/default-functions/xxx.chunk
  254. #include <shared-ubos>
  255. #include <surface-vertex>
  256. // 4. surface include with corresponding shader stage and shading-model (optional)
  257. #include <surfaces/includes/toon-vs>
  258. // 5. shader entry with corresponding shader stage and technique usage/type
  259. #include <shading-entries/main-functions/render-to-scene/vs>
  260. }%
  261. CCProgram shadow-caster-vs %{
  262. precision highp float;
  263. #include <macro-remapping>
  264. #include <surfaces/effect-macros/render-to-shadowmap>
  265. #include <surfaces/includes/common-vs>
  266. #include <shared-ubos>
  267. #include <surface-vertex>
  268. #include <shading-entries/main-functions/render-to-shadowmap/vs>
  269. }%
  270. CCProgram planar-shadow-vs %{
  271. precision highp float;
  272. #include <macro-remapping>
  273. #include <surfaces/effect-macros/render-planar-shadow>
  274. #include <surfaces/includes/common-vs>
  275. #include <shared-ubos>
  276. #include <surface-vertex>
  277. #include <shading-entries/main-functions/render-planar-shadow/vs>
  278. }%
  279. CCProgram toon-fs %{
  280. // shading-model : toon
  281. // lighting-model : toon
  282. // shader stage : fs
  283. // technique usage/type : render-to-scene
  284. precision highp float;
  285. // 1. surface internal macros, for technique usage or remapping some user (material) macros to surface internal macros
  286. #include <macro-remapping>
  287. #include <surfaces/effect-macros/common-macros>
  288. // 2. common include with corresponding shader stage
  289. #include <surfaces/includes/common-fs>
  290. // 3. user surface functions that can use user (effect) parameters (ubo Constants)
  291. // see surfaces/default-functions/xxx.chunk
  292. #include <shared-ubos>
  293. #include <surface-fragment>
  294. // 4. lighting-model (optional)
  295. #include <lighting-models/includes/toon>
  296. // 5. surface include with corresponding shader stage and shading-model (optional)
  297. #include <surfaces/includes/toon-fs>
  298. // 6. shader entry with corresponding shader stage and technique usage/type
  299. #include <shading-entries/main-functions/render-to-scene/fs>
  300. }%
  301. CCProgram shadow-caster-fs %{
  302. precision highp float;
  303. #include <macro-remapping>
  304. #include <surfaces/effect-macros/render-to-shadowmap>
  305. #include <surfaces/includes/common-fs>
  306. #include <shared-ubos>
  307. #include <surface-fragment>
  308. #include <shading-entries/main-functions/render-to-shadowmap/fs>
  309. }%
  310. CCProgram planar-shadow-fs %{
  311. precision highp float;
  312. #include <macro-remapping>
  313. #include <surfaces/effect-macros/render-planar-shadow>
  314. #include <surfaces/includes/common-fs>
  315. #include <shared-ubos>
  316. #include <surface-fragment>
  317. #include <shading-entries/main-functions/render-planar-shadow/fs>
  318. }%
  319. ////////////////////////////////////////////////silhouette-edge sample
  320. // how to write a simple surface material
  321. // change technique outline-vs/fs:vert/frag to silhouette-edge-vs/fs
  322. CCProgram surface-vertex-silhouette-edge %{
  323. uniform OutlineVert {
  324. vec4 outlineParams; // x: line width, y: depth hack
  325. };
  326. #define CC_SURFACES_VERTEX_MODIFY_LOCAL_POS
  327. vec3 SurfacesVertexModifyLocalPos(in SurfacesStandardVertexIntermediate In)
  328. {
  329. float width = outlineParams.x * 0.001;
  330. vec3 localPos = In.position.xyz;
  331. #if USE_POSITION_SCALING
  332. vec3 dir = normalize(localPos);
  333. float flip = dot(dir, normalize(In.normal)) < 0.0 ? -1.0 : 1.0;
  334. localPos += flip * dir * width * 2.0;
  335. #else
  336. localPos += normalize(In.normal) * width;
  337. #endif
  338. return localPos;
  339. }
  340. #define CC_SURFACES_VERTEX_MODIFY_CLIP_POS
  341. vec4 SurfacesVertexModifyClipPos(in SurfacesStandardVertexIntermediate In)
  342. {
  343. vec4 clipPos = In.clipPos;
  344. float scaleZ = cc_nearFar.z == 0.0 ? 0.5 : 1.0;
  345. clipPos.z -= outlineParams.y * 0.002 * scaleZ;
  346. return clipPos;
  347. }
  348. }%
  349. CCProgram surface-fragment-silhouette-edge %{
  350. uniform OutlineFrag {
  351. vec4 baseColor;
  352. };
  353. #if USE_BASE_COLOR_MAP
  354. uniform sampler2D baseColorMap;
  355. #endif
  356. // only for render-to-shadow-map or misc usage
  357. #define CC_SURFACES_FRAGMENT_MODIFY_BASECOLOR_AND_TRANSPARENCY
  358. vec4 SurfacesFragmentModifyBaseColorAndTransparency()
  359. {
  360. vec4 color = vec4(cc_mainLitColor.rgb, 1.0);
  361. color.rgb = SRGBToLinear(baseColor.rgb);
  362. #if USE_BASE_COLOR_MAP
  363. vec4 texColor = texture(baseColorMap, FSInput_texcoord);
  364. texColor.rgb = SRGBToLinear(texColor.rgb);
  365. color *= texColor;
  366. #endif
  367. return color;
  368. }
  369. }%
  370. CCProgram silhouette-edge-vs %{
  371. precision highp float;
  372. #include <surfaces/effect-macros/silhouette-edge>
  373. #include <surfaces/includes/common-vs>
  374. #include <surface-vertex-silhouette-edge>
  375. #include <shading-entries/main-functions/misc/silhouette-edge-vs>
  376. }%
  377. CCProgram silhouette-edge-fs %{
  378. precision highp float;
  379. #include <surfaces/effect-macros/silhouette-edge>
  380. #include <surfaces/includes/common-fs>
  381. #include <surface-fragment-silhouette-edge>
  382. #include <shading-entries/main-functions/misc/silhouette-edge-fs>
  383. }%