예제 #1
0
    public void SetUp(ref ScriptableRenderContext srContext, ref CullingResults cullResults, ref ShadowSetting shadowSetting) {
        _dirLightCount = _spotLightCount = _pointLightCount = 0;
        _shadowMgr.SetUp(ref srContext, ref cullResults, ref shadowSetting);
        _srContext = srContext;
        
        int visibleIndex = 0;
        foreach(var visibleLight in cullResults.visibleLights) {
            switch(visibleLight.lightType) {
                case LightType.Directional: {
                    if (_dirLightCount < MAX_NUM_DIRECTIONAL_LIGHT && visibleLight.light.intensity > 0) {
                        _dirLightColors[_dirLightCount] = visibleLight.finalColor.linear;
                        _dirLightDirections[_dirLightCount] = -visibleLight.light.transform.forward;
                        _dirLightShadowData[_dirLightCount] = _shadowMgr.GetDirectionalShadowData(visibleIndex);
                        _dirLightCount++;
                    }
                    break;
                }

                case LightType.Point: {
                    if (_pointLightCount < MAX_NUM_POINT_LIGHT && visibleLight.light.intensity > 0) {
                        _pointLightColors[_pointLightCount] = visibleLight.finalColor.linear;
                        _pointLightPositions[_pointLightCount] = visibleLight.light.transform.position;
                        _pointLightPositions[_pointLightCount].w = visibleLight.light.range;
                        _pointLightShadowData[_pointLightCount] = _shadowMgr.GetPointShadowData(visibleIndex);
                        _pointLightCount++;
                    }
                    break;
                }

                case LightType.Spot: {
                    if (_spotLightCount < MAX_NUM_SPOT_LIGHT && visibleLight.light.intensity > 0) {
                        _spotLightColors[_spotLightCount] = visibleLight.finalColor.linear;
                        _spotLightPositions[_spotLightCount] = visibleLight.light.transform.position;
                        _spotLightPositions[_spotLightCount].w = visibleLight.light.range;
                        _spotLightDirections[_spotLightCount] = -visibleLight.light.transform.forward;
                        _spotLightAngles[_spotLightCount].x = Mathf.Deg2Rad * visibleLight.light.innerSpotAngle;
                        _spotLightAngles[_spotLightCount].y = Mathf.Deg2Rad * visibleLight.spotAngle;
                        _spotLightShadowData[_spotLightCount] = _shadowMgr.GetSpotLightShadowData(visibleIndex);
                        _spotLightCount++;
                    }
                    break;
                }
                
            }

            visibleIndex++;
        }

        _shadowMgr.Render(); // generate shadow map

        _cmdBuffer.BeginSample(CmdBufferName);
        ExecuteCmdBuffer();

        _cmdBuffer.SetGlobalInt(DirLightCountId, _dirLightCount);
        if (_dirLightCount > 0) {
            _cmdBuffer.SetGlobalVectorArray(DirLightColorsId, _dirLightColors);
            _cmdBuffer.SetGlobalVectorArray(DirLightDirectionsId, _dirLightDirections);
            _cmdBuffer.SetGlobalVectorArray(DirLightShadowDataId, _dirLightShadowData);
        }

        _cmdBuffer.SetGlobalInt(SpotLightCountId, _spotLightCount);
        if (_spotLightCount > 0) {
            _cmdBuffer.SetGlobalVectorArray(SpotLightColorsId, _spotLightColors);
            _cmdBuffer.SetGlobalVectorArray(SpotLightPositionsId, _spotLightPositions);
            _cmdBuffer.SetGlobalVectorArray(SpotLightDirectionsId, _spotLightDirections);
            _cmdBuffer.SetGlobalVectorArray(SpotLightAnglesId, _spotLightAngles);
            _cmdBuffer.SetGlobalVectorArray(SpotLightShadowDataId, _spotLightShadowData);
        }

        _cmdBuffer.SetGlobalInt(PointLightCountId, _pointLightCount);
        if (_pointLightCount > 0) {
            _cmdBuffer.SetGlobalVectorArray(PointLightColorsId, _pointLightColors);
            _cmdBuffer.SetGlobalVectorArray(PointLightPositionsId, _pointLightPositions);
            _cmdBuffer.SetGlobalVectorArray(PointLightShadowDataId, _pointLightShadowData);
        }

        _cmdBuffer.EndSample(CmdBufferName);
        ExecuteCmdBuffer();
    }