toon.effect 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
  2. CCEffect %{
  3. techniques:
  4. - passes:
  5. - switch: USE_OUTLINE_PASS
  6. vert: legacy/main-functions/outline-vs:vert
  7. frag: legacy/main-functions/outline-fs:frag
  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:vert
  20. frag: toon-fs:frag
  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:vert
  44. frag: toon-fs:frag
  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:vert
  61. frag: shadow-caster-fs:frag
  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:vert
  84. frag: planar-shadow-fs:frag
  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: legacy/main-functions/outline-vs:vert
  104. frag: legacy/main-functions/outline-fs:frag
  105. phase: deferred-forward
  106. rasterizerState: *r1
  107. depthStencilState: *d1
  108. propertyIndex: 0
  109. - vert: toon-vs:vert
  110. frag: toon-fs:frag
  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. }%
  128. CCProgram toon-vs %{
  129. precision highp float;
  130. #include <legacy/input-standard>
  131. #include <builtin/uniforms/cc-global>
  132. #include <legacy/decode-base>
  133. #include <legacy/local-batch>
  134. #include <shared-ubos>
  135. #include <legacy/shadow-map-vs>
  136. out vec3 v_position;
  137. out vec2 v_uv;
  138. out mediump vec3 v_normal;
  139. #if CC_RECEIVE_SHADOW
  140. out mediump vec2 v_shadowBias;
  141. #endif
  142. #if USE_NORMAL_MAP
  143. out mediump vec4 v_tangent;
  144. #endif
  145. vec4 vert () {
  146. StandardVertInput In;
  147. CCVertInput(In);
  148. mat4 matWorld, matWorldIT;
  149. CCGetWorldMatrixFull(matWorld, matWorldIT);
  150. vec4 pos = matWorld * In.position;
  151. v_position = pos.xyz;
  152. v_uv = a_texCoord * tilingOffset.xy + tilingOffset.zw;
  153. #if CC_RECEIVE_SHADOW
  154. v_shadowBias = CCGetShadowBias();
  155. #endif
  156. v_normal = (matWorldIT * vec4(In.normal, 0.0)).xyz;
  157. #if USE_NORMAL_MAP
  158. v_tangent.xyz = normalize((matWorld * vec4(In.tangent.xyz, 0.0)).xyz);
  159. v_tangent.w = In.tangent.w;
  160. #endif
  161. CC_TRANSFER_SHADOW(pos);
  162. #if CC_USE_2D
  163. return cc_matProj * cc_matView * In.position;
  164. #else
  165. return cc_matProj * (cc_matView * matWorld) * In.position;
  166. #endif
  167. }
  168. }%
  169. CCProgram toon-fs %{
  170. precision highp float;
  171. #include <builtin/uniforms/cc-global>
  172. #include <legacy/shading-toon>
  173. #include <shared-ubos>
  174. #include <legacy/output-standard>
  175. #include <common/color/gamma>
  176. in vec3 v_position;
  177. in vec2 v_uv;
  178. #if CC_RECEIVE_SHADOW
  179. in mediump vec2 v_shadowBias;
  180. #endif
  181. #if USE_BASE_COLOR_MAP
  182. uniform sampler2D baseColorMap;
  183. #endif
  184. in mediump vec3 v_normal;
  185. #if USE_NORMAL_MAP
  186. in mediump vec4 v_tangent;
  187. uniform sampler2D normalMap;
  188. #endif
  189. #if USE_1ST_SHADE_MAP
  190. uniform sampler2D shadeMap1;
  191. #endif
  192. #if USE_2ND_SHADE_MAP
  193. uniform sampler2D shadeMap2;
  194. #endif
  195. #if USE_SPECULAR_MAP
  196. uniform sampler2D specularMap;
  197. #endif
  198. #if USE_EMISSIVE_MAP
  199. uniform sampler2D emissiveMap;
  200. #endif
  201. #if USE_ALPHA_TEST
  202. #pragma define-meta ALPHA_TEST_CHANNEL options([a, r, g, b])
  203. #endif
  204. void surf (out ToonSurface s) {
  205. s.shade2 = shadeColor2.rgb * colorScaleAndCutoff.rgb;
  206. #if USE_2ND_SHADE_MAP
  207. s.shade2 *= SRGBToLinear(texture(shadeMap2, v_uv).rgb);
  208. #endif
  209. s.shade1 = shadeColor1.rgb * colorScaleAndCutoff.rgb;
  210. #if USE_1ST_SHADE_MAP
  211. s.shade1 *= SRGBToLinear(texture(shadeMap1, v_uv).rgb);
  212. #if SHADE_MAP_1_AS_SHADE_MAP_2
  213. s.shade2 *= s.shade1.rgb;
  214. #endif
  215. #endif
  216. vec4 localBaseColor = baseColor;
  217. #if USE_BASE_COLOR_MAP
  218. vec4 baseColorMap = texture(baseColorMap, v_uv);
  219. baseColorMap.rgb = SRGBToLinear(baseColorMap.rgb);
  220. localBaseColor *= baseColorMap;
  221. #if BASE_COLOR_MAP_AS_SHADE_MAP_1
  222. s.shade1 *= baseColorMap.rgb;
  223. #endif
  224. #if BASE_COLOR_MAP_AS_SHADE_MAP_2
  225. s.shade2 *= baseColorMap.rgb;
  226. #endif
  227. #endif
  228. s.baseColor = localBaseColor;
  229. s.baseColor.rgb *= colorScaleAndCutoff.xyz;
  230. #if USE_ALPHA_TEST
  231. if (s.baseColor.ALPHA_TEST_CHANNEL < colorScaleAndCutoff.w) discard;
  232. #endif
  233. s.normal = v_normal;
  234. #if CC_RECEIVE_SHADOW
  235. s.shadowBias = v_shadowBias;
  236. #endif
  237. #if USE_NORMAL_MAP
  238. vec3 nmmp = texture(normalMap, v_uv).xyz - vec3(0.5);
  239. vec3 bitangent = cross(v_normal, v_tangent.xyz) * (v_tangent.w > 0.0 ? 1.0 : -1.0); // note the cross order
  240. s.normal =
  241. (nmmp.x * emissiveScaleAndStrenth.w) * normalize(v_tangent.xyz) +
  242. (nmmp.y * emissiveScaleAndStrenth.w) * normalize(bitangent) +
  243. nmmp.z * normalize(s.normal);
  244. #endif
  245. HIGHP_VALUE_TO_STRUCT_DEFINED(v_position, s.position);
  246. s.specular = specular;
  247. #if USE_SPECULAR_MAP
  248. s.specular.rgb *= SRGBToLinear(texture(specularMap, v_uv).rgb);
  249. #endif
  250. s.emissive = emissive.rgb * emissiveScaleAndStrenth.xyz;
  251. #if USE_EMISSIVE_MAP
  252. s.emissive *= SRGBToLinear(texture(emissiveMap, v_uv).rgb);
  253. #endif
  254. s.baseStep = shadeParams.x;
  255. s.baseFeather = shadeParams.y;
  256. s.shadeStep = shadeParams.z;
  257. s.shadeFeather = shadeParams.w;
  258. s.shadowCover = miscParams.x;
  259. }
  260. vec4 frag () {
  261. ToonSurface s; surf(s);
  262. vec4 color = CCToonShading(s);
  263. return CCFragOutput(color);
  264. }
  265. }%
  266. CCProgram shadow-caster-vs %{
  267. precision highp float;
  268. #include <legacy/input-standard>
  269. #include <legacy/decode-base>
  270. #include <legacy/local-batch>
  271. #include <shared-ubos>
  272. #include <builtin/uniforms/cc-shadow>
  273. out vec2 v_uv;
  274. out vec4 v_worldPos;
  275. out highp vec2 v_clip_depth;
  276. vec4 vert () {
  277. StandardVertInput In;
  278. CCVertInput(In);
  279. mat4 matWorld, matWorldIT;
  280. CCGetWorldMatrixFull(matWorld, matWorldIT);
  281. v_worldPos = matWorld * In.position;
  282. vec4 clipPos = cc_matLightViewProj * v_worldPos;
  283. v_clip_depth = clipPos.zw;
  284. v_uv = a_texCoord * tilingOffset.xy + tilingOffset.zw;
  285. return clipPos;
  286. }
  287. }%
  288. CCProgram shadow-caster-fs %{
  289. precision highp float;
  290. #include <shared-ubos>
  291. #include <common/data/packing>
  292. #include <legacy/shadow-map-base>
  293. in vec2 v_uv;
  294. in vec4 v_worldPos;
  295. in highp vec2 v_clip_depth;
  296. #if USE_BASE_COLOR_MAP
  297. uniform sampler2D baseColorMap;
  298. #endif
  299. #if USE_ALPHA_TEST
  300. #pragma define-meta ALPHA_TEST_CHANNEL options([a, r, g, b])
  301. #endif
  302. vec4 frag () {
  303. vec4 baseColor = baseColor;
  304. #if USE_ALPHA_TEST
  305. #if USE_BASE_COLOR_MAP
  306. baseColor *= texture(baseColorMap, v_uv);
  307. #endif
  308. if (baseColor.ALPHA_TEST_CHANNEL < colorScaleAndCutoff.w) discard;
  309. #endif
  310. highp float clipDepth = v_clip_depth.x / v_clip_depth.y * 0.5 + 0.5;
  311. // spot use linear
  312. #if CC_SHADOWMAP_USE_LINEAR_DEPTH
  313. if (IS_SPOT_LIGHT(cc_shadowLPNNInfo.x)) {
  314. clipDepth = CCGetLinearDepth(v_worldPos.xyz);
  315. }
  316. #endif
  317. #if CC_SHADOWMAP_FORMAT == SHADOWMAP_FORMAT_RGBA8
  318. return packDepthToRGBA(clipDepth);
  319. #else
  320. return vec4(clipDepth, 1.0, 1.0, 1.0);
  321. #endif
  322. }
  323. }%
  324. CCProgram planar-shadow-vs %{
  325. precision highp float;
  326. #include <legacy/input>
  327. #include <builtin/uniforms/cc-global>
  328. #include <legacy/decode-base>
  329. #include <legacy/local-batch>
  330. #include <builtin/uniforms/cc-shadow>
  331. #include <common/lighting/functions>
  332. out float v_dist;
  333. vec4 vert () {
  334. vec4 position;
  335. CCVertInput(position);
  336. // World Space
  337. mat4 matWorld, matWorldIT;
  338. CCGetWorldMatrixFull(matWorld, matWorldIT);
  339. vec3 worldPos = (matWorld * position).xyz;
  340. vec4 shadowPos = CalculatePlanarShadowPos(worldPos, cc_cameraPos.xyz, cc_mainLitDir.xyz, cc_planarNDInfo);
  341. position = CalculatePlanarShadowClipPos(shadowPos, cc_cameraPos.xyz, cc_matView, cc_matProj, cc_nearFar, cc_shadowWHPBInfo.w);
  342. v_dist = shadowPos.w;
  343. return position;
  344. }
  345. }%
  346. CCProgram planar-shadow-fs %{
  347. precision highp float;
  348. #include <builtin/uniforms/cc-shadow>
  349. #include <legacy/output>
  350. in float v_dist;
  351. vec4 frag () {
  352. if(v_dist < 0.0)
  353. discard;
  354. return CCFragOutput(cc_shadowColor);
  355. }
  356. }%