Skip to content
Snippets Groups Projects
Commit 57c08442 authored by joni's avatar joni
Browse files

Remove automatic box shape if an inflated gltf created its own shapes.

parent 8f2ea3fb
No related branches found
No related tags found
No related merge requests found
import { calculateBoxShape } from "../utils/auto-box-collider";
AFRAME.registerComponent("auto-box-collider", {
schema: {
resize: { default: false },
resizeLength: { default: 0.5 }
},
init() {}
});
...@@ -278,7 +278,7 @@ AFRAME.registerComponent("gltf-model-plus", { ...@@ -278,7 +278,7 @@ AFRAME.registerComponent("gltf-model-plus", {
} }
} }
this.el.emit("model-loaded", { format: "gltf", model: this.model }); this.el.emit("model-loaded", { format: "gltf", model: this.model, didInflate: this.data.inflate }); // TODO: this.data.inflate is not the same as actually inflating
} catch (e) { } catch (e) {
console.error("Failed to load glTF model", e, this); console.error("Failed to load glTF model", e, this);
this.el.emit("model-error", { format: "gltf", src }); this.el.emit("model-error", { format: "gltf", src });
......
...@@ -160,13 +160,14 @@ ...@@ -160,13 +160,14 @@
<template id="interactable-model"> <template id="interactable-model">
<a-entity <a-entity
gltf-model-plus="inflate: false;" gltf-model-plus="inflate: true;"
class="interactable" class="interactable"
super-networked-interactable="counter: #media-counter;" super-networked-interactable="counter: #media-counter;"
body="type: dynamic; shape: none; mass: 1;" body="type: dynamic; shape: none; mass: 1;"
grabbable grabbable
stretchable="useWorldPosition: true; usePhysics: never" stretchable="useWorldPosition: true; usePhysics: never"
hoverable hoverable
auto-scale-cannon-physics-body
sticky-object="autoLockOnRelease: true; autoLockOnLoad: true;" sticky-object="autoLockOnRelease: true; autoLockOnLoad: true;"
position-at-box-shape-border="target:.delete-button" position-at-box-shape-border="target:.delete-button"
destroy-at-extreme-distances destroy-at-extreme-distances
......
...@@ -64,7 +64,6 @@ import "./components/css-class"; ...@@ -64,7 +64,6 @@ import "./components/css-class";
import "./components/scene-shadow"; import "./components/scene-shadow";
import "./components/avatar-replay"; import "./components/avatar-replay";
import "./components/image-plus"; import "./components/image-plus";
import "./components/auto-box-collider";
import "./components/pinch-to-move"; import "./components/pinch-to-move";
import "./components/look-on-mobile"; import "./components/look-on-mobile";
import "./components/pitch-yaw-rotator"; import "./components/pitch-yaw-rotator";
......
export function getBox(entity) { export function getBox(entity, boxRoot) {
const box = new THREE.Box3(); const box = new THREE.Box3();
const mesh = entity.getObject3D("mesh");
const rotation = entity.object3D.rotation.clone(); const rotation = entity.object3D.rotation.clone();
entity.object3D.rotation.set(0, 0, 0); entity.object3D.rotation.set(0, 0, 0);
entity.object3D.updateMatrixWorld(true); entity.object3D.updateMatrixWorld(true);
box.expandByObject = expandByObject; box.expandByObject = expandByObject;
box.setFromObject(mesh); box.setFromObject(boxRoot);
entity.object3D.rotation.copy(rotation); entity.object3D.rotation.copy(rotation);
return box; return box;
} }
......
...@@ -25,20 +25,22 @@ const offset = { x: 0, y: 0, z: -1.5 }; ...@@ -25,20 +25,22 @@ const offset = { x: 0, y: 0, z: -1.5 };
export const spawnNetworkedImage = (entity, src, contentType) => { export const spawnNetworkedImage = (entity, src, contentType) => {
entity.id = "interactable-image-" + interactableId++; entity.id = "interactable-image-" + interactableId++;
entity.setAttribute("networked", { template: "#interactable-image" }); entity.setAttribute("networked", { template: "#interactable-image" });
entity.addEventListener("image-loaded", function onBodyLoaded() { entity.addEventListener("image-loaded", function onImageLoaded() {
entity.removeEventListener("image-loaded", onBodyLoaded); entity.removeEventListener("image-loaded", onImageLoaded);
}); });
entity.setAttribute("image-plus", { src, contentType }); entity.setAttribute("image-plus", { src, contentType });
return entity;
}; };
export const spawnNetworkedInteractable = (entity, src, basePath) => { export const spawnNetworkedInteractable = (entity, src, basePath) => {
entity.id = "interactable-model-" + interactableId++; entity.id = "interactable-model-" + interactableId++;
entity.setAttribute("networked", { template: "#interactable-model" }); entity.setAttribute("networked", { template: "#interactable-model" });
entity.addEventListener("model-loaded", function onBodyLoaded() { entity.addEventListener("model-loaded", function onModelLoaded(evt) {
entity.removeEventListener("model-loaded", onBodyLoaded); entity.removeEventListener("model-loaded", onModelLoaded);
setShapeAndScale(entity); setShapeAndScale(entity, evt.detail.didInflate);
}); });
entity.setAttribute("gltf-model-plus", { src: src, basePath: basePath }); entity.setAttribute("gltf-model-plus", { src: src, basePath: basePath });
return entity;
}; };
export const addMedia = async url => { export const addMedia = async url => {
...@@ -79,26 +81,27 @@ export const addMedia = async url => { ...@@ -79,26 +81,27 @@ export const addMedia = async url => {
} }
}; };
function setShapeAndScale(entity) { function setShapeAndScale(entity, didInflate) {
const box = getBox(entity); const mesh = entity.getObject3D("mesh");
const center = new THREE.Vector3(); const boxRoot = didInflate ? mesh.parent : mesh;
const halfExtents = new THREE.Vector3(); const box = getBox(entity, boxRoot);
getCenterAndHalfExtents(entity, box, center, halfExtents);
entity.getObject3D("mesh").position.sub(center);
const scaleCoefficient = getScaleCoefficient(0.5, box); const scaleCoefficient = getScaleCoefficient(0.5, box);
entity.setAttribute("shape", { if (entity.components.body && entity.components.body.body && entity.components.body.body.shapes.length > 1) {
shape: "box", entity.removeAttribute("shape");
halfExtents: halfExtents } else {
}); const center = new THREE.Vector3();
const halfExtents = new THREE.Vector3();
getCenterAndHalfExtents(entity, box, center, halfExtents);
boxRoot.position.sub(center);
entity.setAttribute("shape", {
shape: "box",
halfExtents: halfExtents
});
}
const scale = entity.object3D.scale; const scale = entity.object3D.scale;
entity.setAttribute("scale", { entity.setAttribute("scale", {
x: scale.x * scaleCoefficient, x: scale.x * scaleCoefficient,
y: scale.y * scaleCoefficient, y: scale.y * scaleCoefficient,
z: scale.z * scaleCoefficient z: scale.z * scaleCoefficient
}); });
if (entity.components.body && entity.components.body.body) {
// TODO: Do this in shape component update
entity.components.body.syncToPhysics();
entity.components.body.updateCannonScale();
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment