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 ...
随机推荐
- 关于Eclipse中的开源框架EMF(Eclipse Modeling Framework)
Eclipse项目本身可以划分为4个主要的子项目:Equinox,平台,Java开发工具(Java Development Tools,JDT)和插件开发环境(Plug-in Development ...
- JS+CSS简单实现DIV遮罩层显示隐藏【转藏】
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 通过GitHub部署网站到Azure WebSite
一.将代码上传到GitHub存储库中 第一步:创建Github新账户 第二步:新建仓库 第三步:安装Github shell程序,地址:http://windows.github.com/ 第四步:打 ...
- freemarker.core.InvalidReferenceException: Expression stackObject.value.get(propertyName).toString() is undefined
很奇怪的报错, 在实体中添加 toString方法后, 想在前台列表中使用 <s:debug标签调试, 发现报错如下: freemarker.core.InvalidReferenceExcep ...
- oracle 11G创建表空间、用户、配置监听和TNS
最近总在安装各种版本的oralce数据库做测试,11G,32位的,64位的,12C的,每次都折腾表空间,用户.tns啥的,这里记录下,再也不用现用现百度找了 一.创建表空间.用户 在plsql工具中 ...
- VIM 语法检查
VIM Grammar Check 一.Language Tool Create by Dominique Pellé REFER:LanguageTool wikipedia REFER:Langu ...
- VirtualBox single usermode boot
VirtualBox single usermode boot 当系统因为某些原因无法通过图形界面登录VirtualBox内的系统时,可以通过Grub进入命令行模式/单一用户界面模式. 参考: 1.R ...
- WPF设置窗口模式(Windowstyle=“None”)
当WindowStyle="None"时,设置AllowsTransparency="True",则不会出现黑色Border,然后可以另外设置外边的Border ...
- Demo02_对结构体进行文件读写_张仕传_作业_
#include <iostream> using namespace std; #define StructArrarySize 5 // 老师数量 #define StudentNum ...
- 二叉搜索的各种bugs——重复递增序列
int binary_search(int* A, int value, int p, int r); int main(int argc, char *argv[]){ , , , , , , , ...