열차 목업의 내부 확인용 프로젝트
smchoi
2024-07-24 ff74c0073046f0e086eb7d355950bda39937c92c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//////////////////////////////////////////////////////
// MK Glow Extensions                                  //
//                                                    //
// Created by Michael Kremmel                       //
// www.michaelkremmel.de                            //
// Copyright © 2020 All rights reserved.           
 //
//////////////////////////////////////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
 
namespace MK.Glow
{
    using ShaderProperties = PipelineProperties.ShaderProperties;
 
    internal static class PipelineExtensions
    {
        private static Mesh _screenMesh;
        private static Mesh screenMesh
        {
            get
            {
                if(_screenMesh == null)
                {
                    _screenMesh = new Mesh { name = "MKGlowScreenMesh" };
 
                    _screenMesh.SetVertices(new List<Vector3>
                    {
                        new Vector3(-1f, -1f, 0f),
                        new Vector3( 3f, -1f, 0f),
                        new Vector3(-1f,  3f, 0f)
                    });
 
                    _screenMesh.SetIndices(new[] { 0, 1, 2 }, MeshTopology.Triangles, 0, false);
                    _screenMesh.UploadMeshData(false);
                }
                
                return _screenMesh;
            }
        }
 
        public static void Destroy(UnityEngine.Object obj)
        {
            if (obj != null)
            {
                #if UNITY_EDITOR
                    if (Application.isPlaying)
                        UnityEngine.Object.Destroy(obj);
                    else
                        UnityEngine.Object.DestroyImmediate(obj);
                #else
                    UnityEngine.Object.Destroy(obj);
                #endif
            }
        }
 
        /// <summary>
        /// Enable or disable a specific Shader keyword
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="keyword"></param>
        /// <param name="enable"></param>
        internal static void SetKeyword(this CommandBuffer cmd, string keyword, bool enable)
        {
            if(enable)
                cmd.EnableShaderKeyword(keyword);
            else
                cmd.DisableShaderKeyword(keyword);
        }
        internal static void SetKeyword(string keyword, bool enable)
        {
            if(enable)
                Shader.EnableKeyword(keyword);
            else
                Shader.DisableKeyword(keyword);
        }
 
        /// <summary>
        /// Draw the currently setuped render context with its targets
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="destinations"></param>
        /// <param name="material"></param>
        /// <param name="pass"></param>
        internal static void Draw(this CommandBuffer cmd, List<RenderTarget> destinations, Material material, int pass, Rect viewport)
        {
            cmd.SetRenderTargetContext(destinations);
            cmd.SetViewport(viewport);
            cmd.DrawMesh(screenMesh, Matrix4x4.identity, material, 0, pass);
        }
        internal static void Draw(List<RenderTarget> destinations, Material material, int pass)
        {
            RenderTargetContext.SetRenderTargetContext(destinations);
            material.SetPass(pass);
            Graphics.DrawMeshNow(screenMesh, Vector3.zero, Quaternion.identity);
        }
 
        /// <summary>
        /// Scaling size correctly, need if single pass stereo is enabled
        /// </summary>
        /// <param name="cameraIsStereo"></param>
        /// <param name="size"></param>
        /// <param name="scale"></param>
        /// <returns></returns>
        private static int SinglePassStereoDownscale(bool cameraIsStereo, int size, int scale)
        {
            //using single pass stereo can introduce some Texeloffset which makes the rendering occur on the wrong place
            //This happens because the samples are build on base of different mip levels
            //single pass stereo TexelSize needs to be adjusted in the shader too
            #if UNITY_2017_1_OR_NEWER
            return cameraIsStereo && PipelineProperties.singlePassStereoDoubleWideEnabled && ((size / 2) % 2 > 0) ? 1 + size / scale : size / scale;
            #else
            return size / scale;
            #endif
        }
 
        /// <summary>
        /// Update a mip based render context array
        /// </summary>
        /// <param name="camera"></param>
        /// <param name="renderContexts"></param>
        /// <param name="rawDimension"></param>
        /// <param name="levels"></param>
        /// <param name="format"></param>
        /// <param name="depthBufferBits"></param>
        internal static void UpdateMipRenderContext(this MK.Glow.ICameraData cameraData, RenderContext[] renderContexts, RenderDimension rawDimension, int levels, RenderTextureFormat format, int depthBufferBits)
        {
            for(int i = 0; i < levels; i++)
            {
                renderContexts[i].UpdateRenderContext(cameraData, format, depthBufferBits, rawDimension);
                rawDimension.width = Mathf.Max(SinglePassStereoDownscale(cameraData.GetStereoEnabled(), rawDimension.width, 2), 1);
                rawDimension.height = Mathf.Max(rawDimension.height / 2, 1);
            }
        }
 
        /// <summary>
        /// Get a temporary render texture
        /// </summary>
        /// <param name="renderContext"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        internal static RenderTexture GetTemporary(RenderContext renderContext, RenderTextureFormat format)
        {
            #if UNITY_2017_1_OR_NEWER
            return RenderTexture.GetTemporary(renderContext.descriptor);
            #else
            RenderTexture renderTexture = RenderTexture.GetTemporary(renderContext.width, renderContext.height, 16, format, RenderTextureReadWrite.Default, 1);
            return renderTexture;
            #endif
        }
    }
}