Render Texture的使用(截取rendertexture的一帧到Texture2D)
游戏里人物角色太多,每个角色都要有张头像或全身照等,这样就必须截取大量的图片,花费大量的时间,有时截取的不满意还得重新截,即浪费时间又浪费精力.所以就想了个投机取巧的方法.那就是用unity搭建一个照相馆.
首先用CreateEmpty创建一个空的GameObject,命名为GetPicsObject,将他的Transform.position置为0;
接着创建一个Camera,命名为ShowCamera,把这个Camera放到上面GetPicsObject下,作为GetPicsObject的子物体.
接着用CreateEmpty创建另一个空的GameObject,命名为charactorPos,用来确定创建角色所在的位置,也放到GetPicsObject下,作为GetPicsObject的子物体.
调整Camera的位置,确保相机视图为你想要截取照,层次如下图所示
.
接着创建C#脚本,命名为GetPicTest
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GetPicTest : MonoBehaviour
{
/// <summary>
/// 创建角色所在位置
/// </summary>
public Transform playerPos;
/// <summary>
/// 声明一个全身照的RenderTexture
/// </summary>
public RenderTexture fullBodyRender;
/// <summary>
/// 将fullBodyRender的特定一帧动画保存到fullBodyTex
/// </summary>
public Texture2D fullBodyTex;
/// <summary>
/// 全身像相机
/// </summary>
public Camera showCamera;
/// <summary>
/// 保存角色预置
/// </summary>
public List<GameObject> charactorsPrefabs = new List<GameObject>();
public int _width = ;
public int _height = ; public GameObject obj;
// Use this for initialization
void Start()
{
fullBodyRender = new RenderTexture(_width, _height, );
//fullBodyRender = RenderTexture.GetTemporary(_width, _height, 0, RenderTextureFormat.ARGBFloat);
fullBodyRender.anisoLevel = ;
fullBodyRender.filterMode = FilterMode.Point;
showCamera.targetTexture = fullBodyRender;
showCamera.pixelRect = new Rect(, , _width, _height);
} void OnGUI()
{
GUI.DrawTexture(new Rect(Screen.width - _width, Screen.height - _height, _width, _height), fullBodyRender, ScaleMode.ScaleToFit);//全身
int j = -;
for (int i = ; i < charactorsPrefabs.Count; i++)
{
if (i % == )
{
j++;
}
if (GUI.Button(new Rect(j*,i%*,,),"" + charactorsPrefabs[i].name))
{
if (obj)
{
Destroy(obj);
}
obj = GameObject.Instantiate(charactorsPrefabs[i]) as GameObject;
obj.transform.parent = playerPos;
obj.transform.localPosition = Vector3.zero;
obj.animation.Play("idle");
obj.animation.wrapMode = WrapMode.Loop;
CharacterController controller = obj.GetComponent<CharacterController>();
float h = controller.height;
float scaleY = obj.transform.localScale.y;
float multiNum = 1.5f / h / scaleY;
obj.transform.localScale = new Vector3(obj.transform.localScale.x * multiNum, obj.transform.localScale.y * multiNum, obj.transform.localScale.z * multiNum);
}
}
GUI.Box(new Rect(Screen.width - _width, , _width, _height),""); if (GUI.Button(new Rect(Screen.width / - , , , ), "GetPic"))
{
fullBodyTex = getTexture2d(fullBodyRender);
}
if (fullBodyTex)
{
GUI.DrawTexture(new Rect(Screen.width - _width, , _width, _height), fullBodyTex, ScaleMode.ScaleToFit);
}
} public Texture2D getTexture2d(RenderTexture renderT)
{
if (renderT == null)
return null;
int width = renderT.width;
int height = renderT.height;
Texture2D tex2d = new Texture2D(width, height, TextureFormat.ARGB32, false);
RenderTexture.active = renderT;
tex2d.ReadPixels(new Rect(, , width, height), , );
tex2d.Apply(); //byte[] b = tex2d.EncodeToPNG();
//Destroy(tex2d); //File.WriteAllBytes(Application.dataPath + "1.jpg", b);
return tex2d;
}
}
将GetPicTest赋给到GetPicsObject,把charactor赋给Playerpos,把ShowCamera赋给Show Camera,如下图。
左边一排按钮选择角色,中心上方GetPic按钮截取图标显示在右上角.右下角为RenderTexture,如下图。

