Beispiel #1
0
        public void findShadowHull(Texture2D texture)
        {
            //Create an array to hold the data from the texture
            uint[] data = new uint[texture.Width * texture.Height];

            //Transfer the texture data to the array
            texture.GetData(data);

            //Find the vertices that makes up the outline of the shape in the texture
            textureVertices = PolygonTools.CreatePolygon(data, texture.Width, false);

            //We simplify the vertices found in the texture.
            textureVertices = SimplifyTools.DouglasPeuckerSimplify(textureVertices, 0.5f);

            //Since it is a concave polygon, we need to partition it into several smaller convex polygons
            vertices = BayazitDecomposer.ConvexPartition(textureVertices);

            _scale = 1f;

            //scale the vertices from graphics space to sim space
            foreach (Vertices vertex in vertices)
            {
                Vector2[] verticesArray = vertex.ToArray();
                var       hull          = ShadowHull.CreateConvex(ref verticesArray);
                hulls.Add(hull);
                krypton.Hulls.Add(hull);
            }
        }
Beispiel #2
0
        public void findShadowHull(Texture2D texture)
        {
            //Create an array to hold the data from the texture
            uint[] data = new uint[texture.Width * texture.Height];

            //Transfer the texture data to the array
            texture.GetData(data);

            //Find the vertices that makes up the outline of the shape in the texture
            textureVertices = PolygonTools.CreatePolygon(data, texture.Width, false);

            //The tool return vertices as they were found in the texture.
            //We need to find the real center (centroid) of the vertices for 2 reasons:

            //1. To translate the vertices so the polygon is centered around the centroid.
            Vector2 centroid = -textureVertices.GetCentroid();

            textureVertices.Translate(ref centroid);

            //2. To draw the texture the correct place.
            _origin = -centroid;

            //We simplify the vertices found in the texture.
            textureVertices = SimplifyTools.CollinearSimplify(textureVertices, 0f);

            //Since it is a concave polygon, we need to partition it into several smaller convex polygons
            list = BayazitDecomposer.ConvexPartition(textureVertices);

            //Adjust the scale of the object for WP7's lower resolution
            //Adjust the scale of the object for WP7's lower resolution
            #if WINDOWS_PHONE
            _scale = 0.6f;
            #else
            _scale = 1f;
            #endif

            //scale the vertices from graphics space to sim space
            Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * _scale;
            foreach (Vertices vertices in list)
            {
                vertices.Scale(ref vertScale);
                Vector2[] verticesArray = vertices.ToArray();
                var       hull          = ShadowHull.CreateConvex(ref verticesArray);
                hulls.Add(hull);
                krypton.Hulls.Add(hull);
            }
        }
Beispiel #3
0
        public void AddShadowForObject(Texture2D texture, Vector2 position)
        {
            uint[] data = new uint[texture.Width * texture.Height];
            texture.GetData <uint>(data);

            foreach (var poly in EarclipDecomposer.ConvexPartition(PolygonTools.CreatePolygon(data, texture.Width)))
            {
                if (poly.Count >= 3)
                {
                    var array = poly.ToArray();

                    var hull = ShadowHull.CreateConvex(ref array);
                    hull.Position = position;

                    _krypton.Hulls.Add(hull);
                }
            }
        }
