awacke1 commited on
Commit
2ffb026
·
verified ·
1 Parent(s): f475b68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -11,32 +11,35 @@ def load_aframe_and_extras():
11
  <script>
12
  AFRAME.registerComponent('draggable', {
13
  init: function () {
 
14
  this.el.setAttribute('cursor-listener', '');
15
- this.dragHandler = e => this.dragMove(e);
16
  this.el.sceneEl.addEventListener('mousemove', this.dragHandler);
17
- this.el.setAttribute('event-set__mousedown', 'startDragging');
18
- this.el.setAttribute('event-set__mouseup', 'stopDragging');
 
19
  },
20
  remove: function () {
21
  this.el.removeAttribute('cursor-listener');
22
  this.el.sceneEl.removeEventListener('mousemove', this.dragHandler);
23
  },
24
- startDragging: function () {
25
  this.isDragging = true;
 
26
  },
27
- stopDragging: function () {
28
  this.isDragging = false;
 
29
  },
30
  dragMove: function (evt) {
31
- if (this.isDragging) {
32
- var camera = this.el.sceneEl.camera;
33
- var vector = new THREE.Vector3(evt.clientX / window.innerWidth * 2 - 1, -(evt.clientY / window.innerHeight) * 2 + 1, 0.5);
34
- vector.unproject(camera);
35
- var dir = vector.sub(camera.position).normalize();
36
- var distance = -camera.position.y / dir.y;
37
- var pos = camera.position.clone().add(dir.multiplyScalar(distance));
38
- this.el.setAttribute('position', pos);
39
- }
40
  }
41
  });
42
  </script>
@@ -44,13 +47,13 @@ def load_aframe_and_extras():
44
 
45
  def create_aframe_entity(file_path, file_type, position):
46
  if file_type == 'obj':
47
- return f'<a-entity position="{position}" obj-model="obj: #{Path(file_path).stem}" draggable></a-entity>'
48
  elif file_type == 'glb':
49
- return f'<a-entity position="{position}" gltf-model="#{Path(file_path).stem}" draggable></a-entity>'
50
  elif file_type in ['webp', 'png']:
51
- return f'<a-image position="{position}" src="#{Path(file_path).stem}" width="1" height="1" draggable></a-image>'
52
  elif file_type == 'mp4':
53
- return f'<a-video position="{position}" src="#{Path(file_path).stem}" width="1" height="1" draggable></a-video>'
54
  return ''
55
 
56
  def encode_file(file_path):
@@ -83,7 +86,6 @@ def main():
83
 
84
  file_types = ['obj', 'glb', 'webp', 'png', 'mp4']
85
 
86
- # Handle uploaded files
87
  if uploaded_files:
88
  for uploaded_file in uploaded_files:
89
  file_extension = Path(uploaded_file.name).suffix.lower()[1:]
@@ -98,7 +100,7 @@ def main():
98
 
99
  aframe_scene = """
100
  <a-scene embedded style="height: 600px; width: 100%;">
101
- <a-entity camera="userHeight: 1.6" position="0 2 2" rotation="-45 0 0" cursor="rayOrigin: mouse"></a-entity>
102
  """
103
 
104
  assets = "<a-assets>"
 
11
  <script>
12
  AFRAME.registerComponent('draggable', {
13
  init: function () {
14
+ this.el.setAttribute('class', 'raycastable');
15
  this.el.setAttribute('cursor-listener', '');
16
+ this.dragHandler = this.dragMove.bind(this);
17
  this.el.sceneEl.addEventListener('mousemove', this.dragHandler);
18
+ this.el.addEventListener('mousedown', this.onDragStart.bind(this));
19
+ this.el.addEventListener('mouseup', this.onDragEnd.bind(this));
20
+ this.camera = document.querySelector('[camera]');
21
  },
22
  remove: function () {
23
  this.el.removeAttribute('cursor-listener');
24
  this.el.sceneEl.removeEventListener('mousemove', this.dragHandler);
25
  },
26
+ onDragStart: function (evt) {
27
  this.isDragging = true;
28
+ this.el.emit('dragstart');
29
  },
30
+ onDragEnd: function (evt) {
31
  this.isDragging = false;
32
+ this.el.emit('dragend');
33
  },
34
  dragMove: function (evt) {
35
+ if (!this.isDragging) return;
36
+ var camera = this.camera;
37
+ var vector = new THREE.Vector3(evt.clientX / window.innerWidth * 2 - 1, -(evt.clientY / window.innerHeight) * 2 + 1, 0.5);
38
+ vector.unproject(camera);
39
+ var dir = vector.sub(camera.position).normalize();
40
+ var distance = -camera.position.y / dir.y;
41
+ var pos = camera.position.clone().add(dir.multiplyScalar(distance));
42
+ this.el.setAttribute('position', pos);
 
43
  }
44
  });
45
  </script>
 
47
 
48
  def create_aframe_entity(file_path, file_type, position):
49
  if file_type == 'obj':
50
+ return f'<a-entity position="{position}" obj-model="obj: #{Path(file_path).stem}" class="raycastable" draggable></a-entity>'
51
  elif file_type == 'glb':
52
+ return f'<a-entity position="{position}" gltf-model="#{Path(file_path).stem}" class="raycastable" draggable></a-entity>'
53
  elif file_type in ['webp', 'png']:
54
+ return f'<a-image position="{position}" src="#{Path(file_path).stem}" width="1" height="1" class="raycastable" draggable></a-image>'
55
  elif file_type == 'mp4':
56
+ return f'<a-video position="{position}" src="#{Path(file_path).stem}" width="1" height="1" class="raycastable" draggable></a-video>'
57
  return ''
58
 
59
  def encode_file(file_path):
 
86
 
87
  file_types = ['obj', 'glb', 'webp', 'png', 'mp4']
88
 
 
89
  if uploaded_files:
90
  for uploaded_file in uploaded_files:
91
  file_extension = Path(uploaded_file.name).suffix.lower()[1:]
 
100
 
101
  aframe_scene = """
102
  <a-scene embedded style="height: 600px; width: 100%;">
103
+ <a-entity camera="userHeight: 1.6" position="0 2 2" rotation="-45 0 0" cursor="rayOrigin: mouse" raycaster="objects: .raycastable"></a-entity>
104
  """
105
 
106
  assets = "<a-assets>"