/
culler.go
70 lines (58 loc) · 2.06 KB
/
culler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright © 2014-2015 Galvanized Logic Inc.
// Use is governed by a BSD-style license found in the LICENSE file.
package vu
import (
"github.com/gazed/vu/math/lin"
)
// Cull is attached to a View in order to reduce the number
// of items sent for rendering.
type Cull interface {
// Cull returns true if a model represented by point, px, py, pz
// should be culled using the given camera.
Cull(cam Camera, px, py, pz float64) bool
}
// =============================================================================
// NewFrontCull returns a culler that keeps objects in a radius directly
// in front of the camera. Objects behind the camera and far away from
// the camera are culled.
func NewFrontCull(r float64) Cull {
if r < 0 {
r = 0
}
return &frontCull{radius: r, rr: r * r}
}
// frontCull removes everything that is not in a circular radius
// projected in front of the camera.
type frontCull struct {
radius float64 // regular radius.
rr float64 // radius squared.
}
// Culler implmentation.
// Project the part location back along the lookat vector.
func (fc *frontCull) Cull(cam Camera, px, py, pz float64) bool {
fudgeFactor := float64(0.8) // don't move all the way up.
cx, cy, cz := lin.MultSQ(0, 0, -fc.radius*fudgeFactor, cam.Lookat())
px, py, pz = px-cx, py-cy, pz-cz // move part location back.
toc := cam.Distance(px, py, pz) // cull if outside radius.
return toc > fc.rr
}
// =============================================================================
// NewRadiusCull returns a culler that removes objects outside a given
// radius from the camera. Can be used for show objects around the camera
// for top down minimaps.
func NewRadiusCull(r float64) Cull {
if r < 0 {
r = 0
}
return &radiusCull{rr: r * r}
}
// radiusCull removes everything far away from the camera.
type radiusCull struct {
rr float64 // radius squared.
}
// Culler implmentation. True true if the given location is
// within the culler radius of the camera.
func (rc *radiusCull) Cull(cam Camera, px, py, pz float64) bool {
toc := cam.Distance(px, py, pz)
return toc > rc.rr
}