-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCamera3DModel.js
100 lines (79 loc) · 2.58 KB
/
Camera3DModel.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import * as util from 'https://code.agentscript.org/src/utils.js'
import Model from 'https://code.agentscript.org/src/Model3D.js'
export default class Camera3DModel extends Model {
width = 32
height = 24
sphereRadius = 12
fieldOfView = 90
// the camera's 3D orientation
heading = 0
pitch = 0
roll = 0
// used for animation of the camera in step()
pitchDelta = 0
rollDelta = 0
headingDelta = 0
// ======================
// constructor(worldOptions) { // not needed
// super(worldOptions) // default world options if "undefined"
// }
setup() {
this.turtleBreeds('cameras pixels')
this.camera = this.cameras.createOne()
for (const y of util.range(this.height)) {
for (const x of util.range(this.width)) {
this.pixels.createOne(px => {
px.u = (x / this.width) * 2 - 1
px.v = (y / this.height) * 2 - 1
})
}
}
this.moveCamera()
}
toggleLinks() {
if (this.links.length === 0) {
this.pixels.ask(px => this.links.createOne(px, this.camera))
} else {
this.links.clear()
}
}
reset() {
this.heading = this.pitch = this.roll = 0
this.headingDelta = this.pitchDelta = this.rollDelta = 0
this.sphereRadius = 12
this.fieldOfView = 90
this.links.clear()
// this.moveCamera()
}
setHeadingPitchRoll(heading, pitch, roll) {
this.heading = heading
his.pitch = pitch
this.roll = roll
}
setHeadingPitchRollDelta(headingDelta, pitchDelta, rollDelta) {
this.headingDelta = headingDelta
this.pitchDelta = pitchDelta
this.rollDelta = rollDelta
}
step() {
this.heading = util.mod180180(this.heading + this.headingDelta)
this.pitch = util.mod180180(this.pitch + this.pitchDelta)
this.roll = util.mod180180(this.roll + this.rollDelta)
this.moveCamera()
}
moveCamera() {
const aspectRatio = this.width / this.height
this.camera.heading = this.heading
this.camera.pitch = this.pitch
this.camera.roll = this.roll
this.pixels.ask(px => {
px.moveTo(this.camera)
px.heading = this.camera.heading
px.pitch = this.camera.pitch
px.roll = this.camera.roll
px.right((px.u * this.fieldOfView) / 2)
px.tiltUp((px.v * this.fieldOfView) / 2 / aspectRatio)
px.forward(this.sphereRadius)
})
}
}