mesh.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. uniMVm gls.Uniform // Model view matrix uniform location cache
  16. uniMVPm gls.Uniform // Model view projection matrix uniform cache
  17. uniNm gls.Uniform // Normal matrix uniform cache
  18. }
  19. // NewMesh creates and returns a pointer to a mesh with the specified geometry and material.
  20. // If the mesh has multi materials, the material specified here must be nil and the
  21. // individual materials must be add using "AddMaterial" or AddGroupMaterial".
  22. func NewMesh(igeom geometry.IGeometry, imat material.IMaterial) *Mesh {
  23. m := new(Mesh)
  24. m.Init(igeom, imat)
  25. return m
  26. }
  27. // Init initializes the Mesh and its uniforms.
  28. func (m *Mesh) Init(igeom geometry.IGeometry, imat material.IMaterial) {
  29. m.Graphic.Init(igeom, gls.TRIANGLES)
  30. // Initialize uniforms
  31. m.uniMVm.Init("ModelViewMatrix")
  32. m.uniMVPm.Init("MVP")
  33. m.uniNm.Init("NormalMatrix")
  34. // Adds single material if not nil
  35. if imat != nil {
  36. m.AddMaterial(imat, 0, 0)
  37. }
  38. }
  39. // AddMaterial adds a material for the specified subset of vertices.
  40. func (m *Mesh) AddMaterial(imat material.IMaterial, start, count int) {
  41. m.Graphic.AddMaterial(m, imat, start, count)
  42. }
  43. // AddGroupMaterial adds a material for the specified geometry group.
  44. func (m *Mesh) AddGroupMaterial(imat material.IMaterial, gindex int) {
  45. m.Graphic.AddGroupMaterial(m, imat, gindex)
  46. }
  47. // RenderSetup is called by the engine before drawing the mesh geometry
  48. // It is responsible to updating the current shader uniforms with
  49. // the model matrices.
  50. func (m *Mesh) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
  51. // Transfer uniform for model view matrix
  52. mvm := m.ModelViewMatrix()
  53. location := m.uniMVm.Location(gs)
  54. gs.UniformMatrix4fv(location, 1, false, &mvm[0])
  55. // Transfer uniform for model view projection matrix
  56. mvpm := m.ModelViewProjectionMatrix()
  57. location = m.uniMVPm.Location(gs)
  58. gs.UniformMatrix4fv(location, 1, false, &mvpm[0])
  59. // Calculates normal matrix and transfer 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)
  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. i := 0
  121. geom.ReadFaces(func(vA, vB, vC math32.Vector3) bool {
  122. // Checks intersection of the ray with this face
  123. mat := m.GetMaterial(i).GetMaterial()
  124. var point math32.Vector3
  125. intersect := checkIntersection(mat, &vA, &vB, &vC, &point)
  126. if intersect != nil {
  127. intersect.Index = uint32(i)
  128. *intersects = append(*intersects, *intersect)
  129. }
  130. i += 3
  131. return false
  132. })
  133. }