-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e5beb6
commit 5b242f7
Showing
4 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |