mesh.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 Mesh struct {
  13. Graphic // Embedded graphic
  14. uniMVM gls.Uniform // Model view matrix uniform location cache
  15. uniMVPM gls.Uniform // Model view projection matrix uniform cache
  16. uniNM gls.Uniform // Normal matrix uniform cache
  17. }
  18. // NewMesh creates and returns a pointer to a mesh with the specified geometry and material
  19. // If the mesh has multi materials, the material specified here must be nil and the
  20. // individual materials must be add using "AddMateria" or AddGroupMaterial"
  21. func NewMesh(igeom geometry.IGeometry, imat material.IMaterial) *Mesh {
  22. m := new(Mesh)
  23. m.Init(igeom, imat)
  24. return m
  25. }
  26. func (m *Mesh) Init(igeom geometry.IGeometry, imat material.IMaterial) {
  27. m.Graphic.Init(igeom, gls.TRIANGLES)
  28. // Initialize uniforms
  29. m.uniMVM.Init("ModelViewMatrix")
  30. m.uniMVPM.Init("MVP")
  31. m.uniNM.Init("NormalMatrix")
  32. // Adds single material if not nil
  33. if imat != nil {
  34. m.AddMaterial(imat, 0, 0)
  35. }
  36. }
  37. func (m *Mesh) AddMaterial(imat material.IMaterial, start, count int) {
  38. m.Graphic.AddMaterial(m, imat, start, count)
  39. }
  40. // Add group material
  41. func (m *Mesh) AddGroupMaterial(imat material.IMaterial, gindex int) {
  42. m.Graphic.AddGroupMaterial(m, imat, gindex)
  43. }
  44. // RenderSetup is called by the engine before drawing the mesh geometry
  45. // It is responsible to updating the current shader uniforms with
  46. // the model matrices.
  47. func (m *Mesh) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
  48. // Calculates model view matrix and transfer uniform
  49. mw := m.MatrixWorld()
  50. var mvm math32.Matrix4
  51. mvm.MultiplyMatrices(&rinfo.ViewMatrix, &mw)
  52. location := m.uniMVM.Location(gs)
  53. gs.UniformMatrix4fv(location, 1, false, &mvm[0])
  54. // Calculates model view projection matrix and updates uniform
  55. var mvpm math32.Matrix4
  56. mvpm.MultiplyMatrices(&rinfo.ProjMatrix, &mvm)
  57. location = m.uniMVPM.Location(gs)
  58. gs.UniformMatrix4fv(location, 1, false, &mvpm[0])
  59. // Calculates normal matrix and updates uniform
  60. var nm math32.Matrix3
  61. nm.GetNormalMatrix(&mvm)
  62. location = m.uniNM.Location(gs)
  63. gs.UniformMatrix3fv(location, 1, false, &nm[0])
  64. }
  65. // Raycast checks intersections between this geometry and the specified raycaster
  66. // and if any found appends it to the specified intersects array.
  67. func (m *Mesh) Raycast(rc *core.Raycaster, intersects *[]core.Intersect) {
  68. // Transform this mesh geometry bounding sphere from model
  69. // to world coordinates and checks intersection with raycaster
  70. geom := m.GetGeometry()
  71. sphere := geom.BoundingSphere()
  72. matrixWorld := m.MatrixWorld()
  73. sphere.ApplyMatrix4(&matrixWorld)
  74. if !rc.IsIntersectionSphere(&sphere) {
  75. return
  76. }
  77. // Copy ray and transform to model coordinates
  78. // This ray will will also be used to check intersects with
  79. // the geometry, as is much less expensive to transform the
  80. // ray to model coordinates than the geometry to world coordinates.
  81. var inverseMatrix math32.Matrix4
  82. inverseMatrix.GetInverse(&matrixWorld, true)
  83. var ray math32.Ray
  84. ray.Copy(&rc.Ray).ApplyMatrix4(&inverseMatrix)
  85. bbox := geom.BoundingBox()
  86. if !ray.IsIntersectionBox(&bbox) {
  87. return
  88. }
  89. // Local function to check the intersection of the ray from the raycaster with
  90. // the specified face defined by three poins.
  91. checkIntersection := func(mat *material.Material, pA, pB, pC, point *math32.Vector3) *core.Intersect {
  92. var intersect bool
  93. switch mat.Side() {
  94. case material.SideBack:
  95. intersect = ray.IntersectTriangle(pC, pB, pA, true, point)
  96. case material.SideFront:
  97. intersect = ray.IntersectTriangle(pA, pB, pC, true, point)
  98. case material.SideDouble:
  99. intersect = ray.IntersectTriangle(pA, pB, pC, false, point)
  100. }
  101. if !intersect {
  102. return nil
  103. }
  104. // Transform intersection point from model to world coordinates
  105. var intersectionPointWorld = *point
  106. intersectionPointWorld.ApplyMatrix4(&matrixWorld)
  107. // Calculates the distance from the ray origin to intersection point
  108. origin := rc.Ray.Origin()
  109. distance := origin.DistanceTo(&intersectionPointWorld)
  110. // Checks if distance is between the bounds of the raycaster
  111. if distance < rc.Near || distance > rc.Far {
  112. return nil
  113. }
  114. return &core.Intersect{
  115. Distance: distance,
  116. Point: intersectionPointWorld,
  117. Object: m,
  118. }
  119. }
  120. // Get buffer with position vertices
  121. vboPos := geom.VBO("VertexPosition")
  122. if vboPos == nil {
  123. panic("mesh.Raycast(): VertexPosition VBO not found")
  124. }
  125. positions := vboPos.Buffer()
  126. indices := geom.Indices()
  127. var vA math32.Vector3
  128. var vB math32.Vector3
  129. var vC math32.Vector3
  130. // Geometry has indexed vertices
  131. if indices.Size() > 0 {
  132. for i := 0; i < indices.Size(); i += 3 {
  133. // Get face indices
  134. a := indices[i]
  135. b := indices[i+1]
  136. c := indices[i+2]
  137. // Get face position vectors
  138. positions.GetVector3(int(3*a), &vA)
  139. positions.GetVector3(int(3*b), &vB)
  140. positions.GetVector3(int(3*c), &vC)
  141. // Checks intersection of the ray with this face
  142. mat := m.GetMaterial(i).GetMaterial()
  143. var point math32.Vector3
  144. intersect := checkIntersection(mat, &vA, &vB, &vC, &point)
  145. if intersect != nil {
  146. intersect.Index = uint32(i)
  147. *intersects = append(*intersects, *intersect)
  148. }
  149. }
  150. // Geometry has NO indexed vertices
  151. } else {
  152. for i := 0; i < positions.Size(); i += 9 {
  153. // Get face indices
  154. a := i / 3
  155. b := a + 1
  156. c := a + 2
  157. // Set face position vectors
  158. positions.GetVector3(int(3*a), &vA)
  159. positions.GetVector3(int(3*b), &vB)
  160. positions.GetVector3(int(3*c), &vC)
  161. // Checks intersection of the ray with this face
  162. mat := m.GetMaterial(i).GetMaterial()
  163. var point math32.Vector3
  164. intersect := checkIntersection(mat, &vA, &vB, &vC, &point)
  165. if intersect != nil {
  166. intersect.Index = uint32(a)
  167. *intersects = append(*intersects, *intersect)
  168. }
  169. }
  170. }
  171. }