mesh.go 5.5 KB

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