Esempio n. 1
0
        public static void CreateShadowMapForAllLights(Scene scene)
        {
            if (scene == null)
            {
                throw new ArgumentNullException("scene");
            }

            Vector3 maxWorld = new Vector3(float.MinValue, float.MinValue, float.MinValue);
            Vector3 minWorld = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);

            foreach (var n in scene.Geometries)
            {
                FrameSkin skin = n.Component as FrameSkin;
                if (skin != null)
                {
                    TransformSkin(skin.Skin, ref minWorld, ref maxWorld, Matrix.Identity);
                }

                FrameMesh meshContainer = n.Component as FrameMesh;
                if (meshContainer != null)
                {
                    Mesh mesh         = meshContainer.Mesh;
                    var  globalPose   = n.GlobalPose;
                    var  positionView = mesh.GetVertexBufferView <Vector3>(IASemantic.Position);
                    for (int i = 0; i < positionView.Count; i++)
                    {
                        var posWorld = Vector3.TransformCoordinates(positionView[i], globalPose);
                        maxWorld = Vector3.Max(posWorld, maxWorld);
                        minWorld = Vector3.Min(posWorld, minWorld);
                    }

                    mesh.ReleaseViews();
                }
            }

            foreach (var lightInstance in scene.FrameLights)
            {
                lightInstance.CreateShadowMap(scene, EngineState.Shadow.ShadowMapping.Size, true, maxWorld, minWorld);
            }


            if (!EngineState.Shadow.ShadowMapping.Enable)
            {
                EngineState.Shadow.ShadowMapping.Enable = true;
                EngineState.Shadow.Enable = true;
            }
        }
Esempio n. 2
0
        public ShadowMapTechnique CreateShadowMap(Scene scene, int size, bool isDynamic, Vector3 maxWorld, Vector3 minWorld)
        {
            if (scene == null)
            {
                throw new NullReferenceException("Scene");
            }

            var sm = new ShadowMapTechnique(size)
            {
                IsDynamic = isDynamic
            };
            var viewMat = Matrix.LookAt(_globalPosition, _globalPosition + _globalDirection, Vector3.UnitY);
            var camera  = new Camera();

            camera.View = viewMat;

            if (_light.Type == LightType.Directional)
            {
                var   centerWorld = 0.5f * (maxWorld + minWorld);
                float distance    = Vector3.Distance(minWorld, maxWorld) * 0.5f;
                var   newPosition = centerWorld - _globalDirection * distance;
                camera.Position = newPosition;
                viewMat         = camera.View;
            }

            Vector3 maxView = new Vector3(float.MinValue, float.MinValue, float.MinValue);
            Vector3 minView = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);

            foreach (var n in scene.Geometries)
            {
                var meshContainer = n.Component as FrameMesh;
                if (meshContainer != null)
                {
                    Mesh mesh         = meshContainer.Mesh;
                    var  globalPose   = n.GlobalPose;
                    var  pose         = globalPose * viewMat;
                    var  positionView = mesh.GetVertexBufferView <Vector3>(IASemantic.Position);

                    for (int i = 0; i < positionView.Count; i++)
                    {
                        var pos     = positionView[i];
                        var posView = Vector3.TransformCoordinates(pos, pose);

                        maxView = Vector3.Max(posView, maxView);
                        minView = Vector3.Min(posView, minView);
                    }

                    mesh.ReleaseViews();
                }

                FrameSkin skin = n.Component as FrameSkin;
                if (skin != null)
                {
                    TransformSkin(skin.Skin, ref minView, ref maxView, viewMat);
                }
            }

            camera.ZNear       = Math.Max(1, minView.Z);
            camera.ZFar        = Math.Max(2, maxView.Z);
            camera.OrthoWidth  = maxView.X - minView.X;
            camera.OrthoHeight = maxView.Y - minView.Y;
            camera.FieldOfView = Numerics.PIover2;

            camera.Type    = ProjectionType.Orthographic;
            sm.Camera      = camera;
            sm.Affector    = Node;
            Node.Technique = sm;

            sm.OnSceneAttach(scene);

            return(sm);
        }