Skip to content
This repository has been archived by the owner on Feb 18, 2020. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcozzi committed Mar 16, 2015
1 parent b86613a commit 9600399
Show file tree
Hide file tree
Showing 26 changed files with 793 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/node_modules
.DS_Store
Thumbs.db
7 changes: 7 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"node": true,
"predef": [
"describe",
"it"
]
}
16 changes: 16 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>collada2gltf-web-service</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.eclipsesource.jshint.ui.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
</natures>
</projectDescription>
3 changes: 3 additions & 0 deletions .settings/com.eclipsesource.jshint.ui.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eclipse.preferences.version=1
included=//*.js
projectSpecificOptions=true
2 changes: 2 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/.jshintrc=UTF-8
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.10"
36 changes: 36 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Copyright 2015 Analytical Graphics, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Third-Party Code
================

### Node.js in Practice

> https://github.com/alexyoung/nodeinpractice
> The MIT License (MIT)
> Copyright (c) 2014 Alex Young
> 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.
> 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.
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,59 @@
# collada2gltf-web-service
Simple Node.js web service to convert 3D models from COLLADA to glTF using COLLADA2GLTF

<p align="center">
<a href="https://www.khronos.org/gltf"><img src="doc/gltf.png" /></a>
</p>

Simple web service to convert 3D models from COLLADA to glTF using COLLADA2GLTF.

