minerender 2.1.0-alpha.42 → 2.1.0-alpha.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bundle.js CHANGED
@@ -81472,6 +81472,10 @@ ${key}` : key;
81472
81472
  this.setMatrixAt(index, new Matrix4());
81473
81473
  return reference;
81474
81474
  }
81475
+ /** Returns the live reference for a raycast hit on this object's instance mesh. */
81476
+ getInstanceReference(mesh, index) {
81477
+ return mesh === this.instanceMesh ? this.instanceReferences.get(index) : void 0;
81478
+ }
81475
81479
  isInstanceActive(index, reference) {
81476
81480
  return this.instanceReferences.get(index) === reference;
81477
81481
  }
@@ -81554,9 +81558,9 @@ ${key}` : key;
81554
81558
  }
81555
81559
  getRotationAt(index, euler = new Euler()) {
81556
81560
  if (!this.isInstanced) throw new MineRenderError("Object is not instanced");
81557
- const matrix = this.getMatrixAt(index);
81558
- euler.setFromRotationMatrix(matrix);
81559
- return euler;
81561
+ const rotation = new Quaternion();
81562
+ this.getMatrixAt(index).decompose(new Vector3(), rotation, new Vector3());
81563
+ return euler.setFromQuaternion(rotation);
81560
81564
  }
81561
81565
  setScaleAt(index, scale) {
81562
81566
  if (!this.isInstanced) throw new MineRenderError("Object is not instanced");
@@ -81836,11 +81840,12 @@ ${key}` : key;
81836
81840
  init_buffer2();
81837
81841
  init_process2();
81838
81842
  var p10 = prefix("SceneInspector");
81839
- var help = "<span>Ctrl+Click to Select<br/></span><br/>";
81843
+ var help = "<span>Ctrl/Cmd+Click to Select<br/></span><br/>";
81840
81844
  var SceneInspector = class {
81841
81845
  constructor(renderer) {
81842
81846
  this.renderer = renderer;
81843
81847
  this.mouse = new Vector2();
81848
+ this.listeners = new AbortController();
81844
81849
  this.objectInfoContainer = document.createElement("div");
81845
81850
  this.objectControlsContainer = document.createElement("div");
81846
81851
  this.raycaster = new Raycaster();
@@ -81850,13 +81855,44 @@ ${key}` : key;
81850
81855
  this.objectInfoContainer.classList.add("minerender-inspector", "minerender-object-info");
81851
81856
  this.objectControlsContainer.classList.add("minerender-inspector", "minerender-object-controls");
81852
81857
  this.objectInfoContainer.innerHTML = help;
81853
- document.addEventListener("click", this.onClick.bind(this), {
81854
- passive: true
81855
- });
81858
+ const canvas = this.renderer.renderer.domElement;
81859
+ const options = { signal: this.listeners.signal };
81860
+ canvas.addEventListener("pointerdown", (event) => {
81861
+ this.pointer = event.button === 0 && (event.ctrlKey || event.metaKey) ? event : void 0;
81862
+ }, options);
81863
+ canvas.addEventListener("pointermove", (event) => {
81864
+ if (this.pointer?.pointerId === event.pointerId && Math.hypot(event.clientX - this.pointer.clientX, event.clientY - this.pointer.clientY) > 5) {
81865
+ this.pointer = void 0;
81866
+ }
81867
+ }, options);
81868
+ canvas.addEventListener("pointerup", (event) => {
81869
+ const start = this.pointer;
81870
+ this.pointer = void 0;
81871
+ if (start?.pointerId === event.pointerId && Math.hypot(event.clientX - start.clientX, event.clientY - start.clientY) <= 5) {
81872
+ this.onClick(event);
81873
+ }
81874
+ }, options);
81875
+ canvas.addEventListener("pointercancel", () => {
81876
+ this.pointer = void 0;
81877
+ }, options);
81878
+ canvas.addEventListener("pointerleave", () => {
81879
+ this.pointer = void 0;
81880
+ }, options);
81881
+ canvas.addEventListener("contextmenu", (event) => {
81882
+ if (this.pointer?.ctrlKey) event.preventDefault();
81883
+ }, options);
81884
+ }
81885
+ dispose() {
81886
+ this.listeners.abort();
81887
+ this.pointer = void 0;
81888
+ this.selectedObject = void 0;
81889
+ this.objectInfoContainer.remove();
81890
+ this.objectControlsContainer.remove();
81856
81891
  }
