首页 > 其他分享 >unity 导出全景图

unity 导出全景图

时间:2022-12-20 19:22:08浏览次数:47  
标签:RenderTexture equirect cubemap 导出 width unity 全景图 new

方法一、直接用unity的api

    /// <summary>
    /// 导出全景图
    /// camera:用来拍场景里内容的相机
    /// width:全景图的宽度,高度是宽度的1/2
    /// </summary>
    void Export360Panorama(Camera camera, int width)
    {
        RenderTexture cubemap = new RenderTexture(width, width,0);
        cubemap.dimension = TextureDimension.Cube;
        camera.RenderToCubemap(cubemap, 63, Camera.MonoOrStereoscopicEye.Mono);

        RenderTexture equirect = new RenderTexture(width, width/2, 0);
        cubemap.ConvertToEquirect(equirect, Camera.MonoOrStereoscopicEye.Mono);

        Texture2D tex = new Texture2D(equirect.width, equirect.height, TextureFormat.ARGB32, false, true);
        tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
        tex.Apply();
        byte[] bytes = tex.EncodeToPNG();
        System.IO.File.WriteAllBytes(@"E:\Temp\3\1\222.png", bytes);

    }

  

方法二、写个shader

Shader "Unlit/CubemapTo360Panorama"
{
	Properties
	{
		_MainTex ("Cubemap (RGB)", CUBE) = "" {}
		_OffsetX("Offset X", Float) = 0.0
	}

	Subshader
	{
		Pass
		{
			ZTest Always Cull Off ZWrite Off

			CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#pragma fragmentoption ARB_precision_hint_fastest
				//#pragma fragmentoption ARB_precision_hint_nicest
				#include "UnityCG.cginc"

				#define PI    3.141592653589793
				#define TWOPI 6.283185307179587

				struct v2f
				{
					float4 pos : POSITION;
					float2 uv : TEXCOORD0;
				};
		
				samplerCUBE _MainTex;
				float _OffsetX;
				
				v2f vert(appdata_img v)
				{
					v2f o;
					o.pos = UnityObjectToClipPos(v.vertex);
					o.uv = (v.texcoord.xy + float2(_OffsetX,0)) * float2(TWOPI, PI);
					return o;
				}
		
				fixed4 frag(v2f i) : COLOR 
				{
					float theta = i.uv.y;
					float phi = i.uv.x;
					float3 unit = float3(0,0,0);

					unit.x = sin(phi) * sin(theta) * -1;
					unit.y = cos(theta) * -1;
					unit.z = cos(phi) * sin(theta) * -1;

					return texCUBE(_MainTex, unit);
				}
			ENDCG
		}
	}
	Fallback Off
}

C#部分

    /// <summary>
    /// 导出全景图
    /// camera:用来拍场景里内容的相机
    /// width:全景图的宽度,高度是宽度的1/2
    /// offsetAngle:全景图内容的偏移角度
    /// </summary>
    void Export360Panorama(Camera camera,int width,float offsetAngle)
    {
        RenderTexture cubemap = RenderTexture.GetTemporary(width, width, 0);
        cubemap.dimension = TextureDimension.Cube;

        camera.RenderToCubemap(cubemap, 63);

        RenderTexture equirect = RenderTexture.GetTemporary(width, width / 2, 0);
        equirect.dimension = TextureDimension.Tex2D;

        Material equirectMat = new Material(Shader.Find("Unlit/CubemapTo360Panorama"));
        equirectMat.SetFloat("_OffsetX", offsetAngle / 360f);
        Graphics.Blit(cubemap, equirect, equirectMat);

        Texture2D t2 = new Texture2D(equirect.width, equirect.height);

        t2.ReadPixels(new Rect(0, 0, equirect.width, equirect.height), 0, 0);
        t2.Apply();

        System.IO.File.WriteAllBytes(@"E:\Temp\3\1\1111.png", t2.EncodeToPNG());
    }

  

调用比较简单,直接在Start里调用:

    void Start()
    {
        Export360Panorama(Camera.main, 1024);
        //Export360Panorama(Camera.main, 1024, 90);
    }

  

效果图:

 

标签:RenderTexture,equirect,cubemap,导出,width,unity,全景图,new
From: https://www.cnblogs.com/Jason-c/p/16994920.html

相关文章

  • Unity Editor批量重命名GameObject和Prefab
    usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEditor;usingUnityEngine;publicclassRenameEidtor:Editor{staticreadonlych......
  • Unity Editor中文字体去重
    [MenuItem("Tools/GenerateZhCharacter")]publicstaticvoidGenerateZhCharacter(){vardataPath=Application.dataPath;varpath=Path.Combine(dataPa......
  • easypoi 4.2.0 大数据导出
      excel导出大数据pom文件<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><ver......
  • Unity-C# 协程 IEnumerator 用法梳理
    引用协程为了使用协程,必须在文件头部引入:usingSystem.Collections;启动协程使用StartCoroutine(IEnumerator)来启动一个协程停止协程使用StartCoroutine(IEnumer......
  • 使用 expdp导入导出oracle 数据库
    expdp用户名/密码@afspdbTABLES=table1dumpfile=/yfq/expdp_a11.dmp(1)执行之后报错了;expdp TNScouldnotresolvetheconnectidentifierspecified查了下是......
  • 基于现有工具类ExcelUtil可以实现导入导出功能
    基于现有工具类可以实现导入导出功能:ExcelUtil<SsupEnterpriseEx>util=newExcelUtil<SsupEnterpriseEx>(SsupEnterpriseEx.class);导入功能:1.下载模板......
  • Unity shader cube纹理采样
    使用cube进行纹理采样,可以很方便的预览全景图,可以用立方体去显示全景图,而不必非得用球甚至还可以用更复杂的网格去贴全景图,只要保证网格的形状和全景图里的内容能对应上就......
  • DataGrip导出数据库sql文件
    选中一个数据库中指定数据表,然后单击右键,选择Exportwithmysqldump如果显示如图所示错误(路径不存在),则在终端里输入whichmysqldump,然后去替代默认路径即可。......
  • 导出sqlserver数据库中的登录用户和密码信息(2)
    sp_hexadecimal和sp_help_revlogin脚本的版本有点老,有人做了更新,因此记录一下。 相关连接:导出sqlserver数据库中的登录用户和密码信息-abce-博客园(cnblogs.com)......
  • Azure脚本分享,批量导出用户MFA状态
    一年时间又快过去了,又攒了一些内容可以拿出来分享,希望能帮到有需要的人,先从一些比较简单的开始吧,最近有个需求是想看到用户是不是都设置了MFA,设置了MFA代表相对来说比较安全......