コード例 #1
0
    void LateUpdate()
    {
        if (spotLight == null)
        {
            spotLight = GetComponent <L2DLSpotLight>();
        }

        Vector3 localPosition = transform.localPosition;

        localPosition.y         = -spotLight.Range / 2f;
        transform.localPosition = localPosition;
    }
コード例 #2
0
    // --------------------------------------------------------------------
    void RunSpotLightRendering(L2DLSpotLight spotLight)
    {
        SetupCommonLightRenderingProperties(m_spotLightRenderingBuffer, spotLight);
        m_spotLightRenderingBuffer.SetGlobalFloat("_lightAttenuation", 1f / Mathf.Max(spotLight.Range * spotLight.Range, 0.00001f));
        m_spotLightRenderingBuffer.SetGlobalVector("_lightPosition", spotLight.transform.position + spotLight.transform.up * spotLight.Range / 2f);
        m_spotLightRenderingBuffer.SetGlobalVector("_lightDirection", -spotLight.transform.up);

        float outerRad   = Mathf.Deg2Rad * 0.5f * spotLight.Angle;
        float outerCos   = Mathf.Cos(outerRad);
        float outerTan   = Mathf.Tan(outerRad);
        float innerCos   = Mathf.Cos(Mathf.Atan((46f / 64f) * outerTan));
        float angleRange = Mathf.Max(innerCos - outerCos, 0.00001f);
        float spotFade   = 1f / angleRange;

        m_spotLightRenderingBuffer.SetGlobalVector("_lightFade", new Vector2(spotFade, -outerCos * spotFade));

        m_spotLightRenderingBuffer.SetGlobalFloat("_maxIntensityOutput", spotLight.MaxIntensityOutput);
        m_spotLightRenderingBuffer.SetGlobalFloat("_startingRange", spotLight.StartingRange);

        m_spotLightRenderingBuffer.SetRenderTarget(new RenderTargetIdentifier[] { L2DLPipelineData.s_cameraDirectLightResultTextureId, L2DLPipelineData.s_cameraEmissionTextureId }, L2DLPipelineData.s_cameraFakeDepthTextureId);
        m_spotLightRenderingBuffer.Blit(null, BuiltinRenderTextureType.CurrentActive, SpotLightPassMaterial);
        L2DLRenderHelpers.ExecuteBuffer(m_context, m_spotLightRenderingBuffer);
    }
コード例 #3
0
 public void CalculateShadowMap(ScriptableRenderContext _context, CommandBuffer _buffer, L2DLSpotLight _spotLight, L2DLDirectLightData _data, RenderTargetIdentifier i_occlusionMap, RenderTargetIdentifier o_shadowMap)
 {
     throw new System.NotImplementedException();
 }
コード例 #4
0
 private void Awake()
 {
     spotLight = GetComponent <L2DLSpotLight>();
 }
コード例 #5
0
 void RunSpotLightOcclusionMapTrace(L2DLSpotLight spotLight)
 {
     m_directLightData.ShadowMapCalculator.CalculateShadowMap(m_context, m_spotLightRenderingBuffer, spotLight, m_directLightData, m_directLightOcclusionMapId, m_directLightShadowMapId);
 }
コード例 #6
0
    // --------------------------------------------------------------------
    public void CalculateShadowMap(ScriptableRenderContext _context, CommandBuffer _buffer, L2DLSpotLight _spotLight, L2DLDirectLightData _data, RenderTargetIdentifier i_occlusionMap, RenderTargetIdentifier o_shadowMap)
    {
        if (!L2DLRenderHelpers.SupportsComputeShaders())
        {
            return;
        }

        ReloadComputeShaders();

        int occlusionTraceComputeKernel = m_spotLightOcclusionTraceCompute.FindKernel("SpotLightOcclusionTrace");

        SetupCommonComputeShaderProperties(_buffer, m_spotLightOcclusionTraceCompute, occlusionTraceComputeKernel, _spotLight, _data);
        _buffer.SetComputeFloatParam(m_spotLightOcclusionTraceCompute, "_WorldDistPerStep", _spotLight.Range / (int)_spotLight.ShadowMapSize);
        _buffer.DispatchCompute(m_spotLightOcclusionTraceCompute, occlusionTraceComputeKernel, (int)_spotLight.ShadowMapSize / 64, 1, 1);
        L2DLRenderHelpers.ExecuteBuffer(_context, _buffer);
    }
コード例 #7
0
    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        // 2D Lights will have associated cameras because amazingly it's impossible to cull without a camera so here I'll need to filter light cameras out from non-light cameras
        // I can then handle the light cameras seperately to generate their occlusion / shadow maps and let each actual camera reuse them
        m_directLights.Clear();

        // When in the scene view it doesn't pass in all the cameras active in the scene
        // so we need to get them manually. The performance hit is only in scene view so won't hit in-game
        Camera[] directLightPotentialCameras = cameras;
#if UNITY_EDITOR
        directLightPotentialCameras = Object.FindObjectsOfType <Camera>();
        Camera mainCamera = null;
#endif

        foreach (Camera camera in directLightPotentialCameras)
        {
            if (camera.tag == "L2DLDirectLight")
            {
                L2DLDirectionalLight directionalLight = camera.gameObject.GetComponent <L2DLDirectionalLight>();
                if (directionalLight != null)
                {
                    m_directLights.m_directionalLights.Add(directionalLight);
                    continue;
                }

                L2DLPointLight pointLight = camera.gameObject.GetComponent <L2DLPointLight>();
                if (pointLight != null)
                {
                    m_directLights.m_pointLights.Add(pointLight);
                    continue;
                }

                L2DLSpotLight spotLight = camera.gameObject.GetComponent <L2DLSpotLight>();
                if (spotLight != null)
                {
                    m_directLights.m_spotLights.Add(spotLight);
                    continue;
                }
            }
#if UNITY_EDITOR
            else if (camera.tag == "MainCamera")
            {
                mainCamera = camera;
            }
#endif
        }

        // Only pass non-direct light rendering cameras to the pipeline
        // Direct light cameras can be accessed via the associated direct light component
        foreach (Camera camera in cameras)
        {
            if (camera.tag != "L2DLDirectLight")
            {
#if UNITY_EDITOR
                // The scene camera is in a strange place, and usually doesn't render depth
                // so we'll find the main camera, and bring it in line with that,
                // shift the clip plants, and render to the depth texture
                camera.depthTextureMode = DepthTextureMode.Depth;
                Vector3 cameraPosition = camera.transform.position;
                cameraPosition.z          = mainCamera.transform.position.z;
                camera.transform.position = cameraPosition;
                camera.nearClipPlane      = mainCamera.nearClipPlane;
                camera.farClipPlane       = mainCamera.farClipPlane;
#endif
                m_cameraRenderer.Render(context, camera, m_directLights);
            }
        }
    }