Exemplo n.º 1
0
    void Update()
    {
        //_lightMap.filterMode = LightMapFilterMode;
        ISM.filterMode = ShadowMapFilterMode;
        //MousePick();
        UpdateLightInfo();
        Vector2         farPlane;
        ShadowMapParams shadowMapFactor = new ShadowMapParams()
        {
            ShadowBias        = SceneParamater.ShadowBias,
            ShadowSlopeFactor = SceneParamater.ShadowSlopeFactor,
        };

        for (int i = 0; i < LightmapSettings.lightmaps.Length; ++i)
        {
            IsmUseCS(i, shadowMapFactor, out farPlane);

            {
                IrradianceUseComputeShader(IndirectLightPassCS, _gBuffer, ISM,
                                           new PolygonLight()
                {
                    Center      = PolygonLightCenter,
                    PolygonHull = _polygonLight,
                    SumPower    = PolygonLightPower,
                    Normal      = PolygonLightNormal
                }, _lightmaps[i], farPlane, i, shadowMapFactor);
            }
        }
        SetLightMap();
    }
Exemplo n.º 2
0
    void Update()
    {
        //MousePick();
        UpdateLightInfo();
        Vector2         farPlane;
        ShadowMapParams shadowMapFactor = new ShadowMapParams()
        {
            ShadowBias        = SceneParamater.ShadowBias,
            ShadowSlopeFactor = SceneParamater.ShadowSlopeFactor,
        };

        for (int i = 0; i < LightmapSettings.lightmaps.Length; ++i)
        {
            {
                ImprefectShadowMap.RenderISM(i, out farPlane, _polygonLightWithCenter,
                                             new List <Vector3>(Enumerable.Repeat(PolygonLightNormal, _polygonLightWithCenter.Count)));
                IrradianceUseComputeShader(IndirectLightPassCS, _gBuffer, ImprefectShadowMap.Ism,
                                           new PolygonLight()
                {
                    Center      = PolygonLightCenter,
                    PolygonHull = _polygonLight,
                    SumPower    = PolygonLightPower,
                    Normal      = PolygonLightNormal
                }, _lightmaps[i], farPlane, i, LightMapSize, ShadowMapSize, shadowMapFactor);
            }
        }
        SetLightMap();
        ISM = ImprefectShadowMap.Ism;
    }
Exemplo n.º 3
0
    public void IsmUseCS(int lightmapIndex, ShadowMapParams shadowFactor, out Vector2 farPlane)
    {
        ISM.Clear(Color.black);
        ZBuffer.Clear(new Color(10, 10, 10, 10));

        int mainKernel = IsmCS.FindKernel("CSMain");

        IsmCS.SetTexture(mainKernel, "_NormalTex", _gBuffer.NormalTex[lightmapIndex]);
        IsmCS.SetTexture(mainKernel, "_PositionTex", _gBuffer.PositionTex[lightmapIndex]);

        Matrix4x4 matrix = _gBuffer.RenderCameraVPInverse[lightmapIndex];

        //转换成行优先?
        float[] matrixFloats =
        {
            matrix[0, 0], matrix[1, 0], matrix[2, 0], matrix[3, 0],
            matrix[0, 1], matrix[1, 1], matrix[2, 1], matrix[3, 1],
            matrix[0, 2], matrix[1, 2], matrix[2, 2], matrix[3, 2],
            matrix[0, 3], matrix[1, 3], matrix[2, 3], matrix[3, 3]
        };

        IsmCS.SetFloats("_GBufferCameraVPInverse", matrixFloats);

        IsmCS.SetVector("_GBufferCameraPos", _gBuffer.RenderCameraPos[lightmapIndex]);
        //塞入polygonlight 多边形的信息
        // ComputeBuffer vplPositionBuffer = new ComputeBuffer(MaxVPLPerIsm, sizeof(float) * 3);
        //vplPositionBuffer.SetData(Enumerable.Repeat(PolygonLightCenter, MaxVPLPerIsm).ToArray());
        ComputeBuffer vplPositionBuffer = new ComputeBuffer(_polygonLightWithCenter.Count, sizeof(float) * 3);

        vplPositionBuffer.SetData(_polygonLightWithCenter);
        //ComputeBuffer vplNormalBuffer = new ComputeBuffer(MaxVPLPerIsm, sizeof(float) * 3);
        //vplNormalBuffer.SetData(Enumerable.Repeat(PolygonLightNormal, MaxVPLPerIsm).ToArray());
        ComputeBuffer vplNormalBuffer = new ComputeBuffer(_polygonLightWithCenter.Count, sizeof(float) * 3);

        vplNormalBuffer.SetData(Enumerable.Repeat(PolygonLightNormal, _polygonLightWithCenter.Count).ToArray());

        IsmCS.SetBuffer(mainKernel, "_VplPosition", vplPositionBuffer);
        IsmCS.SetBuffer(mainKernel, "_VplNormal", vplNormalBuffer);

        //IsmCS.SetInt("_IsmCount", MaxVPLPerIsm);
        IsmCS.SetInt("_IsmCount", _polygonLightWithCenter.Count);
        IsmCS.SetInt("_GBufferTexSize", LightMapSize);
        IsmCS.SetInt("_ShadowMapSize", ShadowMapSize);
        var farClipPlane = _gBuffer.SceneAABB.size.y * 3;

        farPlane = new Vector2(farClipPlane, 1f / farClipPlane);
        IsmCS.SetVector("_ZFar", farPlane);

        IsmCS.SetTexture(mainKernel, "_Ism", ISM);
        IsmCS.SetTexture(mainKernel, "_ZBuffer", ZBuffer);

        IsmCS.Dispatch(mainKernel, LightMapSize / 32, LightMapSize / 32, 1);

        //dispose
        vplPositionBuffer.Dispose();
        vplNormalBuffer.Dispose();
    }
