Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 695f5c4bda6a141b3fee5f46e74e80178b8792df 0 parents
@robashton authored
Showing with 544 additions and 0 deletions.
  1. +1 −0  .gitignore
  2. +39 −0 LICENSE
  3. +55 −0 config.js
  4. +86 −0 directorywalker.js
  5. +23 −0 package.json
  6. +70 −0 processor.js
  7. +28 −0 swallow-build.js
  8. +5 −0 test/in/assets/models/hovercraft.js
  9. +11 −0 test/in/assets/shaders/colour.fragment
  10. +13 −0 test/in/assets/shaders/colour.shader
  11. +6 −0 test/in/assets/shaders/default.fragment
  12. +8 −0 test/in/assets/shaders/default.shader
  13. +28 −0 test/in/assets/shaders/landscape.fragment
  14. +20 −0 test/in/assets/shaders/landscape.shader
  15. +15 −0 test/in/assets/shaders/particles.fragment
  16. +35 −0 test/in/assets/shaders/particles.shader
  17. +23 −0 test/in/assets/shaders/specular.fragment
  18. +33 −0 test/in/assets/shaders/specular.shader
  19. +19 −0 test/in/assets/shaders/texture.fragment
  20. +22 −0 test/in/assets/shaders/texture.shader
  21. BIN  test/in/assets/sounds/pigeon.wav
  22. BIN  test/in/assets/sounds/star.wav
  23. BIN  test/in/assets/textures/Hovercraft.jpg
  24. BIN  test/in/assets/textures/bars.jpg
  25. BIN  test/in/assets/textures/cartoonterrain.jpg
  26. BIN  test/in/assets/textures/cloud.png
  27. BIN  test/in/assets/textures/grass.jpg
  28. BIN  test/in/assets/textures/grassdetail.jpg
  29. BIN  test/in/assets/textures/gridhigh.jpg
  30. BIN  test/in/assets/textures/gridlow.jpg
  31. BIN  test/in/assets/textures/particle.png
  32. BIN  test/in/assets/textures/pigeon-particle.png
  33. BIN  test/in/assets/textures/pigeon.png
  34. BIN  test/in/assets/textures/plane.png
  35. BIN  test/in/assets/textures/star-particle.png
  36. BIN  test/in/assets/textures/star.png
  37. BIN  test/in/assets/textures/testbump.jpg
  38. BIN  test/in/assets/textures/tree.png
  39. +4 −0 test/in/config.json
