unity3d - 将渲染纹理分配给相机 - Unity
问题描述
嗨,我正在学习在 Unity 中制作音频可视化器。我面临的问题是我有一个按钮,单击时会在运行时以编程方式加载另一个场景,(称之为场景 Phyllo)Phyllo 有一个摄像头。现在还有一个“主要”场景,称之为 UI_v15。我已将 Phyllo 的相机视图放入渲染纹理中。在UI_v15中,我有一个Cube
GameObject
,我想将 Phyllo 的渲染纹理分配给cube
,以便在按下上述按钮时,Cube
可以显示来自 Phyllo 的相机视图。但这是我失败的地方......
相反,我只是从 Phyllo 那里得到这样的视图(我只是从 Phyllo 那里得到完整的视图)。相反,应该发生的是视图应该渲染到我之前谈到的 Cube 中。
当程序运行时,targetTexture
forPhyllo
的相机没有分配纹理,它应该。如红色部分所示
这是按下按钮(在 UI_v15 内)时调用的方法。
public void SelectVisualiser()
{
string sceneName = GetComponent<Button>().GetComponentInChildren<Text>().text;
print("scene name " + sceneName);
SceneManager.LoadScene(sceneName, LoadSceneMode.Additive);
VisualiserManager.instance.LoadVisualiser(sceneName);
}
public void LoadVisualiser(string name)
{
//the script PhylloCamera is attached to PhylloCamera in the Phyllo scene.
Camera cam = PhylloCameraRef.instance.phylloCam;
Material mat = new Material(Shader.Find("Standard"));
RenderTexture rt = CreateTexture();
mat.SetTexture("_MainTex", rt);
cam.targetTexture = rt;
display.GetComponent<MeshRenderer>().materials[0] = mat;
}
PhylloCameraRef 类附加到场景 Phyllo,可以在运行时加载。
public class PhylloCameraRef : MonoBehaviour
{
public Camera phylloCam;
public static PhylloCameraRef instance;
void Start()
{
if (instance is null)
{
instance = this;
//getting camera
phylloCam = gameObject.GetComponent<Camera>();
}
}
}
有谁知道为什么 Phyllo 的相机没有渲染到 Cube 中?谢谢。
编辑:下面是 CreateTexture() 的代码
public RenderTexture CreateTexture()
{
RenderTexture rt = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32);
rt.Create();
return rt;
}
由于某些未知的原因,我忘了添加,UIv_15 中的相机 targetTexture 被分配了渲染纹理。立方体是一个实际的 3D 游戏对象。
解决方案
推荐阅读
- java-native-interface - nvcc 在编译 JNI 代码时生成无效错误
- android - 找不到 Gradle DSL 方法:'implementation()' (Android React Native)
- asp.net-mvc - 设置 / 到 /home 的默认路由
- typescript - 打字稿中“as”运算符的用途是什么?
- reactjs - 停止在挂载上运行 useEffect
- go - DynamoDB 更新项目失败
- swift - 如何获取在 UICollectionView 中点击的自定义单元格的索引
- oracle-apex-5.1 - 列表项 URL 目标在新选项卡中打开
- docker - azure ml 和 Pytorch:示例 conda-dependencies.yml 和 docker?
- mongodb - 如何存储一个圆并查看它是否与mongodb中的框/ Geojson点重叠