CameraRenderer.cs 4.2 KB
Newer Older
H
Init  
happyfire 已提交
1 2 3 4 5 6 7 8 9
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace URasterizer
{
    public class CameraRenderer : MonoBehaviour
    {        
H
happyfire 已提交
10 11 12 13 14
        IRasterizer _rasterizer;
        IRasterizer _lastRasterizer;

        CPURasterizer _cpuRasterizer;
        GPURasterizer _gpuRasterizer;
H
Init  
happyfire 已提交
15

H
happyfire 已提交
16
        public RawImage rawImg;        
17 18
        
        private List<RenderingObject> renderingObjects = new List<RenderingObject>();
H
Init  
happyfire 已提交
19
        
H
happyfire 已提交
20
        private Camera _camera;
H
happyfire 已提交
21 22 23 24

        [SerializeField]
        private Light _mainLight;

H
happyfire 已提交
25
        public RenderingConfig _config;
H
Init  
happyfire 已提交
26

H
happyfire 已提交
27 28
        StatsPanel _statsPanel;

H
Init  
happyfire 已提交
29 30 31 32 33 34
        private void Start()
        {
            Init();
        }

        private void OnPostRender()
H
happyfire 已提交
35 36
        {            
            Render();            
H
Init  
happyfire 已提交
37 38 39 40 41 42
        }        

        void Init()
        {
            _camera = GetComponent<Camera>();

43 44 45
            var rootObjs = this.gameObject.scene.GetRootGameObjects();
            renderingObjects.Clear();
            foreach(var o in rootObjs)
H
Init  
happyfire 已提交
46
            {
47
                renderingObjects.AddRange(o.GetComponentsInChildren<RenderingObject>());
H
Init  
happyfire 已提交
48
            }
49 50 51
            
            Debug.Log($"Find rendering objs count:{renderingObjects.Count}");
            
H
happyfire 已提交
52
            
H
Init  
happyfire 已提交
53
            RectTransform rect = rawImg.GetComponent<RectTransform>();
H
happyfire 已提交
54
            rect.sizeDelta = new Vector2(Screen.width, Screen.height);
H
Init  
happyfire 已提交
55 56 57 58
            int w = Mathf.FloorToInt(rect.rect.width);
            int h = Mathf.FloorToInt(rect.rect.height);
            Debug.Log($"screen size: {w}x{h}");

H
happyfire 已提交
59 60
            _cpuRasterizer = new CPURasterizer(w, h, _config);
            _gpuRasterizer = new GPURasterizer(w, h, _config);
H
happyfire 已提交
61

H
happyfire 已提交
62 63 64 65
            _statsPanel = this.GetComponent<StatsPanel>();
            if (_statsPanel != null) {
                _cpuRasterizer.StatDelegate += _statsPanel.StatDelegate;
                _gpuRasterizer.StatDelegate += _statsPanel.StatDelegate;
H
happyfire 已提交
66
            }
H
Init  
happyfire 已提交
67 68 69 70 71
        }


        void Render()
        {
H
happyfire 已提交
72 73
            ProfileManager.BeginSample("CameraRenderer.Render");

H
happyfire 已提交
74 75 76 77 78 79 80 81 82
            if(_config.RasterizerType == RasterizerType.GPUDriven && _config.ComputeShader != null){
                _rasterizer = _gpuRasterizer;                
            }
            else{
                _rasterizer = _cpuRasterizer;                
            }

            if(_rasterizer != _lastRasterizer){
                _lastRasterizer = _rasterizer;
H
happyfire 已提交
83 84 85
                
                rawImg.texture = _rasterizer.ColorTexture;
                _statsPanel.SetRasterizerType(_rasterizer.Name);   
H
happyfire 已提交
86 87
            }

H
Init  
happyfire 已提交
88 89 90
            var r = _rasterizer;
            r.Clear(BufferMask.Color | BufferMask.Depth);

H
happyfire 已提交
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
            switch (_config.FragmentShaderType)
            {
                case ShaderType.VertexColor:
                    r.CurrentFragmentShader = ShaderContext.FSVertexColor;
                    break;
                case ShaderType.BlinnPhong:
                    r.CurrentFragmentShader = ShaderContext.FSBlinnPhong;
                    break;
                case ShaderType.NormalVisual:
                    r.CurrentFragmentShader = ShaderContext.FSNormalVisual;
                    break;
                default:
                    r.CurrentFragmentShader = ShaderContext.FSBlinnPhong;
                    break;
            }

            ShaderContext.Config = _config;

            var camPos = transform.position;
            camPos.z *= -1;
            ShaderContext.Uniforms.WorldSpaceCameraPos = camPos;            

            var lightDir = _mainLight.transform.forward;
            lightDir.z *= -1;
            ShaderContext.Uniforms.WorldSpaceLightDir = -lightDir;
            ShaderContext.Uniforms.LightColor = _mainLight.color * _mainLight.intensity;
            ShaderContext.Uniforms.AmbientColor = _config.AmbientColor;
H
happyfire 已提交
118
            
H
happyfire 已提交
119
            for (int i=0; i<renderingObjects.Count; ++i)
H
Init  
happyfire 已提交
120
            {
121
                if (renderingObjects[i].gameObject.activeInHierarchy)
H
happyfire 已提交
122
                {                    
H
happyfire 已提交
123
                    r.Draw(renderingObjects[i], _camera);
124
                }
H
happyfire 已提交
125 126 127
            }    
                                                    
            r.UpdateFrame();            
H
Init  
happyfire 已提交
128

H
happyfire 已提交
129
            ProfileManager.EndSample();
H
Init  
happyfire 已提交
130 131 132
        }
    }
}