Пример #1
0
    public override void Inject(Vapor vapor, ComputeShader compute, Matrix4x4 viewProj)
    {
        compute.SetMatrix("_ZoneWorldToLocal", transform.worldToLocalMatrix);
        compute.SetFloat("_ZoneRadiusSqr", Radius * Radius);
        compute.SetVector("_ZoneSize", Size * 0.5f);

        Setting.Bind(compute, vapor.ZoneKernel, Setting, 0.0f);
        compute.SetTexture(vapor.ZoneKernel, "_DensityTextureWrite", vapor.GetDensityTex());
        vapor.SetLightAccum(vapor.ZoneKernel, false);
        vapor.InjectObject(viewProj, vapor.ZoneKernel, this);
    }
Пример #2
0
    public override void Inject(Vapor vapor, ComputeShader compute, Matrix4x4 viewProj)
    {
        if (m_light.type != LightType.Spot)
        {
            Debug.LogError("Custom lights only work for spot lights!");
            return;
        }

        compute.SetMatrix("_ZoneWorldToLocal", transform.worldToLocalMatrix);
        compute.SetVector("_ZoneSize", Size * 0.5f);

        s_shadowMapMultiplierMaterial.SetFloat("_Range", ShadowValue);

        if (CustomShadowMap)
        {
            s_shadowMapMultiplierMaterial.EnableKeyword("CustomMap");
            s_shadowMapMultiplierMaterial.SetTexture("_VaporCustomShadowMap", CustomShadowMap);
        }
        else
        {
            s_shadowMapMultiplierMaterial.DisableKeyword("CustomMap");
            s_shadowMapMultiplierMaterial.SetTexture("_VaporCustomShadowMap", Texture2D.blackTexture);
        }

        //setup the light properties here so that we can use the light-shafts
        //make such that we do not require the vapor light component
        var   lightProjMatrix = Matrix4x4.identity;
        float d = Mathf.Deg2Rad * m_light.spotAngle * 0.5f;

        d = Mathf.Cos(d) / Mathf.Sin(d);
        lightProjMatrix[3, 2] = 2f / d;
        lightProjMatrix[3, 3] = SpotBaseSize;
        var mat = lightProjMatrix * transform.worldToLocalMatrix;

        compute.SetMatrix("_SpotMatrix", mat);

        //Setup the lightPosRange etc params:
        //Setup basic params
        Vector4 posRange = transform.position;

        float range = m_light.range;

        posRange.w = 1.0f / (range * range);
        compute.SetVector("_LightPosRange", posRange);

        Vector4 lightStrength = m_light.color * m_light.intensity * Intensity;

        lightStrength *= 10;
        compute.SetVector("_LightColor", lightStrength);

        //Setup the _SpotCookie:
        if (m_light.cookie != null)
        {
            compute.SetTexture(vapor.CustomLightKernel, "_SpotCookie", m_light.cookie);
        }
        else
        {
            compute.SetTexture(vapor.CustomLightKernel, "_SpotCookie", vapor.SpotCookie);
        }

        //Handle the shadows:
        Matrix4x4 v = transform.worldToLocalMatrix;
        Matrix4x4 p =
            GL.GetGPUProjectionMatrix(Matrix4x4.Perspective(m_light.spotAngle, 1.0f,
                                                            m_light.shadowNearPlane,
                                                            m_light.range), true);

        //For some reason z is flipped :(
        p *= Matrix4x4.Scale(new Vector3(1.0f, 1.0f, -1.0f));

        compute.SetMatrix("_SpotShadowMatrix", p * v);
        compute.SetTexture(vapor.CustomLightKernel, "_SpotShadow", m_shadowmapCopy);

        Setting.Bind(compute, vapor.CustomLightKernel, Setting, 0.0f);
        compute.SetTexture(vapor.CustomLightKernel, "_DensityTextureWrite", vapor.GetDensityTex());
        vapor.SetLightAccum(vapor.CustomLightKernel, false);
        vapor.InjectObject(viewProj, vapor.CustomLightKernel, this);
    }
