Skip to content

Instantly share code, notes, and snippets.

@calebhsu
Last active August 30, 2015 15:19
Show Gist options
  • Save calebhsu/e40a7ba21f36e89d6df6 to your computer and use it in GitHub Desktop.
Save calebhsu/e40a7ba21f36e89d6df6 to your computer and use it in GitHub Desktop.
Bed using CraftML tags
title author
Building a Bed
Caleb Hsu

Objective: Build a bed with adjustable length and leg height.

Start by making the mattress. We can add parameters after we have determined our dimensions.

<craft>
    <cube size="40 75 7" color="teal"></cube>
</craft>

Next, make the base of the bed frame.

<craft>
    <cube size="42 77 5" color="brown"></cube>
</craft>

Create a headboard <part> by lining up two supports and a headboard in a <row>. To make the rounded headboard, <stack> a scaled <cylinder> over the row.

<craft>
    <part name="headboard">
        <stack spacing="-27">
            <stack spacing="-22">
                <cylinder radius="20" height="4" transform="
                    rotateX(90) 
                    scale(1,1,0.7)
                "></cylinder>
                <cube size="38 4 30"></cube>
            </stack>
            
            <row spacing="38">
                <repeat n="2">
                    <cube size="2 4 36"></cube>
                </repeat>
            </row>
        </stack>
    </part>
    
    <headboard></headboard>
    
</craft>

With the dimensions of the frame in mind, make the front legs for the frame by lining two sized <cubes> in a <row>.

<craft>
    <row spacing="36">
        <cube size="3 3 8"></cube>
        <cube size="3 3 8"></cube>
    </row>
</craft>

Create a pillow <part> by scaling a <cylinder> so it becomes more flat.

<craft>
    <part name="pillow">
        <cylinder radius="10" height="26" color="white" transform="
            rotateY(90) 
            scale(1,1,0.4)
        "></cylinder>
    </part>
    
    <pillow></pillow>
    
</craft>

Now <stack> the mattress and frame base together, centering and aligning with the pillow using <g>. At this point, we can name the lower cube "base" using the class attribute for future stacking purposes.

<craft>
    <part name="pillow">
        <cylinder radius="10" height="26" color="white" transform="
            rotateY(90) 
            scale(1,1,0.4)
        "></cylinder>
    </part>
    
    <g layout="centerX() alignY(5%) lineupZ()">
        <stack>
            <cube size="40 75 7" color="teal"/>
            <cube size="42 77 5" class="base" color="brown"/>
        </stack>
        
         <pillow></pillow>
    </g>
</craft>

Center and align the headboard using <g>.

<craft>
    <part name="pillow">
        <cylinder radius="10" height="26" color="white" transform="
            rotateY(90) 
            scale(1,1,0.4)
        "></cylinder>
    </part>
    
    <part name="headboard">
        <stack spacing="-27">
            <stack spacing="-22">
                <cylinder radius="20" height="4" transform="
                    rotateX(90) 
                    scale(1,1,0.7)
                "></cylinder>
                <cube size="38 4 30"></cube>
            </stack>
            
            <row spacing="38">
                <repeat n="2">
                    <cube size="2 4 36"></cube>
                </repeat>
            </row>
        </stack>
    </part>
    
    <g layout="centerX() alignY(0%)">
        <g layout="centerX() alignY(5%) lineupZ()">
            <stack>
                <cube size="40 75 7" color="teal"/>
                <cube size="42 77 5" class="base" color="brown"/>
            </stack>
            
             <pillow></pillow>
        </g>
        
        <headboard transform="translate(0,0,-8)"></headboard>
        
    </g>
    
</craft>

Stack the assembled bed over the front legs, using the at and layout attributes.