Exemplo n.º 4
0
    public void IrradianceUseComputeShader(ComputeShader indirectLightPassCS, UnityLmGBuffer gBuffer, RenderTexture shadowmap, PolygonLight polygonLight, RenderTexture rt,
                                           Vector2 farPlane, int lightmapIndex, ShadowMapParams shadowFactor)
    {
        //rt.Clear();

        int mainKernel = indirectLightPassCS.FindKernel("CSMain");


        indirectLightPassCS.SetTexture(mainKernel, "_NormalTex", gBuffer.NormalTex[lightmapIndex]);
        indirectLightPassCS.SetTexture(mainKernel, "_PositionTex", gBuffer.PositionTex[lightmapIndex]);

        Matrix4x4 matrix = gBuffer.RenderCameraVPInverse[lightmapIndex];

        //转换成行优先?
        float[] matrixFloats =
        {
            matrix[0, 0], matrix[1, 0], matrix[2, 0], matrix[3, 0],
            matrix[0, 1], matrix[1, 1], matrix[2, 1], matrix[3, 1],
            matrix[0, 2], matrix[1, 2], matrix[2, 2], matrix[3, 2],
            matrix[0, 3], matrix[1, 3], matrix[2, 3], matrix[3, 3]
        };
        float[] matrixFloats1 =
        {
            matrix[0, 0], matrix[0, 1], matrix[0, 2], matrix[0, 3],
            matrix[1, 0], matrix[1, 1], matrix[1, 2], matrix[1, 3],
            matrix[2, 0], matrix[2, 1], matrix[2, 2], matrix[2, 3],
            matrix[3, 0], matrix[3, 1], matrix[3, 2], matrix[3, 3]
        };
        indirectLightPassCS.SetFloats("_GBufferCameraVPInverse", matrixFloats);

        indirectLightPassCS.SetVector("_GBufferCameraPos", gBuffer.RenderCameraPos[lightmapIndex]);


        //塞入polygonlight 多边形的信息
        ComputeBuffer polygonLightBuffer = new ComputeBuffer(_polygonLight.Count, sizeof(float) * 3);

        //polygonLightBuffer.SetData(polygonLight.PolygonHull.ToArray());
        polygonLightBuffer.SetData(_polygonLight.ToArray());
        indirectLightPassCS.SetBuffer(mainKernel, "_PolygonHull", polygonLightBuffer);

        indirectLightPassCS.SetVector("_PolygonCenter", polygonLight.Center);
        indirectLightPassCS.SetVector("_PolygonNormal", polygonLight.Normal);
        indirectLightPassCS.SetVector("_PolygonTotalFlux", polygonLight.SumPower);
        indirectLightPassCS.SetInt("_PolygonVertexCount", _polygonLight.Count);
        indirectLightPassCS.SetFloat("_ShadowBias", shadowFactor.ShadowBias);

        indirectLightPassCS.SetVector("_FarPlane", farPlane);
        indirectLightPassCS.SetInt("_LightMapSize", LightMapSize);
        indirectLightPassCS.SetInt("_ShadowMapSize", ShadowMapSize);
        indirectLightPassCS.SetInt("_IsmCount", MaxVPLPerIsm);
        indirectLightPassCS.SetInt("_IsmIndexOffset", 0);
        indirectLightPassCS.SetFloat("_ShadowSoftness", shadowFactor.ShadowSoftness);
        indirectLightPassCS.SetFloat("_BiasFade", shadowFactor.BiasFade);
        //塞入最后返回的RenderTexture
        indirectLightPassCS.SetTexture(mainKernel, "_LightMap", rt);

        indirectLightPassCS.SetTexture(mainKernel, "_Ism", ISM);

        //  ComputeBuffer outputPosBuffer = new ComputeBuffer(100*1000, sizeof(float));
        //  IndirectLightPassCS.SetBuffer(mainKernel, "_OutputGBufferPos", outputPosBuffer);


        indirectLightPassCS.Dispatch(mainKernel, LightMapSize / 32, LightMapSize / 32, 1);


        //dispose
        polygonLightBuffer.Dispose();
    }
