-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1531830
commit 74f9270
Showing
5 changed files
with
193 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/unixpickle/model3d/model2d" | ||
"github.com/unixpickle/model3d/model3d" | ||
"github.com/unixpickle/model3d/render3d" | ||
"github.com/unixpickle/model3d/toolbox3d" | ||
) | ||
|
||
const ( | ||
CherryRadius = 0.25 | ||
CherryStemLength = 0.15 | ||
CherryStemRadius = 0.05 | ||
) | ||
|
||
func CherrySolid() (model3d.Solid, toolbox3d.CoordColorFunc) { | ||
curve := model2d.SmoothBezier( | ||
model2d.XY(0, -CherryRadius), | ||
model2d.XY(-CherryRadius*0.7, -CherryRadius), | ||
model2d.XY(-CherryRadius, -CherryRadius*0.3), | ||
model2d.XY(-CherryRadius, 0), | ||
// Point 1 | ||
model2d.XY(-CherryRadius*0.9, CherryRadius*0.5), | ||
model2d.XY(-CherryRadius*0.6, CherryRadius*0.7), | ||
// Point 2 | ||
model2d.XY(-CherryRadius*0.4, CherryRadius*0.9), | ||
model2d.XY(-CherryRadius*0.1, CherryRadius*0.8), | ||
// Point 3 | ||
model2d.XY(-CherryRadius*0.1, CherryRadius*0.7), | ||
model2d.XY(0, CherryRadius*0.7), | ||
) | ||
profile := model2d.CurveMesh(curve, 100) | ||
profile.AddMesh(profile.MapCoords(model2d.XY(-1, 1).Mul)) | ||
solid2d := model2d.NewColliderSolid(model2d.MeshToCollider(profile)) | ||
center := CreamHeight + CupHeight | ||
solid := model3d.CheckedFuncSolid( | ||
model3d.XYZ(-CherryRadius, -CherryRadius, center-CherryRadius), | ||
model3d.XYZ(CherryRadius, CherryRadius, center+CherryRadius+0.2), | ||
func(c model3d.Coord3D) bool { | ||
x := c.XY().Norm() | ||
y := c.Z - center | ||
return solid2d.Contains(model2d.XY(x, y)) | ||
}, | ||
) | ||
|
||
stemCurve := model2d.BezierCurve{ | ||
model2d.XY(0.0, center+CherryRadius-0.03), | ||
model2d.XY(0.0, center+CherryRadius+CherryStemLength/2), | ||
model2d.XY(CherryStemLength*0.4, center+CherryRadius+CherryStemLength), | ||
} | ||
var segments []model3d.Segment | ||
eps := 0.01 | ||
for t := 0.0; t < 1.0-eps; t += eps { | ||
p1 := stemCurve.Eval(t) | ||
p2 := stemCurve.Eval(t + eps) | ||
segments = append( | ||
segments, | ||
model3d.NewSegment( | ||
model3d.YZ(p1.X, p1.Y), | ||
model3d.YZ(p2.X, p2.Y), | ||
), | ||
) | ||
} | ||
stem := toolbox3d.LineJoin(CherryStemRadius, segments...) | ||
|
||
colorFn := func(c model3d.Coord3D) render3d.Color { | ||
if stem.Contains(c) { | ||
return render3d.NewColorRGB(0.2, 0.0, 0.0) | ||
} else { | ||
return render3d.NewColorRGB(1.0, 0.0, 0.0) | ||
} | ||
} | ||
|
||
return model3d.JoinedSolid{solid, stem}, colorFn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/unixpickle/model3d/model3d" | ||
"github.com/unixpickle/model3d/render3d" | ||
"github.com/unixpickle/model3d/toolbox3d" | ||
) | ||
|
||
const ( | ||
StrawHeight = 1.5 | ||
StrawRadius = 0.1 | ||
StrawInset = 0.02 | ||
StrawTwistRate = 5.0 | ||
) | ||
|
||
func StrawSolid() (model3d.Solid, toolbox3d.CoordColorFunc) { | ||
s1, c1 := SingleStrawSolid() | ||
xf1 := model3d.JoinedTransform{ | ||
model3d.Rotation(model3d.Y(1), 0.5), | ||
&model3d.Translate{Offset: model3d.XZ(-1.1, 0.1)}, | ||
} | ||
s2 := model3d.TransformSolid(xf1, s1) | ||
c2 := c1.Transform(xf1) | ||
xf2 := model3d.JoinedTransform{ | ||
model3d.Rotation(model3d.XY(-0.2, 1).Normalize(), -0.2), | ||
&model3d.Translate{Offset: model3d.XZ(0.3, 0.0)}, | ||
} | ||
s3 := model3d.TransformSolid(xf2, s1) | ||
c3 := c1.Transform(xf2) | ||
return model3d.JoinedSolid{s2, s3}, toolbox3d.JoinedSolidCoordColorFunc(nil, s2, c2, s3, c3) | ||
} | ||
|
||
func SingleStrawSolid() (model3d.Solid, toolbox3d.CoordColorFunc) { | ||
outerSolid := &model3d.Cylinder{ | ||
P1: model3d.Z(CupHeight * 0.9), | ||
P2: model3d.Z(CupHeight + StrawHeight), | ||
Radius: StrawRadius, | ||
} | ||
solid := &model3d.SubtractedSolid{ | ||
Positive: outerSolid, | ||
Negative: &model3d.Cylinder{ | ||
P1: model3d.Z(CupHeight + StrawHeight - 0.05), | ||
P2: model3d.Z(CupHeight + StrawHeight), | ||
Radius: StrawRadius - StrawInset, | ||
}, | ||
} | ||
colorFn := func(c model3d.Coord3D) render3d.Color { | ||
if c.XY().Norm() <= StrawRadius-StrawInset { | ||
return render3d.NewColor(0.8) | ||
} | ||
theta := math.Atan2(c.X, c.Y) | ||
twist := c.Z * StrawTwistRate | ||
theta += twist | ||
for theta < 0 { | ||
theta += math.Pi * 2 | ||
} | ||
for theta > math.Pi*2 { | ||
theta -= math.Pi * 2 | ||
} | ||
if theta < math.Pi/4 || (theta > math.Pi && theta < math.Pi+math.Pi/4) { | ||
return render3d.NewColorRGB(1.0, 0.0, 0.0) | ||
} else { | ||
return render3d.NewColor(0.8) | ||
} | ||
} | ||
return solid, colorFn | ||
} |