首頁畫好立方體 怎麼畫立方體英文WebGL video动画纹理- Web API 接口参考 - MDN Webgl animationWebGLRenderingContextWebGL cubeWebGL 2D animationWebGL textureGl vertexShaderWebGL video player动画纹理- Web API 接口参考 - MDN2024-07-05文章推薦指數: 80 %投票人數:10人 第一步是创建 将用于检索视频帧的元素: ... 检查这两个事件可确保有可用数据,并且可以安全地开始将视频上传到WebGL 纹理。在上面的代码中, ... SkiptomaincontentSkiptosearchSkiptoselectlanguageWeb开发技术WebAPI接口参考WebGLWebGL教程动画纹理ArticleActions中文(简体)此页面由社区从英文翻译而来。了解更多并加入MDNWebDocs社区。获取视频用视频帧作为纹理参见RelatedTopics WebGLAPI WebGLtutorial GettingstartedwithWebGL Adding2DcontenttoaWebGLcontext UsingshaderstoapplycolorinWebGL AnimatingobjectswithWebGL Creating3DobjectsusingWebGL UsingtexturesinWebGL LightinginWebGL AnimatingtexturesinWebGL Examplesandarticles Matrixmathfortheweb WebGLmodelviewprojection WebGLbestpractices UsingWebGLextensions Abasic2DWebGLanimationexample WebGLbyexample Interfaces WebGLRenderingContext WebGL2RenderingContext WebGLActiveInfo WebGLBuffer WebGLContextEvent WebGLFramebuffer WebGLProgram WebGLQuery WebGLRenderbuffer WebGLSampler WebGLShader WebGLShaderPrecisionFormat WebGLSync WebGLTexture WebGLTransformFeedback(en-US) WebGLUniformLocation WebGLVertexArrayObject Documentation: Contribute TheMDNproject 获取视频用视频帧作为纹理参见动画纹理 «上一页 在本演示中,我们以上一个示例为基础,将静态纹理替换为正在播放的mp4视频文件的帧。实际上,这很容易做到,而且观看起来很有趣,所以让我们开始吧。您可以使用类似的代码来使用任何类型的数据(例如)作为纹理的源。获取视频第一步是创建将用于检索视频帧的元素: //willsettotruewhenvideocanbecopiedtotexture varcopyVideo=false; functionsetupVideo(url){ constvideo=document.createElement('video'); varplaying=false; vartimeupdate=false; video.autoplay=true; video.muted=true; video.loop=true; //Waitingforthese2eventsensures //thereisdatainthevideo video.addEventListener('playing',function(){ playing=true; checkReady(); },true); video.addEventListener('timeupdate',function(){ timeupdate=true; checkReady(); },true); video.src=url; video.play(); functioncheckReady(){ if(playing&&timeupdate){ copyVideo=true; } } returnvideo; } 首先,我们创建一个视频元素。我们将其设置为自动播放,静音和循环播放视频。然后,我们设置了两个事件以确保视频正在播放并且时间轴已更新。我们需要进行这两项检查,因为如果将视频上传到WebGL尚无可用数据,它将产生错误。检查这两个事件可确保有可用数据,并且可以安全地开始将视频上传到WebGL纹理。在上面的代码中,我们确认是否同时发生了这两个事件。如果是这样,我们将全局变量设置copyVideo为true,以表示可以安全地开始将视频复制到纹理。 最后,我们将src属性设置为start并调用play以开始加载和播放视频。用视频帧作为纹理接下来的更改是initTexture(),它变得更加简单,因为它不再需要加载图像文件。相反,它所做的只是创建一个空的纹理对象,在其中放置一个像素,然后设置其过滤条件供以后使用: functioninitTexture(gl){ consttexture=gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D,texture); //Becausevideohastobedownloadovertheinternet //theymighttakeamomentuntilit'sreadyso //putasinglepixelinthetexturesowecan //useitimmediately. constlevel=0; constinternalFormat=gl.RGBA; constwidth=1; constheight=1; constborder=0; constsrcFormat=gl.RGBA; constsrcType=gl.UNSIGNED_BYTE; constpixel=newUint8Array([0,0,255,255]);//opaqueblue gl.texImage2D(gl.TEXTURE_2D,level,internalFormat, width,height,border,srcFormat,srcType, pixel); //Turnoffmipsandsetwrappingtoclamptoedgesoit //willworkregardlessofthedimensionsofthevideo. gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.LINEAR); returntexture; } 这里是updateTexture()方法,这是完成实际工作的地方: functionupdateTexture(gl,texture,video){ constlevel=0; constinternalFormat=gl.RGBA; constsrcFormat=gl.RGBA; constsrcType=gl.UNSIGNED_BYTE; gl.bindTexture(gl.TEXTURE_2D,texture); gl.texImage2D(gl.TEXTURE_2D,level,internalFormat, srcFormat,srcType,video); } 您之前已经看过此代码。它与上一个示例中的imageonload函数几乎相同-除非我们调用texImage2D(),不是传递Image对象,而是传递元素。WebGL知道如何拉出当前帧并将其用作纹理。 然后,在main()代替通话,以loadTexture()在前面的例子中,我们调用initTexture()之后setupVideo()。 在render()是否copyVideo为真的定义中,则updateTexture()每次调用drawScene()函数之前都会调用一次。 consttexture=initTexture(gl); constvideo=setupVideo('Firefox.mp4'); varthen=0; //Drawthescenerepeatedly functionrender(now){ now*=0.001;//converttoseconds constdeltaTime=now-then; then=now; if(copyVideo){ updateTexture(gl,texture,video); } drawScene(gl,programInfo,buffers,texture,deltaTime); requestAnimationFrame(render); } requestAnimationFrame(render); 下面是结果: 查看完整的代码|在新页中打开这个demo参见 UsingaudioandvideoinFirefox «上一页 Foundaproblemwiththispage?EditonGitHubSourceonGitHubReportaproblemwiththiscontentonGitHubWanttofixtheproblemyourself?SeeourContributionguide.Lastmodified:2022年6月2日,byMDNcontributors 完整訊息請參考請為這篇文章評分?有幫助沒幫助延伸文章資訊1How can I draw and project a video from an HTML5 player ...Here is an example code to setup a webGL object that can receive your video pixels. Hope its usef...2Adding Video to Unity WebGL - SIMMER.ioHow do I get video to work for Unity WebGL? This is one of my number one questions and I finally ...3动画纹理- Web API 接口参考 - MDN第一步是创建 <video> 将用于检索视频帧的元素: ... 检查这两个事件可确保有可用数据,并且可以安全地开始将视频上传到WebGL 纹理。在上面的代码中, ...4HTML5 Video WebGL Test - krpano.comThis is a very simple test-case / example for using a HTML5 video as WebGL texture. This example ...5Animating textures in WebGL - Web APIs - MDNIn this demonstration, we build upon the previous example by replacing our static textures with t...