File size: 8,003 Bytes
7847be5
ce95d07
7847be5
 
eaa35ab
7847be5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c48b13
7847be5
 
 
 
 
 
 
 
 
 
9c48b13
7847be5
 
 
 
 
 
 
3ec5fca
 
 
 
 
7847be5
 
9c48b13
04e6357
 
9c48b13
 
 
7847be5
9c48b13
 
 
eaa35ab
 
 
 
04e6357
 
7847be5
 
9c48b13
 
 
 
 
 
7847be5
 
 
 
 
eaa35ab
7847be5
3ec5fca
7847be5
 
 
9c48b13
7847be5
 
eaa35ab
7847be5
eaa35ab
cf85c1d
7847be5
 
 
 
9c48b13
7847be5
 
9c48b13
cf85c1d
 
 
 
 
 
 
 
04e6357
cf85c1d
 
 
 
eaa35ab
 
 
 
7847be5
 
04e6357
7847be5
 
 
 
 
04e6357
 
 
7847be5
 
 
eaa35ab
 
 
7847be5
04e6357
 
eaa35ab
04e6357
 
cf85c1d
 
 
04e6357
 
 
 
 
8cb8d52
 
 
 
 
 
 
9c48b13
 
8cb8d52
 
 
 
 
 
 
 
 
04e6357
cf85c1d
 
 
 
7847be5
 
eaa35ab
 
 
 
 
 
 
 
 
 
 
 
 
9c48b13
 
 
cf85c1d
 
 
eaa35ab
 
 
7847be5
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>A-Frame 3D Editor with Animated Rotation</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/aframe/1.2.0/aframe.min.js"></script>
    <style>
        .ui-panel {
            position: fixed;
            top: 20px;
            left: 20px;
            z-index: 9999;
            background: rgba(0,0,0,0.7);
            padding: 10px;
            border-radius: 5px;
            color: white;
            font-family: Arial, sans-serif;
        }
        .ui-panel button {
            display: block;
            margin: 5px 0;
            padding: 5px 10px;
        }
        .control-panel {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 9999;
            background: rgba(0,0,0,0.7);
            padding: 10px;
            border-radius: 5px;
            color: white;
            font-family: Arial, sans-serif;
        }
        .control-panel input {
            width: 50px;
            margin-right: 5px;
        }
    </style>