Render Texture的使用(截取rendertexture的一帧到Texture2D)的更多相关文章
- 利用Render Texture实现游戏的小雷达效果(摄影机分屏)
最近游戏蛮牛在举办一个活动,就是要做出这样的效果: 题目:实现游戏分屏效果 要求:1. 分屏,且分割线不规则(即非水平或垂直):2. 各屏可单独操作(移动.缩放),操作指该 ...
- camera render texture 游戏里的监控视角
Camera里: 新建render texture并拖入到target texture里 新建材质球 拖入render texture camera里的视角会在材质球上出现 新建一个pla ...
- Render Texture
[Render Texture] Render Textures are special types of Textures that are created and updated at runti ...
- Render Texture coordinates
https://docs.unity3d.com/550/Documentation/Manual/SL-PlatformDifferences.html Render Texture coordin ...
- iOS截取视频某一帧图片(关键帧,AVAssetImageGenerator)
获取第一帧图片 导入 AVFoundation.Framework.CoreMedia.Framework 实现代码例如以下: + (UIImage*) thumbnailImageForVideo: ...
- 【Unity Shaders】游戏性和画面特效——创建一个夜视效果的画面特效
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- 【Unity Shaders】游戏性和画面特效——创建一个老电影式的画面特效
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- cocos源码分析--RenderTexture
cocos中RenderTexture主要用来实现截屏,然后把截取出来的图片保存到磁盘中,除了保存图片和渲染纹理,它还可以得到一些预渲染结果,并将这些结果作为一种纹理数据. 例如我们可以用RGB5_A ...
- LittleTools之输出RenderTexture工具
using UnityEngine; using System.Collections; using System.IO; /// <summary> /// Save render te ...
随机推荐
- CentOS7安装Tomcat8.X
安装说明 安装环境:CentOS7安装方式:源码安装软件:apache-tomcat-8.0.30.tar.gz下载地址:http://tomcat.apache.org/download-80.cg ...
- CentOS下安装福昕PDF软件
官方下载地址:http://www.foxitsoftware.cn/downloads/ tar -jxvf foxreader.tar.bz2问题:下载官方包以后解压,双击不能打开,也没有任何提示 ...
- java算法小知识练习
偶尔翻开了以前的练习题,不自觉又想随手敲一遍,虽然有些思想依然是那么老套,但毕竟也算是对知识的巩固 了. 一.题目:有1.2.3.4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 具体 ...
- 【Objective-C】4-空指针和野指针
一.什么是空指针和野指针 1.空指针 1> 没有存储任何内存地址的指针就称为空指针(NULL指针) 2> 空指针就是被赋值为0的指针,在没有被具体初始化之前,其值为0. 下面两个都是空指针 ...
- 一般处理程序生成简单的图片验证码并通过html验证用户输入的验证码是否正确
一般处理程序生成简单的图片验证码并通过html验证用户输入的验证码是否正确 最近没事研究了下验证码的的动态生成及通过cookie实现HTML页面对用户输入的验证码的校验,简要如下: 1.写 ...
- 20160503-spring入门1
一.Spring是什么 Spring是一个开源的控制反转(Inversion of Control ,IoC)和面向切面(AOP)的容器框架.它的主要目得是简化企业开发. IOC 控制反转 publ ...
- Android MVP架构浅析
Android之MVC模式 MVC好处: 从用户的角度出发,用户可以根据自己的需求,选择自己合适的浏览数据的方式.比如说,对于一篇在线文档,用户可以选择以HTML网页的方式阅读,也可以选择以pdf的方 ...
- [转]在SQLServer中实现Sequence的高效方法
如果在ORACLE里面用惯了Sequence的兄弟们,要在SqlServer里实现Sequence,就会发现没有现成的Sequence对象可以Create了.那应该怎么办呢? 当然这点小问题是难不倒我 ...
- java多线程总结二:后台线程(守护线程)
所谓的后台线程,是指在程序运行的时候在后台提供一种通用服务的线程,并且这种线程并不属于程序中不可或缺的部分.因此当所有的非后台线程结束时,程序也就终止了,同时会杀死所有后台线程.反过来说,只要有任何非 ...
- cocos2d-x实战 C++卷 学习笔记--第6章 场景与层
前言: 一个场景(Scene)是由多个层(Layer)组成,而且层的个数要至少是1,不能为0. 场景切换 场景切换相关函数 1)void runWithScene(Scene* scene) 该函 ...