截图!长按保存分享!Cocos Creator|当前报道

一种3D截图方案

背景

参考 TRUE SPACE with Cocos 技术分享[1]中的动态生成海报的介绍,以及作者的支持。实现一个长按保存分享截图的方案。

效果

在微信浏览器中预览效果如下


(资料图片)

环境

Cocos Creator 3.7.1

Web 浏览器

原理

原作者讲的非常清楚,只需要按照以下步骤实现即可。

3D相机生成一个RT 赋予给2D精灵

摆上一些二维码等UI

UI相机再生成一张RT

读取RT中的数据,传给 canvas

canvas 生成图片数据传给 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年版

点击“阅读原文”查看精选导航

“点赞“ ”在看”鼓励一下▼

标签:

x 广告
重磅!2022年自然指数年度榜单揭晓 中国位居第二

6月16日,2022自然指数年度榜单(NatureIndex2022annualtables)揭晓,展示了不同国家和科研机构在自然科学领域的高质量科研产出情况。其中,

最新!科学家开发出半透明载玻片控制光流的能力

据最新一期《自然·光子学》杂志报道,澳大利亚国立大学(ANU)物理学家开发出一种微小的半透明载玻片,通过操纵光在纳米尺度上的传播方向,

重磅!内蒙古自治区部署“十四五”期间节能减排十大重点工程

节能减排是党中央、国务院作出的重大决策部署。近年来,自治区党委、政府高度重视节能减排各项工作,将其作为推动内蒙古自治区经济社会全面

时政现场评丨跟随总书记的脚步 在三晋大地上踏雪迎春

<script src="http: tv people com cn img player v js">< script><script>showPlayer({id:" pvservice xml 202

第一观察·瞬间 | 两度踏雪 一路为民

春节前夕,习近平总书记赴山西考察调研,看望慰问基层干部群众。总书记走进两个村——霍州市师庄乡冯南垣村、汾西

100的背后是什么?

<script src="http: tv people com cn img player v js">< script><script>showPlayer({id:" pvservice xml 202

在北京的非洲体育人:准备好为北京冬奥会喝彩

<script src="http: tv people com cn img player v js">< script><script>showPlayer({id:" pvservice xml 202

国网绍兴供电公司:打造新型有源配电网 保障山区用电可靠

1月25日,国网绍兴供电公司通过配电自动化主站平台远程控制位于绍兴南部山区线路末端发电机成功启动,成功以小成

x 广告

Copyright ©  2015-2022 人人城市网版权所有  备案号:粤ICP备18023326号-36   联系邮箱:8557298@qq.com