Skip to content

Commit

Permalink
Clean.
Browse files Browse the repository at this point in the history
  • Loading branch information
gonnavis committed Aug 17, 2022
1 parent ed1ef3d commit 82fa161
Show file tree
Hide file tree
Showing 12 changed files with 8 additions and 194 deletions.
9 changes: 0 additions & 9 deletions avatar-spriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ class SpriteMegaAvatarMesh extends THREE.Mesh {
playerAvatar: avatar,
camera,
}) {
// const velocityScaleFactor = 10; // because avatars multiply velocity by 0.1
const velocityScaleFactor = 1;

if (preview) {
Expand Down Expand Up @@ -623,7 +622,6 @@ class SpriteMegaAvatarMesh extends THREE.Mesh {
}
} else if (spriteSpecBaseName === 'run') {
if (playerSide === 'forward') {
// debugger
return 'run';
} else if (playerSide === 'backward') {
return 'run backward';
Expand All @@ -637,11 +635,7 @@ class SpriteMegaAvatarMesh extends THREE.Mesh {
throw new Error('unhandled case');
}
})();
// if (spriteSpecName === 'run') debugger
// console.log(spriteSpecName, avatar.velocity.x.toFixed(2))
console.log(spriteSpecName, window.logVector3(avatar.velocity))
this.setTexture(spriteSpecName);
// console.log(spriteSpecName)

if (spriteSpecName !== this.lastSpriteSpecName) {
this.lastSpriteSpecName = spriteSpecName;
Expand Down Expand Up @@ -811,7 +805,6 @@ const getSpriteSpecs = () => {
return {
reset() {},
update(timestamp, timeDiffMs) {
// debugger
positionOffset -= walkSpeed/1000 * timeDiffMs;

const euler = new THREE.Euler(0, angle, 0, 'YXZ');
Expand Down Expand Up @@ -1549,7 +1542,6 @@ const _renderSpriteImages = skinnedVrm => {
const {renderer, scene} = metaversefile.useInternals();
const pixelRatio = renderer.getPixelRatio();
const _renderSpriteFrame = () => {
// console.log('_renderSpriteFrame');
const oldParent = skinnedModel.parent;
scene2.add(skinnedModel);

Expand Down Expand Up @@ -1577,7 +1569,6 @@ const _renderSpriteImages = skinnedVrm => {
};

const spriteSpecs = getSpriteSpecs();
console.log('getSpriteSpecs()');
let canvasIndex2 = 0;
const spriteImages = [];
// console.time('render');
Expand Down
1 change: 0 additions & 1 deletion avatars/animationHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,6 @@ export const _applyAnimation = (avatar, now) => {
const t3 = timeSinceLastMoveSeconds % idleAnimation.duration;
const src3 = idleAnimation.interpolants[k];
const v3 = src3.evaluate(t3);
// if (isPosition) console.log(t3);

target.fromArray(v3);
if (isPosition) {
Expand Down
43 changes: 1 addition & 42 deletions avatars/avatars.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ const _makeDebugMesh = (avatar) => {

class Avatar {
constructor(object, options = {}) {
console.log('Avatar');
if (!object) {
object = {};
}
Expand Down Expand Up @@ -786,7 +785,6 @@ class Avatar {
Left_toe: this.legsManager.leftLeg.toe,
Right_toe: this.legsManager.rightLeg.toe,
};
window.modelBoneOutputs = this.modelBoneOutputs;

this.debugMesh = null;

Expand Down Expand Up @@ -1226,8 +1224,6 @@ class Avatar {
for (const k in modelBones) {
const modelBone = modelBones[k];
const modelBoneOutput = modelBoneOutputs[k];
// console.log(111)
// window.modelBoneOutput = modelBoneOutput;

modelBone.position.copy(modelBoneOutput.position);
modelBone.quaternion.multiplyQuaternions(
Expand Down Expand Up @@ -1409,7 +1405,6 @@ class Avatar {
return localEuler.y;
}
async setQuality(quality) {
console.log({quality});

this.model.visible = false;
if ( this.crunchedModel ) this.crunchedModel.visible = false;
Expand Down Expand Up @@ -1503,7 +1498,6 @@ class Avatar {
}

setVelocity(timestamp, timeDiffS, lastPosition, currentPosition, currentQuaternion, isBoundPlayer) {
// console.log('setVelocity')
// Set the velocity, which will be considered by the animation controller
const positionDiff = localVector.copy(lastPosition)
.sub(currentPosition)
Expand All @@ -1512,23 +1506,17 @@ class Avatar {
localEuler.setFromQuaternion(currentQuaternion, 'YXZ');
localEuler.set(0, -(localEuler.y + Math.PI), 0);
positionDiff.applyEuler(localEuler);
// console.log(positionDiff.x);
this.calcedVelocity.copy(positionDiff); // For testing only, check if the physics.velocity correct. Can't use this in formal, to calc such as idleWalkFactor/walkRunFactor, will cause aniamtions jitter in low fps.
this.calcedVelocity.y *= -1;
if (!isBoundPlayer) {
this.velocity.copy(this.calcedVelocity);
// this.velocity.x *= 1 / 3; // walk speed 2.5 // wrong, don't need modify.SS
// this.velocity.z *= 1 / 3; // walk speed 2.5 // wrong, don't need modify.SS
}
// this.velocity.applyEuler(localEuler);
this.direction.copy(positionDiff).normalize();
this.lastPosition.copy(currentPosition);

if (this.velocity.length() > maxIdleVelocity) {
this.lastMoveTime = timestamp;
}
// if (this.velocity.x > 100) debugger
// if (this.velocity.x === 2.5) debugger
}

update(timestamp, timeDiff, isBoundPlayer = true) {
Expand All @@ -1549,17 +1537,9 @@ class Avatar {
}

const currentSpeed = localVector.set(this.velocity.x, 0, this.velocity.z).length();
// console.log(this.velocity.x.toFixed(2))
// console.log(currentSpeed)

this.idleWalkFactor = Math.min(Math.max((currentSpeed - idleSpeed) / (walkSpeed - idleSpeed), 0), 1);
this.walkRunFactor = Math.min(Math.max((currentSpeed - walkSpeed) / (runSpeed - walkSpeed), 0), 1);
// if (this.idleWalkFactor >= 0.9 && this.walkRunFactor < 1 && this.velocity.x > 0.1) {
// console.log( this.velocity.x)
// debugger
// }
// console.log(this.walkRunFactor)
// console.log(this.idleWalkFactor, this.walkRunFactor)
this.crouchFactor = Math.min(Math.max(1 - (this.crouchTime / crouchMaxTime), 0), 1);
// console.log('current speed', currentSpeed, idleWalkFactor, walkRunFactor);
this.aimRightFactor = this.aimRightTransitionTime / aimTransitionMaxTime;
Expand Down Expand Up @@ -1894,7 +1874,6 @@ class Avatar {

const _updateSubAvatars = () => {
if (this.spriteMegaAvatarMesh) {
// debugger
this.spriteMegaAvatarMesh.update(timestamp, timeDiff, {
playerAvatar: this,
camera,
Expand Down Expand Up @@ -1964,27 +1943,7 @@ class Avatar {
if (this.getTopEnabled() || this.getHandEnabled(0) || this.getHandEnabled(1)) {
_motionControls.call(this)
}

const player = window.localPlayer;
// const player = window.npcPlayers[0];
if (true && player && this === player.avatar) {
window.domInfo.innerHTML += `
<div style="display:;">actions: --- ${player.getActionsArray().map(n=>n.type)}</div>
<div style="display:;">targetMoveDistancePerFrame: --- ${window.logVector3(player.characterPhysics.targetMoveDistancePerFrame)} | ${window.logNum(player.characterPhysics.targetMoveDistancePerFrame.length())} of characterPhysics ( correct )</div>
<div style="display:;">targetMoveDistancePerFrame: --- ${window.logVector3(player.characterPhysics.wantMoveDistancePerFrame)} | ${window.logNum(player.characterPhysics.wantMoveDistancePerFrame.length() * 6)} damped ( length * 6 )</div>
<div style="display:;">velocity: --- ${window.logVector3(player.characterPhysics.velocity)} | ${window.logNum(player.characterPhysics.velocity.length())} | ${window.logNum(localVector.copy(player.characterPhysics.velocity).setY(0).length())} of characterPhysics</div>
<div style="display:;">velocity: --- ${window.logVector3(this.velocity)} | ${window.logNum(this.velocity.length())} | ${window.logNum(localVector.copy(this.velocity).setY(0).length())} of avatar</div>
<div style="display:;">velocity: --- ${window.logVector3(this.calcedVelocity)} | ${window.logNum(this.calcedVelocity.length())} | ${window.logNum(localVector.copy(this.calcedVelocity).setY(0).length())} of avatar test</div>
<div style="display:;">idleWalkFactor: --- ${window.logNum(this.idleWalkFactor)}</div>
<div style="display:;">walkRunFactor: --- ${window.logNum(this.walkRunFactor)}</div>
<div style="display:;">avatar.direction: --- ${window.logVector3(this.direction)}</div>
<div style="display:;">player.direction: --- ${window.logVector3(player.getWorldDirection(localVector))}</div>
<div style="display:;">angle: --- ${window.logNum(this.getAngle())}</div>
`
}
// console.log(player.characterPhysics.velocity.y)
// console.log(this.velocity.x.toFixed(2))
// console.log('applyAnimation')

_applyAnimation(this, now);

if (this.poseAnimation) {
Expand Down
1 change: 0 additions & 1 deletion character-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,6 @@ class UninterpolatedPlayer extends StatePlayer {
}
class LocalPlayer extends UninterpolatedPlayer {
constructor(opts) {
console.log('LocalPlayer');
super(opts);

this.isLocalPlayer = !opts.npc;
Expand Down
87 changes: 3 additions & 84 deletions character-physics.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,79 +100,43 @@ class CharacterPhysics {
}
/* apply the currently held keys to the character */
applyWasd(velocity, timeDiff) {
// console.log('wasd')
if (this.character.avatar) {
this.targetVelocity.copy(velocity);
window.domInfo.innerHTML += `<div style="display:;">targetVelocity: --- ${window.logVector3(this.targetVelocity)}</div>`;
// window.visKeysDirectionX = keysDirection.x;
// this.velocity.add(keysDirection);
this.targetMoveDistancePerFrame.copy(this.targetVelocity).multiplyScalar(timeDiff / 1000);
if (this.character === window.npcPlayer) if (this.targetMoveDistancePerFrame.x !== 0) debugger
window.domInfo.innerHTML += `<div style="display:;">targetMoveDistancePerFrame: --- ${window.logVector3(this.targetMoveDistancePerFrame)}</div>`;
// console.log(this.velocity.x, this.velocity.z)
// window.visVelocityBeforeDampingX = this.velocity.x;
}
}
applyGravity(nowS, timeDiffS) {
// if (this.character) {
const fallLoopAction = this.character.getAction('fallLoop');
if (fallLoopAction) {
if (!this.lastFallLoopAction) {
// console.log('start fallLoop')
this.fallLoopStartTimeS = nowS;
this.lastGravityH = 0;
if (fallLoopAction.from === 'jump') {
const aestheticJumpBias = 1;
const t = flatGroundJumpAirTime / 1000 / 2 + aestheticJumpBias;
this.fallLoopStartTimeS -= t;
const previousT = t - timeDiffS;
this.lastGravityH = 0.5 * physicsScene.getGravity().y * previousT * previousT; // todo: consider xyz.
this.lastGravityH = 0.5 * physicsScene.getGravity().y * previousT * previousT;
}
}
const t = nowS - this.fallLoopStartTimeS;
const h = 0.5 * physicsScene.getGravity().y * t * t; // todo: consider xyz.
// this.velocity.y += h;
const h = 0.5 * physicsScene.getGravity().y * t * t;
this.wantMoveDistancePerFrame.y = h - this.lastGravityH;
// console.log(this.wantMoveDistancePerFrame.y)
// debugger
// todo: this.wantVelocity.y = ???

this.lastGravityH = h;
}
this.lastFallLoopAction = fallLoopAction;

// if ((this.character.hasAction('jump') || this.character.hasAction('fallLoop')) && !this.character.hasAction('fly') && !this.character.hasAction('swim')) {

// const gravityTargetVelocity = localVector.copy(physicsScene.getGravity());
// const gravityTargetMoveDistancePerFrame = gravityTargetVelocity.multiplyScalar(timeDiffS);
// this.targetVelocity.add(gravityTargetVelocity); // todo: acceleration of gravity.
// this.targetMoveDistancePerFrame.add(gravityTargetMoveDistancePerFrame);

// //// move this.applyGravity(timeDiffS); after this.updateVelocity(timeDiffS);
// // const gravityVelocity = localVector.copy(physicsScene.getGravity());
// // const gravityMoveDistancePerFrame = gravityVelocity.multiplyScalar(timeDiffS);
// // this.velocity.add(gravityVelocity); // todo: acceleration of gravity.
// // this.wantMoveDistancePerFrame.add(gravityMoveDistancePerFrame);
// }
// }
}
updateVelocity(timeDiffS) {
this.applyVelocityDamping(this.velocity, timeDiffS);
}
applyCharacterPhysicsDetail(velocityAvatarDirection, updateRig, now, timeDiffS) {
if (this.character.avatar) {
// console.log('apply avatar physics', this.character);
// move character controller
const minDist = 0;
// localVector3.copy(this.velocity) // todo: rename?: this.velocity is not velocity, but move distance per frame now ?
// if (this.character === window.npcPlayer) console.log(this.wantMoveDistancePerFrame.x)
localVector3.copy(this.wantMoveDistancePerFrame)
// .multiplyScalar(timeDiffS);
// .multiplyScalar(timeDiffS);
// .multiplyScalar(0.016);
window.domInfo.innerHTML += `<div style="display:;">localVector3: --- ${window.logVector3(localVector3)}</div>`;

// console.log('set localVector3')
localVector3.copy(this.wantMoveDistancePerFrame);

// aesthetic jump
const jumpAction = this.character.getAction('jump');
Expand Down Expand Up @@ -213,8 +177,6 @@ class CharacterPhysics {
}
}

// console.log('move')

const positionXZBefore = localVector2D.set(this.characterController.position.x, this.characterController.position.z);
const positionYBefore = this.characterController.position.y;
const flags = physicsScene.moveCharacterController(
Expand All @@ -233,17 +195,13 @@ class CharacterPhysics {
this.velocity.copy(this.wantVelocity);
if (wantMoveDistancePerFrameXZLength > 0) { // prevent divide 0, and reduce calculations.
const movedRatioXZ = (positionXZAfter.sub(positionXZBefore).length()) / wantMoveDistancePerFrameXZLength;
// console.log(movedRatioXZ.toFixed(2));
// if (this.character === window.npcPlayer) debugger
// if (movedRatioXZ < 1) this.velocity.multiplyScalar(movedRatioXZ); // todo: multiply targetVelocity.
if (movedRatioXZ < 1) {
this.velocity.x *= movedRatioXZ;
this.velocity.z *= movedRatioXZ;
}
}
if (wantMoveDistancePerFrameYLength > 0) { // prevent divide 0, and reduce calculations.
const movedRatioY = (positionYAfter - positionYBefore) / wantMoveDistancePerFrameYLength;
// console.log(movedRatioY)
if (movedRatioY < 1) {
this.velocity.y *= movedRatioY;
}
Expand Down Expand Up @@ -435,56 +393,18 @@ class CharacterPhysics {
/* dampen the velocity to make physical sense for the current avatar state */
applyVelocityDamping(velocity, timeDiffS) {
const doDamping = (factor) => {
// console.log('damping')
// const factor = getVelocityDampingFactor(groundFriction, timeDiff);
// // const factor = getVelocityDampingFactor(window.aaa, timeDiff);
// this.wantMoveDistancePerFrame.x = this.wantMoveDistancePerFrame.x * factor;
// this.wantMoveDistancePerFrame.z = this.wantMoveDistancePerFrame.z * factor;

this.wantMoveDistancePerFrame.x = THREE.MathUtils.damp(this.wantMoveDistancePerFrame.x, this.lastTargetMoveDistancePerFrame.x, factor, timeDiffS);
this.wantMoveDistancePerFrame.z = THREE.MathUtils.damp(this.wantMoveDistancePerFrame.z, this.lastTargetMoveDistancePerFrame.z, factor, timeDiffS);
// this.wantMoveDistancePerFrame.y = THREE.MathUtils.damp(this.wantMoveDistancePerFrame.y, this.lastTargetMoveDistancePerFrame.y, factor, timeDiffS);
this.wantMoveDistancePerFrame.y = this.targetMoveDistancePerFrame.y;
// if (this.targetMoveDistancePerFrame.x > 0) debugger
// if (this.character === window.npcPlayer) if (this.wantMoveDistancePerFrame.x !== 0) debugger

this.wantVelocity.x = THREE.MathUtils.damp(this.wantVelocity.x, this.lastTargetVelocity.x, factor, timeDiffS);
this.wantVelocity.z = THREE.MathUtils.damp(this.wantVelocity.z, this.lastTargetVelocity.z, factor, timeDiffS);
// this.wantVelocity.y = THREE.MathUtils.damp(this.wantVelocity.y, this.lastTargetVelocity.y, factor, timeDiffS);
this.wantVelocity.y = this.targetVelocity.y;
// this.velocity.x = this.targetVelocity.x;
// this.velocity.z = this.targetVelocity.z;

// const testMoveDistancePerFrameX = this.velocity.x * timeDiffS;
// const testMoveDistancePerFrameZ = this.velocity.z * timeDiffS;
// const testMoveDistancePerFrameY = this.velocity.y * timeDiffS;

// console.log(
// // this.wantMoveDistancePerFrame.x === testMoveDistancePerFrameX ? 0 : (this.wantMoveDistancePerFrame.x > testMoveDistancePerFrameX ? 1 : 2),
// // Math.abs(this.wantMoveDistancePerFrame.x - testMoveDistancePerFrameX) < 1e-6 ? 0 : Math.abs(this.wantMoveDistancePerFrame.x - testMoveDistancePerFrameX),
// Math.abs(this.wantMoveDistancePerFrame.x - testMoveDistancePerFrameX) < 1e-6 ? 0 : (this.wantMoveDistancePerFrame.x > testMoveDistancePerFrameX ? 1 : 2),
// // '-',
// // this.wantMoveDistancePerFrame.z, testMoveDistancePerFrameZ,
// // '-',
// // this.wantMoveDistancePerFrame.y, testMoveDistancePerFrameY,
// )

// console.log('damping')

// this.velocity.copy(this.lastTargetMoveDistancePerFrame).divideScalar(timeDiffS)
// console.log(Math.round(this.velocity.length()))

// this.velocity.copy(this.wantMoveDistancePerFrame).divideScalar(timeDiffS)
// console.log((this.velocity.length()))
}
if (this.character.hasAction('fly')) {
// const factor = getVelocityDampingFactor(flyFriction, timeDiff);
// velocity.multiplyScalar(factor);
doDamping(flyFriction);
}
else if(this.character.hasAction('swim')){
// const factor = getVelocityDampingFactor(swimFriction, timeDiff);
// velocity.multiplyScalar(factor);
doDamping(swimFriction);
}
else {
Expand Down Expand Up @@ -715,7 +635,6 @@ class CharacterPhysics {
this.applyCharacterPhysics(now, timeDiffS);
this.applyCharacterActionKinematics(now, timeDiffS);

// console.log('update end')
this.lastTargetVelocity.copy(this.targetVelocity);
this.lastTargetMoveDistancePerFrame.copy(this.targetMoveDistancePerFrame);
}
Expand Down
1 change: 0 additions & 1 deletion character-sfx.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ class CharacterSfx {
const timeSeconds = timestamp/1000;
const currentSpeed = localVector.set(this.player.avatar.velocity.x, 0, this.player.avatar.velocity.z).length();

// todo: Can use logics already existing in avatars.js?
const idleWalkFactor = Math.min(Math.max((currentSpeed - idleSpeed) / (walkSpeed - idleSpeed), 0), 1);
const walkRunFactor = Math.min(Math.max((currentSpeed - walkSpeed) / (runSpeed - walkSpeed), 0), 1);
const crouchFactor = Math.min(Math.max(1 - (this.player.avatar.crouchTime / crouchMaxTime), 0), 1);
Expand Down
2 changes: 1 addition & 1 deletion game.js
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ class GameManager extends EventTarget {
}
const localPlayer = playersManager.getLocalPlayer();
const sprintMultiplier = isRunning ?
(localPlayer.hasAction('narutoRun') ? 20 : 3) // todo: Use constants: narutoRunSpeed, runSpeed.
(localPlayer.hasAction('narutoRun') ? 20 : 3)
:
((isSwimming && !isFlying) ? 5 - localPlayer.getAction('swim').swimDamping : 1);
speed *= sprintMultiplier;
Expand Down
4 changes: 0 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
<body>
<div id=iframe-container></div>
<div id="root"></div>
<div>
<button style="position: fixed; left: 0; top: 0; z-index: 999999;" onclick="domInfo.style.display = domInfo.style.display === 'none' ? 'block' : 'none';">toggle</button>
<div id="domInfo" style="position: fixed; left: 0; top: 140px; background: rgba(0,0,0,.5); color: white; padding: 5px; width: 600px; pointer-events: none; font-family: Consolas,'Courier New',Courier,FreeMono,monospace;z-index: 999999; display: /* none */;"></div>
</div>
<script type=module src="/pre.js"></script>
<script type=module src="/src/main.jsx"></script>
</body>
Expand Down
Loading

0 comments on commit 82fa161

Please sign in to comment.