</head>
<body>
    <div class="ui-panel">
        <button onclick="addPrimitive('box')">Add Box</button>
        <button onclick="addPrimitive('sphere')">Add Sphere</button>
        <button onclick="addPrimitive('cylinder')">Add Cylinder</button>
        <button onclick="addPrimitive('cone')">Add Cone</button>
        <button onclick="addPrimitive('torus')">Add Torus</button>
    </div>

    <div class="control-panel">
        <h3>Object Controls</h3>
        <p>Position:</p>
        <input id="px" type="number" step="0.1" placeholder="X"> 
        <input id="py" type="number" step="0.1" placeholder="Y"> 
        <input id="pz" type="number" step="0.1" placeholder="Z">
        <p>Rotation (degrees):</p>
        <input id="rx" type="number" step="1" placeholder="X"> 
        <input id="ry" type="number" step="1" placeholder="Y"> 
        <input id="rz" type="number" step="1" placeholder="Z">
        <p>Rotation Velocity (degrees/second):</p>
        <input id="rvx" type="number" step="1" placeholder="X"> 
        <input id="rvy" type="number" step="1" placeholder="Y"> 
        <input id="rvz" type="number" step="1" placeholder="Z">
        <button onclick="applyTransform()">Apply Transform</button>
        <button onclick="placeInFrontOfCamera()">Place in Front of Camera</button>
    </div>

    <a-scene>
        <a-sky color="#ECECEC"></a-sky>
        <a-entity id="rig" position="0 1.6 0">
            <a-camera look-controls wasd-controls>
                <a-cursor></a-cursor>
            </a-camera>
        </a-entity>
    </a-scene>

    <script>
        let selectedObject = null;
        let objects = [];

        function addPrimitive(type) {
            const scene = document.querySelector('a-scene');
            const newEntity = document.createElement('a-entity');
            newEntity.setAttribute('geometry', `primitive: ${type}`);
            newEntity.setAttribute('material', 'color: #4CC3D9');
            newEntity.setAttribute('class', 'editable');
            newEntity.addEventListener('click', function() { selectObject(this); });
            newEntity.rotationVelocity = { x: 0, y: 30, z: 0 }; // Default rotation velocity
            scene.appendChild(newEntity);
            objects.push(newEntity);
            placeInFrontOfCamera(newEntity);
        }

        function selectObject(obj) {
            if (selectedObject) {
                selectedObject.setAttribute('material', 'color', '#4CC3D9');
            }
            selectedObject = obj;
            selectedObject.setAttribute('material', 'color', '#FF0000');
            updateTransformInputs(selectedObject);
        }

        function updateTransformInputs(obj) {
            const position = obj.getAttribute('position');
            document.getElementById('px').value = position.x.toFixed(2);
            document.getElementById('py').value = position.y.toFixed(2);
            document.getElementById('pz').value = position.z.toFixed(2);
            
            const rotation = obj.getAttribute('rotation');
            document.getElementById('rx').value = rotation.x.toFixed(2);
            document.getElementById('ry').value = rotation.y.toFixed(2);
            document.getElementById('rz').value = rotation.z.toFixed(2);

            document.getElementById('rvx').value = obj.rotationVelocity.x;
            document.getElementById('rvy').value = obj.rotationVelocity.y;
            document.getElementById('rvz').value = obj.rotationVelocity.z;
        }

        function applyTransform() {
            if (!selectedObject) {
                alert('Please select an object first');
                return;
            }

            const px = parseFloat(document.getElementById('px').value) || 0;
            const py = parseFloat(document.getElementById('py').value) || 0;
            const pz = parseFloat(document.getElementById('pz').value) || 0;
            const rx = parseFloat(document.getElementById('rx').value) || 0;
            const ry = parseFloat(document.getElementById('ry').value) || 0;
            const rz = parseFloat(document.getElementById('rz').value) || 0;
            const rvx = parseFloat(document.getElementById('rvx').value) || 0;
            const rvy = parseFloat(document.getElementById('rvy').value) || 0;
            const rvz = parseFloat(document.getElementById('rvz').value) || 0;

            selectedObject.setAttribute('position', `${px} ${py} ${pz}`);
            selectedObject.setAttribute('rotation', `${rx} ${ry} ${rz}`);
            selectedObject.rotationVelocity = { x: rvx, y: rvy, z: rvz };
        }

        function placeInFrontOfCamera(obj = null) {
            const targetObject = obj || selectedObject;
            if (!targetObject) {
                alert('Please select an object first');
                return;
            }

            const camera = document.querySelector('[camera]');
            const cameraPosition = camera.object3D.position;
            const cameraRotation = camera.object3D.rotation;
            
            // Calculate the direction the camera is looking
            const direction = new THREE.Vector3(0, 0, -1);
            direction.applyQuaternion(camera.object3D.quaternion);
            
            // Set the distance to be 3 units away from the camera
            const distance = 3;
            
            // Calculate the new position
            const newPosition = new THREE.Vector3(
                cameraPosition.x + direction.x * distance,
                cameraPosition.y + direction.y * distance,
                cameraPosition.z + direction.z * distance
            );

            targetObject.setAttribute('position', newPosition);
            
            // Update position inputs if the object is selected
            if (targetObject === selectedObject) {
                updateTransformInputs(targetObject);
            }
        }

        function animateObjects() {
            objects.forEach(obj => {
                const currentRotation = obj.getAttribute('rotation');
                const newRotation = {
                    x: (currentRotation.x + obj.rotationVelocity.x * 0.016) % 360, // 0.016 is approximately 1/60, assuming 60 FPS
                    y: (currentRotation.y + obj.rotationVelocity.y * 0.016) % 360,
                    z: (currentRotation.z + obj.rotationVelocity.z * 0.016) % 360
                };
                obj.setAttribute('rotation', newRotation);
            });
            requestAnimationFrame(animateObjects);
        }

        // Event listener for object selection
        document.querySelector('a-scene').addEventListener('click', (event) => {
            if (event.detail.intersectedEl && event.detail.intersectedEl.classList.contains('editable')) {
                selectObject(event.detail.intersectedEl);
            }
        });

        // Start the animation loop
        animateObjects();
    </script>
</body>
</html>