1  .gitignore
@@ -0,0 +1 @@
+assets.json
39 LICENSE
@@ -0,0 +1,39 @@
+Copyright 2011 Rob Ashton (the "Author")
+All rights reserved.
+
+MIT +no-false-attribs License
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+Distributions of all or part of the Software intended to be used
+by the recipients as they would use the unmodified Software,
+containing modifications that substantially alter, remove, or
+disable functionality of the Software, outside of the documented
+configuration mechanisms provided by the Software, shall be
+modified such that the Author's bug reporting email addresses and
+urls are either replaced with the contact information of the
+parties responsible for the changes, or removed entirely.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+
+Except where noted, this license applies to any and all software
+programs and associated documentation files created by the
+Author, when distributed with the Software.
55 config.js
@@ -0,0 +1,55 @@
+var fs = require('fs');
+var path = require('path');
+
+var Config = function(data) {
+ var self = this;
+
+ data.in = path.join(data.baseDir, data.in);
+ data.out = path.join(data.baseDir, data.out);
+
+ self.forEachInput = function(callback) {
+ callback(data.in, {
+ out: data.out,
+ getHandlerForFile: self.getHandlerForFile
+ });
+ };
+
+ self.getHandlerForFile = function(filename) {
+ var extension = path.extname(filename);
+ switch(extension) {
+ case '.jpg':
+ case '.png':
+ case '.wav':
+ return processBinaryHandler;
+ default:
+ return unknownHandler;
+ };
+ };
+
+ var processBinaryHandler = function(input, callback) {
+ fs.readFile(input, function(err, data) {
+ var translated = new Buffer(data).toString('base64');
+ return callback(translated);
+ });
+ };
+
+ var unknownHandler = function(input, callback) {
+ return callback('');
+ };
+};
+
+Config.LoadFrom = function(filename, callback) {
+ fs.readFile(filename, function(err, data) {
+ if(err)
+ return callback(err);
+ var input = JSON.parse(data);
+ input.baseDir = input.baseDir || path.dirname(filename);
+ var config = new Config(input);
+
+ callback(null, config);
+ });
+};
+
+exports.Config = Config;
+
+
86 directorywalker.js
@@ -0,0 +1,86 @@
+var fs = require('fs');
+var path = require('path');
+var util = require('util');
+var EventEmitter = require('events').EventEmitter;
+
+var DirectoryWalker = function(directory) {
+ EventEmitter.call(this);
+
+ this.directory = directory;
+};
+util.inherits(DirectoryWalker, EventEmitter);
+
+DirectoryWalker.prototype.handleError = function(err) {
+ this.emit('error', err);
+};
+
+DirectoryWalker.prototype.run = function() {
+ var self = this;
+
+ var directoryChildren = null;
+ var processedCount = 0;
+ var directory = self.directory;
+
+ var onDirectoryRead = function(err, children) {
+ if(err)
+ return self.handleError(err);
+
+ directoryChildren = children;
+ processedCount = 0;
+ self.emit('started');
+ processChildren();
+ };
+
+ var processChildren = function() {
+ for(var i in directoryChildren) {
+ processChild(directoryChildren[i]);
+ }
+ };
+
+ var processChild = function(child) {
+ var fullPath = path.join(directory, child);
+ fs.stat(fullPath, function(err, stat) {
+ if(stat && stat.isDirectory())
+ processChildDirectory(fullPath);
+ else
+ processChildFile(fullPath);
+ });
+ };
+
+ var processChildDirectory = function(fullPath) {
+ self.emit('directory', fullPath);
+ var walker = new DirectoryWalker(fullPath);
+ walker.on('completed', onChildWalkerCompleted);
+ walker.on('file', onChildFile);
+ walker.run();
+ };
+
+ var onChildWalkerCompleted = function() {
+ increaseProcessedCount();
+ };
+
+ var onChildFile = function(fullPath) {
+ self.emit('file', fullPath);
+ };
+
+ var processChildFile = function(fullPath) {
+ self.emit('file', fullPath);
+ increaseProcessedCount();
+ };
+
+ var increaseProcessedCount = function() {
+ processedCount++;
+ if(processedCount === directoryChildren.length)
+ self.emit('completed');
+ };
+
+ var handleError = function(err) {
+ return self.emit('error', err);
+ };
+
+ fs.readdir(directory, onDirectoryRead);
+};
+
+exports.DirectoryWalker = DirectoryWalker;
+
+
23 package.json
@@ -0,0 +1,23 @@
+{ "name" : "Swallow"
+, "description" : "A resource manager for delivering and reading content in browser-based games, work in progress"
+, "keywords" : [ "resource manager", "resources", "games", "browser", "delivery" ]
+, "version" : "0.1"
+, "preferGlobal" : false
+, "homepage" : "http://github.com/robashton/swallow"
+, "author" : "Rob Ashton <robashton@codeofrob.com> (http://codeofrob.com)"
+, "repository" :
+ { "type" : "git"
+ , "url" : "https://github.com/robashton/swallow"
+ }
+, "bugs" :
+ { "email" : "swallow@codeofrob.com"
+ , "url" : "http://github.com/robashton/swallow/issues"
+ }
+, "main" : "./swallow-build.js"
+, "bin" : { "swallow" : "./swallow-build.js" }
+, "licenses" :
+ [ { "type" : "MIT +no-false-attribs"
+ , "url" : "http://github.com/robashton/swallow/raw/master/LICENSE"
+ }
+ ]
+}
70 processor.js
@@ -0,0 +1,70 @@
+var DirectoryWalker = require('./directorywalker').DirectoryWalker;
+var EventEmitter = require('events').EventEmitter;
+var util = require('util');
+var fs = require('fs');
+
+var Processor = function(directory, inputConfig) {
+ this.directory = directory;
+ this.inputConfig = inputConfig;
+};
+util.inherits(Processor, EventEmitter);
+
+Processor.prototype.run = function() {
+ var self = this;
+
+ var completedScan = false;
+ var pendingCount = 0;
+ var directory = this.directory;
+ var inputConfig = this.inputConfig;
+ var data = {};
+
+ var execute = function() {
+ var walker = new DirectoryWalker(directory);
+ completedScan = false;
+ pendingCount = 0;
+
+ walker.on('file', function(fullPath) {
+ onFileStarted();
+ var handler = inputConfig.getHandlerForFile(fullPath);
+ var friendlyPath = fullPath.replace(directory, '');
+ handler(fullPath, function(transformed) {
+ data[friendlyPath] = transformed;
+ onFileProcessed(fullPath);
+ });
+ });
+ walker.on('completed', onCompletedScan);
+ walker.run();
+ };
+
+ var onFileStarted = function() {
+ pendingCount++;
+ };
+
+ var onFileProcessed = function(fullPath) {
+ self.emit('file', fullPath);
+ pendingCount--;
+ tryToFinish();
+ };
+
+ var onCompletedScan = function() {
+ completedScan = true;
+ tryToFinish();
+ };
+
+ var tryToFinish = function() {
+ if(pendingCount === 0 && completedScan) {
+ writeToOutputFile();
+ }
+ };
+
+ var writeToOutputFile = function() {
+ fs.writeFile(inputConfig.out, JSON.stringify(data), function(err) {
+ self.emit('completed');
+ });
+ };
+
+ execute();
+};
+
+exports.Processor = Processor;
+
28 swallow-build.js
@@ -0,0 +1,28 @@
+var fs = require('fs');
+var path = require('path');
+var util = require('util');
+
+var Config = require('./config').Config;
+var Processor = require('./processor').Processor;
+
+var configPath = process.argv[2];
+
+var onFileProcessed = function(fullPath) {
+ console.log('Processed: ' + fullPath);
+};
+
+var onProcessorCompleted = function() {
+ console.log('Completed');
+};
+
+var processInput = function(dir, input) {
+ var processor = new Processor(dir, input);
+ processor.on('file', onFileProcessed);
+ processor.on('completed', onProcessorCompleted);
+ processor.run();
+};
+
+Config.LoadFrom(configPath, function(err, config) {
+ if(err) throw err;
+ config.forEachInput(processInput);
+});
5 test/in/assets/models/hovercraft.js
@@ -0,0 +1,5 @@
+{
+ vertices: [0,0,1,2,3,4,5],
+ indices: [2,4,5,6,7,7,8],
+ texture: [2,4,5,6,7,8,8]
+}
11 test/in/assets/shaders/colour.fragment
@@ -0,0 +1,11 @@
+#ifdef GL_ES
+precision highp float;
+#endif
+
+varying vec4 vColour;
+
+
+void main(void) {
+ gl_FragColor = vColour;
+
+}
13 test/in/assets/shaders/colour.shader
@@ -0,0 +1,13 @@
+attribute vec3 aVertexPosition;
+attribute vec4 aVertexColour;
+
+uniform mat4 uProjection;
+uniform mat4 uView;
+uniform mat4 uWorld;
+
+varying vec4 vColour;
+
+void main(void){
+ gl_Position = uProjection * uView * uWorld * vec4(aVertexPosition, 1.0);
+ vColour = aVertexColour;
+}
6 test/in/assets/shaders/default.fragment
@@ -0,0 +1,6 @@
+#ifdef GL_ES
+precision highp float;
+#endif
+void main(void) {
+ gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
+}
8 test/in/assets/shaders/default.shader
@@ -0,0 +1,8 @@
+attribute vec3 aVertexPosition;
+uniform mat4 uProjection;
+uniform mat4 uView;
+uniform mat4 uWorld;
+
+void main(void){
+ gl_Position = uProjection * uView * uWorld * vec4(aVertexPosition, 1.0);
+}
28 test/in/assets/shaders/landscape.fragment
@@ -0,0 +1,28 @@
+#ifdef GL_ES
+precision highp float;
+#endif
+
+varying vec2 vTexCoords;
+varying vec3 vNormal;
+varying float vDistance;
+uniform sampler2D uSampler;
+
+bool isEdge(float value) {
+ return false;
+ if(floor(mod(value,10.0) * 5.0) == 0.0) { return true; }
+ return false;
+}
+
+void main(void) {
+ float x = vTexCoords.x * 100.0;
+ float y = vTexCoords.y * 100.0;
+
+ vec4 grid = vec4(1,1,1,1);
+ if(isEdge(x) || isEdge(y)) {
+ grid = vec4(0.4, 0.4, 0.4, 1.0);
+ }
+
+ gl_FragColor = texture2D(uSampler, vec2(vTexCoords.s, vTexCoords.t));
+ gl_FragColor *= grid;
+ gl_FragColor.g *= (vNormal.y * vNormal.y); // Want it a little darker k?
+}
20 test/in/assets/shaders/landscape.shader
@@ -0,0 +1,20 @@
+attribute vec3 aVertexPosition;
+attribute vec3 aNormal;
+attribute vec2 aTextureCoord;
+
+uniform mat4 uProjection;
+uniform mat4 uView;
+uniform vec3 uLightPosition;
+
+varying vec3 vNormal;
+varying vec2 vTexCoords;
+varying float vDistance;
+
+void main(void){
+ vTexCoords = aTextureCoord;
+ vNormal = normalize(aNormal);
+ vec4 transformedPosition = uView * vec4(aVertexPosition, 1.0);
+
+ vDistance = transformedPosition.w;
+ gl_Position = uProjection * transformedPosition;
+}
15 test/in/assets/shaders/particles.fragment
@@ -0,0 +1,15 @@
+#ifdef GL_ES
+precision highp float;
+#endif
+
+varying float life;
+varying vec3 vColour;
+
+uniform sampler2D uSampler;
+uniform float time;
+
+void main(void) {
+ vec4 texColor = texture2D(uSampler, gl_PointCoord);
+ vec4 colour = vec4(vColour, life);
+ gl_FragColor = colour * texColor;
+}
35 test/in/assets/shaders/particles.shader
@@ -0,0 +1,35 @@
+attribute vec3 aVertexPosition;
+attribute vec3 aVelocity;
+attribute vec3 aColour;
+attribute float aSize;
+attribute float aCreationTime;
+attribute float aLifetime;
+
+uniform mat4 uProjection;
+uniform mat4 uView;
+
+uniform float maxsize;
+uniform float time;
+uniform vec3 vCamera;
+
+varying vec3 vColour;
+varying float life;
+
+void main(void){
+
+ float age = (time - aCreationTime);
+ vec3 position = aVertexPosition + (aVelocity * age);
+ vColour = aColour;
+
+ vec3 vectorToPoint = (position - vCamera);
+ float distanceSquared = abs(dot(vectorToPoint, vectorToPoint));
+ float scale = clamp(distanceSquared, 1.0, 10000.0);
+
+
+
+ life = 1.0 - (age / aLifetime);
+ life = clamp(life, 0.0, 1.0);
+
+ gl_PointSize = (aSize * maxsize) / (scale / 100.0);
+ gl_Position = uProjection * uView * vec4(position, 1.0);
+}
23 test/in/assets/shaders/specular.fragment
@@ -0,0 +1,23 @@
+#ifdef GL_ES
+precision highp float;
+#endif
+
+varying vec2 vTextureCoord;
+varying vec3 vDirectionToLight;
+varying vec3 vHalfNormal;
+varying vec3 vNormal;
+
+uniform sampler2D uBumpSampler;
+
+void main(void) {
+
+ // Sample the texture to get our new normal in texture space
+ vec3 bumpNormal = (texture2D(uBumpSampler, vTextureCoord.st)).rgb * 2.0 - 1.0;
+
+ vec3 diffuse = clamp(dot(bumpNormal, vDirectionToLight), 0.0, 1.0) * vec3(1.0, 1.0, 1.0);
+ float specularComponent = clamp(dot(bumpNormal, normalize(vHalfNormal)), 0.0, 1.0);
+
+ vec3 specular = pow(specularComponent, 64.0) * vec3(0.4,0.4,0.9);
+ vec3 light = diffuse + specular;
+ gl_FragColor = vec4(light, 1.0);
+}
33 test/in/assets/shaders/specular.shader
@@ -0,0 +1,33 @@
+attribute vec3 aVertexPosition;
+attribute vec3 aNormal;
+attribute vec2 aTextureCoord;
+
+uniform mat4 uProjection;
+uniform mat4 uView;
+uniform mat4 uWorld;
+uniform mat3 uNormal;
+uniform vec3 uLightPosition;
+
+varying vec2 vTextureCoord;
+varying vec3 vDirectionToLight;
+varying vec3 vHalfNormal;
+varying vec3 vNormal;
+
+void main(void){
+ vec3 tangent = normalize(uNormal * vec3(1,0,0));
+ vec3 normal = normalize(uNormal * aNormal);
+ vec3 bitangent = normalize(cross(tangent, normal));
+ mat3 transform = mat3(tangent, bitangent, normal);
+
+ vec4 transformedPosition = uView * uWorld * vec4(aVertexPosition, 1.0);
+ vec3 directionToCamera = normalize(-vec3(transformedPosition));
+ vec3 directionToLight = normalize(uLightPosition - vec3(transformedPosition));
+
+ // Transform vectors to texture space
+ vDirectionToLight = directionToLight * transform;
+ vHalfNormal = normalize(vDirectionToLight + (directionToCamera * transform));
+
+ vNormal = normal * transform;
+ vTextureCoord = aTextureCoord;
+ gl_Position = uProjection * transformedPosition;
+}
19 test/in/assets/shaders/texture.fragment
@@ -0,0 +1,19 @@
+#ifdef GL_ES
+precision highp float;
+#endif
+
+varying vec2 vTextureCoords;
+varying vec3 vNormal;
+varying vec3 vVectorToLight;
+uniform sampler2D uSampler;
+
+void main(void) {
+ vec3 normal = normalize(vNormal);
+ vec3 vector = normalize(vVectorToLight);
+
+ vec4 colour = texture2D(uSampler, vec2(vTextureCoords.s, vTextureCoords.t));
+ float diffuse = max(dot(normal, vector), 0.0);
+ vec4 lightColour = vec4(colour.rgb * 0.8 * diffuse, 0.5);
+ vec4 ambientColour = vec4(colour.rgb * 0.2, 0.5);
+ gl_FragColor = lightColour + ambientColour;
+}
22 test/in/assets/shaders/texture.shader
@@ -0,0 +1,22 @@
+attribute vec3 aVertexPosition;
+attribute vec2 aTextureCoords;
+attribute vec3 aNormals;
+
+uniform mat4 uProjection;
+uniform mat4 uView;
+uniform mat4 uWorld;
+uniform mat3 uNormalMatrix;
+
+varying vec2 vTextureCoords;
+varying vec3 vNormal;
+varying vec3 vVectorToLight;
+
+void main(void){
+ vec4 lightPosition = vec4(0.0 ,1000.0, 0.0, 0.0);
+ vec4 transformedPosition = uView * uWorld * vec4(aVertexPosition, 1.0);
+ gl_Position = uProjection * transformedPosition;
+
+ vNormal = uNormalMatrix * aNormals;
+ vVectorToLight = vec3(lightPosition - transformedPosition);
+ vTextureCoords = aTextureCoords;
+}
BIN  test/in/assets/sounds/pigeon.wav
Binary file not shown
BIN  test/in/assets/sounds/star.wav
Binary file not shown
BIN  test/in/assets/textures/Hovercraft.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/bars.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/cartoonterrain.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/cloud.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/grass.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/grassdetail.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/gridhigh.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/gridlow.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/particle.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/pigeon-particle.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/pigeon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/plane.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/star-particle.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/star.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/testbump.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  test/in/assets/textures/tree.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 test/in/config.json
@@ -0,0 +1,4 @@
+{
+ "in": "./assets",
+ "out": "../assets.json"
+}
Please sign in to comment.
Something went wrong with that request. Please try again.