sprite.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package graphic
  5. import (
  6. "github.com/g3n/engine/core"
  7. "github.com/g3n/engine/geometry"
  8. "github.com/g3n/engine/gls"
  9. "github.com/g3n/engine/material"
  10. "github.com/g3n/engine/math32"
  11. )
  12. // Sprite is a potentially animated image positioned in space that always faces the camera.
  13. type Sprite struct {
  14. Graphic // Embedded graphic
  15. uniMVPM gls.Uniform // Model view projection matrix uniform location cache
  16. }
  17. // NewSprite creates and returns a pointer to a sprite with the specified dimensions and material
  18. func NewSprite(width, height float32, imat material.IMaterial) *Sprite {
  19. s := new(Sprite)
  20. // Creates geometry
  21. geom := geometry.NewGeometry()
  22. w := width / 2
  23. h := height / 2
  24. // Builds array with vertex positions and texture coordinates
  25. positions := math32.NewArrayF32(0, 12)
  26. positions.Append(
  27. -w, -h, 0, 0, 0,
  28. w, -h, 0, 1, 0,
  29. w, h, 0, 1, 1,
  30. -w, h, 0, 0, 1,
  31. )
  32. // Builds array of indices
  33. indices := math32.NewArrayU32(0, 6)
  34. indices.Append(0, 1, 2, 0, 2, 3)
  35. // Set geometry buffers
  36. geom.SetIndices(indices)
  37. geom.AddVBO(
  38. gls.NewVBO(positions).
  39. AddAttrib(geometry.VertexPosition, 3).
  40. AddAttrib(geometry.VertexTexcoord, 2),
  41. )
  42. s.Graphic.Init(geom, gls.TRIANGLES)
  43. s.AddMaterial(s, imat, 0, 0)
  44. s.uniMVPM.Init("MVP")
  45. return s
  46. }
  47. // RenderSetup sets up the rendering of the sprite.
  48. func (s *Sprite) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
  49. // Calculates model view matrix
  50. mw := s.MatrixWorld()
  51. var mvm math32.Matrix4
  52. mvm.MultiplyMatrices(&rinfo.ViewMatrix, &mw)
  53. // Decomposes model view matrix
  54. var position math32.Vector3
  55. var quaternion math32.Quaternion
  56. var scale math32.Vector3
  57. mvm.Decompose(&position, &quaternion, &scale)
  58. // Removes any rotation in X and Y axes and compose new model view matrix
  59. rotation := s.Rotation()
  60. rotation.X = 0
  61. rotation.Y = 0
  62. quaternion.SetFromEuler(&rotation)
  63. var mvmNew math32.Matrix4
  64. mvmNew.Compose(&position, &quaternion, &scale)
  65. // Calculates final MVP and updates uniform
  66. var mvpm math32.Matrix4
  67. mvpm.MultiplyMatrices(&rinfo.ProjMatrix, &mvmNew)
  68. location := s.uniMVPM.Location(gs)
  69. gs.UniformMatrix4fv(location, 1, false, &mvpm[0])
  70. }
  71. // Raycast checks intersections between this geometry and the specified raycaster
  72. // and if any found appends it to the specified intersects array.
  73. func (s *Sprite) Raycast(rc *core.Raycaster, intersects *[]core.Intersect) {
  74. // Copy and convert ray to camera coordinates
  75. var ray math32.Ray
  76. ray.Copy(&rc.Ray).ApplyMatrix4(&rc.ViewMatrix)
  77. // Calculates ViewMatrix * MatrixWorld
  78. var mv math32.Matrix4
  79. matrixWorld := s.MatrixWorld()
  80. mv.MultiplyMatrices(&rc.ViewMatrix, &matrixWorld)
  81. // Decompose transformation matrix in its components
  82. var position math32.Vector3
  83. var quaternion math32.Quaternion
  84. var scale math32.Vector3
  85. mv.Decompose(&position, &quaternion, &scale)
  86. // Remove any rotation in X and Y axis and
  87. // compose new transformation matrix
  88. rotation := s.Rotation()
  89. rotation.X = 0
  90. rotation.Y = 0
  91. quaternion.SetFromEuler(&rotation)
  92. mv.Compose(&position, &quaternion, &scale)
  93. // Get buffer with vertices and uvs
  94. geom := s.GetGeometry()
  95. vboPos := geom.VBO(geometry.VertexPosition)
  96. if vboPos == nil {
  97. panic("sprite.Raycast(): VertexPosition VBO not found")
  98. }
  99. // Get vertex positions, transform to camera coordinates and
  100. // checks intersection with ray
  101. buffer := vboPos.Buffer()
  102. indices := geom.Indices()
  103. var v1 math32.Vector3
  104. var v2 math32.Vector3
  105. var v3 math32.Vector3
  106. var point math32.Vector3
  107. intersect := false
  108. for i := 0; i < indices.Size(); i += 3 {
  109. pos := indices[i]
  110. buffer.GetVector3(int(pos*5), &v1)
  111. v1.ApplyMatrix4(&mv)
  112. pos = indices[i+1]
  113. buffer.GetVector3(int(pos*5), &v2)
  114. v2.ApplyMatrix4(&mv)
  115. pos = indices[i+2]
  116. buffer.GetVector3(int(pos*5), &v3)
  117. v3.ApplyMatrix4(&mv)
  118. if ray.IntersectTriangle(&v1, &v2, &v3, false, &point) {
  119. intersect = true
  120. break
  121. }
  122. }
  123. if !intersect {
  124. return
  125. }
  126. // Get distance from intersection point
  127. origin := ray.Origin()
  128. distance := origin.DistanceTo(&point)
  129. // Checks if distance is between the bounds of the raycaster
  130. if distance < rc.Near || distance > rc.Far {
  131. return
  132. }
  133. // Appends intersection to received parameter.
  134. *intersects = append(*intersects, core.Intersect{
  135. Distance: distance,
  136. Point: point,
  137. Object: s,
  138. })
  139. }