points.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // Points represents a geometry containing only points
  13. type Points struct {
  14. Graphic // Embedded graphic
  15. uniMVPm gls.Uniform // Model view projection matrix uniform location cache
  16. }
  17. // NewPoints creates and returns a graphic points object with the specified
  18. // geometry and material.
  19. func NewPoints(igeom geometry.IGeometry, imat material.IMaterial) *Points {
  20. p := new(Points)
  21. p.Graphic.Init(p, igeom, gls.POINTS)
  22. if imat != nil {
  23. p.AddMaterial(p, imat, 0, 0)
  24. }
  25. p.uniMVPm.Init("MVP")
  26. return p
  27. }
  28. // RenderSetup is called by the engine before rendering this graphic.
  29. func (p *Points) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
  30. // Transfer model view projection matrix uniform
  31. mvpm := p.ModelViewProjectionMatrix()
  32. location := p.uniMVPm.Location(gs)
  33. gs.UniformMatrix4fv(location, 1, false, &mvpm[0])
  34. }
  35. // Raycast satisfies the INode interface and checks the intersections
  36. // of this geometry with the specified raycaster.
  37. func (p *Points) Raycast(rc *core.Raycaster, intersects *[]core.Intersect) {
  38. // Checks intersection with the bounding sphere transformed to world coordinates
  39. geom := p.GetGeometry()
  40. sphere := geom.BoundingSphere()
  41. matrixWorld := p.MatrixWorld()
  42. sphere.ApplyMatrix4(&matrixWorld)
  43. if !rc.IsIntersectionSphere(&sphere) {
  44. return
  45. }
  46. // Copy ray and transforms to model coordinates
  47. var inverseMatrix math32.Matrix4
  48. var ray math32.Ray
  49. inverseMatrix.GetInverse(&matrixWorld)
  50. ray.Copy(&rc.Ray).ApplyMatrix4(&inverseMatrix)
  51. // Checks intersection with all points
  52. scale := p.Scale()
  53. localThreshold := rc.PointPrecision / ((scale.X + scale.Y + scale.Z) / 3)
  54. localThresholdSq := localThreshold * localThreshold
  55. // internal function to check intersection with a point
  56. testPoint := func(point *math32.Vector3, index int) {
  57. // Get distance from ray to point and if greater than threshold,
  58. // nothing to do.
  59. rayPointDistanceSq := ray.DistanceSqToPoint(point)
  60. if rayPointDistanceSq >= localThresholdSq {
  61. return
  62. }
  63. var intersectPoint math32.Vector3
  64. ray.ClosestPointToPoint(point, &intersectPoint)
  65. intersectPoint.ApplyMatrix4(&matrixWorld)
  66. origin := rc.Ray.Origin()
  67. distance := origin.DistanceTo(&intersectPoint)
  68. if distance < rc.Near || distance > rc.Far {
  69. return
  70. }
  71. // Appends intersection of raycaster with this point
  72. *intersects = append(*intersects, core.Intersect{
  73. Distance: distance,
  74. Point: intersectPoint,
  75. Index: uint32(index),
  76. Object: p,
  77. })
  78. }
  79. i := 0
  80. geom.ReadVertices(func(vertex math32.Vector3) bool {
  81. testPoint(&vertex, i)
  82. i++
  83. return false
  84. })
  85. }