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html>
|
2 |
<html>
|
3 |
+
<head>
|
4 |
+
<meta charset="utf-8">
|
5 |
+
<title>A-Frame 3D Editor with Animated Rotation and Color Picker</title>
|
6 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/aframe/1.2.0/aframe.min.js"></script>
|
7 |
+
<style>
|
8 |
+
.ui-panel {
|
9 |
+
position: fixed;
|
10 |
+
top: 20px;
|
11 |
+
left: 20px;
|
12 |
+
z-index: 9999;
|
13 |
+
background: rgba(0,0,0,0.7);
|
14 |
+
padding: 10px;
|
15 |
+
border-radius: 5px;
|
16 |
+
color: white;
|
17 |
+
font-family: Arial, sans-serif;
|
18 |
+
}
|
19 |
+
.ui-panel button {
|
20 |
+
display: block;
|
21 |
+
margin: 5px 0;
|
22 |
+
padding: 5px 10px;
|
23 |
+
}
|
24 |
+
.control-panel {
|
25 |
+
position: fixed;
|
26 |
+
top: 20px;
|
27 |
+
right: 20px;
|
28 |
+
z-index: 9999;
|
29 |
+
background: rgba(0,0,0,0.7);
|
30 |
+
padding: 10px;
|
31 |
+
border-radius: 5px;
|
32 |
+
color: white;
|
33 |
+
font-family: Arial, sans-serif;
|
34 |
+
}
|
35 |
+
.control-panel input {
|
36 |
+
width: 50px;
|
37 |
+
margin-right: 5px;
|
38 |
+
}
|
39 |
+
.color-picker {
|
40 |
+
display: flex;
|
41 |
+
align-items: center;
|
42 |
+
margin-top: 10px;
|
43 |
+
}
|
44 |
+
.color-picker input[type="color"] {
|
45 |
+
margin-right: 10px;
|
46 |
+
}
|
47 |
+
</style>
|
48 |
+
</head>
|
49 |
+
<body>
|
50 |
+
<div class="ui-panel">
|
51 |
+
<button onclick="addPrimitive('box')">Add Box</button>
|
52 |
+
<button onclick="addPrimitive('sphere')">Add Sphere</button>
|
53 |
+
<button onclick="addPrimitive('cylinder')">Add Cylinder</button>
|
54 |
+
<button onclick="addPrimitive('cone')">Add Cone</button>
|
55 |
+
<button onclick="addPrimitive('torus')">Add Torus</button>
|
56 |
+
</div>
|
57 |
+
|
58 |
+
<div class="control-panel">
|
59 |
+
<h3>Object Controls</h3>
|
60 |
+
<p>Position:</p>
|
61 |
+
<input id="px" type="number" step="0.1" placeholder="X">
|
62 |
+
<input id="py" type="number" step="0.1" placeholder="Y">
|
63 |
+
<input id="pz" type="number" step="0.1" placeholder="Z">
|
64 |
+
<p>Rotation (degrees):</p>
|
65 |
+
<input id="rx" type="number" step="1" placeholder="X">
|
66 |
+
<input id="ry" type="number" step="1" placeholder="Y">
|
67 |
+
<input id="rz" type="number" step="1" placeholder="Z">
|
68 |
+
<p>Rotation Velocity (degrees/second):</p>
|
69 |
+
<input id="rvx" type="number" step="1" placeholder="X">
|
70 |
+
<input id="rvy" type="number" step="1" placeholder="Y">
|
71 |
+
<input id="rvz" type="number" step="1" placeholder="Z">
|
72 |
+
<button onclick="applyTransform()">Apply Transform</button>
|
73 |
+
<button onclick="placeInFrontOfCamera()">Place in Front of Camera</button>
|
74 |
+
<div class="color-picker">
|
75 |
+
<input type="color" id="colorPicker" value="#4CC3D9">
|
76 |
+
<button onclick="applyColor()">Apply Color</button>
|
77 |
+
</div>
|
78 |
+
</div>
|
79 |
+
|
80 |
+
<a-scene>
|
81 |
+
<a-sky color="#ECECEC"></a-sky>
|
82 |
+
<a-entity id="rig" position="0 1.6 0">
|
83 |
+
<a-camera look-controls wasd-controls>
|
84 |
+
<a-cursor></a-cursor>
|
85 |
+
</a-camera>
|
86 |
+
</a-entity>
|
87 |
+
</a-scene>
|
88 |
+
|
89 |
+
<script>
|
90 |
+
let selectedObject = null;
|
91 |
+
let objects = [];
|
92 |
+
|
93 |
+
function addPrimitive(type) {
|
94 |
+
const scene = document.querySelector('a-scene');
|
95 |
+
const newEntity = document.createElement('a-entity');
|
96 |
+
newEntity.setAttribute('geometry', `primitive: ${type}`);
|
97 |
+
newEntity.setAttribute('material', 'color: #4CC3D9');
|
98 |
+
newEntity.setAttribute('class', 'editable');
|
99 |
+
newEntity.addEventListener('click', function() { selectObject(this); });
|
100 |
+
newEntity.rotationVelocity = { x: 0, y: 30, z: 0 }; // Default rotation velocity
|
101 |
+
scene.appendChild(newEntity);
|
102 |
+
objects.push(newEntity);
|
103 |
+
placeInFrontOfCamera(newEntity);
|
104 |
+
}
|
105 |
+
|
106 |
+
function selectObject(obj) {
|
107 |
+
if (selectedObject) {
|
108 |
+
selectedObject.setAttribute('material', 'color', selectedObject.getAttribute('material').color);
|
109 |
+
}
|
110 |
+
selectedObject = obj;
|
111 |
+
document.getElementById('colorPicker').value = selectedObject.getAttribute('material').color;
|
112 |
+
selectedObject.setAttribute('material', 'color', '#FF0000');
|
113 |
+
updateTransformInputs(selectedObject);
|
114 |
+
}
|
115 |
+
|
116 |
+
function updateTransformInputs(obj) {
|
117 |
+
const position = obj.getAttribute('position');
|
118 |
+
document.getElementById('px').value = position.x.toFixed(2);
|
119 |
+
document.getElementById('py').value = position.y.toFixed(2);
|
120 |
+
document.getElementById('pz').value = position.z.toFixed(2);
|
121 |
+
|
122 |
+
const rotation = obj.getAttribute('rotation');
|
123 |
+
document.getElementById('rx').value = rotation.x.toFixed(2);
|
124 |
+
document.getElementById('ry').value = rotation.y.toFixed(2);
|
125 |
+
document.getElementById('rz').value = rotation.z.toFixed(2);
|
126 |
+
|
127 |
+
document.getElementById('rvx').value = obj.rotationVelocity.x;
|
128 |
+
document.getElementById('rvy').value = obj.rotationVelocity.y;
|
129 |
+
document.getElementById('rvz').value = obj.rotationVelocity.z;
|
130 |
+
}
|
131 |
+
|
132 |
+
function applyTransform() {
|
133 |
+
if (!selectedObject) {
|
134 |
+
alert('Please select an object first');
|
135 |
+
return;
|
136 |
+
}
|
137 |
+
|
138 |
+
const px = parseFloat(document.getElementById('px').value) || 0;
|
139 |
+
const py = parseFloat(document.getElementById('py').value) || 0;
|
140 |
+
const pz = parseFloat(document.getElementById('pz').value) || 0;
|
141 |
+
const rx = parseFloat(document.getElementById('rx').value) || 0;
|
142 |
+
const ry = parseFloat(document.getElementById('ry').value) || 0;
|
143 |
+
const rz = parseFloat(document.getElementById('rz').value) || 0;
|
144 |
+
const rvx = parseFloat(document.getElementById('rvx').value) || 0;
|
145 |
+
const rvy = parseFloat(document.getElementById('rvy').value) || 0;
|
146 |
+
const rvz = parseFloat(document.getElementById('rvz').value) || 0;
|
147 |
+
|
148 |
+
selectedObject.setAttribute('position', `${px} ${py} ${pz}`);
|
149 |
+
selectedObject.setAttribute('rotation', `${rx} ${ry} ${rz}`);
|
150 |
+
selectedObject.rotationVelocity = { x: rvx, y: rvy, z: rvz };
|
151 |
+
}
|
152 |
+
|
153 |
+
function placeInFrontOfCamera(obj = null) {
|
154 |
+
const targetObject = obj || selectedObject;
|
155 |
+
if (!targetObject) {
|
156 |
+
alert('Please select an object first');
|
157 |
+
return;
|
158 |
+
}
|
159 |
+
|
160 |
+
const camera = document.querySelector('[camera]');
|
161 |
+
const cameraPosition = camera.object3D.position;
|
162 |
+
const cameraRotation = camera.object3D.rotation;
|
163 |
+
|
164 |
+
// Calculate the direction the camera is looking
|
165 |
+
const direction = new THREE.Vector3(0, 0, -1);
|
166 |
+
direction.applyQuaternion(camera.object3D.quaternion);
|
167 |
+
|
168 |
+
// Set the distance to be 3 units away from the camera
|
169 |
+
const distance = 3;
|
170 |
+
|
171 |
+
// Calculate the new position
|
172 |
+
const newPosition = new THREE.Vector3(
|
173 |
+
cameraPosition.x + direction.x * distance,
|
174 |
+
cameraPosition.y + direction.y * distance,
|
175 |
+
cameraPosition.z + direction.z * distance
|
176 |
+
);
|
177 |
+
|
178 |
+
targetObject.setAttribute('position', newPosition);
|
179 |
+
|
180 |
+
// Update position inputs if the object is selected
|
181 |
+
if (targetObject === selectedObject) {
|
182 |
+
updateTransformInputs(targetObject);
|
183 |
+
}
|
184 |
+
}
|
185 |
+
|
186 |
+
function applyColor() {
|
187 |
+
if (!selectedObject) {
|
188 |
+
alert('Please select an object first');
|
189 |
+
return;
|
190 |
+
}
|
191 |
+
const color = document.getElementById('colorPicker').value;
|
192 |
+
selectedObject.setAttribute('material', 'color', color);
|
193 |
+
}
|
194 |
+
|
195 |
+
function animateObjects() {
|
196 |
+
objects.forEach(obj => {
|
197 |
+
const currentRotation = obj.getAttribute('rotation');
|
198 |
+
const newRotation = {
|
199 |
+
x: (currentRotation.x + obj.rotationVelocity.x * 0.016) % 360, // 0.016 is approximately 1/60, assuming 60 FPS
|
200 |
+
y: (currentRotation.y + obj.rotationVelocity.y * 0.016) % 360,
|
201 |
+
z: (currentRotation.z + obj.rotationVelocity.z * 0.016) % 360
|
202 |
+
};
|
203 |
+
obj.setAttribute('rotation', newRotation);
|
204 |
+
});
|
205 |
+
requestAnimationFrame(animateObjects);
|
206 |
+
}
|
207 |
+
|
208 |
+
// Event listener for object selection
|
209 |
+
document.querySelector('a-scene').addEventListener('click', (event) => {
|
210 |
+
if (event.detail.intersectedEl && event.detail.intersectedEl.classList.contains('editable')) {
|
211 |
+
selectObject(event.detail.intersectedEl);
|
212 |
+
}
|
213 |
+
});
|
214 |
+
|
215 |
+
// Start the animation loop
|
216 |
+
animateObjects();
|
217 |
+
</script>
|
218 |
+
</body>
|
219 |
+
</html>
|