Exemplo n.º 1
0
        private void CalculateProjectionData(CharacterShadowCommonSettingsComponent settings, ICollection <CharacterShadowNode> 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 (CharacterShadowNode node in characters)
            {
                int  yIndex    = num3 / countX;
                int  xIndex    = num3 - (yIndex * countX);
                Rect atlasData = CalculateLocationInAtlas(countX, countY, xIndex, yIndex);
                CharacterShadowInternalComponent characterShadowInternal = node.characterShadowInternal;
                Bounds projectionBoundInLightSpace = characterShadowInternal.ProjectionBoundInLightSpace;
                this.SetProjectorData(characterShadowInternal, localToWorldMatrix.MultiplyPoint3x4(projectionBoundInLightSpace.center), rotation, node.characterShadow, 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);
                characterShadowInternal.CasterMaterial.SetMatrix("ViewToAtlas", GL.GetGPUProjectionMatrix(matrixx7 * matrixx6, true));
                num3++;
            }
        }
Exemplo n.º 2
0
        private void UpdateBoundsAndCullCharacters(ICollection <CharacterShadowNode> collector, ICollection <CharacterShadowNode> characters, CameraComponent cameraComponent, CharacterShadowCommonSettingsComponent settings)
        {
            Matrix4x4 localToWorldMatrix = settings.virtualLight.localToWorldMatrix;
            Matrix4x4 worldToLocalMatrix = settings.virtualLight.worldToLocalMatrix;

            Plane[] planes = GeometryUtility.CalculateFrustumPlanes((cameraComponent.ProjectionMatrix * cameraComponent.WorldToCameraMatrix) * localToWorldMatrix);
            int     num    = 0;
            int     maxCharactersCountInAtlas = settings.MaxCharactersCountInAtlas;

            foreach (CharacterShadowNode node in characters)
            {
                bool   flag = num < maxCharactersCountInAtlas;
                Bounds boundsInLightSpace = BoundsUtils.TransformBounds(node.characterShadowCasters.BoundsInWorldSpace, worldToLocalMatrix);
                CharacterShadowComponent         characterShadow         = node.characterShadow;
                CharacterShadowInternalComponent characterShadowInternal = node.characterShadowInternal;
                characterShadowInternal.ProjectionBoundInLightSpace = this.CalculateProjectionBoundInLightSpace(settings, boundsInLightSpace, characterShadow.offset);
                Bounds projectionBoundInLightSpace = characterShadowInternal.ProjectionBoundInLightSpace;
                projectionBoundInLightSpace.max += new Vector3(0f, 0f, characterShadow.attenuation);
                flag &= GeometryUtility.TestPlanesAABB(planes, projectionBoundInLightSpace);
                characterShadowInternal.Projector.enabled = flag;
                if (flag)
                {
                    collector.Add(node);
                }
                num++;
            }
        }
Exemplo n.º 3
0
        private void CalculateShadowInternalData(CharacterShadowInternalComponent shadowInternalData, CharacterShadowComponent characterShadow, CharacterShadowCommonSettingsComponent shadowCommonSettings)
        {
            Projector projector = this.CreateProjector(shadowCommonSettings);

            shadowInternalData.Projector      = projector;
            shadowInternalData.BaseAlpha      = characterShadow.color.a;
            shadowInternalData.CasterMaterial = new Material(shadowCommonSettings.casterShader);
        }
Exemplo n.º 4
0
        public void CleanInternalShadowData(NodeRemoveEvent evt, CharacterShadowNode node)
        {
            CharacterShadowInternalComponent characterShadowInternal = node.characterShadowInternal;
            GameObject gameObject = characterShadowInternal.Projector.gameObject;

            Object.Destroy(characterShadowInternal.Projector.material);
            Object.Destroy(characterShadowInternal.Projector);
            Object.Destroy(characterShadowInternal.CasterMaterial);
            Object.Destroy(gameObject);
            characterShadowInternal.Projector      = null;
            characterShadowInternal.CasterMaterial = null;
        }
Exemplo n.º 5
0
 public void Init(NodeAddedEvent evt, NotInitedCharacterShadowNode node, [JoinAll] SingleNode <CharacterShadowCommonSettingsComponent> characterShadowCommonSettings)
 {
     if (!node.characterShadowCasters.HasBounds)
     {
         LoggerProvider.GetLogger(this).Error("Incorrect character shadow elements on gameObject " + node.characterShadow.gameObject.name);
     }
     else
     {
         CharacterShadowInternalComponent shadowInternalData = new CharacterShadowInternalComponent();
         this.CalculateShadowInternalData(shadowInternalData, node.characterShadow, characterShadowCommonSettings.component);
         node.Entity.AddComponent(shadowInternalData);
     }
 }
Exemplo n.º 6
0
        private void SetProjectorData(CharacterShadowInternalComponent 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;
        }
Exemplo n.º 7
0
        private void GenerateDrawCharacterCommands(CommandBuffer commandBuffer, CharacterShadowCastersComponent casters, CharacterShadowInternalComponent shadowInternal)
        {
            List <Renderer> renderers = casters.Renderers;

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