背景一种3D截图方案
(相关资料图)
参考 TRUE SPACE with Cocos 技术分享[1]中的动态生成海报的介绍,以及作者的支持。实现一个长按保存分享截图的方案。
效果在微信浏览器中预览效果如下
环境Cocos Creator 3.7.1Web 浏览器原理原作者讲的非常清楚,只需要按照以下步骤实现即可。
3D相机生成一个RT 赋予给2D精灵摆上一些二维码等UIUI相机再生成一张RT读取RT中的数据,传给 canvascanvas 生成图片数据传给 Image适配Image对象大小位置视频录了一段操作视频[2]供大家参考。
代码这段代码的截图功能是按照高度适配的方式写的,如有其他需求,可自行修改相关逻辑。仅供大家参考学习。
import{_decorator,Component,Node,Camera,RenderTexture,view,UITransform,log,game,screen,NodeEventType}from"cc";const{ccclass,property}=_decorator;@ccclass("CaptureImage")exportclassCaptureImageextendsComponent{@property(Camera)copyCamera:Camera=null!;@property(Node)targetNode:Node=null!;@property(Node)captureBtn:Node=null!;@property(Node)closeBtn:Node=null!;rt:RenderTextureprivate_image:HTMLImageElement_canvas:HTMLCanvasElement=null!;_buffer:ArrayBufferView=null!;start(){log("欢迎关注微信公众号【白玉无冰】https://mp.weixin.qq.com/s/4WwCjWBtZNnONh8hZ7JVDA")this.rt=newRenderTexture();this.rt.reset({width:view.getVisibleSize().width,height:view.getVisibleSize().height,})this.copyCamera.targetTexture=this.rt;this.captureBtn.active=true;this.closeBtn.active=false;this.captureBtn.on(NodeEventType.TOUCH_END,this.copyRenderTex,this)this.closeBtn.on(NodeEventType.TOUCH_END,this.clearCapture,this)}privatecopyRenderTex(){constwidth=this.targetNode.getComponent(UITransform).width;constheight=this.targetNode.getComponent(UITransform).height;constanchorPoint=this.targetNode.getComponent(UITransform).anchorPoint;constworldPos=this.targetNode.getWorldPosition();this._buffer=this.rt.readPixels(Math.round(worldPos.x-width*anchorPoint.x),Math.round(worldPos.y-height*anchorPoint.y),width,height);if(!this._canvas){this._canvas=document.createElement("canvas");this._canvas.width=width;this._canvas.height=height;}else{letctx=this._canvas.getContext("2d");ctx.clearRect(0,0,this._canvas.width,this._canvas.height);}letctx=this._canvas.getContext("2d")!;letrowBytes=width*4;for(letrow=0;row其他 最近写文偏笔记的形式,主要是记录一下代码,下次要用的时候再翻出来。希望对大家有所帮助。
参考资料[1]
TRUE SPACE with Cocos 技术分享:https://forum.cocos.org/t/topic/146459
[2]视频:https://www.bilibili.com/video/BV1w84y1N7XQ
往期精彩:
3DUI Cocos Creator
三渲二 Cocos Creator
零代码实现面片效果(UV滚动,帧动画) Cocos Creator
游戏开发资料合集,2022年版
点击“阅读原文”查看精选导航
“点赞“ ”在看”鼓励一下▼
关键词:
截图!长按保存分享!Cocos Creator_天天热讯
放弃50万年薪!浙江姑娘辞职冲上热搜第一|天天关注
环球热门:人民币“朋友圈”越来越大
中甲今日综述:广州队无缘开门红,四川九牛、苏州东吴均一球小胜 全球资讯
2022儿童摄影十大品牌排行榜_儿童摄影哪个牌子好-世界播报
刘浩然-今日热门
全省水旱灾害防御技术培训班在长沙举办
外媒:澳政商界代表团排队访华
全速自适应巡航是什么意思呀(全速自适应巡航是什么意思)-今日热门
头皮发麻!浙江一居民打开自家柜子,竟看到了这一幕!_环球新消息
技能培训的目的和意义怎么写 技能培训的目的和意义 天天播资讯
2023年3月6级成绩查询_6级成绩查询
文化活动有哪些形式_文化活动有哪些
热资讯!一季度四川金融服务文旅产业发展取得新成效 文旅产业贷款余额1.5万亿元
升级2.8K屏幕 首发1899元!小米平板6系列是否值得买?
快手小店已结算的钱怎么提现?提现需注意哪些?-当前快播
环球百事通!黄梅县气象台发布雷电黄色预警【III级/较重】
《黑暗之魂3》高墙刷骑士盾与剑技巧解析攻略
“光影科技,共话未来” 中国科技馆特效电影展映活动开幕|全球微速讯
ah122govcn个人登录_ah122-今日聚焦
奥马电器:截止2023年3月31日,公司股东人数为30,112名
沈阳和平区2023社区工作者准考证打印与入口 环球播报
柴桥打造“5分钟优享阅读圈”
焦点讯息:佟大为关悦带孩子郊外度假,爸爸全程陪儿子挖土有耐心,画面温馨
拾回丢失的记忆
「股东变更流程」因疫情推迟还房贷可行吗?如何防止弄虚作假|观天下
做到极致!冀中新材“水纱”销量逆势上扬,供不应求
好用的防晒推荐国货_好用的防晒
win7关机后电脑自动重启 环球看热讯
李国庆称ChatGPT替代不了阅读 后者可以安慰心灵|天天关注