lines.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // Lines is a Graphic which is rendered as a collection of independent lines
  13. type Lines struct {
  14. Graphic
  15. mvpm gls.UniformMatrix4f // Model view projection matrix uniform
  16. }
  17. func (l *Lines) Init(igeom geometry.IGeometry, imat material.IMaterial) {
  18. l.Graphic.Init(igeom, gls.LINES)
  19. l.AddMaterial(l, imat, 0, 0)
  20. l.mvpm.Init("MVP")
  21. }
  22. func NewLines(igeom geometry.IGeometry, imat material.IMaterial) *Lines {
  23. l := new(Lines)
  24. l.Init(igeom, imat)
  25. return l
  26. }
  27. // RenderSetup is called by the engine before drawing this geometry
  28. func (l *Lines) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
  29. // Calculates model view projection matrix and updates uniform
  30. mw := l.MatrixWorld()
  31. var mvpm math32.Matrix4
  32. mvpm.MultiplyMatrices(&rinfo.ViewMatrix, &mw)
  33. mvpm.MultiplyMatrices(&rinfo.ProjMatrix, &mvpm)
  34. l.mvpm.SetMatrix4(&mvpm)
  35. l.mvpm.Transfer(gs)
  36. }
  37. // Raycast satisfies the INode interface and checks the intersections
  38. // of this geometry with the specified raycaster
  39. func (l *Lines) Raycast(rc *core.Raycaster, intersects *[]core.Intersect) {
  40. lineRaycast(l, rc, intersects, 2)
  41. }
  42. // Internal function used by raycasting for Lines and LineStrip
  43. func lineRaycast(igr IGraphic, rc *core.Raycaster, intersects *[]core.Intersect, step int) {
  44. // Get the bounding sphere
  45. gr := igr.GetGraphic()
  46. geom := igr.GetGeometry()
  47. sphere := geom.BoundingSphere()
  48. // Transform bounding sphere from model to world coordinates and
  49. // checks intersection with raycaster
  50. matrixWorld := gr.MatrixWorld()
  51. sphere.ApplyMatrix4(&matrixWorld)
  52. if !rc.IsIntersectionSphere(&sphere) {
  53. return
  54. }
  55. // Copy ray and transform to model coordinates
  56. // This ray will will also be used to check intersects with
  57. // the geometry, as is much less expensive to transform the
  58. // ray to model coordinates than the geometry to world coordinates.
  59. var inverseMatrix math32.Matrix4
  60. var ray math32.Ray
  61. inverseMatrix.GetInverse(&matrixWorld, true)
  62. ray.Copy(&rc.Ray).ApplyMatrix4(&inverseMatrix)
  63. var vstart math32.Vector3
  64. var vend math32.Vector3
  65. var interSegment math32.Vector3
  66. var interRay math32.Vector3
  67. // Get geometry positions and indices buffers
  68. vboPos := geom.VBO("VertexPosition")
  69. if vboPos == nil {
  70. return
  71. }
  72. positions := vboPos.Buffer()
  73. indices := geom.Indices()
  74. precisionSq := rc.LinePrecision * rc.LinePrecision
  75. // Checks intersection with individual lines for indexed geometry
  76. if indices.Size() > 0 {
  77. for i := 0; i < indices.Size()-1; i += step {
  78. // Calculates distance from ray to this line segment
  79. a := indices[i]
  80. b := indices[i+1]
  81. positions.GetVector3(int(3*a), &vstart)
  82. positions.GetVector3(int(3*b), &vend)
  83. distSq := ray.DistanceSqToSegment(&vstart, &vend, &interRay, &interSegment)
  84. if distSq > precisionSq {
  85. continue
  86. }
  87. // Move back to world coordinates for distance calculation
  88. interRay.ApplyMatrix4(&matrixWorld)
  89. origin := rc.Ray.Origin()
  90. distance := origin.DistanceTo(&interRay)
  91. if distance < rc.Near || distance > rc.Far {
  92. continue
  93. }
  94. interSegment.ApplyMatrix4(&matrixWorld)
  95. *intersects = append(*intersects, core.Intersect{
  96. Distance: distance,
  97. Point: interSegment,
  98. Index: uint32(i),
  99. Object: igr,
  100. })
  101. }
  102. // Checks intersection with individual lines for NON indexed geometry
  103. } else {
  104. for i := 0; i < positions.Size()/3-1; i += step {
  105. positions.GetVector3(int(3*i), &vstart)
  106. positions.GetVector3(int(3*i+3), &vend)
  107. distSq := ray.DistanceSqToSegment(&vstart, &vend, &interRay, &interSegment)
  108. if distSq > precisionSq {
  109. continue
  110. }
  111. // Move back to world coordinates for distance calculation
  112. interRay.ApplyMatrix4(&matrixWorld)
  113. origin := rc.Ray.Origin()
  114. distance := origin.DistanceTo(&interRay)
  115. if distance < rc.Near || distance > rc.Far {
  116. continue
  117. }
  118. interSegment.ApplyMatrix4(&matrixWorld)
  119. *intersects = append(*intersects, core.Intersect{
  120. Distance: distance,
  121. Point: interSegment,
  122. Index: uint32(i),
  123. Object: igr,
  124. })
  125. }
  126. }
  127. }