Skip to content

Commit

Permalink
Add M2 support on client
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Jan 18, 2015
1 parent 1e5beb6 commit 5b242f7
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
82 changes: 82 additions & 0 deletions lib/client/pipeline/m2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var ArrayUtil, DecodeStream, Decoder, Loader, M2, Skin, THREE, attr;

attr = require('attr-accessor');

ArrayUtil = require('../../utils/array-util');

Decoder = require('blizzardry/lib/m2');

DecodeStream = require('blizzardry/lib/restructure').DecodeStream;

Loader = require('../../net/loader');

Skin = require('./skin');

THREE = require('three');

M2 = (function() {
var get, self;

module.exports = self = M2;

get = attr.accessors(M2)[0];

function M2(data, skin) {
var indices, triangle, vertex, _i, _j, _len, _len1, _ref, _ref1;
this.data = data;
this.skin = skin;
this.geometry = new THREE.Geometry();
_ref = this.data.vertices;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
vertex = _ref[_i];
this.geometry.vertices.push((function(func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor, result = func.apply(child, args);
return Object(result) === result ? result : child;
})(THREE.Vector3, vertex.position, function(){}));
}
_ref1 = this.skin.data.triangles;
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
triangle = _ref1[_j];
indices = triangle.map((function(_this) {
return function(index) {
return _this.skin.data.indices[index];
};
})(this));
this.geometry.faces.push((function(func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor, result = func.apply(child, args);
return Object(result) === result ? result : child;
})(THREE.Face3, indices, function(){}));
}
}

get({
mesh: function() {
var material;
material = new THREE.MeshBasicMaterial({
wireframe: true
});
return new THREE.Mesh(this.geometry, material);
}
});

M2.load = function(path, callback) {
this.loader || (this.loader = new Loader());
return this.loader.load(path, (function(_this) {
return function(raw) {
var data, quality, skinPath, stream;
stream = new DecodeStream(ArrayUtil.toBuffer(raw));
data = Decoder.decode(stream);
quality = data.viewCount - 1;
skinPath = path.replace(/\.m2/i, "0" + quality + ".skin");
return Skin.load(skinPath, function(skin) {
return callback(new self(data, skin));
});
};
})(this));
};

return M2;

})();
32 changes: 32 additions & 0 deletions lib/client/pipeline/m2/skin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var ArrayUtil, DecodeStream, Decoder, Loader, Skin;

ArrayUtil = require('../../utils/array-util');

Decoder = require('blizzardry/lib/m2/skin');

DecodeStream = require('blizzardry/lib/restructure').DecodeStream;

Loader = require('../../net/loader');

Skin = (function() {
var self;

module.exports = self = Skin;

function Skin(data) {
this.data = data;
}

Skin.load = function(path, callback) {
this.loader || (this.loader = new Loader());
return this.loader.load(path, function(raw) {
var data, stream;
stream = new DecodeStream(ArrayUtil.toBuffer(raw));
data = Decoder.decode(stream);
return callback(new self(data));
});
};

return Skin;

})();
39 changes: 39 additions & 0 deletions src/scripts/lib/client/pipeline/m2/index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
attr = require('attr-accessor')
ArrayUtil = require('../../utils/array-util')
Decoder = require('blizzardry/lib/m2')
{DecodeStream} = require('blizzardry/lib/restructure')
Loader = require('../../net/loader')
Skin = require('./skin')
THREE = require('three')

class M2
module.exports = self = this

[get] = attr.accessors(this)

constructor: (@data, @skin) ->
@geometry = new THREE.Geometry()

for vertex in @data.vertices
@geometry.vertices.push new THREE.Vector3 vertex.position...

for triangle in @skin.data.triangles
indices = triangle.map (index) => @skin.data.indices[index]
@geometry.faces.push new THREE.Face3 indices...

get mesh: ->
material = new THREE.MeshBasicMaterial wireframe: true
new THREE.Mesh @geometry, material

@load: (path, callback) ->
@loader ||= new Loader()
@loader.load path, (raw) =>
stream = new DecodeStream(ArrayUtil.toBuffer raw)
data = Decoder.decode stream

# TODO: Allow configuring quality
quality = data.viewCount - 1
skinPath = path.replace /\.m2/i, "0#{quality}.skin"

Skin.load skinPath, (skin) ->
callback new self(data, skin)
16 changes: 16 additions & 0 deletions src/scripts/lib/client/pipeline/m2/skin.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ArrayUtil = require('../../utils/array-util')
Decoder = require('blizzardry/lib/m2/skin')
{DecodeStream} = require('blizzardry/lib/restructure')
Loader = require('../../net/loader')

class Skin
module.exports = self = this

constructor: (@data) ->

@load: (path, callback) ->
@loader ||= new Loader()
@loader.load path, (raw) ->
stream = new DecodeStream(ArrayUtil.toBuffer raw)
data = Decoder.decode stream
callback new self(data)

0 comments on commit 5b242f7

Please sign in to comment.