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); } }
private bool IsHullWithinRange(ShadowHull hull) { var offset = hull.Position - Position; var distance = hull.MaxRadius * Math.Max(hull.Scale.X, hull.Scale.Y) + Range; return(offset.X * offset.X + offset.Y * offset.Y < distance * distance); }
/// <summary> /// Remove ShadowHull /// </summary> public void RemoveHull() { LightingEngine LE = ((LightingEngine)Renderer.GetRenderEffect("LightingEngine")); LE.RemoveHull(Hull); this._Hull = null; }
public void CreateRectangleShadowHull(Vector2 position, Rectangle rectangle) { ShadowHull shadowHull = ShadowHull.CreateRectangle(new Vector2(rectangle.Width, rectangle.Height)); shadowHull.Position = position; Engine.Lighting.Krypton.Hulls.Add(shadowHull); }
public void CreateShadowHulls() { var hull2 = ShadowHull.CreateRectangle(new Vector2(7, 7)); hull2.Position.X = 30; hull2.Position.Y = -20; hull2.Scale.X = (float)(1); hull2.Scale.Y = (float)(1); krypton.Hulls.Add(hull2); }
/// <summary> /// Set ShadowHull of Tile /// </summary> /// <param name="Size">Size</param> /// <param name="Pos">Position</param> public void SetRectHull(Vector2 Size, Vector2 Pos) { ShadowHull H = ShadowHull.CreateRectangle(Size); H.Position = Pos; LightingEngine LE = ((LightingEngine)Renderer.GetRenderEffect("LightingEngine")); if (Hull != null) { LE.RemoveHull(Hull); } LE.AddHull(H); this._Hull = H; }
public Platform(Rectangle platformBounds, TileCollision collision, Boolean isShadowCaster, KryptonEngine krypton) { this.krypton = krypton; Collision = collision; IsShadowCaster = isShadowCaster; Bounds = platformBounds; if (isShadowCaster) { ShadowHull shadowHull = ShadowHull.CreateRectangle(new Vector2(platformBounds.Width, platformBounds.Height)); shadowHull.Position.X = platformBounds.X + platformBounds.Width / 2; shadowHull.Position.Y = platformBounds.Y + platformBounds.Height / 2; krypton.Hulls.Add(shadowHull); } }
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); } }
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); } } }
public bool InShadowRange(ShadowHull hull, SpriteOrigin so = SpriteOrigin.SpriteCenter) { Vector2 distance; if (so == SpriteOrigin.SpriteCenter) { distance = hull.Position - Center; } else if (so == SpriteOrigin.SpritePosition) { distance = hull.Position - Position; } else { distance = hull.Position - Origin; } return(distance.Length() < this.lightingRange); }
public ShadowHull CreateHull(float radius, float angle, Vector2 position, Vector2 scale, bool circlular = false) { ShadowHull hull; if (!circlular) { hull = ShadowHull.CreateRectangle(Vector2.One); } else { hull = ShadowHull.CreateCircle(radius, 20); } hull.Position = position; hull.Scale = scale; hull.Angle = angle; hull.MaxRadius = radius; Krypton.Hulls.Add(hull); return(hull); }
private void CreateHulls(int x, int y) { float w = 50; float h = 50; // Make lines of lines of hulls! for (int j = 0; j < y; j++) { // Make lines of hulls! for (int i = 0; i < x; i++) { var posX = (((i + 0.5f) * w) / x) - w / 2 + (j % 2 == 0 ? w / x / 2 : 0); var posY = (((j + 0.5f) * h) / y) - h / 2; // +(i % 2 == 0 ? h / y / 4 : 0); var hull = ShadowHull.CreateRectangle(Vector2.One * 1f); hull.Position.X = posX; hull.Position.Y = posY; hull.Scale.X = (float)(this.mRandom.NextDouble() * 0.75f + 0.25f); hull.Scale.Y = (float)(this.mRandom.NextDouble() * 0.75f + 0.25f); krypton.Hulls.Add(hull); } } }
public static ShadowHull BuildHull(float size) { return(ShadowHull.CreateCircle(size, 20)); }
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); }); }
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); }
public void RemoveHull(ShadowHull Hull) { Krypton.Hulls.Remove(Hull); }
public void AddHull(ShadowHull Hull) { Krypton.Hulls.Add(Hull); }
public static ShadowHull BuildHull(float width, float height) { return(ShadowHull.CreateRectangle(new Vector2(width, height))); }