standard.effect 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. 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. mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } }
  21. normalMap: { value: normal }
  22. pbrMap: { value: grey }
  23. metallicRoughnessMap: { value: grey }
  24. occlusionMap: { value: white }
  25. emissiveMap: { value: grey }
  26. - &forward-add
  27. vert: standard-vs
  28. frag: standard-fs
  29. phase: forward-add
  30. propertyIndex: 0
  31. embeddedMacros: { CC_FORWARD_ADD: true }
  32. depthStencilState:
  33. depthFunc: equal
  34. depthTest: true
  35. depthWrite: false
  36. blendState:
  37. targets:
  38. - blend: true
  39. blendSrc: one
  40. blendDst: one
  41. blendSrcAlpha: zero
  42. blendDstAlpha: one
  43. - &shadow-caster
  44. vert: shadow-caster-vs:vert
  45. frag: shadow-caster-fs:frag
  46. phase: shadow-caster
  47. propertyIndex: 0
  48. rasterizerState:
  49. cullMode: front
  50. properties:
  51. tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] }
  52. mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, editor: { displayName: Albedo, type: color } }
  53. albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz }
  54. alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } }
  55. mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } }
  56. - &planar-shadow
  57. vert: planar-shadow-vs:vert
  58. frag: planar-shadow-fs:frag
  59. phase: planar-shadow
  60. propertyIndex: 0
  61. depthStencilState:
  62. depthTest: true
  63. depthWrite: false
  64. stencilTestFront: true
  65. stencilFuncFront: not_equal
  66. stencilPassOpFront: replace
  67. stencilRef: 0x80 # only use the leftmost bit
  68. stencilReadMask: 0x80
  69. stencilWriteMask: 0x80
  70. blendState:
  71. targets:
  72. - blend: true
  73. blendSrc: src_alpha
  74. blendDst: one_minus_src_alpha
  75. blendDstAlpha: one_minus_src_alpha
  76. - &deferred
  77. vert: standard-vs
  78. frag: standard-fs
  79. pass: gbuffer
  80. phase: gbuffer
  81. embeddedMacros: { CC_PIPELINE_TYPE: 1 }
  82. propertyIndex: 0
  83. - name: transparent
  84. passes:
  85. - vert: standard-vs
  86. frag: standard-fs
  87. embeddedMacros: { CC_FORCE_FORWARD_SHADING: true }
  88. depthStencilState: &d1
  89. depthTest: true
  90. depthWrite: false
  91. blendState: &b1
  92. targets:
  93. - blend: true
  94. blendSrc: src_alpha
  95. blendDst: one_minus_src_alpha
  96. blendDstAlpha: one_minus_src_alpha
  97. properties: *props
  98. - *forward-add
  99. - *shadow-caster
  100. - *planar-shadow
  101. - &deferred-forward
  102. vert: standard-vs
  103. frag: standard-fs
  104. phase: deferred-forward
  105. embeddedMacros: { CC_PIPELINE_TYPE: 0 }
  106. propertyIndex: 0
  107. depthStencilState: *d1
  108. blendState: *b1
  109. }%
  110. CCProgram shared-ubos %{
  111. uniform Constants {
  112. vec4 tilingOffset;
  113. vec4 albedo;
  114. vec4 albedoScaleAndCutoff;
  115. vec4 pbrParams;
  116. vec4 emissive;
  117. vec4 emissiveScaleParam;
  118. };
  119. }%
  120. CCProgram standard-vs %{
  121. precision highp float;
  122. #include <legacy/input-standard>
  123. #include <builtin/uniforms/cc-global>
  124. #include <legacy/decode-base>
  125. #include <legacy/local-batch>
  126. #include <shared-ubos>
  127. #include <legacy/fog-vs>
  128. #include <legacy/shadow-map-vs>
  129. #include <legacy/sh-vs>
  130. #if USE_VERTEX_COLOR
  131. in vec4 a_color;
  132. out lowp vec4 v_color;
  133. #endif
  134. out vec3 v_position;
  135. out vec3 v_normal;
  136. out vec2 v_uv;
  137. #if HAS_SECOND_UV
  138. out mediump vec2 v_uv1;
  139. #endif
  140. #if CC_RECEIVE_SHADOW || CC_USE_REFLECTION_PROBE
  141. out mediump vec4 v_shadowBiasAndProbeId;
  142. #endif
  143. #if CC_USE_REFLECTION_PROBE && USE_INSTANCING
  144. out mediump vec4 v_reflectionProbeData;
  145. #endif
  146. #if USE_NORMAL_MAP
  147. out mediump vec4 v_tangent;
  148. #endif
  149. #if HAS_SECOND_UV || CC_USE_LIGHTMAP
  150. in vec2 a_texCoord1;
  151. #endif
  152. #if CC_USE_LIGHTMAP && !CC_FORWARD_ADD
  153. #include <legacy/lightingmap-vs>
  154. #endif
  155. void main () {
  156. StandardVertInput In;
  157. CCVertInput(In);
  158. mat4 matWorld, matWorldIT;
  159. CCGetWorldMatrixFull(matWorld, matWorldIT);
  160. vec4 pos = matWorld * In.position;
  161. v_position = pos.xyz;
  162. v_normal = normalize((matWorldIT * vec4(In.normal, 0.0)).xyz);
  163. #if CC_RECEIVE_SHADOW
  164. v_shadowBiasAndProbeId.xy = CCGetShadowBias();
  165. #endif
  166. #if CC_RECEIVE_SHADOW || CC_USE_REFLECTION_PROBE
  167. #if USE_INSTANCING
  168. v_shadowBiasAndProbeId.zw = a_localShadowBiasAndProbeId.zw;
  169. #else
  170. v_shadowBiasAndProbeId.zw = cc_localShadowBias.zw;
  171. #endif
  172. #endif
  173. #if CC_USE_REFLECTION_PROBE && USE_INSTANCING
  174. v_reflectionProbeData = a_reflectionProbeData;
  175. #endif
  176. #if USE_TWOSIDE
  177. vec3 viewDirect = normalize(cc_cameraPos.xyz - v_position);
  178. v_normal *= dot(v_normal, viewDirect) < 0.0 ? -1.0 : 1.0;
  179. #endif
  180. #if USE_NORMAL_MAP
  181. v_tangent.xyz = normalize((matWorld * vec4(In.tangent.xyz, 0.0)).xyz);
  182. v_tangent.w = In.tangent.w;
  183. #endif
  184. v_uv = a_texCoord * tilingOffset.xy + tilingOffset.zw;
  185. #if SAMPLE_FROM_RT
  186. CC_HANDLE_RT_SAMPLE_FLIP(v_uv);
  187. #endif
  188. #if HAS_SECOND_UV
  189. v_uv1 = a_texCoord1 * tilingOffset.xy + tilingOffset.zw;
  190. #if SAMPLE_FROM_RT
  191. CC_HANDLE_RT_SAMPLE_FLIP(v_uv1);
  192. #endif
  193. #endif
  194. #if USE_VERTEX_COLOR
  195. v_color = a_color;
  196. #endif
  197. CC_TRANSFER_FOG(pos);
  198. CC_TRANSFER_SHADOW(pos);
  199. CC_TRANSFER_SH();
  200. #if CC_USE_LIGHTMAP && !CC_FORWARD_ADD
  201. CCLightingMapCaclUV();
  202. #endif
  203. #if CC_USE_2D
  204. gl_Position = cc_matProj * cc_matView * In.position;
  205. #else
  206. gl_Position = cc_matProj * (cc_matView * matWorld) * In.position;
  207. #endif
  208. }
  209. }%
  210. CCProgram standard-fs %{
  211. precision highp float;
  212. #include <builtin/uniforms/cc-global>
  213. #include <shared-ubos>
  214. #include <legacy/fog-fs>
  215. #include <legacy/standard-surface-entry>
  216. #if CC_USE_LIGHTMAP && !CC_FORWARD_ADD
  217. #include <legacy/lightingmap-fs>
  218. #endif
  219. in vec3 v_position;
  220. in vec2 v_uv;
  221. #if HAS_SECOND_UV
  222. in mediump vec2 v_uv1;
  223. #endif
  224. in vec3 v_normal;
  225. #if CC_RECEIVE_SHADOW || CC_USE_REFLECTION_PROBE
  226. in mediump vec4 v_shadowBiasAndProbeId;
  227. #endif
  228. #if CC_USE_REFLECTION_PROBE && USE_INSTANCING
  229. in mediump vec4 v_reflectionProbeData;
  230. #endif
  231. #if USE_VERTEX_COLOR
  232. in lowp vec4 v_color;
  233. #endif
  234. #if USE_ALBEDO_MAP
  235. uniform sampler2D albedoMap;
  236. #pragma define-meta ALBEDO_UV options([v_uv, v_uv1])
  237. #endif
  238. #if USE_NORMAL_MAP
  239. in mediump vec4 v_tangent;
  240. uniform sampler2D normalMap;
  241. #pragma define-meta NORMAL_UV options([v_uv, v_uv1])
  242. #endif
  243. #pragma define-meta PBR_UV options([v_uv, v_uv1])
  244. #if USE_PBR_MAP
  245. uniform sampler2D pbrMap;
  246. #endif
  247. #if USE_METALLIC_ROUGHNESS_MAP
  248. uniform sampler2D metallicRoughnessMap;
  249. #endif
  250. #if USE_OCCLUSION_MAP
  251. uniform sampler2D occlusionMap;
  252. #endif
  253. #if USE_EMISSIVE_MAP
  254. uniform sampler2D emissiveMap;
  255. #pragma define-meta EMISSIVE_UV options([v_uv, v_uv1])
  256. #endif
  257. #pragma define OCCLUSION_CHANNEL r
  258. #pragma define ROUGHNESS_CHANNEL g
  259. #pragma define METALLIC_CHANNEL b
  260. #pragma define SPECULAR_INTENSITY_CHANNEL a
  261. #if USE_ALPHA_TEST
  262. #pragma define-meta ALPHA_TEST_CHANNEL options([a, r])
  263. #endif
  264. void surf (out StandardSurface s) {
  265. vec4 baseColor = albedo;
  266. #if USE_VERTEX_COLOR
  267. baseColor.rgb *= SRGBToLinear(v_color.rgb); // use linear
  268. baseColor.a *= v_color.a;
  269. #endif
  270. #if USE_ALBEDO_MAP
  271. vec4 texColor = texture(albedoMap, ALBEDO_UV);
  272. texColor.rgb = SRGBToLinear(texColor.rgb);
  273. baseColor *= texColor;
  274. #endif
  275. s.albedo = baseColor;
  276. s.albedo.rgb *= albedoScaleAndCutoff.xyz;
  277. #if USE_ALPHA_TEST
  278. if (s.albedo.ALPHA_TEST_CHANNEL < albedoScaleAndCutoff.w) discard;
  279. #endif
  280. s.normal = v_normal;
  281. #if CC_RECEIVE_SHADOW
  282. s.shadowBias = v_shadowBiasAndProbeId.xy;
  283. #endif
  284. #if CC_USE_REFLECTION_PROBE
  285. s.reflectionProbeId = v_shadowBiasAndProbeId.z;
  286. #endif
  287. #if CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_TYPE_BLEND || CC_USE_REFLECTION_PROBE == REFLECTION_PROBE_TYPE_BLEND_AND_SKYBOX
  288. s.reflectionProbeBlendId = v_shadowBiasAndProbeId.w;
  289. #if USE_INSTANCING
  290. s.reflectionProbeBlendFactor = v_reflectionProbeData.x;
  291. #else
  292. s.reflectionProbeBlendFactor = cc_reflectionProbeBlendData1.w;
  293. #endif
  294. #endif
  295. #if USE_NORMAL_MAP
  296. vec3 nmmp = texture(normalMap, NORMAL_UV).xyz - vec3(0.5);
  297. vec3 bitangent = cross(v_normal, v_tangent.xyz) * (v_tangent.w > 0.0 ? 1.0 : -1.0); // note the cross order
  298. s.normal =
  299. (nmmp.x * emissiveScaleParam.w) * normalize(v_tangent.xyz) +
  300. (nmmp.y * emissiveScaleParam.w) * normalize(bitangent) +
  301. nmmp.z * normalize(s.normal);
  302. #endif
  303. #if CC_USE_LIGHTMAP && !CC_FORWARD_ADD
  304. SampleAndDecodeLightMapColor(s.lightmap.rgb, s.lightmap.a, s.lightmap_test, cc_lightingMap, v_luv.xy, v_luv.z, s.normal);
  305. #endif
  306. HIGHP_VALUE_TO_STRUCT_DEFINED(v_position, s.position);
  307. vec4 pbr = pbrParams;
  308. pbr.x = 1.0;
  309. #if USE_PBR_MAP
  310. vec4 res = texture(pbrMap, PBR_UV);
  311. pbr.x = mix(1.0, res.OCCLUSION_CHANNEL, pbrParams.x);
  312. pbr.y *= res.ROUGHNESS_CHANNEL;
  313. pbr.z *= res.METALLIC_CHANNEL;
  314. pbr.w *= res.SPECULAR_INTENSITY_CHANNEL;
  315. #endif
  316. #if USE_METALLIC_ROUGHNESS_MAP
  317. vec4 metallicRoughness = texture(metallicRoughnessMap, PBR_UV);
  318. pbr.z *= metallicRoughness.METALLIC_CHANNEL;
  319. pbr.y *= metallicRoughness.ROUGHNESS_CHANNEL;
  320. #endif
  321. #if USE_OCCLUSION_MAP
  322. pbr.x = mix(1.0, texture(occlusionMap, PBR_UV).OCCLUSION_CHANNEL, pbrParams.x);
  323. #endif
  324. s.occlusion = pbr.x;
  325. s.roughness = pbr.y;
  326. s.metallic = pbr.z;
  327. s.specularIntensity = pbr.w;
  328. s.emissive = emissive.rgb;
  329. #if USE_EMISSIVE_MAP
  330. s.emissive = SRGBToLinear(texture(emissiveMap, EMISSIVE_UV).rgb);
  331. #endif
  332. s.emissive *= emissiveScaleParam.xyz;
  333. }
  334. CC_STANDARD_SURFACE_ENTRY()
  335. }%
  336. CCProgram shadow-caster-vs %{
  337. precision highp float;
  338. #include <legacy/input-standard>
  339. #include <legacy/decode-base>
  340. #include <legacy/local-batch>
  341. #include <shared-ubos>
  342. #include <builtin/uniforms/cc-shadow>
  343. #if HAS_SECOND_UV || CC_USE_LIGHTMAP
  344. in vec2 a_texCoord1;
  345. #endif
  346. out vec2 v_uv;
  347. #if HAS_SECOND_UV
  348. out vec2 v_uv1;
  349. #endif
  350. out vec4 v_worldPos;
  351. out highp vec2 v_clip_depth;
  352. vec4 vert () {
  353. StandardVertInput In;
  354. CCVertInput(In);
  355. mat4 matWorld, matWorldIT;
  356. CCGetWorldMatrixFull(matWorld, matWorldIT);
  357. v_worldPos = matWorld * In.position;
  358. vec4 clipPos = cc_matLightViewProj * v_worldPos;
  359. v_uv = a_texCoord * tilingOffset.xy + tilingOffset.zw;
  360. #if HAS_SECOND_UV
  361. v_uv1 = a_texCoord1 * tilingOffset.xy + tilingOffset.zw;
  362. #endif
  363. v_clip_depth = clipPos.zw;
  364. return clipPos;
  365. }
  366. }%
  367. CCProgram shadow-caster-fs %{
  368. precision highp float;
  369. #include <shared-ubos>
  370. #include <common/data/packing>
  371. #include <legacy/shadow-map-base>
  372. in vec2 v_uv;
  373. #if HAS_SECOND_UV
  374. in vec2 v_uv1;
  375. #endif
  376. in vec4 v_worldPos;
  377. in highp vec2 v_clip_depth;
  378. #if USE_ALBEDO_MAP
  379. uniform sampler2D albedoMap;
  380. #pragma define-meta ALBEDO_UV options([v_uv, v_uv1])
  381. #endif
  382. #if USE_ALPHA_TEST
  383. #pragma define-meta ALPHA_TEST_CHANNEL options([a, r])
  384. #endif
  385. vec4 frag () {
  386. vec4 baseColor = albedo;
  387. #if USE_ALPHA_TEST
  388. #if USE_ALBEDO_MAP
  389. baseColor *= texture(albedoMap, ALBEDO_UV);
  390. #endif
  391. if (baseColor.ALPHA_TEST_CHANNEL < albedoScaleAndCutoff.w) discard;
  392. #endif
  393. highp float clipDepth = v_clip_depth.x / v_clip_depth.y * 0.5 + 0.5;
  394. // spot use linear
  395. #if CC_SHADOWMAP_USE_LINEAR_DEPTH
  396. if (IS_SPOT_LIGHT(cc_shadowLPNNInfo.x)) {
  397. clipDepth = CCGetLinearDepth(v_worldPos.xyz);
  398. }
  399. #endif
  400. #if CC_SHADOWMAP_FORMAT == SHADOWMAP_FORMAT_RGBA8
  401. return packDepthToRGBA(clipDepth);
  402. #else
  403. return vec4(clipDepth, 1.0, 1.0, 1.0);
  404. #endif
  405. }
  406. }%
  407. CCProgram planar-shadow-vs %{
  408. precision highp float;
  409. #include <legacy/input>
  410. #include <builtin/uniforms/cc-global>
  411. #include <legacy/decode-base>
  412. #include <legacy/local-batch>
  413. #include <builtin/uniforms/cc-shadow>
  414. #include <common/lighting/functions>
  415. out float v_dist;
  416. vec4 vert () {
  417. vec4 position;
  418. CCVertInput(position);
  419. // World Space
  420. mat4 matWorld, matWorldIT;
  421. CCGetWorldMatrixFull(matWorld, matWorldIT);
  422. vec3 worldPos = (matWorld * position).xyz;
  423. vec4 shadowPos = CalculatePlanarShadowPos(worldPos, cc_cameraPos.xyz, cc_mainLitDir.xyz, cc_planarNDInfo);
  424. position = CalculatePlanarShadowClipPos(shadowPos, cc_cameraPos.xyz, cc_matView, cc_matProj, cc_nearFar, cc_shadowWHPBInfo.w);
  425. v_dist = shadowPos.w;
  426. return position;
  427. }
  428. }%
  429. CCProgram planar-shadow-fs %{
  430. precision highp float;
  431. #include <builtin/uniforms/cc-shadow>
  432. #include <legacy/output>
  433. in float v_dist;
  434. vec4 frag () {
  435. if(v_dist < 0.0)
  436. discard;
  437. return CCFragOutput(cc_shadowColor);
  438. }
  439. }%