Unity VR黑屏

picosdk里面的,有修改

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScreenFade : MonoBehaviour
{
    [Tooltip("颜色")]
    public Color fadeColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);
    private int renderQueue = 4000;
    private MeshRenderer gradientMeshRenderer;
    private MeshFilter gradientMeshFilter;
    private Material gradientMaterial = null;
    private bool isGradient = false;
    private float currentAlpha;
    private float nowFadeAlpha;
    private List<Vector3> verts;
    private List<int> indices;
    private int N = 5;

    void Awake()
    {
        CreateFadeMesh();
        SetNowFadeAlpha(0f);
    }

    void OnDestroy()
    {
        DestoryGradientMesh();
    }

    private void CreateFadeMesh()
    {
        verts = new List<Vector3>();
        indices = new List<int>();
        gradientMaterial = new Material(Shader.Find("HappyMaster/ScreenFade"));
        gradientMeshFilter = gameObject.AddComponent<MeshFilter>();
        gradientMeshRenderer = gameObject.AddComponent<MeshRenderer>();

        CreateModel();
    }


    public void SetNowFadeAlpha(float alpha)
    {
        nowFadeAlpha = alpha;
        SetAlpha();
    }

    /// <summary>
    /// 渐显
    /// </summary>
    public void FadeIn(float gradientTime = 5f)
    {
        StartCoroutine(ScreenFadeIn(gradientTime));

    }
    /// <summary>
    /// 渐隐
    /// </summary>
    public void FadeOut(float gradientTime = 5f)
    {
        StartCoroutine(ScreenFadeOut(gradientTime));
    }
    IEnumerator ScreenFadeIn(float gradientTime = 5f)
    {
        float nowTime = 0.0f;
        while (nowTime < gradientTime)
        {
            nowTime += Time.deltaTime;
            nowFadeAlpha = Mathf.Lerp(1.0f, 0, Mathf.Clamp01(nowTime / gradientTime));
            SetAlpha();
            yield return null;
        }
    }
    IEnumerator ScreenFadeOut(float gradientTime = 5f)
    {
        float nowTime = 0.0f;
        while (nowTime < gradientTime)
        {
            nowTime += Time.deltaTime;
            nowFadeAlpha = Mathf.Lerp(0, 1f, Mathf.Clamp01(nowTime / gradientTime));
            SetAlpha();
            yield return null;
        }
    }

    private void SetAlpha()
    {
        Color color = fadeColor;
        color.a = Mathf.Max(currentAlpha, nowFadeAlpha);
        isGradient = color.a > 0;
        if (gradientMaterial != null)
        {
            gradientMaterial.color = color;
            gradientMaterial.renderQueue = renderQueue;
            gradientMeshRenderer.material = gradientMaterial;
            gradientMeshRenderer.enabled = isGradient;
        }
    }

    void CreateModel()
    {
        for (float i = -N / 2f; i <= N / 2f; i++)
        {
            for (float j = -N / 2f; j <= N / 2f; j++)
            {
                verts.Add(new Vector3(i, j, -N / 2f));
            }
        }
        for (float i = -N / 2f; i <= N / 2f; i++)
        {
            for (float j = -N / 2f; j <= N / 2f; j++)
            {
                verts.Add(new Vector3(N / 2f, j, i));
            }
        }
        for (float i = -N / 2f; i <= N / 2f; i++)
        {
            for (float j = -N / 2f; j <= N / 2f; j++)
            {
                verts.Add(new Vector3(i, N / 2f, j));
            }
        }
        for (float i = -N / 2f; i <= N / 2f; i++)
        {
            for (float j = -N / 2f; j <= N / 2f; j++)
            {
                verts.Add(new Vector3(-N / 2f, j, i));
            }
        }
        for (float i = -N / 2f; i <= N / 2f; i++)
        {
            for (float j = -N / 2f; j <= N / 2f; j++)
            {
                verts.Add(new Vector3(i, j, N / 2f));
            }
        }
        for (float i = -N / 2f; i <= N / 2f; i++)
        {
            for (float j = -N / 2f; j <= N / 2f; j++)
            {
                verts.Add(new Vector3(i, -N / 2f, j));
            }
        }

        for (int i = 0; i < verts.Count; i++)
        {
            verts[i] = verts[i].normalized * 0.7f;
        }

        CreateMakePos(0);
        CreateMakePos(1);
        CreateMakePos(2);
        OtherMakePos(3);
        OtherMakePos(4);
        OtherMakePos(5);
        Mesh mesh = new Mesh();
        mesh.vertices = verts.ToArray();
        mesh.triangles = indices.ToArray();
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        Vector3[] normals = mesh.normals;
        for (int i = 0; i < normals.Length; i++)
        {
            normals[i] = -normals[i];
        }
        mesh.normals = normals;
        int[] triangles = mesh.triangles;
        for (int i = 0; i < triangles.Length; i += 3)
        {
            int t = triangles[i];
            triangles[i] = triangles[i + 2];
            triangles[i + 2] = t;
        }
        mesh.triangles = triangles;
        gradientMeshFilter.mesh = mesh;
    }
    public void CreateMakePos(int num)
    {
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                int index = j * (N + 1) + (N + 1) * (N + 1) * num + i;
                int up = (j + 1) * (N + 1) + (N + 1) * (N + 1) * num + i;
                indices.AddRange(new int[] { index, index + 1, up + 1 });
                indices.AddRange(new int[] { index, up + 1, up });
            }
        }
    }
    public void OtherMakePos(int num)
    {
        for (int i = 0; i < N + 1; i++)
        {
            for (int j = 0; j < N + 1; j++)
            {
                if (i != N && j != N)
                {
                    int index = j * (N + 1) + (N + 1) * (N + 1) * num + i;
                    int up = (j + 1) * (N + 1) + (N + 1) * (N + 1) * num + i;
                    indices.AddRange(new int[] { index, up + 1, index + 1 });
                    indices.AddRange(new int[] { index, up, up + 1 });
                }
            }
        }
    }
    private void DestoryGradientMesh()
    {
        if (gradientMeshRenderer != null)
            Destroy(gradientMeshRenderer);

        if (gradientMaterial != null)
            Destroy(gradientMaterial);

        if (gradientMeshFilter != null)
            Destroy(gradientMeshFilter);
    }
}
csharp 复制代码
Shader "HappyMaster/ScreenFade" {
	Properties{
		 _Color("Color", Color) = (0,0,0,1)
	}
		SubShader{
		Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
		LOD 100

		ZWrite Off
		ZTest Always
		Blend SrcAlpha OneMinusSrcAlpha
		Color[_Color]

		Pass{}
	}
}
相关推荐
与火星的孩子对话3 小时前
Unity进阶课程【六】Android、ios、Pad 终端设备打包局域网IP调试、USB调试、性能检测、控制台打印日志等、C#
android·unity·ios·c#·ip
幻世界4 小时前
【Unity智能模型系列】Unity + MediaPipe + Sentis + ArcFace模型:构建高效人脸识别比对系统
unity·游戏引擎
在下胡三汉8 小时前
3dmax烘焙插件3dmax法线贴图烘焙教程glb和gltf元宇宙灯光效果图烘焙烘焙光影贴图支持VR渲染器
vr·贴图
在下胡三汉8 小时前
3dmax标准材质转物理材质插件,支持VR材质和CR材质转换成功物理材质,支持多维子材质
vr·材质
广州华锐视点9 小时前
VR小鼠解剖虚拟仿真:开启生命科学教育新视野
vr·vr小鼠解剖虚拟仿真
zhongqu_3dnest9 小时前
虚拟现实:开启沉浸式体验新时代
vr·虚拟现实·教育·房产·应用领域·技术原理·沉浸式体验
漫游者Nova12 小时前
虚幻引擎Unreal Engine5恐怖游戏设计制作教程,从入门到精通从零开始完整项目开发实战详细讲解中英字幕
ue5·游戏引擎·虚幻·游戏开发完整教程·恐怖游戏开发
死也不注释20 小时前
【Unity 编辑器工具开发:GUILayout 与 EditorGUILayout 对比分析】
unity·编辑器·游戏引擎
小赖同学啊1 天前
物联网中的Unity/Unreal引擎集成:数字孪生与可视化控制
物联网·unity·游戏引擎
Zlzxzw1 天前
使用unity创建项目,进行动画制作
unity·游戏引擎