微信小程序XR黑色背景视频透明效果(一)
去除黑色背景主要依赖于effect="removeBlack",其中removeBlack为官方demo写的规则,代码如下
const xrFrameSystem = wx.getXrFrameSystem(); xrFrameSystem.registerEffect('removeBlack', scene => scene.createEffect({ name: "removeBlack", images: [{ key: 'u_videoMap', default: 'white', macro: 'WX_USE_VIDEOMAP' }], defaultRenderQueue: 2000, passes: [{ "renderStates": { cullOn: false, blendOn: true, blendSrc: xrFrameSystem.EBlendFactor.SRC_ALPHA, blendDst: xrFrameSystem.EBlendFactor.ONE_MINUS_SRC_ALPHA, cullFace: xrFrameSystem.ECullMode.BACK, }, lightMode: "ForwardBase", useMaterialRenderStates: true, shaders: [0, 1] }], shaders: [ `#version 100 uniform highp mat4 u_view; uniform highp mat4 u_viewInverse; uniform highp mat4 u_vp; uniform highp mat4 u_projection; uniform highp mat4 u_world; attribute vec3 a_position; attribute highp vec2 a_texCoord; varying highp vec2 v_UV; void main() { v_UV = a_texCoord; vec4 worldPosition = u_world * vec4(a_position, 1.0); gl_Position = u_projection * u_view * worldPosition; }`, `#version 100 precision mediump float; precision highp int; varying highp vec2 v_UV; #ifdef WX_USE_VIDEOMAP uniform sampler2D u_videoMap; #endif void main() { #ifdef WX_USE_VIDEOMAP vec4 baseColor = texture2D(u_videoMap, v_UV); #else vec4 baseColor = vec4(1.0, 1.0, 1.0, 1.0); #endif float rgbSum = baseColor.r + baseColor.g + baseColor.b; // 设定阈值避免异常情况 if (rgbSum < 0.1) { gl_FragData[0] = vec4(1.0, 1.0, 1.0, 0.0); } else { gl_FragData[0] = vec4(pow(baseColor.rgb, vec3(2.2)), 1.0); } } `], }));
再进行规则引入
import '../../xr-custom/assets/effect-removeBlack';
<xr-scene ar-system="modes:Marker" bind:ready="handleReady"> <xr-assets bind:progress="handleAssetsProgress" bind:loaded="handleAssetsLoaded"> <xr-asset-load type="video-texture" asset-id="test3" options="autoPlay:true,loop:true" src="https://cdn.apix.cn/xxx.mp4" /> <xr-asset-material asset-id="removeBlack-mat3" effect="removeBlack"/> </xr-assets> <xr-node> <xr-ar-tracker mode="Marker" bind:ar-tracker-switch="handleTrackerSwitch" src="https://cdn.apix.cn/xxx.png"> <xr-mesh node-id="video-item2" geometry="plane" material="removeBlack-mat3" uniforms="u_videoMap: video-test3" states="renderQueue:3000" position="0 0 0" rotation="90 180 0"/> </xr-ar-tracker> <xr-camera id="camera" background="ar" is-ar-camera /> </xr-node> </xr-scene>