当前位置:网站首页>Unity intercepts 3D images and the implementation of picture-in-picture PIP
Unity intercepts 3D images and the implementation of picture-in-picture PIP
2022-08-03 23:23:00 【Yangtze River Lot】
1 前言
在Unityto get some3DA screenshot of the game object,是比较容易的,本文来讨论一下.
Implemented core program,是新建一个Camera,Camera指向目标对象.Camera有个参数是TargetTexture,Called the target render texture,Extract the pixels of this texture,生成图片
.
来看一下Camera的属性:
一般情况下,The camera renders directly to the screen,即,当 targetTexture 为 null 时,The camera renders to the screen.But if create one RenderTexture 对象, Set it to on the camera targetTexture,The camera will render to it 该纹理.when rendering to a texture,The camera always renders into the entire texture. There is one离屏渲染FBO
的味道了^^
再补充一下RenderTexture
的知识.RenderTexture即,渲染纹理
,Refers to a texture that can be rendered dynamically.
They can be used to implement image-based rendering effects、动态阴影、 投影器、Reflection or surveillance cameras.
A typical use of a render texture is to set it to 摄像机的“目标纹理”属性 (Camera.targetTexture),This will render the camera to the texture, instead of rendering to the screen.
2 具体实现
背景
:There is a simple one as follows3D世界,包括一个Cube正方体,一个Plane Green tablet,一个PlaneWhite tablet.目标
:把CubeTaken together with the green plate.
2.1 New off-screen renderingCamera
新建一个Camera,视锥Projection
Set to orthographic projectionOrthographic
,位置放在3Dbehind the object,Make it shine on the target GameObject,具体如下:
选中Camera对象,There will be a small window,arrow in the picture,可以看到CameraThe resulting actual view,Convenient for your debugging location,大小.
如果要调整Camerarange of the viewing cone,parameters can be modifiedSize
.
接下来,就可以写代码,Go take a screenshot.
2.2 代码实现
具体如下:
using Assets.Scripts;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Get3DImage : MonoBehaviour
{
private RenderTexture shortcutRenderTexture;
private Camera cutImageCamera;
// Start is called before the first frame update
void Start()
{
cutImageCamera = GameObject.Find("CutImageCamera").GetComponent<Camera>();
init();
}
// Update is called once per frame
void Update()
{
}
private void init()
{
shortcutRenderTexture = RenderTexture.GetTemporary(600, 600, 16, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
shortcutRenderTexture.enableRandomWrite = true;
cutImageCamera.targetTexture = shortcutRenderTexture;
}
public void StartCutImage()
{
// Create a new Texture2D and read the RenderTexture image into it
RenderTexture.active = shortcutRenderTexture;
Texture2D drawTexture2D = new Texture2D(shortcutRenderTexture.width, shortcutRenderTexture.height, TextureFormat.RGB24, false);
drawTexture2D.ReadPixels(new Rect(0, 0, shortcutRenderTexture.width, shortcutRenderTexture.height), 0, 0);
drawTexture2D.Apply();
TextureUtils.saveTextureToFile(drawTexture2D, "SaveShortcut");
}
private void OnDestroy()
{
RenderTexture.ReleaseTemporary(shortcutRenderTexture);
}
}
核心代码
RenderTexture.active = shortcutRenderTexture;
The core code above,表示设置当前的activeRenderTexture
为 Camera所指向的RenderTexture(There may be more than one in the same game sceneRenderTexture).此时可以新建一个Texture2D
,调用ReadPixels
,The active state is readRenderTexture的内容.
接下来就简单了,把Texture2D保存即可.代码如下:
using System.IO;
using UnityEngine;
namespace Assets.Scripts
{
class TextureUtils
{
public static Texture2D DeCompress(Texture2D source)
{
RenderTexture renderTex = RenderTexture.GetTemporary(
source.width,
source.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(source, renderTex);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = renderTex;
Texture2D readableText = new Texture2D(source.width, source.height);
readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
readableText.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(renderTex);
return readableText;
}
public static string saveTextureToFile(Texture2D texture, string fileName)
{
if (texture == null)
{
return "";
}
byte[] bytes = TextureUtils.DeCompress(texture).EncodeToPNG();
string filename = Application.persistentDataPath + "/" + fileName + ".png";
Debug.Log("DrawManager::saveTextureToFile = " + filename);
File.WriteAllBytes(filename, bytes);
return filename;
}
}
}
3 用RenderTexture实现画中画
本文使用到了RenderTexture
,And it is created dynamically by code.We can also create in the menu,You can even do a picture-in-picturePIP的功能,比较有意思,顺着思路,Let's talk about the implementation of picture-in-picture.
3.1 建RenderTexture
在Assets目录下,右键,Create – RenderTexture,新建一个渲染纹理.
然后,把SizeThe parameters are set to be the same as the screen width and height,例如1920x1080.
3.2 build material
接着,右键,Create – Material,新建一个材质.Shader选为Unlit/Texture
.然后,Right arrow below,The texture used by the material,选择为上面新建的RenderTexture.
There is material,It can be used in game scenes!
3.3 建Image作为PIP窗口
新建一个2D Image,然后把Image的材质,设置为3.2The material that was built.
好了,运行起来,效果如下:
最后,上代码:
TestGet3DImage
边栏推荐
猜你喜欢
随机推荐
CAS:178744-28-0,mPEG-DSPE,DSPE-mPEG,甲氧基-聚乙二醇-磷脂酰乙醇胺供应
rosbridge-WSL2 && carla-win11
JS获得URL超链接的参数值
Live Preview | Build Business Intelligence, Quickly Embrace Financial Digital Transformation
RSS订阅微信公众号初探-feed43
Another MySQL masterpiece published by Glacier (send the book at the end of the article)!!
用队列模拟实现栈
用两个栈模拟队列
响应式织梦模板塑身瑜伽类网站
Work Subtotal QT Packing
SolidEdge ST8安装教程
- the skip/skipif Pytest learning
【并发编程】ReentrantLock的lockInterruptibly()方法源码分析
What is the difference between the generator version and the viewer version?
设置工作模式与环境(下):探查和收集信息
Binary search tree to solve the fallen leaves problem
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
剑指offer第22题-链表中倒数第K个节点
牛客2022 暑期多校3 H Hacker(SAM + 线段树查询区间内部最大子段和)
【职场杂谈】售前与销售工作配合探讨