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

remove decal after collision #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
37 changes: 34 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,17 @@ export default e => {
// const debugMesh = [];
const debugDecalVertPos = false;




const maxNumDecals = 128;
const decalGeometry = new THREE.PlaneBufferGeometry(0.5, 0.5, 8, 8).toNonIndexed();

let currentDecal = 0;
let collisionTime = [];
for(let i = 0; i < maxNumDecals; i++){
collisionTime[i] = -1;
}
const _makeDecalMesh = () => {
const geometry = new THREE.BufferGeometry();
const positions = new Float32Array(decalGeometry.attributes.position.array.length * maxNumDecals);
Expand Down Expand Up @@ -219,7 +228,6 @@ export default e => {
{
const result = physics.raycast(gunApp.position, gunApp.quaternion.clone().multiply(z180Quaternion));
if (result) {

const targetApp = getAppByPhysicsId(result.objectId);

const normal = new THREE.Vector3().fromArray(result.normal);
Expand Down Expand Up @@ -324,9 +332,11 @@ export default e => {
// };
//decalMesh.geometry.index.needsUpdate = true;
// update geometry attribute offset

decalMesh.offset += localDecalGeometry.attributes.position.count;
decalMesh.offset = decalMesh.offset % decalMesh.geometry.attributes.position.count;

currentDecal++;

explosionApp.position.fromArray(result.point);
explosionApp.quaternion.setFromRotationMatrix(
new THREE.Matrix4().lookAt(
Expand Down Expand Up @@ -420,8 +430,29 @@ export default e => {
gunApp.use();
}
});

let positionAttributeLength = decalGeometry.attributes.position.array.length/3;
useFrame(({timestamp}) => {
if(currentDecal>0){
//handle if index equal -1
let index = decalMesh.offset / positionAttributeLength - 1 >= 0 ? decalMesh.offset / positionAttributeLength - 1 : maxNumDecals - 1;
if(collisionTime[index] == -1){ //means haven't recorded the collision time
collisionTime[index] = timestamp;
}
for(let i = 0; i < maxNumDecals; i++){
// if collision time larger than 1.5 sec, then clean the position attribute
if(collisionTime[i] != -1 && timestamp - collisionTime[i] > 1500){
collisionTime[i] = -1;
for(let j = 0; j < positionAttributeLength; j++)
decalMesh.geometry.attributes.position.setXYZ(i * positionAttributeLength + j, 0, 0, 0);
currentDecal--;
}
}
decalMesh.geometry.attributes.position.needsUpdate = true;
}




if (!wearing) {
if (gunApp) {
gunApp.position.copy(app.position);
Expand Down