sprite.go 4.2 KB

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