Beispiel #4
0
        void rebuild( )
        {
            _krypton.Lights.Clear(  );
            _krypton.Hulls.Clear(  );

            LayerEditor activeLayer = IoC.Model.ActiveLayer;

            if (activeLayer == null)
            {
                return;
            }

            activeLayer.OfType <LightEditor>( ).ForEach(
                light =>
            {
                var props = (LightProperties)light.ItemProperties;

                Texture2D texture;
                if (props.TypeOfLight == TypeOfLight.Point)
                {
                    texture = LightTextureBuilder.CreatePointLight(_game.GraphicsDevice, props.TextureSize);
                }
                else
                {
                    texture = LightTextureBuilder.CreateConicLight(_game.GraphicsDevice, props.TextureSize, props.FieldOfView);
                }

                if (texture == null)
                {
                    throw new InvalidOperationException(@"Cannot create light of type {0}.".FormatWith(props.TypeOfLight));
                }

                _krypton.Lights.Add(new Light2D
                {
                    Angle      = props.Rotation,
                    Color      = props.Color,
                    Intensity  = props.Intensity,
                    Fov        = props.FieldOfView,
                    Position   = props.Position,
                    IsOn       = props.IsOn,
                    Range      = props.Range,
                    ShadowType = convertShadowType(props.ShadowType),
                    Texture    = texture
                });
            });

            activeLayer.OfType <RectangularHullEditor>( ).ForEach(
                light =>
            {
                var props = (RectangularHullProperties)light.ItemProperties;

                ShadowHull shadowHull = ShadowHull.CreateRectangle(new Vector2(props.Width, props.Height));
                shadowHull.Angle      = props.Rotation;
                shadowHull.Opacity    = props.Opacity;
                shadowHull.Position   = props.Position + new Vector2(props.Width / 2, props.Height / 2);
                shadowHull.Scale      = props.Scale;
                shadowHull.Visible    = props.Visible;

                _krypton.Hulls.Add(shadowHull);
            });

            activeLayer.OfType <CircularHullEditor>( ).ForEach(
                light =>
            {
                var props = (CircularHullProperties)light.ItemProperties;

                ShadowHull shadowHull = ShadowHull.CreateCircle(props.Radius, props.Sides);
                shadowHull.Opacity    = props.Opacity;
                shadowHull.Position   = props.Position;
                shadowHull.Scale      = props.Scale;
                shadowHull.Visible    = props.Visible;

                _krypton.Hulls.Add(shadowHull);
            });

            activeLayer.OfType <ConvexHullEditor>( ).ForEach(
                light =>
            {
                var props = (ConvexHullProperties)light.ItemProperties;

                Vector2[] worldPoints = props.WorldPoints.ToArray( );

                // === DON'T SET THE POSITION!! ===
                ShadowHull shadowHull = ShadowHull.CreateConvex(ref worldPoints);
                shadowHull.Opacity    = props.Opacity;
                shadowHull.Visible    = props.Visible;

                _krypton.Hulls.Add(shadowHull);
            });

            activeLayer.OfType <PreShapedConvexHullEditor>( ).ForEach(
                light =>
            {
                var props = (ConvexHullProperties)light.ItemProperties;

                Vector2[] worldPoints = props.WorldPoints.ToArray( );

                // === DON'T SET THE POSITION!! ===
                ShadowHull shadowHull = ShadowHull.CreateConvex(ref worldPoints);
                shadowHull.Opacity    = props.Opacity;
                shadowHull.Visible    = props.Visible;

                _krypton.Hulls.Add(shadowHull);
            });
        }
Beispiel #5
0
        private Body LevelFromTexture(Texture2D Texture, ref Vector2 origin)
        {
            // Get the world
            World World = GamePlayManager.WorldManager.World;

            World.Gravity = Vector2.Zero;
            _scale        = 1.15f;

            // Create an array to hold the data from the texture
            uint[] data = new uint[Texture.Width * Texture.Height];

            // Transfer the texture data to the array
            Texture.GetData(data);

            // Find the vertices that makes up the outline of the shape in the texture
            Vertices textureVertices = PolygonTools.CreatePolygon(data, Texture.Width, false);

            // The tool return vertices as they were found in the texture.
            // We need to find the real center (centroid) of the vertices for 2 reasons:
            // 1. To translate the vertices so the polygon is centered around the centroid.
            // 2. To draw the texture and lighting the correct place.

            Vector2 centroid = -textureVertices.GetCentroid();

            textureVertices.Translate(ref centroid);

            Vector2 textureCenter = new Rectangle(0, 0, Texture.Width, Texture.Height).Center.ToVector2();

            origin = -centroid;

            Vector2 offset = -(textureCenter - origin) * _scale;

            // We simplify the vertices found in the texture.
            textureVertices = SimplifyTools.ReduceByDistance(textureVertices, 4f);

            // Since it is a concave polygon, we need to partition it into several smaller convex polygons
            // -- EarclipDecomposer - 303 polygons - 60fps on XBOX - 7835 Milliseconds on XBOX
            // -- BayazitDecomposer - 108 polygons - 60fps on XBOX -  222 Milliseconds on XBOX
            List <Vertices> list = BayazitDecomposer.ConvexPartition(textureVertices);

            // Scale the vertices from graphics space to sim space
            Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * _scale;

            foreach (Vertices vertices in list)
            {
                vertices.Scale(ref vertScale);
            }

            // Create a single body with multiple fixtures
            Body compound = BodyFactory.CreateCompoundPolygon(World, list, 1f, BodyType.Static);

            compound.BodyType = BodyType.Static;
            compound.Position = ConvertUnits.ToSimUnits(MazeOffset + offset);

            // Add to krypton
            foreach (Vertices vertices in list)
            {
                Vector2[] points = new Vector2[vertices.Count];
                int       i      = vertices.Count;
                foreach (Vector2 vector in vertices)
                {
                    i--;
                    points[i] = ConvertUnits.ToDisplayUnits(vector) + offset + MazeOffset;
                }
                ShadowHull hull = ShadowHull.CreateConvex(ref points);
                KryptonEngine.Hulls.Add(hull);
            }

            new Mob(GamePlayManager.MobManager, new FarseerPhysicalBody(compound))
            {
                AnimatedSprite = new AnimatedSprite(@"Blank")
            };

            return(compound);
        }