Exemplo n.º 5
0
    public void Init()
    {
        var unityAPIInit = UnityAPICaller.Instance;

        _geometryBuffer?.Release();
        _gi?.Release();
        SceneParamater.InitLightmapSetting();
        Debug.Log("GI Init Begin");

        var meshRenderers = GameObject.FindObjectsOfType <MeshRenderer>();

        //gen mesh collider

        /*
         * foreach (var go in meshRenderers)
         * {
         *  if (go.GetComponent<MeshCollider>() == null)
         *  {
         *      go.gameObject.AddComponent<MeshCollider>();
         *  }
         * }*/
        Debug.LogWarning("Has Added MeshCollider");
        var boxColliders = meshRenderers.Select(x => x.bounds).ToList();
        var sceneAABB    = boxColliders[0];

        foreach (var obj in boxColliders)
        {
            sceneAABB.Encapsulate(obj);
        }

        _geometryBuffer = new UnityLmGBuffer(CollectGeomerty, LightMapSize);
        _geometryBuffer.PrepareGeometry(SaveGBufferTexture);
        ImprefectShadowMap ism;

        switch (ISMType)
        {
        case ISMGenType.TS:
            ism = new ISM_TS(ShadowMapSize, _geometryBuffer);
            break;

        case ISMGenType.CS:
            ism = new ISM_CS(IsmPass, ShadowMapSize, _geometryBuffer);
            break;

        default:
            throw new ArgumentOutOfRangeException();
        }
        _gi = new VplWithIsm(_geometryBuffer, ism, LightMapSize, TotalSampleCount, Bounce,
                             RenderTextureFormat.ARGBFloat,
                             DirectLightPass, IndirectLightPass, AccumlatePass, SpeID);
        _gi.EncodeRgbmCS        = EncodeRgbm;
        _gi.DirectLightWeight   = DirectWight;
        _gi.IndirectLightWeight = IndirectWight;


        _gi.BuildFinish = false;

        _gi.Init();
        List <GILightBase> lights = new List <GILightBase>();

        lights.AddRange(FindObjectsOfType <GIPointLight>());
        lights.AddRange(FindObjectsOfType <GIAreaLight>());
        lights.AddRange(FindObjectsOfType <GIDirectionalLight>());

        _gi.EmitVPL(lights);
        //1.准备G-buffer
        //3.聚类成polygonlight

        _gi.Cluster2Polygon();

        ShadowMapParams param = new ShadowMapParams();

        param.ShadowSlopeFactor = SceneParamater.ShadowSlopeFactor;
        param.ShadowBias        = SceneParamater.ShadowBias;
        _gi.ShadowMapParam      = param;
        _gi.BuildLightMap(BuildDirectLight, BuildIndirectLight);

        Debug.Log("GI Finish");
    }
Exemplo n.º 6
0
    public void IrradianceUseComputeShader(ComputeShader indirectLightPassCS, UnityLmGBuffer gBuffer, RenderTexture shadowmap, PolygonLight polygonLight, RenderTexture rt,
                                           Vector2 farPlane, int lightmapIndex, int lightmapSize, int shadowmapSize, ShadowMapParams shadowFactor)
    {
        //rt.Clear();

        int mainKernel = indirectLightPassCS.FindKernel("CSMain");

        _gBuffer.SetComputeShaderData(indirectLightPassCS, mainKernel, lightmapIndex);
        //塞入polygonlight 多边形的信息
        ComputeBuffer polygonLightBuffer = new ComputeBuffer(_polygonLight.Count, sizeof(float) * 3);

        //polygonLightBuffer.SetData(polygonLight.PolygonHull.ToArray());
        polygonLightBuffer.SetData(_polygonLight.ToArray());
        indirectLightPassCS.SetBuffer(mainKernel, "_PolygonHull", polygonLightBuffer);

        indirectLightPassCS.SetVector("_PolygonCenter", polygonLight.Center);
        indirectLightPassCS.SetVector("_PolygonNormal", polygonLight.Normal);
        indirectLightPassCS.SetVector("_PolygonTotalFlux", polygonLight.SumPower);
        indirectLightPassCS.SetInt("_PolygonVertexCount", _polygonLight.Count);
        indirectLightPassCS.SetFloat("_ShadowBias", shadowFactor.ShadowBias);

        indirectLightPassCS.SetVector("_FarPlane", farPlane);
        indirectLightPassCS.SetInt("_LightMapSize", lightmapSize);
        indirectLightPassCS.SetInt("_ShadowMapSize", shadowmapSize);
        indirectLightPassCS.SetInt("_IsmIndexOffset", 0);
        indirectLightPassCS.SetFloat("_ShadowSoftness", shadowFactor.ShadowSoftness);
        indirectLightPassCS.SetFloat("_BiasFade", shadowFactor.BiasFade);
        //塞入最后返回的RenderTexture
        indirectLightPassCS.SetTexture(mainKernel, "_LightMap", rt);

        indirectLightPassCS.SetTexture(mainKernel, "_Ism", shadowmap);

        //  ComputeBuffer outputPosBuffer = new ComputeBuffer(100*1000, sizeof(float));
        //  IndirectLightPassCS.SetBuffer(mainKernel, "_OutputGBufferPos", outputPosBuffer);


        indirectLightPassCS.Dispatch(mainKernel, lightmapSize / 32, lightmapSize / 32, 1);


        //dispose
        polygonLightBuffer.Dispose();
    }