[![Build Status](https://travis-ci.org/AnalyticalGraphicsInc/collada2gltf-web-service.svg?branch=master)](https://travis-ci.org/AnalyticalGraphicsInc/collada2gltf-web-service)

## Overview

This is a code sample that can serve as a starting point for a Node.js web service that converts COLLADA models to
glTF. This version just converts a .dae file to a .gltf with embedded geometry, animations, skins, and shaders. It does not handle textures.

## Install

Install [Node.js](http://nodejs.org/), then run:
```
npm install collada2gltf-web-service
```

## Usage

Start server using [nodemon](http://nodemon.io/) so it is automatically restarted when the code changes:
```
npm start
```

Invoke the web service by issuing an HTTP request and providing the COLLADA model with POST:
```
curl -X POST -H "Content-Type:text/plain" -d @test/data/box.dae localhost:3000/convert
```

A few settings can be changed by modifying [config.json](config.json). This is loaded using [nconf](https://www.npmjs.com/package/nconf) so environment variables and command-line arguments can override this.

## Tests and JSHint

Run the tests:
```
npm test
```

Run JSHint:
```
npm jshint
```

## Resources

* [9 uses for cURL worth knowing](http://httpkit.com/resources/HTTP-from-the-Command-Line/)
* [nodeinpractice](https://github.com/alexyoung/nodeinpractice) - book and sample code

***

Developed by <a href="http://www.agi.com/">AGI</a>, founders of the Cesium WebGL engine.
<p align="center">
<a href="http://cesiumjs.org/"><img src="doc/cesium.png" /></a>
</p>
38 changes: 38 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var express = require('express');
var bodyParser = require('body-parser');
var routes = require('./routes');
var nconf = require('./lib/nconf');
var Verbose = require('./lib/Verbose');

var app = express();

// Requests post the .dae file using Content-Type: text/plain
// The upload filesize limit is defined in config.json
app.use(bodyParser.text({
limit : nconf.get('uploadLimit')
}));

// Custom middleware fore enabling CORS. From http://enable-cors.org/server_expressjs.html
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});

// Custom middleware for handling errors
app.use(function(err, req, res, next) {
Verbose.error(err.stack);

res.status(err.statusCode || 500);
res.format({
json: function() {
res.send(err);
}
});
});

// Map, for example, localhost:3000/convert, to the function that does the conversion.
// There is also app.get() for requests that pass parameters with GET.
app.post('/convert', routes.convert.convert);

module.exports = app;
Binary file added collada2gltf/mac/collada2gltf
Binary file not shown.
Binary file added collada2gltf/win32/collada2gltf.exe
Binary file not shown.
6 changes: 6 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"host" : "localhost",
"port" : 3000,
"verbose" : true,
"uploadLimit" : "10mb"
}
Binary file added doc/cesium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/gltf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions lib/Errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var util = require('util');

// Module based on https://github.com/alexyoung/nodeinpractice/tree/master/listings/web/error-handling

function HTTPError() {
Error.call(this, arguments);
}

util.inherits(HTTPError, Error);

function Conversion(message) {
HTTPError.call(this);
Error.captureStackTrace(this, arguments.callee);
this.statusCode = 500;
this.message = message;
this.name = 'Conversion';
}
util.inherits(Conversion, HTTPError);

module.exports = {
HTTPError: HTTPError,
Conversion: Conversion
};
12 changes: 12 additions & 0 deletions lib/Verbose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var nconf = require('./nconf');

var verbose = !!nconf.get('verbose');
var f = function(arg) {};

// Timing, log, and error messages based on the verbose flag in config.json
module.exports = {
log : verbose ? function(message) { console.log(message); } : f,
error : verbose ? function(message) { console.error(message); } : f,
time : verbose ? function(label) { console.time(label); } : f,
timeEnd : verbose ? function(label) { console.timeEnd(label); } : f
};
79 changes: 79 additions & 0 deletions lib/collada2gltf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"use strict";
var fs = require('fs');
var path = require('path');
var child_process = require('child_process');
var temp = require('temp').track();
var Errors = require('./Errors');
var Verbose = require('./Verbose');

module.exports = collada2gltf;

function returnError(err, message, callback) {
temp.cleanup(function(err, stats) {
Verbose.log('Temp cleanup stats: ' + JSON.stringify(stats));
});

process.nextTick(function() {
Verbose.timeEnd('total time');
callback(new Errors.Conversion(message + ': ' + err.message), undefined);
});

return undefined;
}

function collada2gltf(collada, callback) {
Verbose.time('total time');

var collada2gltfPath = '';
if (process.platform === 'darwin') {
collada2gltfPath = 'collada2gltf/mac/collada2gltf';
} else if (process.platform === 'win32') {
// TODO: test this on Windows
collada2gltfPath = 'collada2gltf/win32/collada2gltf.exe';
} else {
process.nextTick(callback('Only Mac and Windows builds of collada2gltf are included. Build from source for your platform: https://github.com/KhronosGroup/glTF', undefined));
return;
}

// 1. Create a temporary directory
// 2. Write the input COLLADA file to the directory
// 3. Run collada2gltf with -e to embed resources
// 4. Read the generated glTF file
// -> Remove the temporary directory on success or failure

temp.mkdir('collada2gltf-web-service', function(err, dirPath) {
if (err) { return returnError(err, '1/4 mkdir', callback); }

var daePath = path.join(dirPath, 'input.dae');
var gltfPath = path.join(dirPath, 'output.gltf');

fs.writeFile(daePath, collada, function(err) {
if (err) { return returnError(err, '2/4 writeFile', callback); }

Verbose.time('collada2gltf time');

child_process.execFile(collada2gltfPath, [
'-f', daePath,
'-o', gltfPath,
'-e'],
function(err, stdout, stderr) {
Verbose.timeEnd('collada2gltf time');

if (err) { return returnError(err, '3/4 execFile', callback); }

fs.readFile(gltfPath, 'utf8', function(err, data){
if (err) { return returnError(err, '4/4 readFile', callback); }

temp.cleanup(function(err, stats) {
Verbose.log('Temp cleanup stats: ' + JSON.stringify(stats));
});

process.nextTick(function() {
Verbose.timeEnd('total time');
callback(undefined, data);
});
});
});
});
});
}
10 changes: 10 additions & 0 deletions lib/nconf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var nconf = require('nconf');

// This module wraps nconf to make sure it is initialized before being used

// Override options in this order: config.json < environment variables < command-line arguments
nconf.argv()
.env()
.file({ file: 'config.json' });

module.exports = nconf;
13 changes: 13 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"verbose": true,
"ignore": [
".git",
"doc",
"README.md",
"LICENSE.md"
],
"watch": [
"**/*.js",
"config.json"
]
}
51 changes: 51 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "collada2gltf-web-service",
"version": "0.0.1",
"description": "Simple web service to convert 3D models from COLLADA to glTF using COLLADA2GLTF.",
"license": "Apache-2.0",
"author": {
"name": "Patrick Cozzi",
"email": "pcozzi@agi.com",
"url": "https://twitter.com/pjcozzi"
},
"keywords": [
"collada",
"gltf"
],
"homepage": "https://github.com/AnalyticalGraphicsInc/collada2gltf-web-service",
"repository": {
"type": "git",
"url": "git@github.com:AnalyticalGraphicsInc/collada2gltf-web-service.git"
},
"bugs": {
"url": "https://github.com/AnalyticalGraphicsInc/collada2gltf-web-service/issues"
},
"scripts": {
"start": "node ./node_modules/nodemon/bin/nodemon server.js",
"test": "npm run jshint && ./node_modules/mocha/bin/mocha test/**/*.js",
"jshint": "./node_modules/jshint/bin/jshint *.js **/*.js"
},
"directories": {},
"dependencies": {
"body-parser": "^1.12.0",
"express": "^4.12.2",
"nconf": "^0.7.1",
"temp": "^0.8.1"
},
"devDependencies": {
"jshint": "^2.6.0",
"mocha": "^2.1.0",
"nodemon": "^1.3.7",
"supertest": "^0.15.0"
},
"engines": {
"node": ">=0.10.x"
},
"jshintConfig": {
"node": true,
"predef": [
"describe",
"it"
]
}
}
11 changes: 11 additions & 0 deletions routes/convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";
var collada2gltf = require('./../lib/collada2gltf.js');

module.exports.convert = function(req, res, next) {
collada2gltf(req.body, function(err, gltf) {
if (err) { return next(err); }

res.setHeader('Content-Type', 'application/json');
res.send(gltf);
});
};
Loading

0 comments on commit 9600399

Please sign in to comment.