当前位置:网站首页>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,视锥ProjectionSet 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
边栏推荐
- 超级完美版布局有快捷键,有背景置换(解决opencv 中文路径问题)
- RPA power business automation super order!
- Code Casual Recording Notes_Dynamic Programming_416 Segmentation and Subsetting
- 创建函数报错,提示DECLARE定义语法问题
- golang写的存储引擎,基于b+树,mmap
- 直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
- Interpretation of ML: A case of global interpretation/local interpretation of EBC model interpretability based on titanic titanic rescued binary prediction data set using interpret
- Pytest learn-setup/teardown
- [Paper Reading] TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
- What is the difference between the generator version and the viewer version?
猜你喜欢

MiniAPI of .NET6 (14): Cross-domain CORS (Part 1)

Click the icon in Canvas App to generate PDF and save it to Dataverse

密码学基础以及完整加密通讯过程解析
![Scala basics [regular expressions, framework development principles]](/img/be/6531f32494b8516cd7031223d14bb2.png)
Scala basics [regular expressions, framework development principles]

.NET6之MiniAPI(十四):跨域CORS(上)

易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元

《数字经济全景白皮书》金融数字用户篇 重磅发布!

BMN: Boundary-Matching Network for Temporal Action Proposal Generation Reading Notes

Recognized by International Authorities | Yunzhuang Technology was selected in "RPA Global Market Pattern Report, Q3 2022"

用队列模拟实现栈
随机推荐
什么是memoization,它有什么用?
【LeetCode】最长回文子序列(动态规划)
【职场杂谈】售前与销售工作配合探讨
log4j-slf4j-impl cannot be present with log4j-to-slf4j
Testng listener
Software testing is seriously involution, how to improve your competitiveness?
Unity2021发布WebGL雾效消失问题
override学习(父类和子类)
超级完美版布局有快捷键,有背景置换
.NET6之MiniAPI(十四):跨域CORS(上)
用队列模拟实现栈
栈的压入、弹出序列
2022/8/3 Exam Summary
射频芯片ATE测试从入门到放弃之参数测试
完全二叉树问题
Another MySQL masterpiece published by Glacier (send the book at the end of the article)!!
leetcode/子串中不能有重复字符的最长子串
响应式织梦模板除尘器类网站
最小化安装debian11
The salary of soft testers at each stage, come to Kangkang, how much can you get?