【问题标题】:How can create 360 Render Texture in Unity?如何在 Unity 中创建 360 度渲染纹理?
【发布时间】:2018-07-06 14:38:17
【问题描述】:

我正在尝试制作 360 度渲染纹理。在游戏模式下,一个 Shephere 模型向我们展示了渲染的内容。我不想录视频。只需像 2d 渲染纹理一样显示。

我正在使用这个着色器:

Shader “Unlit/Pano360Shader”
{
   Properties
   {
       _MainTex (“Base (RGB)”, 2D) = “white” {}
       _Color (“Main Color”, Color) = (1,1,1,0.5)
   }

   SubShader 
   {
      Tags { “RenderType” = “Opaque” }

      //This is used to print the texture inside of the sphere
      Cull Front

      CGPROGRAM
      #pragma surface surf SimpleLambert
      half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten)
      {
         half4 c;
         c.rgb = s.Albedo;
         return c;
      }

      sampler2D _MainTex;
      struct Input
      {
         float2 uv_MainTex;
         float4 myColor : COLOR;
      };

      fixed3 _Color;
      void surf (Input IN, inout SurfaceOutput o)
      {
         //This is used to mirror the image correctly when printing it inside of the sphere
         IN.uv_MainTex.x = 1 — IN.uv_MainTex.x;
         fixed3 result = tex2D(_MainTex, IN.uv_MainTex)*_Color;
         o.Albedo = result.rgb;
         o.Alpha = 1;
      }
      ENDCG
   }
   Fallback “Diffuse”
}

我复制到那里 -> https://medium.com/game-development-stuff/how-to-make-a-360%C2%BA-image-viewer-with-unity3d-b1aa9f99cabb

它正在工作,但它只显示照片。当我尝试渲染纹理时,它没有显示任何内容。 如何在 Unity 中创建 360 度渲染纹理?

【问题讨论】:

    标签: c# unity3d shader virtual-reality


    【解决方案1】:

    Unity 相机可以渲染到立方体贴图纹理。不需要自定义着色器。

    您需要创建一个渲染纹理,将其设置为立方体贴图:

    并在相机上创建一个简单的脚本:

    public class RenderCameraToCubemap : Monobehaviour {
      public RenderTexture rt;
      void LateUpdate() {
        GetComponent<Camera>().RenderToCubemap(rt);
      }
    }
    

    警告:UI 将不会被渲染(已知错误)。

    【讨论】:

    • 我使用了你的 solition 但我失败了。
    【解决方案2】:

    我这样解决我的问题;

    • 1- 创建 2 个 RendererTexture 类型的 Cube。设置大小为 1024*1024。
    • 2- 创建 1 个 2D 渲染器纹理类型。设置大小为 1024*1024。
    • 3- 创建一个脚本名称,如 RenderCamera 并将脚本添加到相机。

    这是相机代码;

    public RenderTexture cubemapLeft;
    public RenderTexture cubemapRight;
    public RenderTexture equirect;
    public bool renderStereo = true;
    public float stereoSeparation = 0.064f;
    
    void LateUpdate()
    {
        Camera cam = GetComponent<Camera>();
    
        if (cam == null)
        {
            cam = GetComponentInParent<Camera>();
        }
    
        if (cam == null)
        {
            Debug.Log("stereo 360 capture node has no camera or parent camera");
        }
    
        if (renderStereo)
        {
            cam.stereoSeparation = stereoSeparation;
            cam.RenderToCubemap(cubemapLeft, 63, Camera.MonoOrStereoscopicEye.Left);
            cam.RenderToCubemap(cubemapRight, 63, Camera.MonoOrStereoscopicEye.Right);
        }
        else
        {
            cam.RenderToCubemap(cubemapLeft, 63, Camera.MonoOrStereoscopicEye.Mono);
        }
    
        //optional: convert cubemaps to equirect
    
        if (equirect == null)
        {
            return;
        }
    
        if (renderStereo)
        {
            cubemapLeft.ConvertToEquirect(equirect, Camera.MonoOrStereoscopicEye.Left);
            cubemapRight.ConvertToEquirect(equirect, Camera.MonoOrStereoscopicEye.Right);
        }
        else
        {
            cubemapLeft.ConvertToEquirect(equirect, Camera.MonoOrStereoscopicEye.Mono);
        }
    }
    
    • 4- 创建一个空的 Shephere。并编写一个脚本以供使用 渲染纹理。

    这是代码:

    public RenderTexture rt;
    public Renderer renderer;
    // Use this for initialization
    void Start () {
        renderer = this.GetComponent<Renderer>();
        renderer.material.SetTexture("_MainTex", rt);
    }
    

    相机脚本有左眼和右眼。使用 Cube 和 Camera 脚本的渲染器纹理类型有一个值 name 是 equirect。将其用于 2D 渲染器纹理。并为 Shephere 脚本使用 2D 渲染器纹理。而 Shephere 材料的耕作值必须是 x: 1 y:0.5

    这是我的解决方案。 这些代码不是我写的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      • 2021-10-04
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多