draw.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2022 The Ebitengine Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package colorm
  15. import (
  16. "github.com/hajimehoshi/ebiten/v2"
  17. "github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
  18. )
  19. // DrawImageOptions represents options for DrawImage.
  20. type DrawImageOptions struct {
  21. // GeoM is a geometry matrix to draw.
  22. // The default (zero) value is identity, which draws the image at (0, 0).
  23. GeoM ebiten.GeoM
  24. // Blend is a blending way of the source color and the destination color.
  25. // The default (zero) value is the regular alpha blending.
  26. Blend ebiten.Blend
  27. // Filter is a type of texture filter.
  28. // The default (zero) value is ebiten.FilterNearest.
  29. Filter ebiten.Filter
  30. }
  31. // DrawImage draws src onto dst.
  32. //
  33. // DrawImage is basically the same as ebiten.DrawImage, but with a color matrix.
  34. func DrawImage(dst, src *ebiten.Image, colorM ColorM, op *DrawImageOptions) {
  35. if op == nil {
  36. op = &DrawImageOptions{}
  37. }
  38. opShader := &ebiten.DrawRectShaderOptions{}
  39. opShader.GeoM = op.GeoM
  40. opShader.CompositeMode = ebiten.CompositeModeCustom
  41. opShader.Blend = op.Blend
  42. opShader.Uniforms = uniforms(colorM)
  43. opShader.Images[0] = src
  44. s := builtinShader(builtinshader.Filter(op.Filter), builtinshader.AddressUnsafe)
  45. dst.DrawRectShader(src.Bounds().Dx(), src.Bounds().Dy(), s, opShader)
  46. }
  47. // DrawTrianglesOptions represents options for DrawTriangles.
  48. type DrawTrianglesOptions struct {
  49. // ColorScaleMode is the mode of color scales in vertices.
  50. // The default (zero) value is ebiten.ColorScaleModeStraightAlpha.
  51. ColorScaleMode ebiten.ColorScaleMode
  52. // Blend is a blending way of the source color and the destination color.
  53. // The default (zero) value is the regular alpha blending.
  54. Blend ebiten.Blend
  55. // Filter is a type of texture filter.
  56. // The default (zero) value is ebiten.FilterNearest.
  57. Filter ebiten.Filter
  58. // Address is a sampler address mode.
  59. // The default (zero) value is ebiten.AddressUnsafe.
  60. Address ebiten.Address
  61. // FillRule indicates the rule how an overlapped region is rendered.
  62. //
  63. // The rules FileRuleNonZero and FillRuleEvenOdd are useful when you want to render a complex polygon.
  64. // A complex polygon is a non-convex polygon like a concave polygon, a polygon with holes, or a self-intersecting polygon.
  65. // See examples/vector for actual usages.
  66. //
  67. // The default (zero) value is ebiten.FillRuleFillAll.
  68. FillRule ebiten.FillRule
  69. // AntiAlias indicates whether the rendering uses anti-alias or not.
  70. // AntiAlias is useful especially when you pass vertices from the vector package.
  71. //
  72. // AntiAlias increases internal draw calls and might affect performance.
  73. // Use the build tag `ebitenginedebug` to check the number of draw calls if you care.
  74. //
  75. // The default (zero) value is false.
  76. AntiAlias bool
  77. }
  78. // DrawTriangles draws triangles onto dst.
  79. //
  80. // DrawTriangles is basically the same as ebiten.DrawTriangles, but with a color matrix.
  81. func DrawTriangles(dst *ebiten.Image, vertices []ebiten.Vertex, indices []uint16, img *ebiten.Image, colorM ColorM, op *DrawTrianglesOptions) {
  82. if op == nil {
  83. op = &DrawTrianglesOptions{}
  84. }
  85. if op.ColorScaleMode == ebiten.ColorScaleModeStraightAlpha {
  86. vs := make([]ebiten.Vertex, len(vertices))
  87. copy(vs, vertices)
  88. for i := range vertices {
  89. vs[i].ColorR *= vs[i].ColorA
  90. vs[i].ColorG *= vs[i].ColorA
  91. vs[i].ColorB *= vs[i].ColorA
  92. }
  93. vertices = vs
  94. }
  95. opShader := &ebiten.DrawTrianglesShaderOptions{}
  96. opShader.CompositeMode = ebiten.CompositeModeCustom
  97. opShader.Blend = op.Blend
  98. opShader.FillRule = op.FillRule
  99. opShader.AntiAlias = op.AntiAlias
  100. opShader.Uniforms = uniforms(colorM)
  101. opShader.Images[0] = img
  102. s := builtinShader(builtinshader.Filter(op.Filter), builtinshader.Address(op.Address))
  103. dst.DrawTrianglesShader(vertices, indices, s, opShader)
  104. }