Skip to content

Commit

Permalink
Merge pull request #55 from M3-org/scale-character-in-editor
Browse files Browse the repository at this point in the history
Scale character in editor
  • Loading branch information
madjin authored Nov 2, 2023
2 parents 8c2dc60 + 3f0375e commit 1150375
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/components/Selector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export default function Selector({confirmDialog, uploadVRMURL, templateInfo, ani
}

const loadSelectedOptions = (opts) => {

loadOptions(opts).then((loadedData)=>{
let newAvatar = {};
loadedData.map((data)=>{
Expand Down Expand Up @@ -530,6 +531,14 @@ export default function Selector({confirmDialog, uploadVRMURL, templateInfo, ani

lookatManager.addVRM(vrm)

const scale = templateInfo.exportScale || 1;
vrm.scene.scale.set(scale,scale,scale);

const offset = templateInfo.offset;
if (offset != null){
vrm.scene.position.set(offset[0],offset[1],offset[2]);
}

//animation setup section
//play animations on this vrm TODO, letscreate a single animation manager per traitInfo, as model may change since it is now a trait option
animationManager.startAnimation(vrm)
Expand Down Expand Up @@ -620,6 +629,7 @@ export default function Selector({confirmDialog, uploadVRMURL, templateInfo, ani
child.userData.isVRM0 = true;
}
}

}
})

Expand Down
27 changes: 20 additions & 7 deletions src/library/cull-mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { BufferAttribute, BackSide, FrontSide, Raycaster, Vector3, Color, Buffer

let origin = new Vector3();
let direction = new Vector3();
let worldScale = new Vector3();
let worldPosition = new Vector3();
const intersections = [];

const raycaster = new Raycaster();
Expand Down Expand Up @@ -35,8 +37,14 @@ export const CullHiddenFaces = async(meshes) => {

// if it hasnt been previously created an array in this index value, create it
if (meshData[mesh.userData.cullLayer] == null){
meshData[mesh.userData.cullLayer] = {origMeshes:[], posMeshes:[], negMeshes:[]}
}
meshData[mesh.userData.cullLayer] = {origMeshes:[], posMeshes:[], negMeshes:[], scaleMeshes:[], positionMeshes:[]}
}

mesh.getWorldScale(worldScale);
mesh.getWorldPosition(worldPosition);
meshData[mesh.userData.cullLayer].scaleMeshes.push(worldScale);
meshData[mesh.userData.cullLayer].positionMeshes.push(worldPosition);


// clone the mesh to only detect collisions in front faces
const cloneP = mesh.clone()
Expand All @@ -49,12 +57,11 @@ export const CullHiddenFaces = async(meshes) => {
meshData[mesh.userData.cullLayer].origMeshes.push(mesh)
meshData[mesh.userData.cullLayer].posMeshes.push(cloneP)
meshData[mesh.userData.cullLayer].negMeshes.push(cloneN)

// reset to original before doing raycasts, modified geom has issues with raycasts
mesh.geometry.setIndex(mesh.userData.origIndexBuffer);
}
});

// remove empty index spaces
for (let i = meshData.length - 1; i >= 0; i--) {
if (meshData[i] == null){
Expand All @@ -74,13 +81,15 @@ export const CullHiddenFaces = async(meshes) => {
for (let k = 0; k < meshData[i].origMeshes.length; k++){

const mesh = meshData[i].origMeshes[k];
const meshScale = meshData[i].scaleMeshes[k];
const meshPosition = meshData[i].positionMeshes[k];
const index = mesh.userData.origIndexBuffer.array;
const vertexData = mesh.geometry.attributes.position.array;
const normalsData = mesh.geometry.attributes.normal.array;
const faceNormals = mesh.geometry.userData.faceNormals;
geomsIndices.push({
geom: mesh.geometry,
index: getIndexBuffer(index,vertexData,normalsData, faceNormals, hitArr,mesh.userData.cullDistance/*,i === 0*/)
index: getIndexBuffer(meshPosition,meshScale, index,vertexData,normalsData, faceNormals, hitArr,mesh.userData.cullDistance/*,i === 0*/)
})
}
}
Expand Down Expand Up @@ -117,7 +126,7 @@ const getDistanceInOut = (distanceArr) => {
return [distIn, distOut]
}

const getIndexBuffer = (index, vertexData, normalsData, faceNormals, intersectModels, distanceArr, debug = false) =>{
const getIndexBuffer = (meshPosition, meshScale, index, vertexData, normalsData, faceNormals, intersectModels, distanceArr, debug = false) =>{

const indexCustomArr = [];
const distArr = getDistanceInOut(distanceArr);
Expand Down Expand Up @@ -148,7 +157,11 @@ const getIndexBuffer = (index, vertexData, normalsData, faceNormals, intersectMo
direction.set(normalsData[vi],normalsData[vi+1],normalsData[vi+2]).normalize();

// move the origin away to have the raycast being casted from outside
origin.set(vertexData[vi],vertexData[vi+1],vertexData[vi+2]).add(direction.clone().multiplyScalar(distIn))
origin.set(
(vertexData[vi] * meshScale.x ) + meshPosition.x,
(vertexData[vi+1] * meshScale.y ) + meshPosition.y,
(vertexData[vi+2] * meshScale.z) + meshPosition.z)
.add(direction.clone().multiplyScalar(distIn))

//invert the direction of the raycaster as we moved it away from its origin
raycaster.set( origin, direction.clone().multiplyScalar(-1));
Expand Down

0 comments on commit 1150375

Please sign in to comment.