effect-toon.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import outlineVert from './toon-shader/outlineVert';
  2. import outlineFrag from './toon-shader/outlineFrag';
  3. import toonVert from './toon-shader/toonVert';
  4. import toonFrag from './toon-shader/toonFrag';
  5. const xrFrameSystem = wx.getXrFrameSystem();
  6. xrFrameSystem.registerEffect('toon-user', scene => scene.createEffect(
  7. {
  8. name: "toon",
  9. defaultRenderQueue: 2000,
  10. passes: [
  11. // Outline
  12. {
  13. renderStates: {
  14. blendOn: false,
  15. depthWrite: true,
  16. // Default FrontFace is CW
  17. cullOn: true,
  18. cullFace: xrFrameSystem.ECullMode.BACK,
  19. },
  20. lightMode: "ForwardBase",
  21. useMaterialRenderStates: false,
  22. shaders: [0, 1]
  23. },
  24. // ForwardBase
  25. {
  26. renderStates: {
  27. blendOn: false,
  28. depthWrite: true,
  29. // Default FrontFace is CW
  30. cullOn: false,
  31. cullFace: xrFrameSystem.ECullMode.NONE,
  32. },
  33. lightMode: "ForwardBase",
  34. useMaterialRenderStates: true,
  35. shaders: [2, 3]
  36. },
  37. {
  38. renderStates: {
  39. cullOn: true,
  40. blendOn: false,
  41. depthWrite: true,
  42. cullFace: xrFrameSystem.ECullMode.FRONT,
  43. },
  44. lightMode: 'ShadowCaster',
  45. useMaterialRenderStates: false,
  46. shaders: [4, 5]
  47. },
  48. ],
  49. properties: [
  50. { key: 'u_baseColorFactor', type: xrFrameSystem.EUniformType.FLOAT4, default: [1, 1, 1, 1] },
  51. { key: 'u_outlineColor', type: xrFrameSystem.EUniformType.FLOAT4, default: [0.0, 0.0, 0.0, 0.0]},
  52. // { key: 'u_outlineWidth', type: xrFrameSystem.EUniformType.FLOAT, default: [0.3]},
  53. { key: 'u_outlineWidth', type: xrFrameSystem.EUniformType.FLOAT, default: [1.0]},
  54. { key: 'u_farthestDistance', type: xrFrameSystem.EUniformType.FLOAT, default: [100]},
  55. { key: 'u_nearestDistance', type: xrFrameSystem.EUniformType.FLOAT, default: [0.5]},
  56. { key: 'u_offsetZ', type: xrFrameSystem.EUniformType.FLOAT, default: [2.0]}
  57. ],
  58. images: [
  59. { key: 'u_baseColorMap', default: 'white', macro: 'WX_USE_BASECOLORMAP' },
  60. { key: 'u_gradientMap', default: 'white' }
  61. ],
  62. shaders: [
  63. // === Outline ===
  64. // Vertex Shader Outline
  65. outlineVert,
  66. // Fragment Shader Outline
  67. outlineFrag,
  68. // === Toon ===
  69. // Vertex Shader Toon
  70. toonVert,
  71. // Fragment Shader Toon
  72. toonFrag,
  73. `#version 100
  74. uniform highp mat4 u_world;
  75. uniform highp mat4 u_lightSpaceMatrix;
  76. attribute vec3 a_position;
  77. attribute highp vec2 a_texCoord;
  78. varying highp vec2 v_uv;
  79. varying highp float v_z;
  80. void main()
  81. {
  82. v_uv = a_texCoord;
  83. vec4 worldPosition = u_world * vec4(a_position, 1.0);
  84. vec4 lightPos = u_lightSpaceMatrix * worldPosition;
  85. v_z = lightPos.z / lightPos.w;
  86. v_z = lightPos.z / lightPos.w;
  87. gl_Position = lightPos;
  88. }`,
  89. `#version 100
  90. precision mediump float;
  91. precision highp int;
  92. varying highp vec2 v_uv;
  93. varying highp float v_z;
  94. uniform highp vec4 u_baseColorFactor;
  95. uniform sampler2D u_baseColorMap;
  96. vec4 packDepth(float depth)
  97. {
  98. vec4 bitShift = vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);
  99. vec4 bitMask = vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
  100. vec4 res = mod(
  101. depth * bitShift * vec4(255.0, 255.0, 255.0, 255.0),
  102. vec4(256.0, 256.0, 256.0, 256.0)) / vec4(255.0, 255.0, 255.0, 255.0);
  103. res -= res.xxyz * bitMask;
  104. return res;
  105. }
  106. void main()
  107. {
  108. gl_FragData[0] = packDepth(v_z * 0.5 + 0.5);
  109. // gl_FragData[0] = vec4(v_z * 0.5 + 0.5, 0.0, 0.0, 1.0);
  110. }
  111. `
  112. ]
  113. }
  114. ));