“Genuary 9: Friendship Plant” by Dave Pagurek
https://openprocessing.org/sketch/1787913
License CreativeCommons Attribution NonCommercial ShareAlike
https://creativecommons.org/licenses/by-nc-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
CC Attribution NonCommercial ShareAlike
Genuary 9: Friendship Plant
Pagurek
xxxxxxxxxx
let plant
let renderer
function setup() {
createCanvas(600, 600, WEBGL)
pixelDensity(2)
renderer = createContactShadowRenderer()
renderer.setSearchRadius(100)
renderer.setBlurRadius(100)
renderer.setIntensity(0.8)
renderer.setShadowSamples(15)
renderer.setBlurSamples(20)
}
function makePlant() {
if (plant) freeGeometry(plant)
plant = buildGeometry('plant', (builder) => {
const stemPts = [createVector(0,0,0)]
const numStemPts = ceil(random(10, 20))
for (let i = 0; i < numStemPts; i++) {
stemPts.push(stemPts[stemPts.length-1].copy().add(createVector(
random(-5, 5),
random(-10, -15),
random(-5, 5)
).setMag(random(15, 25))))
}
const branches = []
const leaves = []
const numBranches = ceil(random(numStemPts * 0.5, numStemPts - 1))
for (let i = 0; i < numBranches; i++) {
const branchStart = random(stemPts.slice(2, -3))
const angle = random(TWO_PI)
const branchLength = map(
branchStart.y,
0,
-200,
random(200, 300),
random(100, 200),
true
)
const branchEnd = branchStart.copy().add(createVector(
branchLength * cos(angle),
random(-200, 50),
branchLength * sin(angle),
))
const c1 = p5.Vector.lerp(branchStart, branchEnd, 0.25).add(createVector(0, -branchLength*0.2, 0))
const c2 = p5.Vector.lerp(branchStart, branchEnd, 0.75).add(createVector(0, -branchLength*0.2, 0))
const controls = [branchStart, c1, c2, branchEnd]
const branch = []
const samples = 20
for (let j = 0; j <= samples; j++) {
const t = j/samples
branch.push(createVector(
bezierPoint(...controls.map((c) => c.x), t),
bezierPoint(...controls.map((c) => c.y), t),
bezierPoint(...controls.map((c) => c.z), t),
))
}
branches.push(branch)
leaves.push({
point: branchEnd,
tangent: createVector(
bezierTangent(...controls.map((c) => c.x), 1),
bezierTangent(...controls.map((c) => c.y), 1),
bezierTangent(...controls.map((c) => c.z), 1),
).normalize(),
maxR: random(50, 70),
})
}
const baseR = map(numStemPts, 10, 20, 10, 15)
builder.fill('#857259')
for (let i = 0; i < numStemPts; i++) {
builder.beginShape(QUAD_STRIP)
for (let j = 0; j <= 12; j++) {
for (const off of [0, 1]) {
const r = baseR * (1 - (i+off)/numStemPts)
const a = TWO_PI * j/12
builder.vertex(
stemPts[i+off].x + r*cos(a),
stemPts[i+off].y,
stemPts[i+off].z + r*sin(a),
)
}
}
builder.endShape()
}
builder.fill('#55a36c')
for (const branch of branches) {
const tangents = branch.slice(0, -1).map((v, i) => branch[i+1].copy().sub(v).normalize())
tangents.push(tangents[tangents.length-1])
for (let i = 0; i < branch.length - 1; i++) {
builder.beginShape(QUAD_STRIP)
for (let j = 0; j <= 12; j++) {
for (const off of [0, 1]) {
const tangent = tangents[i+off]
const up = createVector(0, -1, 0)
const normal = tangent.cross(up)
const biNormal = tangent.cross(normal)
const r = lerp(5, 2, (i+off)/(branch.length - 1))
const a = TWO_PI * j/12
const pt = branch[i+off].copy()
pt.add(normal.copy().mult(r * cos(a)))
pt.add(biNormal.copy().mult(r * sin(a)))
builder.vertex(pt.x, pt.y, pt.z)
}
}
builder.endShape()
}
}
for (const { point, tangent, maxR } of leaves) {
const up = createVector(0, -1, 0)
const normal = tangent.cross(up)
const biNormal = tangent.cross(normal)
builder.beginShape(QUAD_STRIP)
for (let i = 0; i < 12; i++) {
for (let j = 0; j <= 20; j++) {
for (const off of [0, 1]) {
const t = (i+off)/12
const r = maxR * (i+off)/12
const a = TWO_PI * j/20
const pt = point.copy()
.add(normal.copy().mult(r * cos(a)))
.add(biNormal.copy().mult(r * sin(a)))
.add(tangent.copy().mult(bezierPoint(0, maxR/10, maxR/3, 0, t)))
builder.vertex(pt.x, pt.y, pt.z)
}
}
}
builder.endShape()
}
})
plant.normalize()
}
let prevIdx = -1
function draw() {
const idx = Math.floor(millis() / 5000)
if (prevIdx !== idx) {
makePlant()
prevIdx = idx
}
clear()
renderer.draw(() => {
clear()
background('#d7f2fa')
noStroke()
// orbitControl()
push()
rotateY(millis() * 0.0001)
flatModelColors()
scale(2)
model(plant)
pop()
})
}
See More Shortcuts