javascript播放带透明通道的mp4动画

inspirer 2020-11-12 16:44:02
javascript 通道 播放 透明 mp4


随着互联网的发展,动画效果也在一直更新,从刚开始的flsh动画,cocos骨骼动画,到YY开源的svga动画。最近1年来,带有透明通道的mp4动画被使用的极为广泛,对于app端。github上有开源的AlphaPlayer可以使用,但是对于web端,相关的资料很少。为了解决这个问题想了很多办法,提出了使用webm格式。但是最终还是要实现播放mp4格式。

播放mp4格式的透明动画,需要什么呢。首先,对于web前端来说,webgl应该大家都很少介绍到,更不要说着色器了(shader)和OpenGL ES的语法了。放弃了好几次,最后看到一个帖子,可以用THREEJS来进行。先上一个图

 

  可以看到素材中左边部分使用RGB通道存储了原透明视频的Alpha值,右边部分使用RGB通道存储了原透明视频的RGB值,然后在端上通过OpenGL重新将每个像素点的Alpha值和RGB值进行组合,重新得到ARGB视频画面,实现透明视频的动画效果。

在web端,具体实现。

1.有个video播放器,播放mp4动画

2.使用threejs和openel把画面重绘到一个容器里。

 1 var lastUpdate;
2 var container;
3 var camera, scene, renderer;
4 var uniforms;
5
6 function init() {
7
8 container = document.getElementById('container');
9 camera = new THREE.Camera();
10 camera.position.z = 1;
11 scene = new THREE.Scene();
12
13 var video = document.getElementById('video');
14 video.src = "https://devimage.91banban.com/topcard.mp4";
15  video.play();
16 video.onended = () => {
17  video.play();
18  }
19 videoTexture = new THREE.VideoTexture(video);
20 videoTexture.minFilter = THREE.LinearFilter;
21 videoTexture.magFilter = THREE.LinearFilter;
22 videoTexture.format = THREE.RGBAFormat;
23
24 // shader stuff
25 uniforms = {
26 time: { type: "f", value: 1.0 },
27 texture: { type: "sampler2D", value: videoTexture }
28  };
29 var material = new THREE.ShaderMaterial({
30  uniforms: uniforms,
31  vertexShader: `varying vec2 vUv;
32 void main() {
33 vUv = uv;
34 gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
35  }`,
36  fragmentShader: `#ifdef GL_ES
37 precision highp float;
38  #endif
39
40 uniform float time;
41  uniform sampler2D texture;
42  varying vec2 vUv;
43
44 void main( void ) {
45 gl_FragColor = vec4(
46 texture2D(texture, vec2(0.5+vUv.x/2., vUv.y)).rgb,
47 texture2D(texture, vec2(vUv.x/2., vUv.y)).r
48  );
49  }`,
50 transparent: true
51  });
52 lastUpdate = new Date().getTime();
53 var geometry = new THREE.PlaneBufferGeometry(2, 2);
54 var mesh = new THREE.Mesh(geometry, material);
55 mesh.scale.setScalar(0.8);
56  scene.add(mesh);
57 renderer = new THREE.WebGLRenderer({ alpha: true });
58 renderer.setPixelRatio(window.devicePixelRatio / 1);
59
60  container.appendChild(renderer.domElement);
61
62 document.getElementById('play-button').addEventListener('click', e => { video.play(); });
63 renderer.setSize(1000, 1100);
64 this.animate();
65 }
66
67 function animate() {
68 var currentTime = new Date().getTime()
69 var timeSinceLastUpdate = currentTime - lastUpdate;
70 lastUpdate = currentTime;
71  requestAnimationFrame(animate);
72  render(timeSinceLastUpdate);
73
74 }
75 function render(timeDelta) {
76 uniforms.time.value += (timeDelta ? timeDelta / 1000 : 0.05);
77  renderer.render(scene, camera);
78 }
79
80 init();

最终实现的效果图

 

 

版权声明
本文为[inspirer]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/inspirer/p/13964642.html

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple