コード例 #1
0
        private void CalculateProjectionData(CharacterShadowCommonSettingsComponent settings, ICollection <CharacterShadowComponent> characters, Camera camera)
        {
            Quaternion rotation           = settings.virtualLight.rotation;
            Matrix4x4  localToWorldMatrix = settings.virtualLight.localToWorldMatrix;
            Matrix4x4  matrixx4           = Matrix4x4.TRS(Vector3.zero, rotation, new Vector3(1f, 1f, -1f)).inverse *camera.cameraToWorldMatrix;
            int        countX             = settings.shadowMap.width / settings.textureSize;
            int        countY             = settings.shadowMap.height / settings.textureSize;
            int        num3 = 0;

            foreach (CharacterShadowComponent component in characters)
            {
                int              yIndex                      = num3 / countX;
                int              xIndex                      = num3 - (yIndex * countX);
                Rect             atlasData                   = CalculateLocationInAtlas(countX, countY, xIndex, yIndex);
                MyShadowInternal characterInternal           = component.GetComponent <MyShadowInternal>();
                Bounds           projectionBoundInLightSpace = characterInternal.ProjectionBoundInLightSpace;
                this.SetProjectorData(characterInternal, localToWorldMatrix.MultiplyPoint3x4(projectionBoundInLightSpace.center), rotation, component, atlasData);
                Vector3   center   = projectionBoundInLightSpace.center;
                Matrix4x4 identity = Matrix4x4.identity;
                identity.SetColumn(3, new Vector4(-center.x, -center.y, center.z, 1f));
                Matrix4x4 matrixx6 = identity * matrixx4;
                Vector3   extents  = projectionBoundInLightSpace.extents;
                Vector3   vector3  = projectionBoundInLightSpace.extents;
                float     num6     = Mathf.Max(extents.x, vector3.y);
                Vector3   vector4  = projectionBoundInLightSpace.extents;
                float     z        = vector4.z;
                float     left     = ((-xIndex * 2) - 1) * num6;
                float     right    = (((-xIndex * 2) - 1) + (countX * 2)) * num6;
                float     bottom   = ((-yIndex * 2) - 1) * num6;
                float     top      = (((-yIndex * 2) - 1) + (countY * 2)) * num6;
                Matrix4x4 matrixx7 = Matrix4x4.Ortho(left, right, bottom, top, -z, z);
                characterInternal.CasterMaterial.SetMatrix("ViewToAtlas", GL.GetGPUProjectionMatrix(matrixx7 * matrixx6, true));
                num3++;
            }
        }
コード例 #2
0
        private void UpdateBoundsAndCullCharacters(ICollection <CharacterShadowComponent> collector, ICollection <CharacterShadowComponent> characters, Camera camera, CharacterShadowCommonSettingsComponent settings)
        {
            Matrix4x4 localToWorldMatrix = settings.virtualLight.localToWorldMatrix;
            Matrix4x4 worldToLocalMatrix = settings.virtualLight.worldToLocalMatrix;

            Plane[] planes = GeometryUtility.CalculateFrustumPlanes((camera.projectionMatrix * camera.worldToCameraMatrix) * localToWorldMatrix);
            int     num    = 0;
            int     maxCharactersCountInAtlas = settings.MaxCharactersCountInAtlas;

            foreach (CharacterShadowComponent component in characters)
            {
                bool   flag = num < maxCharactersCountInAtlas;
                Bounds boundsInLightSpace           = BoundsUtils.TransformBounds(component.GetComponent <MyShadowCaster>().BoundsInWorldSpace, worldToLocalMatrix);
                CharacterShadowComponent component2 = component;
                MyShadowInternal         internal2  = component.GetComponent <MyShadowInternal>();
                internal2.ProjectionBoundInLightSpace = this.CalculateProjectionBoundInLightSpace(settings, boundsInLightSpace, component2.offset);
                Bounds projectionBoundInLightSpace = internal2.ProjectionBoundInLightSpace;
                projectionBoundInLightSpace.max += new Vector3(0f, 0f, component2.attenuation);
                flag &= GeometryUtility.TestPlanesAABB(planes, projectionBoundInLightSpace);
                internal2.Projector.enabled = flag;
                if (flag)
                {
                    collector.Add(component);
                }
                num++;
            }
        }
コード例 #3
0
        public void Init(CharacterShadowComponent characterShadow)
        {
            MyShadowInternal internal2 = characterShadow.gameObject.AddComponent <MyShadowInternal>();

            internal2.Projector      = this.CreateProjector(this.shadowCommonSettings);
            internal2.BaseAlpha      = characterShadow.color.a;
            internal2.CasterMaterial = new Material(this.shadowCommonSettings.casterShader);
            Debug.Log("Shadow System Init " + this.shadowCommonSettings.casterShader.name);
        }
コード例 #4
0
        private void SetProjectorData(MyShadowInternal characterInternal, Vector3 position, Quaternion rotation, CharacterShadowComponent characterShadow, Rect atlasData)
        {
            Projector projector = characterInternal.Projector;
            Bounds    projectionBoundInLightSpace = characterInternal.ProjectionBoundInLightSpace;
            float     num = projectionBoundInLightSpace.size.x / projectionBoundInLightSpace.size.y;

            projector.transform.position = position;
            projector.transform.rotation = rotation;
            projector.orthographicSize   = Mathf.Max(projectionBoundInLightSpace.extents.x, projectionBoundInLightSpace.extents.y);
            projector.aspectRatio        = num;
            projector.nearClipPlane      = -projectionBoundInLightSpace.extents.z;
            projector.farClipPlane       = projectionBoundInLightSpace.extents.z + characterShadow.attenuation;
            float    num2     = projectionBoundInLightSpace.size.z + 0.01f;
            float    x        = num;
            float    y        = (num2 + characterShadow.attenuation) / num2;
            float    z        = (characterShadow.attenuation <= 0f) ? 100000f : (1f / (characterShadow.attenuation / num2));
            Material material = projector.material;

            material.SetVector("Params", new Vector4(x, y, z, (characterShadow.backFadeRange <= 0f) ? 100000f : (1f / (characterShadow.backFadeRange / num2))));
            material.mainTextureOffset = atlasData.position;
            material.mainTextureScale  = atlasData.size;
            material.color             = characterShadow.color;
        }
コード例 #5
0
        private void GenerateDrawCharacterCommands(CommandBuffer commandBuffer, MyShadowCaster casters, MyShadowInternal shadowInternal)
        {
            List <Renderer> renderers = casters.Renderers;

            for (int i = 0; i < renderers.Count; i++)
            {
                commandBuffer.DrawRenderer(renderers[i], shadowInternal.CasterMaterial);
            }
        }