81857
81892
  findMeshChildren(from, out = []) {
81893
+ if (!from.visible) return out;
81858
81894
  for (let c4 of from.children) {
81859
- if (isMesh(c4)) {
81895
+ if (c4.visible && isMesh(c4)) {
81860
81896
  out.push(c4);
81861
81897
  }
81862
81898
  this.findMeshChildren(c4, out);
@@ -81864,50 +81900,43 @@ ${key}` : key;
81864
81900
  return out;
81865
81901
  }
81866
81902
  onClick(event) {
81867
- if (event.target?.nodeName !== "CANVAS") return;
81868
- if (!event.ctrlKey) return;
81869
- setTimeout(() => {
81870
- this.mouse.x = event.clientX / window.innerWidth * 2 - 1;
81871
- this.mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
81872
- this.raycaster.setFromCamera(this.mouse, this.renderer.camera);
81873
- let start = Date.now();
81874
- let targets = this.findMeshChildren(this.renderer.scene);
81875
- const intersects = this.raycaster.intersectObjects(targets, false);
81876
- console.debug("Raycast took " + (Date.now() - start) + "ms for " + targets.length + " possible targets with " + intersects.length + " results");
81877
- if (intersects.length > 0) {
81878
- this.handleRaycasterObjects(intersects);
81879
- }
81880
- });
81903
+ const canvas = this.renderer.renderer.domElement;
81904
+ if (event.target !== canvas || event.button !== 0 || !(event.ctrlKey || event.metaKey)) return;
81905
+ const rect = canvas.getBoundingClientRect();
81906
+ const x4 = event.clientX - rect.left, y5 = event.clientY - rect.top;
81907
+ if (rect.width <= 0 || rect.height <= 0 || x4 < 0 || y5 < 0 || x4 >= rect.width || y5 >= rect.height) return;
81908
+ this.mouse.set(x4 / rect.width * 2 - 1, 1 - y5 / rect.height * 2);
81909
+ this.renderer.camera.updateWorldMatrix(true, false);
81910
+ this.renderer.scene.updateMatrixWorld(true);
81911
+ this.raycaster.setFromCamera(this.mouse, this.renderer.camera);
81912
+ this.handleRaycasterObjects(this.raycaster.intersectObjects(this.findMeshChildren(this.renderer.scene), false));
81881
81913
  }
81882
81914
  appendTo(el) {
81883
81915
  el.append(this.objectInfoContainer);
81884
81916
  el.append(this.objectControlsContainer);
81885
81917
  }
81886
81918
  handleRaycasterObjects(intersections) {
81887
- console.debug(intersections);
81888
- const firstIntersection = intersections[0];
81889
- if (firstIntersection && firstIntersection.object) {
81890
- const firstObject = firstIntersection.object;
81891
- let targetIntersection = void 0;
81892
- let targetObject = void 0;
81893
- const firstSceneObjectIntersection = intersections.find((i4) => i4.object && i4.object.parent && isSceneObject(i4.object.parent));
81894
- if (firstSceneObjectIntersection && firstSceneObjectIntersection.object) {
81895
- targetIntersection = firstSceneObjectIntersection;
81896
- targetObject = firstSceneObjectIntersection.object;
81897
- } else {
81898
- targetIntersection = firstIntersection;
81899
- targetObject = firstObject;
81919
+ const hit = intersections[0];
81920
+ if (!hit) return;
81921
+ const instance = this.getIntersectionInstance(hit);
81922
+ this.selectObject(instance?.instanceable ?? hit.object, hit, instance);
81923
+ }
81924
+ getIntersectionInstance(hit) {
81925
+ if (hit?.instanceId === void 0) return void 0;
81926
+ for (let parent = hit.object.parent; parent; parent = parent.parent) {
81927
+ if (isSceneObject(parent)) {
81928
+ const instance = parent.getInstanceReference(hit.object, hit.instanceId);
81929
+ if (instance) return instance;
81900
81930
  }
81901
- if (!targetIntersection || !targetObject) return;
81902
- this.selectObject(targetObject, targetIntersection);
81903
81931
  }
81932
+ return void 0;
81904
81933
  }
81905
- selectObject(targetObject, targetIntersection) {
81934
+ selectObject(targetObject, targetIntersection, instance = this.getIntersectionInstance(targetIntersection)) {
81906
81935
  this.selectedObject = targetObject;
81907
81936
  console.log(p10, "selected", targetObject);
81908
- this.redraw(targetObject, targetIntersection);
81937
+ this.redraw(targetObject, targetIntersection, instance);
81909
81938
  }
81910
- redraw(targetObject, targetIntersection) {
81939
+ redraw(targetObject, targetIntersection, instance) {
81911
81940
  this.objectInfoContainer.innerHTML = help;
81912
81941
  if (targetIntersection) {
81913
81942
  this.addInfoLine("Distance", "D", targetIntersection.distance);
@@ -81920,8 +81949,8 @@ ${key}` : key;
81920
81949
  if (isSceneObject(targetObject.parent)) {
81921
81950
  }
81922
81951
  }
81923
- if ("atlas" in targetObject) {
81924
- const atlas = targetObject["atlas"];
81952
+ const atlas = "atlas" in targetObject ? targetObject["atlas"] : void 0;
81953
+ if (atlas?.image.canvas) {
81925
81954
  const img = atlas.image.canvas;
81926
81955
  img.style.width = "100px";
81927
81956
  img.style.height = "100px";
@@ -81930,28 +81959,31 @@ ${key}` : key;
81930
81959
  this.objectInfoContainer.prepend(img);
81931
81960
  }
81932
81961
  this.objectControlsContainer.innerHTML = "";
81933
- this.addControls(targetObject, targetIntersection);
81962
+ this.addControls(targetObject, targetIntersection, instance);
81934
81963
  }
81935
- addControls(object, intersection) {
81964
+ addControls(object, intersection, instance) {
81936
81965
  const container = document.createElement("div");
81937
81966
  container.append(this.separator("Select Parent/Child"));
81938
81967
  if (object.parent) {
81939
81968
  container.append(this.buttonControl("Select Parent " + object.parent.constructor.name + " " + object.parent.name, "P", () => {
81940
- this.selectObject(object.parent, intersection);
81969
+ this.selectObject(object.parent, intersection, instance);
81941
81970
  }));
81942
81971
  }
81943
81972
  if (object.children.length > 0) {
81944
81973
  let i4 = 1;
81945
81974
  for (let child of object.children) {
81946
81975
  container.append(this.buttonControl("Select Child " + child.constructor.name + " " + child.name, "C" + i4++, () => {
81947
- this.selectObject(child, intersection);
81976
+ this.selectObject(child, intersection, instance);
81948
81977
  }));
81949
81978
  }
81950
81979
  }
81951
81980
  container.append(this.separator());
81952
- container.append(this.toggleControl("Visibility", "V", object.visible, (v5) => object.visible = v5));
81981
+ const selectedInstance = instance && (object === instance.instanceable || object === intersection?.object) ? instance : void 0;
81982
+ if (!selectedInstance) {
81983
+ container.append(this.toggleControl("Visibility", "V", object.visible, (v5) => object.visible = v5));
81984
+ }
81953
81985
  container.append(this.separator("Mesh"));
81954
- this.addObjectControls(object, intersection, container);
81986
+ this.addObjectControls(object, intersection, container, selectedInstance);
81955
81987
  container.append(this.separator());
81956
81988
  this.objectControlsContainer.append(container);
81957
81989
  }
@@ -81963,78 +81995,26 @@ ${key}` : key;
81963
81995
  this.objectInfoContainer.append(document.createElement("br"));
81964
81996
  return el;
81965
81997
  }
81966
- addObjectControls(target, intersection, container) {
81998
+ addObjectControls(target, intersection, container, instance) {
81967
81999
  const posRange = 16 * 16;
81968
82000
  const rotRange = 360;
81969
82001
  const scaleRange = 4;
81970
- if (typeof intersection !== "undefined" && typeof intersection.instanceId !== "undefined" && isSceneObject(target) && target.isInstanced) {
81971
- const scObj = target;
81972
- container.append(this.separator("Position"));
81973
- let pos = scObj.getPositionAt(intersection.instanceId);
81974
- container.append(this.rangeControl("X Position", "X", -posRange + pos.x, posRange + pos.x, pos.x, 1, (v5) => {
81975
- pos = scObj.getPositionAt(intersection.instanceId);
81976
- pos.x = v5;
81977
- scObj.setPositionAt(intersection.instanceId, pos);
81978
- }));
81979
- container.append(this.rangeControl("Y Position", "Y", -posRange + pos.y, posRange + pos.y, pos.y, 1, (v5) => {
81980
- pos = scObj.getPositionAt(intersection.instanceId);
81981
- pos.y = v5;
81982
- scObj.setPositionAt(intersection.instanceId, pos);
81983
- }));
81984
- container.append(this.rangeControl("Z Position", "Z", -posRange + pos.z, posRange + pos.z, pos.z, 1, (v5) => {
81985
- pos = scObj.getPositionAt(intersection.instanceId);
81986
- pos.z = v5;
81987
- scObj.setPositionAt(intersection.instanceId, pos);
81988
- }));
81989
- container.append(this.separator("Rotation"));
81990
- let rot = scObj.getRotationAt(intersection.instanceId);
81991
- container.append(this.rangeControl("X Rotation", "X", 0, rotRange, Math.round(toDegrees(rot.x)), 1, (v5) => {
81992
- rot = scObj.getRotationAt(intersection.instanceId);
81993
- rot.x = toRadians(v5);
81994
- scObj.setRotationAt(intersection.instanceId, rot);
81995
- }));
81996
- container.append(this.rangeControl("Y Rotation", "Y", 0, rotRange, Math.round(toDegrees(rot.y)), 1, (v5) => {
81997
- rot = scObj.getRotationAt(intersection.instanceId);
81998
- rot.y = toRadians(v5);
81999
- scObj.setRotationAt(intersection.instanceId, rot);
82000
- }));
82001
- container.append(this.rangeControl("Z Rotation", "Z", 0, rotRange, Math.round(toDegrees(rot.z)), 1, (v5) => {
82002
- rot = scObj.getRotationAt(intersection.instanceId);
82003
- rot.z = toRadians(v5);
82004
- scObj.setRotationAt(intersection.instanceId, rot);
82005
- }));
82006
- container.append(this.separator("Scale"));
82007
- let scl = scObj.getScale();
82008
- container.append(this.rangeControl("X Scale", "X", 0, scaleRange, scl.x, 0.1, (v5) => {
82009
- scl = scObj.getScaleAt(intersection.instanceId);
82010
- scl.x = v5;
82011
- scObj.setScaleAt(intersection.instanceId, scl);
82012
- }));
82013
- container.append(this.rangeControl("Y Scale", "Y", 0, scaleRange, scl.y, 0.1, (v5) => {
82014
- scl = scObj.getScaleAt(intersection.instanceId);
82015
- scl.y = v5;
82016
- scObj.setScaleAt(intersection.instanceId, scl);
82017
- }));
82018
- container.append(this.rangeControl("Z Scale", "Z", 0, scaleRange, scl.z, 0.1, (v5) => {
82019
- scl = scObj.getScaleAt(intersection.instanceId);
82020
- scl.z = v5;
82021
- scObj.setScaleAt(intersection.instanceId, scl);
82022
- }));
82023
- } else if (isTransformable(target)) {
82024
- const parent = target;
82002
+ const transform = instance ?? (isTransformable(target) ? target : void 0);
82003
+ if (transform) {
82004
+ const parent = transform;
82025
82005
  container.append(this.separator("Position"));
82026
82006
  let pos = parent.getPosition();
82027
- container.append(this.rangeControl("X Position", "X", -posRange, posRange, pos.x, 1, (v5) => {
82007
+ container.append(this.rangeControl("X Position", "X", pos.x - posRange, pos.x + posRange, pos.x, 1, (v5) => {
82028
82008
  pos = parent.getPosition();
82029
82009
  pos.x = v5;
82030
82010
  parent.setPosition(pos);
82031
82011
  }));
82032
- container.append(this.rangeControl("Y Position", "Y", -posRange, posRange, pos.y, 1, (v5) => {
82012
+ container.append(this.rangeControl("Y Position", "Y", pos.y - posRange, pos.y + posRange, pos.y, 1, (v5) => {
82033
82013
  pos = parent.getPosition();
82034
82014
  pos.y = v5;
82035
82015
  parent.setPosition(pos);
82036
82016
  }));
82037
- container.append(this.rangeControl("Z Position", "Z", -posRange, posRange, pos.z, 1, (v5) => {
82017
+ container.append(this.rangeControl("Z Position", "Z", pos.z - posRange, pos.z + posRange, pos.z, 1, (v5) => {
82038
82018
  pos = parent.getPosition();
82039
82019
  pos.z = v5;
82040
82020
  parent.setPosition(pos);
@@ -82123,6 +82103,7 @@ ${key}` : key;
82123
82103
  toggle.checked = val;
82124
82104
  toggle.addEventListener("change", (e6) => {
82125
82105
  change(toggle.checked);
82106
+ this.renderer.scene.dirty = true;
82126
82107
  });
82127
82108
  label.append(toggle);
82128
82109
  label.append(document.createElement("br"));
@@ -82135,7 +82116,6 @@ ${key}` : key;
82135
82116
  label.setAttribute("title", name2);
82136
82117
  labelText.style.width = "18%";
82137
82118
  labelText.style.display = "inline-block";
82138
- labelText.innerText = `${id} (${val})`;
82139
82119
  const range = document.createElement("input");
82140
82120
  range.setAttribute("type", "range");
82141
82121
  range.style.width = "80%";
@@ -82143,8 +82123,10 @@ ${key}` : key;
82143
82123
  range.min = `${min}`;
82144
82124
  range.step = `${step}`;
82145
82125
  range.value = `${val}`;
82126
+ labelText.innerText = `${id} (${range.value})`;
82146
82127
  const onChange = () => {
82147
82128
  change(parseFloat(range.value));
82129
+ this.renderer.scene.dirty = true;
82148
82130
  labelText.innerText = `${id} (${range.value})`;
82149
82131
  };
82150
82132
  range.addEventListener("input", onChange);