Ejemplo n.º 1
0
        public void RebuildLightsList(List <VisibleLight> visibleLights, CameraData cameraData, PipelineSettings pipelineSettings, ref RenderingData renderingData)
        {
            UnityEngine.Profiling.Profiler.BeginSample("RebuildLightsList");
            lights.Clear();
            var unityLights = visibleLights;

            VisibleLight = 0;
            LightsDataForShadow.Clear();
            bool softShadow         = false;
            int  j                  = 0;
            int  currentShadowIndex = 0;
            int  maxShadowNum       = pipelineSettings.MaxShadowLightsNum;

            for (int i = 0; i < unityLights.Count; ++i)
            {
                var ul = unityLights[i];
                var pl = ul.light.GetComponent <ProjectorLight>();
                if (pl == null)
                {
                    continue;
                }
                lights.Add(pl);
                var ifLight = pl;
                if (ifLight.spritesAtlas != spritesAtlas)
                {
                    spritesAtlas = ifLight.spritesAtlas;
                }
                //矩阵是从右向左乘的,view需要z取反
                Matrix4x4 c2w         = /*Camera.main*/ cameraData.camera.cameraToWorldMatrix * Matrix4x4.Scale(new Vector3(1, 1, -1));// Camera.main.transform.localToWorldMatrix;
                int       shadowIndex = currentShadowIndex;
                if (currentShadowIndex >= maxShadowNum || !ifLight.lightParms_Shadow)
                {
                    shadowIndex = -1;
                }
                LightDataInAll lightDataInAll = ifLight.GetLightData(c2w, shadowIndex);
                NativeLightsBoundList[i] = lightDataInAll.sFiniteLightBound;
                NativeLightsDataList[i]  = lightDataInAll.lightData;
                ++VisibleLight;

                //for shadow
                // TODO  frustum cull
                if (ifLight.lightParms_Shadow && currentShadowIndex < maxShadowNum)
                {
                    LightDataForShadow lightDataForShadow = new LightDataForShadow();
                    lightDataForShadow.lightIndex      = j++;
                    lightDataForShadow.shadowData      = lightDataInAll.shadowData;
                    lightDataForShadow.visibleLight    = ul;
                    lightDataForShadow.projectorLight  = ifLight;
                    lightDataForShadow.unityLightIndex = i;

                    //Matrix4x4 scaleMatrix = Matrix4x4.identity;
                    ////scaleMatrix.m22 = -1.0f;
                    //Matrix4x4 view = scaleMatrix * ul.localToWorld.inverse;
                    //lightDataForShadow.shadowData.viewMatrix = view;

                    LightsDataForShadow.Add(lightDataForShadow);

                    softShadow |= ifLight.softShadow;
                    ++currentShadowIndex;
                }
            }
            renderingData.shadowData.supportsSoftShadows = softShadow;
            culsterDataGenerator.Begin(pipelineSettings);
            culsterDataGenerator.Run(cameraData, pipelineSettings, this);
            lightLoopLightsData.LightsDatasBuf.SetData(LightsDataList);
            tileAndClusterData.itemsIDListBuf.SetData(culsterDataGenerator.ResultItemsIDList);
            tileAndClusterData.clusterNumItemsBuf.SetData(culsterDataGenerator.ResultClusterNumItems);
            culsterDataGenerator.End();
            UnityEngine.Profiling.Profiler.EndSample();
        }
Ejemplo n.º 2
0
        public LightDataInAll GetLightData(Matrix4x4 c2w, int currentShadowIndex)
        {
            LightDataInAll lightDataInAll = new LightDataInAll();

            var ps = iFPipelineProjector.GetProjectorSettings;
            {
                //bound.planes = Projector.GetCullingPlanes(ps.frustumMatrix, c2w);//world to view
                Projector.GetCullingPlanesAndSphere(
                    out lightDataInAll.sFiniteLightBound.planes,
                    out lightDataInAll.sFiniteLightBound.aabb,
                    ps.frustumMatrix, c2w);
            }

            LightData lightData = new LightData();

            lightData.pos = cacheTransform.position;
            //lightData.lightParms = (uint)LightParams;
            lightData.posShadow   = new Vector4();
            lightData.falloffR    = ps.falloffR;
            lightData.projS       = ps.projectMatrixX;
            lightData.projT       = ps.projectMatrixY;
            lightData.projQ       = ps.projectMatrixW;
            lightData.colorPacked = ColorTools.packRGBE(ColorTools.Color2Vec3(FinalColor));
            var unpack = ColorTools.unpackRGBE(2256963201);

            //Debug.Log(unpack.ToString());
            //Debug.Log(FinalColor.ToString());
            lightData.specMultiplier    = specMultiplier;
            lightData.shadowBleedReduce = shadowBleedReduce;

            int sizex = spritesAtlas.width;
            int sizey = spritesAtlas.height;
            {
                SpriteData spriteData = projSpriteData;
                float      x          = spriteData.x;
                float      y          = sizey - spriteData.y - spriteData.height;//GPU得纹理坐标y轴反的,原点左上角
                float      width      = spriteData.width;
                float      height     = spriteData.height;
                float      scalex     = width / sizex;//todo:optimize
                float      scaley     = height / sizey;
                float      offsetx    = x / sizex;
                float      offsety    = y / sizey;
                ColorTools.packR15G15B15A15(
                    out lightData.scaleBias.x,
                    out lightData.scaleBias.y,
                    new Vector4(scalex, scaley, offsetx, offsety));
            }

            {
                SpriteData spriteData = falloffSpriteData;
                float      x          = spriteData.x;
                float      y          = sizey - spriteData.y - spriteData.height;//GPU得纹理坐标y轴反的,原点左上角
                float      width      = spriteData.width;
                float      height     = spriteData.height;
                float      scalex     = width / sizex;//todo:optimize
                float      scaley     = height / sizey;
                float      offsetx    = x / sizex;
                float      offsety    = y / sizey;
                ColorTools.packR15G15B15A15(
                    out lightData.scaleBias.z,
                    out lightData.scaleBias.w,
                    new Vector4(scalex, scaley, offsetx, offsety));
            }

            //public Vector4 boxMin;
            //public Vector4 boxMax;
            //public Vector4 areaPlane;
            {
                Plane   plane    = lightDataInAll.sFiniteLightBound.planes.Near;
                Vector3 normalws = plane.normal.normalized;
                plane.distance      = Vector3.Dot(lightData.pos, normalws);
                plane.normal        = normalws * area_falloff;
                lightData.areaPlane = new Vector4(
                    plane.normal.x,
                    plane.normal.y,
                    plane.normal.z,
                    plane.distance);

                //问题:已知plane,求面上偏移width,height的点
                Vector3   topright    = new Vector3(area_width, area_height, 0);
                Vector3   bottomleft  = new Vector3(-area_width, -area_height, 0);
                Matrix4x4 local2world = cacheTransform.localToWorldMatrix;
                topright   = local2world.MultiplyPoint3x4(topright);
                bottomleft = local2world.MultiplyPoint3x4(bottomleft);
#if UNITY_EDITOR
                posa = bottomleft;
                posc = topright;
                posb = new Vector3(-area_width, area_height, 0);
                posb = local2world.MultiplyPoint3x4(posb);
                posd = new Vector3(area_width, -area_height, 0);
                posd = local2world.MultiplyPoint3x4(posd);
#endif
                Vector3 boxMin = new Vector3(
                    Mathf.Min(bottomleft.x, topright.x),
                    Mathf.Min(bottomleft.y, topright.y),
                    Mathf.Min(bottomleft.z, topright.z)
                    );
                Vector3 boxMax = new Vector3(
                    Mathf.Max(bottomleft.x, topright.x),
                    Mathf.Max(bottomleft.y, topright.y),
                    Mathf.Max(bottomleft.z, topright.z)
                    );

                lightData.boxMin = boxMin;
                lightData.boxMax = boxMax;
            }

#if UNITY_EDITOR
            cacheLightData = lightDataInAll;
#endif

            // shadow
            {
                LightData_Shadow shadowData = new LightData_Shadow();
                if (lightParms_Shadow && currentShadowIndex != -1)
                {
                    needDrawShadow          = true;
                    lightParams_ShadowIndex = currentShadowIndex;
                    shadowData.projMatrix   = iFPipelineProjector.GetProjectorSettings.projMatrix;
                    shadowData.viewMatrix   = iFPipelineProjector.GetProjectorSettings.viewMatrix;
#if UNITY_EDITOR
                    shadowData.planes = cacheLightData.sFiniteLightBound.planes;
#endif
                }
                else
                {
                    needDrawShadow = false;
                }
                lightDataInAll.shadowData = shadowData;
            }

            lightData.lightParms     = (uint)LightParams;
            lightDataInAll.lightData = lightData;

            return(lightDataInAll);
        }