<craft name="bed">
    <part name="pillow">
        <cylinder radius="10" height="26" color="white" transform="
            rotateY(90) 
            scale(1,1,0.4)
        "></cylinder>
    </part>
    
    <part name="headboard">
        <stack spacing="-27" color="brown">
            <stack spacing="-22">
                <cylinder radius="20" height="4" 
                    transform="rotateX(90) 
                               scale(1,1,0.7)
                "></cylinder>
                <cube size="38 4 30"></cube>
            </stack>
            
            <row spacing="38">
                <repeat n="2">
                    <cube size="2 4 36"></cube>
                </repeat>
            </row>
        </stack>
    </part>
    
    <stack spacing="-8" layout="alignY(100%)">
        <g layout="centerX() alignY(0%)">
            <g layout="centerX() alignY(5%) lineupZ()">
                <stack>
                    <cube size="40 75 7" color="teal"/>
                    <cube size="42 77 5" class="base" color="brown"/>
                </stack>
                
                 <pillow></pillow>
            </g>
            
            <headboard transform="translate(0,0,-8)"></headboard>
            
        </g> 
        
        <row spacing="36">
            <cube size="3 3 8"></cube>
            <cube size="3 3 8"></cube>
        </row>
    </stack>
    
</craft>

Now that we've finalized the dimensions, parameterize length and leg height.

<craft name="bed">
    <parameter name="length" type="int" default="75"/>
    <parameter name="legHeight" type="int" default="8"/>

    <style>
        .base { color: brown; }
        .mattress { color: teal; }
        .legs { color: brown; }
        headboard { color: brown; }
        pillow { color: white; }
    </style>
    
    <part name="headboard">
        <stack spacing="-27">
            <stack spacing="-22">
                <cylinder radius="20" height="4" t="
                    rotateX(90) 
                    scale(1,1,0.7)
                "/>
                <cube size="38 4 30"/>
            </stack>
            
            <row spacing="38">
                <repeat n="2">
                    <cube size="2 4 {{(legHeight * 2 + 20)}}"/>
                </repeat>
            </row>
        </stack>
    </part>
    
    <part name="pillow">
        <cylinder radius="10" height="26" t="
            rotateY(90) 
            scale(1,1,0.4)
        "/>
    </part>

    <stack spacing="{{-legHeight}}" l="alignY(100%)">
        <g l="centerX() alignY(0%)">
            <headboard t="translate({{0,0,-legHeight}})"/>
            
            <stack l="alignY(5%)">
                <pillow></pillow>
                
                <stack>
                    <cube size="40 {{length}} 7" class="mattress"/>
                    <cube size="42 {{length + 2}} 5" class="base"/>
                </stack>
            </stack>
        </g>  

        <row spacing="36" class="legs">
            <cube size="3 3 {{legHeight}}"></cube>
            <cube size="3 3 {{legHeight}}"></cube>
        </row>
    </stack>
    
</craft>
<craft name="bed">
<param name="length" type="int" default="75"/>
<param name="legHeight" type="int" default="8"/>
<style>
.base { color: brown; }
.mattress { color: teal; }
.legs { color: brown; }
headboard { color: brown; }
pillow { color: white; }
</style>
<part name="headboard">
<stack spacing="-27">
<stack spacing="-22">
<cylinder radius="20" height="4" t="
rotateX(90)
scale(1,1,0.7)
"/>
<cube size="38 4 30"/>
</stack>
<row spacing="38">
<repeat n="2">
<cube size="2 4 {{(legHeight * 2 + 20)}}"/>
</repeat>
</row>
</stack>
</part>
<part name="pillow">
<cylinder radius="10" height="26" t="
rotateY(90)
scale(1,1,0.4)
"/>
</part>
<!-- Assembled Bed -->
<stack spacing="{{-legHeight}}" l="alignY(100%)">
<g l="centerX() alignY(0%)">
<headboard t="translate({{0,0,-legHeight}})"/>
<stack l="alignY(5%)">
<pillow></pillow>
<stack>
<cube size="40 {{length}} 7" class="mattress"/>
<cube size="42 {{length + 2}} 5" class="base"/>
</stack>
</stack>
</g>
<!-- Front Legs -->
<row spacing="36" class="legs">
<cube size="3 3 {{legHeight}}"></cube>
<cube size="3 3 {{legHeight}}"></cube>
</row>
</stack>
</craft>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment