Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / map / image / ImageCanvas.cs
@小李xl 小李xl on 11 Oct 2023 8 KB 日志系统
  1.  
  2. using System;
  3. using Godot;
  4.  
  5. /// <summary>
  6. /// 静态图像画布类, 用于处理游戏中大量静态物体的解决方案<br/>
  7. /// 将物体纹理绘直接绘制到画布上, 这样大大减少GPU开销, 从而提高帧率<br/>
  8. /// 图像旋转遵循完美像素
  9. /// </summary>
  10. public partial class ImageCanvas : Sprite2D, IDestroy
  11. {
  12. /// <summary>
  13. /// 画布大小
  14. /// </summary>
  15. public int Width { get; }
  16. /// <summary>
  17. /// 画布高度
  18. /// </summary>
  19. public int Height { get; }
  20.  
  21. public bool IsDestroyed { get; private set; }
  22.  
  23. private Image _canvas;
  24. private ImageTexture _texture;
  25.  
  26. public ImageCanvas(int width, int height)
  27. {
  28. Width = width;
  29. Height = height;
  30.  
  31. _canvas = Image.Create(width, height, false, Image.Format.Rgba8);
  32. _texture = ImageTexture.CreateFromImage(_canvas);
  33. }
  34.  
  35. public override void _Ready()
  36. {
  37. Centered = false;
  38. Texture = _texture;
  39. }
  40.  
  41. /// <summary>
  42. /// 将指定纹理添加到预渲染队列中, 完成后会调用 onDrawingComplete 回调函数
  43. /// </summary>
  44. /// <param name="texture">需要渲染的纹理</param>
  45. /// <param name="material">渲染材质, 不需要则传null</param>
  46. /// <param name="x">离画布左上角x坐标</param>
  47. /// <param name="y">离画布左上角y坐标</param>
  48. /// <param name="angle">旋转角度, 角度制</param>
  49. /// <param name="centerX">旋转中心点x</param>
  50. /// <param name="centerY">旋转中心点y</param>
  51. /// <param name="flipY">是否翻转y轴</param>
  52. /// <param name="onDrawingComplete">绘制完成的回调函数</param>
  53. public void DrawImageInCanvas(Texture2D texture, Material material, float x, float y, float angle, int centerX, int centerY, bool flipY, Action onDrawingComplete = null)
  54. {
  55. DrawImageInCanvas(texture, material, x, y, angle, centerX, centerY, flipY, true, onDrawingComplete);
  56. }
  57. private void DrawImageInCanvas(Texture2D texture, Material material, float x, float y, float angle, int centerX, int centerY, bool flipY, bool enableQueueCutting, Action onDrawingComplete)
  58. {
  59. var item = new ImageRenderData();
  60. item.OnDrawingComplete = onDrawingComplete;
  61. item.EnableQueueCutting = enableQueueCutting;
  62. item.ImageCanvas = this;
  63. item.SrcImage = texture.GetImage();
  64. item.Material = material;
  65. var width = item.SrcImage.GetWidth();
  66. var height = item.SrcImage.GetHeight();
  67. if (width > 128)
  68. {
  69. Debug.LogError("警告: 图像宽度大于 128, 旋转后像素点可能绘制到画布外导致像素丢失!");
  70. }
  71. if (height > 128)
  72. {
  73. Debug.LogError("警告: 图像高度大于 128, 旋转后像素点可能绘制到画布外导致像素丢失!");
  74. }
  75. item.X = Mathf.RoundToInt(x);
  76. item.Y = Mathf.RoundToInt(y);
  77. item.Rotation = Mathf.DegToRad(Mathf.RoundToInt(Utils.ConvertAngle(angle)));
  78. item.CenterX = centerX;
  79. item.CenterY = centerY;
  80. if (item.Rotation > Mathf.Pi)
  81. {
  82. item.CenterX = width - item.CenterX;
  83. item.CenterY = height - item.CenterY;
  84. item.Rotation -= Mathf.Pi;
  85. item.RotationGreaterThanPi = true;
  86. }
  87. item.FlipY = flipY;
  88. var cosAngle = Mathf.Cos(item.Rotation);
  89. var sinAngle = Mathf.Sin(item.Rotation);
  90. if (cosAngle == 0)
  91. {
  92. cosAngle = 1e-6f;
  93. }
  94.  
  95. if (sinAngle == 0)
  96. {
  97. sinAngle = 1e-6f;
  98. }
  99.  
  100. //旋转后的图片宽高
  101. item.RenderWidth = Mathf.CeilToInt(width * Mathf.Abs(cosAngle) + height * sinAngle) + 2;
  102. item.RenderHeight = Mathf.CeilToInt(width * sinAngle + height * Mathf.Abs(cosAngle)) + 2;
  103.  
  104. if (item.RenderWidth >= RenderViewportSize.X)
  105. {
  106. Debug.LogError($"图像旋转后的宽度大于{RenderViewportSize.X}, 不支持绘制到 ImageCanvas 下!");
  107. item.SrcImage.Dispose();
  108. return;
  109. }
  110. //旋转后的图像中心点偏移
  111. item.RenderOffsetX =
  112. (int)((item.CenterX / sinAngle +
  113. (0.5f * item.RenderWidth * sinAngle - 0.5f * item.RenderHeight * cosAngle +
  114. 0.5f * height) /
  115. cosAngle -
  116. (-0.5f * item.RenderWidth * cosAngle - 0.5f * item.RenderHeight * sinAngle +
  117. 0.5f * width) /
  118. sinAngle - item.CenterY / cosAngle) /
  119. (cosAngle / sinAngle + sinAngle / cosAngle)) + 1;
  120. item.RenderOffsetY =
  121. (int)((item.CenterX / cosAngle -
  122. (-0.5f * item.RenderWidth * cosAngle - 0.5f * item.RenderHeight * sinAngle + 0.5f * width) /
  123. cosAngle -
  124. (0.5f * item.RenderWidth * sinAngle - 0.5f * item.RenderHeight * cosAngle + 0.5f * height) /
  125. sinAngle + item.CenterY / sinAngle) /
  126. (sinAngle / cosAngle + cosAngle / sinAngle)) + 1;
  127.  
  128. _queueItems.Add(item);
  129. }
  130. /// <summary>
  131. /// 将指定 ActivityObject 添加到预渲染队列中, 完成后会调用 onDrawingComplete 回调函数
  132. /// </summary>
  133. /// <param name="activityObject">物体实例</param>
  134. /// <param name="x">离画布左上角x坐标</param>
  135. /// <param name="y">离画布左上角y坐标</param>
  136. /// <param name="onDrawingComplete">绘制完成的回调</param>
  137. public void DrawActivityObjectInCanvas(ActivityObject activityObject, float x, float y, Action onDrawingComplete = null)
  138. {
  139. //是否翻转y轴
  140. var flipY = activityObject.Scale.Y < 0;
  141.  
  142. var animatedSprite = activityObject.AnimatedSprite;
  143. var animatedSpritePosition = animatedSprite.Position;
  144. var ax = x + animatedSpritePosition.X;
  145. var ay = y + animatedSpritePosition.X;
  146.  
  147. //先绘制阴影
  148. var shadowSprite = activityObject.ShadowSprite;
  149. var shadowSpriteTexture = activityObject.ShadowSprite.Texture;
  150. if (shadowSpriteTexture != null)
  151. {
  152. var spriteOffset = shadowSprite.Offset;
  153. var centerX = (int)-spriteOffset.X;
  154. var centerY = (int)-spriteOffset.Y;
  155. var angle = Utils.ConvertAngle(shadowSprite.GlobalRotationDegrees);
  156. if (shadowSprite.Centered)
  157. {
  158. centerX += shadowSpriteTexture.GetWidth() / 2;
  159. centerY += shadowSpriteTexture.GetHeight() / 2;
  160. }
  161. DrawImageInCanvas(
  162. shadowSprite.Texture, shadowSprite.Material,
  163. ax + activityObject.ShadowOffset.X, ay + activityObject.ShadowOffset.Y,
  164. angle,
  165. centerX, centerY, flipY,
  166. true, null
  167. );
  168. }
  169. //再绘制纹理
  170. var texture = activityObject.GetCurrentTexture();
  171. if (texture != null)
  172. {
  173. var spriteOffset = animatedSprite.Offset;
  174. var centerX = (int)-spriteOffset.X;
  175. var centerY = (int)-spriteOffset.Y;
  176. if (animatedSprite.Centered)
  177. {
  178. centerX += texture.GetWidth() / 2;
  179. centerY += texture.GetHeight() / 2;
  180. }
  181. //为了保证阴影在此之前渲染, 所以必须关闭插队渲染
  182. DrawImageInCanvas(
  183. texture, animatedSprite.Material,
  184. ax, ay,
  185. animatedSprite.GlobalRotationDegrees,
  186. centerX, centerY, flipY,
  187. false, onDrawingComplete
  188. );
  189. }
  190. }
  191. public void Destroy()
  192. {
  193. if (IsDestroyed)
  194. {
  195. return;
  196. }
  197.  
  198. IsDestroyed = true;
  199. QueueFree();
  200. _canvas.Dispose();
  201. _texture.Dispose();
  202. }
  203.  
  204. /// <summary>
  205. /// 使用透明色替换掉整个画布
  206. /// </summary>
  207. public void Clear()
  208. {
  209. Clear(new Color(0, 0, 0, 0));
  210. }
  211.  
  212. /// <summary>
  213. /// 使用指定颜色替换掉整个画布
  214. /// </summary>
  215. public void Clear(Color color)
  216. {
  217. _canvas.Fill(color);
  218. Redraw();
  219. }
  220. private void Redraw()
  221. {
  222. _texture.Update(_canvas);
  223. }
  224. }