started slow refactor to break things down for easier understanding

This commit is contained in:
Kirk Spencer 2020-03-25 23:28:47 -07:00
parent dc7f84cdb9
commit b4563afa6e
4 changed files with 243 additions and 246 deletions

6
.editorconfig Normal file
View File

@ -0,0 +1,6 @@
charset = utf-8
indent_size = 2
indent_style = space
insert_final_newline = true
tab_width = 2
trim_trailing_whitespace = true

6
dist/saver.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,45 +1,38 @@
import { Matrix4, Vector3, Vector4 } from "three";
var finalizeMesh = function() {};
finalizeMesh.prototype = {
constructor: finalizeMesh,
parse: function (mesh) {
let matrixRotation = new Matrix4().makeRotationX(90 * Math.PI / 180);
let matrixScale = new Matrix4().makeScale(10, 10, 10);
let vertex = new Vector3();
let geometry = mesh.geometry;
export function parse(mesh) {
if (!mesh.isMesh) {
console.warn('Mesh type unsupported', mesh);
return;
}
var vertex = new Vector3();
var i, l = [];
var nbVertex = 0;
var geometry = mesh.geometry;
var mrot = new Matrix4().makeRotationX(90 * Math.PI / 180);
var msca = new Matrix4().makeScale(10, 10, 10);
if (geometry.isBufferGeometry) {
var newGeometry = geometry.clone(geometry);
var vertices = geometry.getAttribute('position');
// vertices
if (vertices !== undefined) {
for (i = 0, l = vertices.count; i < l; i++ , nbVertex++) {
let verticesCount = vertices.count;
for (let i = 0; i < verticesCount; i++) {
vertex.x = vertices.getX(i);
vertex.y = vertices.getY(i);
vertex.z = vertices.getZ(i);
if (geometry.skinIndexNames == undefined
|| geometry.skinIndexNames == 0) {
vertex.applyMatrix4(mesh.matrixWorld).applyMatrix4(mrot).applyMatrix4(msca);
if (geometry.skinIndexNames == undefined || geometry.skinIndexNames == 0) {
vertex
.applyMatrix4(mesh.matrixWorld)
.applyMatrix4(matrixRotation)
.applyMatrix4(matrixScale);
newGeometry.attributes.position.setXYZ(i, vertex.x, vertex.y, vertex.z);
} else {
var finalVector = new Vector4();
if (geometry.morphTargetInfluences !== undefined) {
var morphVector = new Vector4(vertex.x, vertex.y, vertex.z);
var tempMorph = new Vector4();
@ -57,61 +50,61 @@ finalizeMesh.prototype = {
morphVector.add(tempMorph);
}
for (var si = 0; si < geometry.skinIndexNames.length; si++) {
var skinIndices = geometry.getAttribute([geometry.skinIndexNames[si]])
var weights = geometry.getAttribute([geometry.skinWeightNames[si]])
var skinIndex = [];
skinIndex[0] = skinIndices.getX(i);
skinIndex[1] = skinIndices.getY(i);
skinIndex[2] = skinIndices.getZ(i);
skinIndex[3] = skinIndices.getW(i);
var skinWeight = [];
skinWeight[0] = weights.getX(i);
skinWeight[1] = weights.getY(i);
skinWeight[2] = weights.getZ(i);
skinWeight[3] = weights.getW(i);
var inverses = [];
inverses[0] = mesh.skeleton.boneInverses[skinIndex[0]];
inverses[1] = mesh.skeleton.boneInverses[skinIndex[1]];
inverses[2] = mesh.skeleton.boneInverses[skinIndex[2]];
inverses[3] = mesh.skeleton.boneInverses[skinIndex[3]];
var skinMatrices = [];
skinMatrices[0] = mesh.skeleton.bones[skinIndex[0]].matrixWorld;
skinMatrices[1] = mesh.skeleton.bones[skinIndex[1]].matrixWorld;
skinMatrices[2] = mesh.skeleton.bones[skinIndex[2]].matrixWorld;
skinMatrices[3] = mesh.skeleton.bones[skinIndex[3]].matrixWorld;
for (var k = 0; k < 4; k++) {
if (geometry.morphTargetInfluences !== undefined) {
var tempVector = new Vector4(morphVector.x, morphVector.y, morphVector.z);
} else {
var tempVector = new Vector4(vertex.x, vertex.y, vertex.z);
}
tempVector.multiplyScalar(skinWeight[k]);
//the inverse takes the vector into local bone space
//which is then transformed to the appropriate world space
tempVector.applyMatrix4(inverses[k])
.applyMatrix4(skinMatrices[k])
.applyMatrix4(mrot).applyMatrix4(msca);
finalVector.add(tempVector);
}
for (let j = 0; j < geometry.skinIndexNames.length; j++) {
newFunction_1(geometry, i, j, mesh, morphVector, finalVector);
}
newGeometry.attributes.position.setXYZ(i, finalVector.x, finalVector.y, finalVector.z);
}
}
}
} else {
console.warn( 'Geometry type unsupported', geometry );
console.warn('Geometry type unsupported', geometry);
}
return newGeometry;
}
};
}
function newFunction_1(geometry, i, j, mesh, morphVector, finalVector) {
var skinIndices = geometry.getAttribute([geometry.skinIndexNames[j]]);
var weights = geometry.getAttribute([geometry.skinWeightNames[j]]);
var skinIndex = [];
skinIndex[0] = skinIndices.getX(i);
skinIndex[1] = skinIndices.getY(i);
skinIndex[2] = skinIndices.getZ(i);
skinIndex[3] = skinIndices.getW(i);
var skinWeight = [];
skinWeight[0] = weights.getX(i);
skinWeight[1] = weights.getY(i);
skinWeight[2] = weights.getZ(i);
skinWeight[3] = weights.getW(i);
var inverses = [];
inverses[0] = mesh.skeleton.boneInverses[skinIndex[0]];
inverses[1] = mesh.skeleton.boneInverses[skinIndex[1]];
inverses[2] = mesh.skeleton.boneInverses[skinIndex[2]];
inverses[3] = mesh.skeleton.boneInverses[skinIndex[3]];
var skinMatrices = [];
skinMatrices[0] = mesh.skeleton.bones[skinIndex[0]].matrixWorld;
skinMatrices[1] = mesh.skeleton.bones[skinIndex[1]].matrixWorld;
skinMatrices[2] = mesh.skeleton.bones[skinIndex[2]].matrixWorld;
skinMatrices[3] = mesh.skeleton.bones[skinIndex[3]].matrixWorld;
for (var k = 0; k < 4; k++) {
newFunction(geometry, morphVector, skinWeight, k, inverses, skinMatrices, finalVector);
}
}
function newFunction(geometry, morphVector, skinWeight, k, inverses, skinMatrices, finalVector) {
var vectorToCopy = geometry.morphTargetInfluences !== undefined
? morphVector
: vertex;
var tempVector = new Vector4(vectorToCopy.x, vectorToCopy.y, vectorToCopy.z)
tempVector.multiplyScalar(skinWeight[k]);
//the inverse takes the vector into local bone space
//which is then transformed to the appropriate world space
tempVector
.applyMatrix4(inverses[k])
.applyMatrix4(skinMatrices[k])
.applyMatrix4(matrixRotation)
.applyMatrix4(matrixScale);
finalVector.add(tempVector);
}
export { finalizeMesh };

View File

@ -4,8 +4,7 @@ import { OBJExporter } from 'three/examples/jsm/exporters/OBJExporter.js';
import { SubdivisionModifier } from 'three/examples/jsm/modifiers/SubdivisionModifier.js';
import { saveAs } from 'file-saver';
import jQuery from 'jquery';
import { arrive } from 'arrive';
import { finalizeMesh } from './finalizeMesh.js';
import * as exporter from './finalizeMesh.js';
function save_stl() {
var smooth = jQuery('#subdivideSLT').val() > 0 ? jQuery('#subdivideSLT').val() : undefined;
@ -38,19 +37,19 @@ function save_obj() {
saveAs(blob, name + ((smooth) ? '-smooth' : '') + '.obj');
};
function save_json(){
function save_json() {
var name = get_name();
var blob = new Blob([JSON.stringify(CK.data.getJson())], {type: "text/plain;charset=utf-8"});
var blob = new Blob([JSON.stringify(CK.data.getJson())], { type: "text/plain;charset=utf-8" });
saveAs(blob, name + ".json");
};
function load_json (e) {
function load_json(e) {
var file = e.target.files[0];
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
reader.onload = (function (theFile) {
return function (e) {
e.preventDefault();
CK.change(JSON.parse(e.target.result));
};
@ -65,8 +64,8 @@ function get_name() {
};
function subdivide(geometry, subdivisions) {
var modifier = new SubdivisionModifier( subdivisions );
var smoothGeometry = modifier.modify( geometry );
var modifier = new SubdivisionModifier(subdivisions);
var smoothGeometry = modifier.modify(geometry);
return smoothGeometry;
};
@ -98,7 +97,6 @@ function process(object3d, smooth, mirroredPose) {
object3d.traverseVisible(function (object) {
if (object.isMesh) {
var exporter = new finalizeMesh();
var geometry = exporter.parse(object);
if (mirroredPose == true) {
@ -131,13 +129,13 @@ document.body.arrive(".footer", { onceOnly: true, existing: true }, function ()
var class_editor_tabs = 'tabs';
var class_shop_button = 'shop-button clickable';
var html_import = '<label id="jsonImport" for="import" ><span class="' + class_header + '" href="#" target="_self"><input type="file" id="import" name="import" style="display: none;"/><div class="' + class_header + '-img"><span style="width:20px">'+icon_import+'</span></div><div class="' + class_header + '-text">Import</div></span></label>';
var html_export = '<a id="jsonExport" class="' + class_header + '" href="#" target="_self"><div class="' + class_header + '-img"><span style="width:20px">'+icon_export+'</span></div><div class="' + class_header + '-text">Export</div></a>';
var html_import = '<label id="jsonImport" for="import" ><span class="' + class_header + '" href="#" target="_self"><input type="file" id="import" name="import" style="display: none;"/><div class="' + class_header + '-img"><span style="width:20px">' + icon_import + '</span></div><div class="' + class_header + '-text">Import</div></span></label>';
var html_export = '<a id="jsonExport" class="' + class_header + '" href="#" target="_self"><div class="' + class_header + '-img"><span style="width:20px">' + icon_export + '</span></div><div class="' + class_header + '-text">Export</div></a>';
var style_editor_footer = { "margin-left": "10px", "width": "50px" };
var style_char_menu_item = { "margin-left": "10px", "width": "50px" };
var style_shop_button = {};
var style_number_label = {'margin-left': '20px;'};
var style_number_label = { 'margin-left': '20px;' };
var char_menu = { "display": "flex", "justify-content": "center", "align-content": "center", "align-items": "center" };
//jQuery('.headerMenu:last').remove();
@ -161,15 +159,15 @@ document.body.arrive(".footer", { onceOnly: true, existing: true }, function ()
var area_char_menu = jQuery('.' + class_char_menu).first();
area_char_menu.css(char_menu);
area_char_menu
.append(jQuery("<label />", { css: style_char_menu_item, class: "jss7", id:'subDSltLabel', text: 'Quality:', title:'Subdivision Passes', for:'subdivideSLT'}))
.append(jQuery("<input />", { css: style_char_menu_item, class: "jss7 jss9 jss10", id:'subdivideSLT', name:'subdivideSLT', title:'Subdivision Passes', type:'number', min:'0', max:'2', step:'1', value:'0' }))
.append(jQuery("<a />", { css: style_char_menu_item, class: "jss7 jss9 jss10", id:'topSaveStl', text: icon_save + " .stl", title:'Download in STL Format'}).on("click", save_stl))
.append(jQuery("<a />", { css: style_char_menu_item, class: "jss7 jss9 jss10", id:'topSaveObj', text: icon_save + " .obj", title:'Download in OBJ Format'}).on("click", save_obj));
.append(jQuery("<label />", { css: style_char_menu_item, class: "jss7", id: 'subDSltLabel', text: 'Quality:', title: 'Subdivision Passes', for: 'subdivideSLT' }))
.append(jQuery("<input />", { css: style_char_menu_item, class: "jss7 jss9 jss10", id: 'subdivideSLT', name: 'subdivideSLT', title: 'Subdivision Passes', type: 'number', min: '0', max: '2', step: '1', value: '0' }))
.append(jQuery("<a />", { css: style_char_menu_item, class: "jss7 jss9 jss10", id: 'topSaveStl', text: icon_save + " .stl", title: 'Download in STL Format' }).on("click", save_stl))
.append(jQuery("<a />", { css: style_char_menu_item, class: "jss7 jss9 jss10", id: 'topSaveObj', text: icon_save + " .obj", title: 'Download in OBJ Format' }).on("click", save_obj));
var area_editor_footer = jQuery("." + class_editor_footer);
area_editor_footer
.prepend(jQuery("<a />", { css: style_shop_button, class: class_shop_button, id:'butSaveStl', text: icon_save + " .stl", title:'Download in STL Format'}).on("click", save_stl))
.prepend(jQuery("<a />", { css: style_shop_button, class: class_shop_button, id:'butSaveObj', text: icon_save + " .obj", title:'Download in OBJ Format'}).on("click", save_obj));
.prepend(jQuery("<a />", { css: style_shop_button, class: class_shop_button, id: 'butSaveStl', text: icon_save + " .stl", title: 'Download in STL Format' }).on("click", save_stl))
.prepend(jQuery("<a />", { css: style_shop_button, class: class_shop_button, id: 'butSaveObj', text: icon_save + " .obj", title: 'Download in OBJ Format' }).on("click", save_obj));
// To Work on
//var area_editor_tabs = jQuery("." + class_editor_tabs + ">ul");