Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: vrm1 physics #1261

Merged
merged 4 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/foundation/components/Physics/PhysicsComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class PhysicsComponent extends Component {
class PhysicsEntity extends (base.constructor as any) {
constructor(
entityUID: EntityUID,
isAlive: Boolean,
isAlive: boolean,
components?: Map<ComponentTID, Component>
) {
super(entityUID, isAlive, components);
Expand Down
10 changes: 5 additions & 5 deletions src/foundation/importer/Vrm0xImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ export class Vrm0xImporter {
const entity = sg.entity;
EntityRepository.addComponentToEntity(PhysicsComponent, entity);
VRMSpringBonePhysicsStrategy.initialize(sg);
if (sg.children.length > 0) {
for (const child of sg.children) {
this.__addPhysicsComponentRecursively(entityRepository, child);
}
}
// if (sg.children.length > 0) {
// for (const child of sg.children) {
// this.__addPhysicsComponentRecursively(entityRepository, child);
// }
// }
}

static _createTextures(gltfModel: RnM2): Texture[] {
Expand Down
10 changes: 5 additions & 5 deletions src/foundation/importer/VrmImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ export class VrmImporter {
const entity = sg.entity;
EntityRepository.addComponentToEntity(PhysicsComponent, entity);
VRMSpringBonePhysicsStrategy.initialize(sg);
if (sg.children.length > 0) {
for (const child of sg.children) {
this.__addPhysicsComponentRecursively(entityRepository, child);
}
}
// if (sg.children.length > 0) {
// for (const child of sg.children) {
// this.__addPhysicsComponentRecursively(entityRepository, child);
// }
// }
}

static _createTextures(gltfModel: RnM2): Texture[] {
Expand Down
36 changes: 20 additions & 16 deletions src/foundation/physics/VRMSpringBonePhysicsStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Index } from '../../types/CommonTypes';
import { PhysicsStrategy } from './PhysicsStrategy';
import { MutableQuaternion } from '../math/MutableQuaternion';
import { IQuaternion } from '../math';
import { Is } from '../misc/Is';

export class VRMSpringBonePhysicsStrategy implements PhysicsStrategy {
private static __tmp_vec3 = MutableVector3.zero();
Expand Down Expand Up @@ -113,21 +114,26 @@ export class VRMSpringBonePhysicsStrategy implements PhysicsStrategy {
static initialize(sceneGraph: SceneGraphComponent) {
const children = sceneGraph.children;

const physicsComponent = sceneGraph.entity.tryToGetPhysics()!;
const physicsComponent = sceneGraph.entity.tryToGetPhysics();
if (Is.not.exist(physicsComponent)) {
new Error('PhysicsComponent is not attached to the entity.');
return;
}

const vrmSpringBone = physicsComponent.strategy as VRMSpringBonePhysicsStrategy;
if (children.length > 0) {
const transform = children[0].entity.getTransform();
// const childPositionInLocal = Matrix44.invert(
// sceneGraph.worldMatrixInner
// ).multiplyVector3(children[0].worldPosition);
const childPositionInLocal = Vector3.fromCopy3(
transform.localPosition.x * transform.localScale.x,
transform.localPosition.y * transform.localScale.y,
transform.localPosition.z * transform.localScale.z
);
vrmSpringBone.initialize(
sceneGraph,
Vector3.fromCopy3(
transform.localPosition.x * transform.localScale.x,
transform.localPosition.y * transform.localScale.y,
transform.localPosition.z * transform.localScale.z
),
// childPositionInLocal,
childPositionInLocal,
void 0
);
} else {
Expand All @@ -139,9 +145,7 @@ export class VRMSpringBonePhysicsStrategy implements PhysicsStrategy {
Vector3.multiply(Vector3.normalize(delta), 0.07)
);
}
const childPositionInLocal = Matrix44.invert(sceneGraph.matrixInner).multiplyVector3(
childPosition
);
const childPositionInLocal = sceneGraph.getLocalPositionOf(childPosition);
vrmSpringBone.initialize(sceneGraph, childPositionInLocal, void 0);
}
}
Expand All @@ -159,8 +163,8 @@ export class VRMSpringBonePhysicsStrategy implements PhysicsStrategy {
center?: SceneGraphComponent
) {
const currentTail =
center != null ? center!.getWorldPositionOf(this.__currentTail) : this.__currentTail;
const prevTail = center != null ? center!.getWorldPositionOf(this.__prevTail) : this.__prevTail;
center != null ? center.getWorldPositionOf(this.__currentTail) : this.__currentTail;
const prevTail = center != null ? center.getWorldPositionOf(this.__prevTail) : this.__prevTail;

// Continues the previous frame's movement (there is also attenuation)
const delta = MutableVector3.multiply(Vector3.subtract(currentTail, prevTail), 1.0 - dragForce);
Expand All @@ -187,8 +191,8 @@ export class VRMSpringBonePhysicsStrategy implements PhysicsStrategy {

// prevTail = currentTail;
// currentTail = nextTail;
this.__prevTail = center != null ? center!.getLocalPositionOf(currentTail) : currentTail;
this.__currentTail = center != null ? center!.getLocalPositionOf(nextTail) : nextTail;
this.__prevTail = center != null ? center.getLocalPositionOf(currentTail) : currentTail;
this.__currentTail = center != null ? center.getLocalPositionOf(nextTail) : nextTail;

const resultRotation = this.applyRotation(nextTail);
if (this.head.children.length > 0) {
Expand All @@ -213,10 +217,10 @@ export class VRMSpringBonePhysicsStrategy implements PhysicsStrategy {
const r = boneHitRadius + collider.radius;
const delta = Vector3.subtract(nextTail, worldColliderPos);
const deltaScalar = r - delta.length();
if (deltaScalar >= 0) {
if (deltaScalar > 0) {
const normal = Vector3.normalize(delta);
const resilienceVec = Vector3.multiply(
Vector3.add(worldColliderPos, normal),
normal,
deltaScalar
);
nextTail = Vector3.add(nextTail, resilienceVec);
Expand Down