-
Notifications
You must be signed in to change notification settings - Fork 18
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
38067a9
commit cbbc667
Showing
63 changed files
with
6,927 additions
and
10 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
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
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,84 @@ | ||
Blocks | ||
--- | ||
|
||
A block is a nested list of instructions, contained inside `{ ... }` braces. Some commands, such as [builders](builders.md) or [CSG operations](csg.md), accept a block parameter instead of a simple value like a number or [vector](literals.md#vectors-and-tuples). | ||
|
||
Instructions inside a block are executed within the [scope](scope.md) of the command that invoked them. Typically that means that any transforms or material changes made inside the block will only apply to geometry created inside the same block. This also applies to any symbols that you define inside the block. | ||
|
||
You can define your own blocks using the `define` command. Here is a block that creates a five-pointed star: | ||
|
||
```swift | ||
define star { | ||
path { | ||
for 1 to 5 { | ||
point 0 -0.5 | ||
rotate 1 / 5 | ||
point 0 -1 | ||
rotate 1 / 5 | ||
} | ||
point 0 -0.5 | ||
} | ||
} | ||
``` | ||
|
||
You can call it by simply referencing its name, like this: | ||
|
||
```swift | ||
star | ||
``` | ||
|
||
![Star](../../images/star.png) | ||
|
||
**Note:** there is a subtle distinction between the code above and the code below: | ||
|
||
```swift | ||
define star path { | ||
for 1 to 5 { | ||
point 0 -0.5 | ||
rotate 1 / 5 | ||
point 0 -1 | ||
rotate 1 / 5 | ||
} | ||
point 0 -0.5 | ||
} | ||
``` | ||
|
||
In the original code, we defined a new block symbol that creates a star-shaped path. In the code above we've defined a symbol whose value is a star-shaped path. The former code is evaluated at the point when it is *called*, whereas the latter code is evaluated at the point when it is *defined*. | ||
|
||
The end-result is the same in this case, so it may seem like the distinction doesn't matter, but the advantage of the former approach is that we can add *options* to vary the behavior of the code when it is called. | ||
|
||
## Options | ||
|
||
To add an option to a block, you use the `option` command. This works in a similar way to the [define](symbols.md) command, but it allows the specified value to be overridden by the caller. | ||
|
||
The code below extends the `star` definition with options for the radius and number of points: | ||
|
||
```swift | ||
define star { | ||
option radius 1 | ||
option points 5 | ||
path { | ||
for 1 to points { | ||
point 0 -0.5 | ||
rotate 1 / points | ||
point 0 -radius | ||
rotate 1 / points | ||
} | ||
point 0 -0.5 | ||
} | ||
} | ||
``` | ||
|
||
Now we can use those options to create a star with 6 points if we choose: | ||
|
||
```swift | ||
star { | ||
points 6 | ||
radius 2 | ||
} | ||
``` | ||
|
||
![Star](../../images/six-pointed-star.png) | ||
|
||
--- | ||
[Index](index.md) | Next: [Scope](scope.md) |
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,115 @@ | ||
Bounds | ||
--- | ||
|
||
ShapeScript's [size](transforms.md#size) ands [scale](transforms.md#relative-transforms) commands let you control the relative size of a shape, but sometimes it's useful to know the exact dimensions. | ||
|
||
A cube of size 1 has an easily-predicted size of one world unit square, but what about a more complex shape, such as a 5-pointed star (see the [procedural paths](paths.md#procedural-paths) and [blocks](blocks.md) sections for details): | ||
|
||
```swift | ||
define star path { | ||
for 1 to 5 { | ||
point 0 -0.5 | ||
rotate 1 / 5 | ||
point 0 -1 | ||
rotate 1 / 5 | ||
} | ||
point 0 -0.5 | ||
} | ||
|
||
// draw star | ||
extrude { | ||
color red | ||
star | ||
} | ||
|
||
// draw cube | ||
cube { | ||
color green 0.5 | ||
} | ||
``` | ||
|
||
![Star with unit cube](../../images/star-with-unit-cube.png) | ||
|
||
We can see that the star is larger than the unit cube, but other than trial-and-error or complex math, how can we get the exact size? This is where the `bounds` [member property](expressions.md#members) comes in. | ||
|
||
## Mesh Bounds | ||
|
||
Paths and meshes both expose a `bounds` property that represents a bounding box around the shape. From this you can get the exact size and position needs to place a box around the star: | ||
|
||
```swift | ||
define star { | ||
... | ||
} | ||
|
||
// define star shape | ||
define shape extrude { | ||
color red | ||
star | ||
} | ||
|
||
// draw star | ||
shape | ||
|
||
// draw box around star | ||
cube { | ||
color green 0.5 | ||
position shape.bounds.center | ||
size shape.bounds.size | ||
} | ||
``` | ||
|
||
![Star with fitted cube](../../images/star-with-fitted-cube.png) | ||
|
||
## Path Bounds | ||
|
||
In the example above we computed the bounds of a solid `mesh` (an extruded star-shaped `path`) but you can also get the bounds of a `path` directly. The following code draws the star path inside its bounding rectangle: | ||
|
||
```swift | ||
define star { | ||
... | ||
} | ||
|
||
// draw star | ||
star | ||
|
||
// draw rectangle around star | ||
square { | ||
position shape.bounds.center | ||
size shape.bounds.size | ||
} | ||
``` | ||
|
||
![Star with fitted rectangle](../../images/star-with-fitted-rect.png) | ||
|
||
## Bounds Members | ||
|
||
The `bounds` member property has the following sub-properties that you can use: | ||
|
||
* `min` - The position of the corner of the box with the smallest X, Y and Z values relative to the origin. | ||
* `max` - The position of the corner of the box with the largest X, Y and Z values relative to the origin. | ||
* `center` - The position of the center of the box relative to the origin. | ||
* `size` - The size (width, height and depth) of the box in world units. | ||
* `width` - The width of the box along the X axis (equivalent to `size.width`) | ||
* `height` - The height of the box along the Y axis (equivalent to `size.height`) | ||
* `depth` - The depth of the box along the Z axis (equivalent to `size.depth`) | ||
|
||
So, for example, to get the height of a shape, you could use: | ||
|
||
```swift | ||
print someShape.bounds.size.height | ||
``` | ||
|
||
or just: | ||
|
||
```swift | ||
print someShape.bounds.height | ||
``` | ||
|
||
And to get the X coordinate of its rightmost edge you could use: | ||
|
||
```swift | ||
print someShape.bounds.max.x | ||
``` | ||
|
||
--- | ||
[Index](index.md) | Next: [Groups](groups.md) |
Oops, something went wrong.