Пример #3
0
    public override void Inject(Vapor vapor, ComputeShader compute, Matrix4x4 viewProj)
    {
        if (HasShadow)
        {
            if (GetShadowMapResolution() != m_shadowMap.width)
            {
                DestroyImmediate(m_shadowMap);
                CreateShadowTex();
            }
        }

        //TODO: This doesn't really need to run every frame
        UpdateCommandBuffer();

        //Setup basic params
        Vector4 posRange = transform.position;

        posRange.w = 1.0f / (m_light.range * m_light.range);
        compute.SetVector("_LightPosRange", posRange);

        Vector4 lightStrength = m_light.color * m_light.intensity * FogScatterIntensity;

        lightStrength *= 10;
        compute.SetVector("_LightColor", lightStrength);

        //Per light type things
        switch (LightType)
        {
        case LightType.Directional:
            int dirKernel;

            if (HasShadow)
            {
                if (QualitySettings.shadowCascades > 1)
                {
                    dirKernel = vapor.LightDirKernel.GetKernel(VaporKernel.ShadowMode.Cascaded);
                }
                else
                {
                    dirKernel = vapor.LightDirKernel.GetKernel(VaporKernel.ShadowMode.Shadowed);
                }
            }
            else
            {
                dirKernel = vapor.LightDirKernel.GetKernel(VaporKernel.ShadowMode.None);
            }

            compute.SetVector("_LightPosRange", m_light.transform.forward);

            if (HasShadow)
            {
                compute.SetBuffer(dirKernel, "_MatrixBuf", MatrixBuffer);
                compute.SetTexture(dirKernel, "_ShadowMapTexture", m_shadowMap);
            }
            else
            {
                compute.SetTexture(dirKernel, "_ShadowMapTexture", Texture2D.whiteTexture);
            }

            vapor.SetLightAccum(dirKernel, false);

            Profiler.BeginSample("Dir Light pass");
            var tex = vapor.GetDensityTex();
            compute.DispatchScaled(dirKernel, tex.width, tex.height, tex.volumeDepth);
            Profiler.EndSample();
            break;

        case LightType.Point:
            vapor.SetLightAccum(vapor.LightPointKernel, false);
            vapor.InjectObject(viewProj, vapor.LightPointKernel, this);
            break;

        case LightType.Spot:
            int spotKernel =
                vapor.LightSpotKernel.GetKernel(HasShadow ? VaporKernel.ShadowMode.Shadowed : VaporKernel.ShadowMode.None);

            if (HasShadow)
            {
                Matrix4x4 v = transform.worldToLocalMatrix;
                Matrix4x4 p =
                    GL.GetGPUProjectionMatrix(Matrix4x4.Perspective(m_light.spotAngle, 1.0f,
                                                                    m_light.shadowNearPlane,
                                                                    m_light.range), true);

                //For some reason z is flipped :(
                p *= Matrix4x4.Scale(new Vector3(1.0f, 1.0f, -1.0f));

                compute.SetMatrix("_SpotShadowMatrix", p * v);
                compute.SetTexture(spotKernel, "_SpotShadow", m_shadowMap);
            }

            var   lightProjMatrix = Matrix4x4.identity;
            float d = Mathf.Deg2Rad * m_light.spotAngle * 0.5f;
            d = Mathf.Cos(d) / Mathf.Sin(d);
            lightProjMatrix[3, 2] = 2f / d;
            lightProjMatrix[3, 3] = SpotBaseSize;
            var mat = lightProjMatrix * transform.worldToLocalMatrix;
            compute.SetMatrix("_SpotMatrix", mat);
            if (m_light.cookie != null)
            {
                compute.SetTexture(spotKernel, "_SpotCookie", m_light.cookie);
            }
            else
            {
                compute.SetTexture(spotKernel, "_SpotCookie", vapor.SpotCookie);
            }

            vapor.SetLightAccum(spotKernel, false);
            vapor.InjectObject(viewProj, spotKernel, this);
            break;
        }
    }