Newer
Older
DungeonShooting / DungeonShooting_Godot / src / test / TestDrawSprite.cs
@小李xl 小李xl on 2 Nov 2023 4 KB 优化可互动物体
  1. using Godot;
  2. using System;
  3. using System.Collections.Generic;
  4.  
  5. public partial class TestDrawSprite : Node2D
  6. {
  7. [Export]
  8. public Label FpsLabel;
  9.  
  10. [Export]
  11. public PathFollow2D PathFollow2D;
  12.  
  13. public override void _Ready()
  14. {
  15. // for (var i = 0; i < 10000; i++)
  16. // {
  17. // var image = Image.Create(50, 50, false, Image.Format.Rgba8);
  18. // image.Fill(new Color(Utils.Random.RandomRangeFloat(0, 1), Utils.Random.RandomRangeFloat(0, 1),
  19. // Utils.Random.RandomRangeFloat(0, 1), Utils.Random.RandomRangeFloat(0, 1)));
  20. // var imageTexture = ImageTexture.CreateFromImage(image);
  21. // var proxySprite = new ProxySprite();
  22. // proxySprite.SetTexture(
  23. // this,
  24. // imageTexture,
  25. // new Vector2(Utils.Random.RandomRangeInt(0, 1600), Utils.Random.RandomRangeInt(0, 900)),
  26. // Utils.Random.RandomRangeFloat(0, Mathf.Pi),
  27. // new Vector2(Utils.Random.RandomRangeFloat(0.2f, 2f), Utils.Random.RandomRangeFloat(0.2f, 2f)),
  28. // new Vector2(0, 5)
  29. // );
  30. // }
  31. }
  32.  
  33. public override void _Process(double delta)
  34. {
  35. FpsLabel.Text = "FPS: " + 1 / delta;
  36. PathFollow2D.Progress += 200 * (float)delta;
  37. }
  38.  
  39. //-------------------------------------------------------------------------------------------------------------------------------
  40. //使用sprite2d绘制精灵
  41. // public override void _Ready()
  42. // {
  43. // for (int i = 0; i < 20000; i++)
  44. // {
  45. // var image = Image.Create(50, 50, false, Image.Format.Rgba8);
  46. // image.Fill(new Color(Utils.Random.RandomRangeFloat(0, 1), Utils.Random.RandomRangeFloat(0, 1), Utils.Random.RandomRangeFloat(0, 1), Utils.Random.RandomRangeFloat(0, 1)));
  47. // var imageTexture = ImageTexture.CreateFromImage(image);
  48. // var sprite2D = new Sprite2D();
  49. // sprite2D.Texture = imageTexture;
  50. // sprite2D.Centered = false;
  51. // sprite2D.Scale = new Vector2(Utils.Random.RandomRangeFloat(0.2f, 2f), Utils.Random.RandomRangeFloat(0.2f, 2f));
  52. // sprite2D.Rotation = Utils.Random.RandomRangeFloat(0, Mathf.Pi);
  53. // sprite2D.Position = new Vector2(Utils.Random.RandomRangeInt(0, 1600), Utils.Random.RandomRangeInt(0, 900));
  54. // AddChild(sprite2D);
  55. // }
  56. // }
  57. //尝试使用DrawTexture绘制texture,结果发现性比sprite2d还差
  58. // private class DrawTextureData
  59. // {
  60. // public Texture2D Texture2D;
  61. // public Vector2 Position;
  62. // public float Rotation;
  63. // public Vector2 Scale;
  64. // }
  65. // private List<DrawTextureData> _texture2Ds = new List<DrawTextureData>();
  66. // public override void _Ready()
  67. // {
  68. // for (int i = 0; i < 10000; i++)
  69. // {
  70. // var image = Image.Create(100, 100, false, Image.Format.Rgba8);
  71. // image.Fill(new Color(Utils.Random.RandomRangeFloat(0, 1), Utils.Random.RandomRangeFloat(0, 1), Utils.Random.RandomRangeFloat(0, 1), Utils.Random.RandomRangeFloat(0, 1)));
  72. // var imageTexture = ImageTexture.CreateFromImage(image);
  73. // _texture2Ds.Add(new DrawTextureData()
  74. // {
  75. // Texture2D = imageTexture,
  76. // Position = new Vector2(Utils.Random.RandomRangeInt(0, 1600), Utils.Random.RandomRangeInt(0, 900)),
  77. // Rotation = Utils.Random.RandomRangeFloat(0, Mathf.Pi),
  78. // Scale = new Vector2(Utils.Random.RandomRangeFloat(0.2f, 2f), Utils.Random.RandomRangeFloat(0.2f, 2f))
  79. // });
  80. // }
  81. // }
  82. //
  83. // public override void _Process(double delta)
  84. // {
  85. // QueueRedraw();
  86. // }
  87. //
  88. // public override void _Draw()
  89. // {
  90. // foreach (var texture2D in _texture2Ds)
  91. // {
  92. // DrawSetTransform(texture2D.Position, texture2D.Rotation, texture2D.Scale);
  93. // DrawTexture(texture2D.Texture2D, Vector2.Zero);
  94. // }
  95. // }
  96. }