Пример #1
0
            private void Core_OnUpdateLightSource(object sender, ShadowMapCore.UpdateLightSourceEventArgs e)
            {
                CameraCore camera = LightCamera ?? null;

                if (LightCamera == null)
                {
                    var lights = e.Context.RenderHost.PerFrameLights.Take(Constants.MaxLights);
                    foreach (var light in lights)
                    {
                        if (light.LightType == LightType.Directional)
                        {
                            var dlight = light.RenderCore as DirectionalLightCore;
                            var dir    = Vector3.TransformNormal(dlight.Direction, dlight.ModelMatrix).Normalized();
                            if (AutoCoverCompleteScene)
                            {
                                if (sceneChanged || e.Context.UpdateSceneGraphRequested || IsSceneDynamic)
                                {
                                    sceneChanged = false;
                                    var boundingBox = FindSceneBound(e.Context.RenderHost.PerFrameOpaqueNodes);
                                    if (!CreateCameraFromBound(ref boundingBox, ref dir))
                                    {
                                        SetOrthoCameraParameters(ref dir);
                                    }
                                }
                            }
                            else
                            {
                                SetOrthoCameraParameters(ref dir);
                            }
                            camera = orthoCamera;
                            break;
                        }
                        else if (light.LightType == LightType.Spot)
                        {
                            var splight = light.RenderCore as SpotLightCore;
                            persCamera.Position = (splight.Position + splight.ModelMatrix.Row4.ToVector3());
                            var look = Vector3.TransformNormal(splight.Direction, splight.ModelMatrix);
                            persCamera.LookDirection    = look;
                            persCamera.FarPlaneDistance = (float)splight.Range;
                            persCamera.FieldOfView      = (float)splight.OuterAngle;
                            persCamera.UpDirection      = Vector3.UnitZ;
                            camera = persCamera;
                            break;
                        }
                    }
                }
                if (camera == null)
                {
                    shadowCore.FoundLightSource = false;
                }
                else
                {
                    shadowCore.FoundLightSource = true;
                    shadowCore.LightView        = camera.CreateViewMatrix();
                    shadowCore.LightProjection  = camera.CreateProjectionMatrix(shadowCore.Width / shadowCore.Height);
                }
            }
Пример #2
0
        private void Core_OnUpdateLightSource(object sender, ShadowMapCore.UpdateLightSourceEventArgs e)
        {
            CameraCore camera = LightCamera ?? null;

            if (LightCamera == null)
            {
                var lights = e.Context.RenderHost.PerFrameLights.Take(Constants.MaxLights);
                foreach (var light in lights)
                {
                    if (light.LightType == LightType.Directional)
                    {
                        var dlight = light.RenderCore as DirectionalLightCore;
                        var dir    = Vector4.Transform(dlight.Direction.ToVector4(0), dlight.ModelMatrix).Normalized() * distance;
                        var pos    = -dir;
                        orthoCamera.LookDirection = new Vector3(dir.X, dir.Y, dir.Z);
                        orthoCamera.Position      = new Vector3(pos.X, pos.Y, pos.Z);
                        orthoCamera.UpDirection   = Vector3.UnitZ;
                        orthoCamera.Width         = orthoWidth;
                        camera = orthoCamera;
                        break;
                    }
                    else if (light.LightType == LightType.Spot)
                    {
                        var splight = light.RenderCore as SpotLightCore;
                        persCamera.Position = (splight.Position + splight.ModelMatrix.Row4.ToVector3());
                        var look = Vector4.Transform(splight.Direction.ToVector4(0), splight.ModelMatrix);
                        persCamera.LookDirection    = new Vector3(look.X, look.Y, look.Z);
                        persCamera.FarPlaneDistance = (float)splight.Range;
                        persCamera.FieldOfView      = (float)splight.OuterAngle;
                        persCamera.UpDirection      = Vector3.UnitZ;
                        camera = persCamera;
                        break;
                    }
                }
            }
            if (camera == null)
            {
                shadowCore.FoundLightSource = false;
            }
            else
            {
                shadowCore.FoundLightSource       = true;
                shadowCore.LightViewProjectMatrix = camera.CreateViewMatrix() * camera.CreateProjectionMatrix(shadowCore.Width / shadowCore.Height);
            }
        }