-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stalk.js
107 lines (92 loc) · 2.68 KB
/
Stalk.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
101
102
103
104
105
106
107
/* Stalk object made out of n = resolution vertices, drawn as the line
connecting them. Stalks are generated in 2d (z=0) and z-rotated afterwards. */
class Stalk extends Mesh {
constructor (positionVector, rotationVector, meshParams, color) {
super()
this.position = positionVector
this.rotation = rotationVector
let params = meshParams || Stalk.getDefaultParams()
this.length = params.length
this.resolution = params.resolution
this.curvature = params.curvature
this.thickness = params.thickness
this.color = color || 'green'
this.vertices = this.generateVertices()
this.rotateMesh(this.rotation)
this.translateMesh(this.position)
}
// Generates a vector point in 2d space at coords, moves the coords n units
// upwards and vector-rotates it a certain amount - and repeats the process.
// This allows for a fixed length with variable curvature.
generateVertices () {
let lengthStep = this.length / this.resolution
let coords = createVector(0,0)
let vertices = []
for (let i = 0; i < this.resolution; i++) {
let vert = createVector(coords.x, coords.y, 0)
vertices.push(vert)
coords.y += lengthStep
coords.rotate(PI * (1.2 / this.resolution) * this.curvature * -1)
}
return vertices
}
draw () {
noFill()
stroke(this.color)
strokeWeight(this.thickness)
beginShape()
for (let vert of this.vertices) {
vertex(vert.x, vert.y, vert.z)
}
endShape()
}
getTip () {
return this.vertices[this.vertices.length - 1]
}
static getDefaultParams () {
return {
length: 80, // 50-100 (short-long stalk)
resolution: 3, // 1-6 (# of vertices)
curvature: 0.33, // 0.1-0.4 (straight-curved stalk)
thickness: 3 // 2-5 (thickness of the drawn line (strokeWeight))
}
}
static getRandomParams () {
return {
length: 50 + random(50),
resolution: 3,
curvature: random(0.1, 0.4),
thickness: random([2,3,4,5])
}
}
static wiggleParams (baseParams) {
return {
length: baseParams.length * random(0.66, 2),
resolution: baseParams.resolution,
curvature: baseParams.curvature * random(0.2, 1.2),
thickness: baseParams.thickness
}
}
log (print) {
let p = {
position: {
x: this.position.x,
y: this.position.y,
z: this.position.z
},
rotation: {
x: this.rotation.x,
y: this.rotation.y,
z: this.rotation.z
},
meshParams: {
length: this.length,
resolution: this.resolution,
curvature: this.curvature,
thickness: this.thickness
}
}
if (print) console.log(p);
return p
}
}