Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
- Added also per-instance culling for updates, for instances inside the visible cells. The cost of it is meager next to the savings it gives when viewing maps. That being said, it doesn't support per-instance culling for rendering, need to think more for that.
- MDX billboarded nodes now have the correct orientation, check with #27.
  • Loading branch information
flowtsohg committed Oct 3, 2019
1 parent d8bbfa3 commit b67d554
Show file tree
Hide file tree
Showing 22 changed files with 169 additions and 117 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mdx-m3-viewer",
"version": "4.8.0",
"version": "4.8.1",
"description": "A browser WebGL model viewer. Mainly focused on models of the games Warcraft 3 and Starcraft 2.",
"main": "src/index.js",
"directories": {
Expand Down
15 changes: 15 additions & 0 deletions src/common/gl-matrix-addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ function distanceToPlane2(plane, px, py) {
return plane[0] * px + plane[1] * py + plane[3];
}

/**
* Get the distance of a point from a plane.
* dot(plane, vec4(point, 1))
*
* @param {vec4} plane
* @param {number} px
* @param {number} py
* @param {number} pz
* @return {number}
*/
function distanceToPlane3(plane, px, py, pz) {
return plane[0] * px + plane[1] * py + plane[2] * pz + plane[3];
}

/**
* Normalize a plane. Note that this is not the same as normalizing a vec4.
*
Expand Down Expand Up @@ -176,6 +190,7 @@ export {
unproject,
distanceToPlane,
distanceToPlane2,
distanceToPlane3,
normalizePlane,
unpackPlanes,
getRotationX,
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import viewer from './viewer';
import utils from './utils';

export default {
version: '4.8.0',
version: '4.8.1',
common,
parsers,
viewer,
Expand Down
23 changes: 18 additions & 5 deletions src/viewer/camera.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {vec3, vec4, quat, mat4} from 'gl-matrix';
import {unproject, distanceToPlane, distanceToPlane2, unpackPlanes, VEC3_UNIT_Y, VEC3_UNIT_X, VEC3_UNIT_Z} from '../common/gl-matrix-addon';
import {unproject, distanceToPlane2, distanceToPlane3, unpackPlanes, VEC3_UNIT_Y, VEC3_UNIT_X, VEC3_UNIT_Z} from '../common/gl-matrix-addon';

let vectorHeap = vec3.create();
let vectorHeap2 = vec3.create();
Expand Down Expand Up @@ -317,20 +317,33 @@ export default class Camera {
/**
* Test it a sphere with the given center and radius intersects this frustum.
*
* @param {vec3} center
* @param {number} radius
* @param {number} x
* @param {number} y
* @param {number} z
* @param {number} r
* @return {boolean}
*/
testSphere(center, radius) {
testSphere(x, y, z, r) {
for (let plane of this.planes) {
if (distanceToPlane(plane, center) <= -radius) {
if (distanceToPlane3(plane, x, y, z) <= -r) {
return false;
}
}

return true;
}

/**
* @param {ModelInstance} instance
* @return {boolean}
*/
testInstance(instance) {
let location = instance.worldLocation;
let bounds = instance.model.bounds;

return this.testSphere(location[0] + bounds.x, location[1] + bounds.y, location[2], bounds.r);
}

/**
* @param {Cell} cell
* @return {boolean}
Expand Down
4 changes: 2 additions & 2 deletions src/viewer/handlers/geo/model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import TexturedModel from '../../texturedmodel';
import Model from '../../model';

/**
* A geometry model.
*
* Used to render simple geometric shapes.
*/
export default class GeometryModel extends TexturedModel {
export default class GeometryModel extends Model {
/**
* Load the model.
*
Expand Down
2 changes: 1 addition & 1 deletion src/viewer/handlers/m3/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default class M3Layer {

if (active) {
gl.uniform1i(uniforms[uniformMap.map], this.textureUnit);
this.model.bindTexture(this.texture, this.textureUnit, bucket.modelView);
bucket.modelView.bindTexture(this.texture, this.textureUnit);

gl.uniform1f(uniforms[uniformMap.op], this.op);
gl.uniform1f(uniforms[uniformMap.channels], this.colorChannels);
Expand Down
4 changes: 2 additions & 2 deletions src/viewer/handlers/m3/model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Parser from '../../../parsers/m3/model';
import TexturedModel from '../../texturedmodel';
import Model from '../../model';
import M3StandardMaterial from './standardmaterial';
import M3Bone from './bone';
import M3Sequence from './sequence';
Expand All @@ -13,7 +13,7 @@ import M3Region from './region';
/**
* An M3 model.
*/
export default class M3Model extends TexturedModel {
export default class M3Model extends Model {
/**
* @param {Object} resourceData
*/
Expand Down
18 changes: 7 additions & 11 deletions src/viewer/handlers/mdx/eventobjectemitterview.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {vec2} from 'gl-matrix';

// Heap allocations needed for this module.
let track = vec2.create();
let valueHeap = new Uint32Array(1);

/**
* An event object emitter view.
Expand All @@ -14,33 +12,31 @@ export default class EventObjectEmitterView {
constructor(instance, emitter) {
this.instance = instance;
this.emitter = emitter;
this.lastTrack = new Uint16Array(2); // Support more than 256 keyframes per sequence, why not.
this.lastValue = 0;
this.currentEmission = 0;
}

/**
*
*/
reset() {
this.lastTrack.fill(0);
this.lastValue = 0;
}

/**
*
*/
update() {
if (this.instance.allowParticleSpawn) {
let emitter = this.emitter;
let lastTrack = this.lastTrack;
this.emitter.getValue(valueHeap, this.instance);

emitter.getValue(track, this.instance);
let value = valueHeap[0];

if (track[0] === 1 && (track[0] !== lastTrack[0] || track[1] !== lastTrack[1])) {
if (value === 1 && value !== this.lastValue) {
this.currentEmission += 1;
}

lastTrack[0] = track[0];
lastTrack[1] = track[1];
this.lastValue = value;
}
}
}
2 changes: 1 addition & 1 deletion src/viewer/handlers/mdx/eventobjectsplemitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class EventObjectSplEmitter extends GeometryEmitter {

gl.blendFunc(modelObject.blendSrc, modelObject.blendDst);

model.bindTexture(modelObject.internalResource, 0, modelView);
modelView.bindTexture(modelObject.internalResource, 0);

gl.uniform1f(uniforms.u_emitter, EMITTER_SPLAT);

Expand Down
2 changes: 1 addition & 1 deletion src/viewer/handlers/mdx/eventobjectsplubr.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const vertexHeap = vec3.create();
*/
export default class EventObjectSplUbr {
/**
* @param {MdxEventObjectEmitter} emitter
* @param {EventObjectSplEmitter|EventObjectUbrEmitter} emitter
*/
constructor(emitter) {
this.emitter = emitter;
Expand Down
2 changes: 1 addition & 1 deletion src/viewer/handlers/mdx/eventobjectubremitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class EventObjectUbrEmitter extends GeometryEmitter {

gl.blendFunc(modelObject.blendSrc, modelObject.blendDst);

model.bindTexture(modelObject.internalResource, 0, modelView);
modelView.bindTexture(modelObject.internalResource, 0);

gl.uniform1f(uniforms.u_emitter, EMITTER_UBER);

Expand Down
6 changes: 3 additions & 3 deletions src/viewer/handlers/mdx/model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Parser from '../../../parsers/mdlx/model';
import TexturedModel from '../../texturedmodel';
import Model from '../../model';
import TextureAnimation from './textureanimation';
import Layer from './layer';
import GeosetAnimation from './geosetanimation';
Expand All @@ -21,7 +21,7 @@ import CollisionShape from './collisionshape';
/**
* An MDX model.
*/
export default class MdxModel extends TexturedModel {
export default class MdxModel extends Model {
/**
* @param {Object} resourceData
*/
Expand Down Expand Up @@ -504,7 +504,7 @@ export default class MdxModel extends TexturedModel {
// Used for layers that use image animations, in order to scale the coordinates to match the generated texture atlas
gl.uniform2f(uniforms.u_uvScale, 1 / layer.uvDivisor[0], 1 / layer.uvDivisor[1]);

this.bindTexture(texture, 0, bucket.modelView);
bucket.modelView.bindTexture(texture, 0);

let geosetColor = attribs.a_geosetColor;
let uvTransRot = attribs.a_uvTransRot;
Expand Down
Loading

0 comments on commit b67d554

Please sign in to comment.