/////////////////////////////////////////////////////////////////////////// /// <summary> /// Creates new vision shape. /// </summary> /// <param name="fow"></param> /// <returns></returns> FogShape CreateShape(FogManager fow) { if (ShapeType == FogShapeType.Circle) { FogCircle shape = new FogCircle(); FillShape(fow, shape); shape.InnerRadius = InnerRadius; shape.Angle = Angle; return(shape); } else if (ShapeType == FogShapeType.Texture) { if (Texture == null) { return(null); } FogShapeTexture shape = new FogShapeTexture(); FillShape(fow, shape); shape.Texture = Texture; shape.RotateToForward = RotateToForward; return(shape); } return(null); }
/////////////////////////////////////////////////////////////////////////// void DrawRotatedTexture(FogShapeTexture shape) { if (shape.Texture == null) { return; } // convert size to fog space float size = new Vector2(shape.Radius, shape.Radius).magnitude *_Map.PixelSize; Vector2 sizemul = new Vector2(size / (shape.Radius * _Map.PixelSize), size / (shape.Radius * _Map.PixelSize)); float fogradius = size; DrawInfo info = new DrawInfo(_Map, shape, size, size); // rotation stuff float sin = Mathf.Sin(info.forwardAngle); float cos = Mathf.Cos(info.forwardAngle); for (int y = info.yMin; y < info.yMax; ++y) { for (int x = info.xMin; x < info.xMax; ++x) { // get rotated uvs float u = Mathf.InverseLerp(info.xMin, info.xMax, x) - 0.5f; float v = Mathf.InverseLerp(info.yMin, info.yMax, y) - 0.5f; float uu = (u * cos - v * sin) * sizemul.x + 0.5f; float vv = (v * cos + u * sin) * sizemul.y + 0.5f; if (uu < 0 || uu >= 1 || vv < 0 || vv >= 1) { continue; } // can see pixel Vector2_1 offset = new Vector2_1(x, y) - info.fogEyePos; if (!LineOfSightCanSee(shape, offset.vector2, fogradius)) { continue; } if (!LineOfSightCanSeeCell(shape, offset)) { continue; } // read texture Unfog(x, y, SampleTexture(shape.Texture, uu, vv)); } } }
/////////////////////////////////////////////////////////////////////////// protected override void DrawTexture(FogShapeTexture shape) { if (shape.Texture == null) { return; } if (shape.RotateToForward) { DrawRotatedTexture(shape); return; } // convert size to fog space float fogradius = shape.Radius * _Map.PixelSize; DrawInfo info = new DrawInfo(_Map, shape, shape.Radius * _Map.PixelSize, shape.Radius * _Map.PixelSize); for (int y = info.yMin; y <= info.yMax; ++y) { for (int x = info.xMin; x <= info.xMax; ++x) { // can see pixel Vector2_1 offset = new Vector2_1(x, y) - info.fogEyePos; if (!LineOfSightCanSee(shape, offset.vector2, fogradius)) { continue; } if (!LineOfSightCanSeeCell(shape, offset)) { continue; } // read texture float u = Mathf.InverseLerp(info.xMin, info.xMax, x); float v = Mathf.InverseLerp(info.yMin, info.yMax, y); Unfog(x, y, SampleTexture(shape.Texture, u, v)); } } }
protected abstract void DrawTexture(FogShapeTexture texture);