index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. Component({
  2. properties: {
  3. },
  4. data: {
  5. },
  6. methods: {
  7. handleReady({detail}) {
  8. const xrFrameSystem = wx.getXrFrameSystem()
  9. const createFrameEffect = (scene) => {
  10. const xrFrameSystem = wx.getXrFrameSystem()
  11. return scene.createEffect({
  12. name: 'frame-effect',
  13. properties: [
  14. {
  15. key: 'columCount',
  16. type: xrFrameSystem.EUniformType.FLOAT,
  17. default: 1
  18. },
  19. {
  20. key: 'rowCount',
  21. type: xrFrameSystem.EUniformType.FLOAT,
  22. default: 1
  23. },
  24. {
  25. key: 'during',
  26. type: xrFrameSystem.EUniformType.FLOAT,
  27. default: 1
  28. }
  29. ],
  30. images: [
  31. {
  32. key: 'u_baseColorMap',
  33. default: 'white',
  34. macro: 'WX_USE_BASECOLORMAP'
  35. }
  36. ],
  37. // 透明物体需要大于`2500`!
  38. defaultRenderQueue: 2501,
  39. passes: [
  40. {
  41. renderStates: {
  42. blendOn: false,
  43. depthWrite: true,
  44. cullOn: false,
  45. // 基础库 v3.0.1 开始 默认的 plane 切为适配 cw 的顶点绕序
  46. },
  47. lightMode: 'ForwardBase',
  48. useMaterialRenderStates: true,
  49. shaders: [0, 1]
  50. }
  51. ],
  52. shaders: [`#version 100
  53. precision highp float;
  54. precision highp int;
  55. attribute vec3 a_position;
  56. attribute highp vec2 a_texCoord;
  57. uniform mat4 u_view;
  58. uniform mat4 u_projection;
  59. uniform mat4 u_world;
  60. varying highp vec2 v_uv;
  61. void main()
  62. {
  63. v_uv = a_texCoord;
  64. gl_Position = u_projection * u_view * u_world * vec4(a_position, 1.0);
  65. }`,
  66. // float 使用 highp 保证精度
  67. `#version 100
  68. precision highp float;
  69. precision highp int;
  70. uniform sampler2D u_baseColorMap;
  71. uniform highp float u_gameTime;
  72. uniform highp float rowCount;
  73. uniform highp float columCount;
  74. uniform highp float during;
  75. varying highp vec2 v_uv;
  76. void main()
  77. {
  78. float loopTime = mod(u_gameTime, during);
  79. float tickPerFrame = during / (columCount * rowCount);
  80. float columTick = mod(floor(loopTime / tickPerFrame), columCount);
  81. float rowTick = floor(loopTime / tickPerFrame / columCount);
  82. vec2 texCoord = vec2(v_uv.x / columCount + (1.0 / columCount) * columTick , v_uv.y / rowCount + (1.0 / rowCount) * rowTick);
  83. vec4 color = texture2D(u_baseColorMap, texCoord);
  84. gl_FragColor = color;
  85. }`
  86. ],
  87. });
  88. }
  89. console.log('序列帧特效加载')
  90. xrFrameSystem.registerEffect('frame-effect', createFrameEffect)
  91. const xrScene = this.scene = detail.value;
  92. this.loadAsset()
  93. },
  94. async loadAsset(){
  95. const xrFrameSystem = wx.getXrFrameSystem();
  96. const xrScene = this.scene;
  97. const shadowRoot = xrScene.getElementById("shadow-root");
  98. await xrScene.assets.loadAsset({type: 'texture', assetId: 'frame', src: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/sprite-frames.png'})
  99. // 第一个参数是效果实例的引用,第二个参数是默认`uniforms`
  100. const frameMaterial = xrScene.createMaterial(
  101. // 使用定制的效果
  102. xrScene.assets.getAsset('effect', 'frame-effect'),
  103. {u_baseColorMap: xrScene.assets.getAsset('texture', 'frame')}
  104. );
  105. // 可以将其添加到资源系统中备用
  106. xrScene.assets.addAsset('material', 'frame-effect', frameMaterial);
  107. const meshElement = xrScene.getElementById("animation-mesh").getComponent(xrFrameSystem.Mesh);
  108. frameMaterial.setFloat('columCount', 4);
  109. frameMaterial.setFloat('rowCount', 32);
  110. frameMaterial.setFloat('during', 2);
  111. frameMaterial.alphaMode = "BLEND";
  112. meshElement.material = frameMaterial
  113. },
  114. }
  115. })