-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3deditor.html
109 lines (98 loc) · 3.15 KB
/
3deditor.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Builder</title>
<style>
body, html {
margin: 0;
padding: 0;
overflow: hidden;
}
#canvas-container {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
}
#toolbar {
background-color: #333;
color: white;
padding: 10px;
display: flex;
justify-content: space-around;
align-items: center;
}
button {
background-color: #555;
color: white;
border: none;
padding: 10px 15px;
cursor: pointer;
}
button:hover {
background-color: #777;
}
#threejs-canvas {
flex: 1;
}
</style>
</head>
<body>
<div id="canvas-container">
<div id="toolbar">
<button id="add-cube">Add Cube</button>
<button id="add-sphere">Add Sphere</button>
<button id="reset-scene">Reset Scene</button>
</div>
<canvas id="threejs-canvas"></canvas>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r146/three.min.js"></script>
<script>
// Initialize Scene
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('threejs-canvas') });
renderer.setSize(window.innerWidth, window.innerHeight);
camera.position.z = 5;
// Lighting
const light = new THREE.PointLight(0xffffff, 1, 100);
light.position.set(10, 10, 10);
scene.add(light);
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
// Helpers
const geometryMaterials = {
cube: new THREE.BoxGeometry(),
sphere: new THREE.SphereGeometry(1, 32, 32),
};
const addCube = () => {
const material = new THREE.MeshStandardMaterial({ color: Math.random() * 0xffffff });
const cube = new THREE.Mesh(geometryMaterials.cube, material);
cube.position.set(Math.random() * 4 - 2, Math.random() * 4 - 2, Math.random() * 4 - 2);
scene.add(cube);
};
const addSphere = () => {
const material = new THREE.MeshStandardMaterial({ color: Math.random() * 0xffffff });
const sphere = new THREE.Mesh(geometryMaterials.sphere, material);
sphere.position.set(Math.random() * 4 - 2, Math.random() * 4 - 2, Math.random() * 4 - 2);
scene.add(sphere);
};
const resetScene = () => {
while (scene.children.length > 2) {
scene.remove(scene.children[scene.children.length - 1]);
}
};
document.getElementById('add-cube').addEventListener('click', addCube);
document.getElementById('add-sphere').addEventListener('click', addSphere);
document.getElementById('reset-scene').addEventListener('click', resetScene);
// Animation Loop
const animate = () => {
requestAnimationFrame(animate);
renderer.render(scene, camera);
};
animate();
</script>
</body>
</html>