<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jb &#187; o3d</title>
	<atom:link href="http://jb55.com/category/programming/o3d/feed/" rel="self" type="application/rss+xml" />
	<link>http://jb55.com</link>
	<description>I code</description>
	<lastBuildDate>Sun, 02 May 2010 17:41:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Static O3D Dependencies</title>
		<link>http://jb55.com/17/static-o3d-dependencies/</link>
		<comments>http://jb55.com/17/static-o3d-dependencies/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 14:47:51 +0000</pubDate>
		<dc:creator>jb55</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[o3d]]></category>

		<guid isPermaLink="false">http://jb55.com/?p=17</guid>
		<description><![CDATA[One thing you might notice when writing your first O3D javascript application are the dependencies which are included at the top of your page:
o3djs.require('o3djs.util');
o3djs.require('o3djs.math');
o3djs.require('o3djs.rendergraph');
o3djs.require('o3djs.primitives');
o3djs.require('o3djs.effect');
o3djs.require('o3djs.io');
o3djs.require('o3djs.arcball');
o3djs.require('o3djs.material');
o3djs.require('o3djs.quaternion');
O3D will use these calls to grab the corresponding script using an XHR request, then write them to the DOM if they haven&#8217;t been written yet. This dynamic resolution of dependencies is [...]]]></description>
			<content:encoded><![CDATA[<p>One thing you might notice when writing your first O3D javascript application are the dependencies which are included at the top of your page:</p>
<p><code class="javascript">o3djs.require('o3djs.util');<br />
o3djs.require('o3djs.math');<br />
o3djs.require('o3djs.rendergraph');<br />
o3djs.require('o3djs.primitives');<br />
o3djs.require('o3djs.effect');<br />
o3djs.require('o3djs.io');<br />
o3djs.require('o3djs.arcball');<br />
o3djs.require('o3djs.material');<br />
o3djs.require('o3djs.quaternion');</code></p>
<p>O3D will use these calls to grab the corresponding script using an XHR request, then write them to the DOM if they haven&#8217;t been written yet. This dynamic resolution of dependencies is nice for development, since you can add and remove &#8216;headers&#8217; on the fly without having to worry about dependencies or modifying your html. The main problem with this method is that if you use o3d alot, the extra requests to your server may seem a bit unnecessary.</p>
<p>My solution was as follows:</p>
<ol>
<li>Determine which scripts are written to the DOM and in what order</li>
<li>Using a makefile, concatenate the files</li>
<li>Remove all require() calls using sed</li>
<li>Compress the javascript using Yahoo&#8217;s YUI compressor</li>
</ol>
<h3>Determining dependencies</h3>
<p>This step is pretty straightforward. Load your o3d application as usual and then use a DOM inspector such as Firebug, or the built in Safari/Chrome Web Inspector, and take a look in your &lt;head&gt; tag. You should see the injected o3d javascript (under /o3djs/):</p>
<div class="wp-caption aligncenter" style="width: 461px"><img title="o3d dependencies" src="http://static.jgblue.com/img/blog/o3d_depends.png" alt="Injected o3d dependencies" width="451" height="391" /><p class="wp-caption-text">Injected o3d dependencies</p></div>
<p>Keep note of these, as they will be used in the next step</p>
<h3>The makefile</h3>
<p>Using a makefile made the process of concatenating and compressing the javascript extremely simple and streamlined. Here&#8217;s the makefile I used to pull this off:</p>
<pre># This is just the root of my static content directory
# you can remove this if you don't need it
STATIC=../../static/ 

# The folder where the the final compressed
# javascript is installed to after 'make install'
PUBDIR=$(STATIC)js/ 

# A temporary folder used to store
# generated javascript (the concatenated/compressed file)
OUTDIR=./build/ 

# Path to the YUI compressor
COMPRESSOR=../yui/build/yuicompressor-2.4.2.jar

# YUI compressor arguments
COMPRESSOR_ARGS=
VPATH=$(OUTDIR)

# Files to copy to the PUBDIR directory on 'make install'
INST_TARGETS=$(OUTDIR)jgblue3d.js

# The location of your o3djs folder
O3D=$(STATIC)j3d/o3djs/

O3D_TARGETS=$(O3D)base.js $(O3D)util.js $(O3D)event.js \
    $(O3D)error.js $(O3D)math.js $(O3D)rendergraph.js $(O3D)primitives.js \
    $(O3D)effect.js $(O3D)io.js $(O3D)arcball.js $(O3D)quaternions.js \
    $(O3D)material.js $(O3D)jgblue-3d.js

jgblue3d.js: combined3d.js
    java -jar $(COMPRESSOR) $(COMPRESSOR_ARGS) $(OUTDIR)$&lt; -o $(OUTDIR)$@

combined3d.js: $(O3D_TARGETS)
    cat $^ &gt; $(OUTDIR)$@
    sed -e 's/^o3djs\.require\(.*\);$$//g' $(OUTDIR)$@ &gt; $(OUTDIR)$@.clean
    mv $(OUTDIR)$@.clean $(OUTDIR)$@

.PHONY: install clean all

all: jgblue3d.js

install:
    cp $(INST_TARGETS) $(PUBDIR)

clean:
    rm $(OUTDIR)*.js</pre>
<p>To get this working, you&#8217;ll need to change some of the variables to fit your project. The most important thing that you will need to change is the <span style="font-family: monospace">O3D_TARGETS</span> variable. This variable holds <span style="font-family: monospace">base.js</span>, the path to all of the o3d dependencies (gathered in step 1), and finally your o3d code.</p>
<p>Now all you have to do is type:<br />
<code>make &#038;&#038; make install</code></p>
<h3>You&#8217;re done!</h3>
<p>You can now remove the base.js from your html and add the newly generated javascript <a href="http://static.jgblue.com/js/jgblue3d.js">file</a>! It should work exactly the same as the original**. </p>
<p><small>** No promises ;)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://jb55.com/17/static-o3d-dependencies/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
