背景一种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年版
点击“阅读原文”查看精选导航
“点赞“ ”在看”鼓励一下▼
一种3D截图方案背景参考TRUESPACEwithCocos技术分享[1]中的动态生成...
“4·18”火灾事故伤病员转运入院后,北京市紧急成立现场医疗救治指...
给我原地退役!NBA千万名嘴怒喷小卡轮休,只想自己不顾球队死活,小...
4月22日,由国家电影局指导、中央广播电视总台和北京市人民政府主办...
4月20日,杭州再次挂牌新一批宅地,合计9宗涉宅地,总用地面积46363...
【来源:晋中市水利局_工作动态】2023年4月19日上午,晋中市水利局...
阿根廷总统费尔南德斯21日宣布不参加将于10月举行的总统选举,不再...
1、琥珀木是未完全挥发的木质为琥珀木。2、亦是南方即北京等地所称...
伴随着五一的临近,结婚便成为了一大热词。一切都是刚刚好,天气刚...
4月22日电,据俄罗斯卫星通讯社,俄原子能集团发布消息称,集团获得...
IT之家4月21日消息,江波龙电子FORESEE近期推出品牌首款专门适用于...
近年来,山东省惠民县持续推进“产业培强”工程,以“企业培龙头、...
河南广电·大象新闻记者龚雪通讯员王纪华为进一步推动信阳市羊山新...
针对近期社会关注的新冠病毒XBB 1 16相关问题,4月21日,国家疾控...
4月21日北向资金增持8 6万股复旦张江(01349 HK)。近5个交易日中...
新华社巴黎4月21日电(记者肖亚卓)凭借姆巴佩的两粒进球,巴黎圣日...
今天是世界地球日仰望夜空,繁星闪烁地球是全人类赖以生存的唯一家...
北京时间9月9日,2021年网球公开赛结束女单1 4决赛争夺。赛会17号...
近段时间,樊振东保持着很不错的状态,连续拿到了WTT新加坡大满贯赛...
1、很简单啊,首先找到文档中的 "绘图 "点击“直线”在需要划线的...
1、戏里叫花灵龙,真名叫许名杰。2、是个完全的大新人,之前应该没...
在我国众多的风景名胜中,古城古镇一直都是非常受欢迎的景点,在这...
在二建考试中,市政这门课程的专业多、技术难、难点多,哪怕是现场...
鞭牛士4月21日消息,小冰公司宣布了AI数字员工SaaS产品线最新进展。...
1、以某知名网购软件为例。首先,打开需要查价格的商品的页面。2、...
新华报业网讯据最高人民法院官网,日前,最高人民法院发布2022年中...
你需要的东西照烧腌料伍斯特沙司肉片脱水机照烧酱是一种结合了甜味...
【美国《通胀削减法案》坑惨盟友加拿大被迫应战】去年8月,美国推出...
WTT澳门冠军赛已经进入半决赛的争夺,四场半决赛即将打响,相比女单...
A题给我的整体感觉就是和2020年国赛中小微企业的信贷决策有点相似,...