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 ...
随机推荐
- 由strupr,strlwr体会如果将字符常量转换为变量进行修改,体会常量的静态存储
#include <stdio.h> #include <string.h> int main() { char s[]="Copywrite 1999-2000 G ...
- swift 定义类方法(type methed)
swift 中声明结构体或者枚举的类型方法,需要在func前加上关键字 ststic ,但是如果要定义一个类的类方法时,需要用关键字 class class SomeClass { class ...
- Objec类和final关键字的用法
一.在继承中,子类继承父类,那父类继承谁呢,java中如果类没有指定继承谁,那么就默认继承Object类,object类是所有对象的直接或者间接的父类,根类,或者叫做基类,它里面定义的功能所有的对象都 ...
- vsftpd给root设置访问权限
1:Linux下安装vsftpd之后,默认的配置是匿名用户可以登录,匿名帐户有两个:用户名:anonymous密码:空 用户名:ftp密码:ftp 2:如果要用匿名进行上传删除等操作需要配置其它参数. ...
- mysql命令行方式添加用户及设置权限
以前总是喜欢通过phpmyadmin去添加用户和数据库,这次装完系统后,配置了一大堆东东,实在不想安装phpmyadmin了,就通过命令行方式创建了数据库和设置权限,记录一下,免得以后总是百度 关键步 ...
- update目标在查询返回结果集中的解决方案
示例: students为学生信息表 Score为成绩表 两个表通过学生号关联 要求:将总成绩小于100的学生名称改为‘天才’ sql如下: error提示:update目标不能在查询返回结果集中 ...
- PHP 点滴疑惑
在数据库中,字段为NULL,可以使用empty()进行判断 <?php $CONFIG['hostname'] = 'localhost'; $CONFIG['username'] = 'roo ...
- Android MVP架构浅析
Android之MVC模式 MVC好处: 从用户的角度出发,用户可以根据自己的需求,选择自己合适的浏览数据的方式.比如说,对于一篇在线文档,用户可以选择以HTML网页的方式阅读,也可以选择以pdf的方 ...
- MongoDB的主从复制和副本集
mongoDB的两个特性主从复制和副本集,实现了数据的同步备份 一.主从复制 主从复制是一个简单的数据库同步备份的集群技术.例如主服务器宕机了,可以直接使用从服务器,主服务器恢复后在进行同步,保证了业 ...
- Lisp与JAVA的酷毙结合——abcl
最近看了一本叫做<黑客与画家>的书,其中对于Lisp语言大加褒奖.自己试着用了一下,虽然确实有反人类之嫌,但是确实是一门不错的语言,New Architect杂志上有一篇介绍ITA软件公司 ...