-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddMesh.html
132 lines (100 loc) · 3.5 KB
/
addMesh.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button onclick="add('cube'); addControls(cube)" id="addCube">cube</button>
<button onclick="add('sphere')" id="addSphere">sphere</button>
<br>
<button onclick="deleteObject(); deleteControls()">delete</button>
<script src="js/three.min.js"></script>
<script src='js/dat.gui.min.js'></script>
<script src="js/OrbitControls.js"></script>
<script>
var scene, camera, renderer;
var light, backgroundLight;
var gui;
var HEIGHT = window.innerHeight,
WIDTH = window.innerWidth;
var objects = new THREE.Object3D();
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75,WIDTH/HEIGHT,.1,1000);
camera.position.set(10,10,10);
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( WIDTH, HEIGHT );
renderer.setClearColor(0xFFFFFF);
document.body.appendChild( renderer.domElement );
var orbit = new THREE.OrbitControls( camera, renderer.domElement );
light = new THREE.PointLight(0xffffff);
light.position.set(15,30,50);
scene.add(light);
var axisHelper = new THREE.AxisHelper( 5 );
scene.add( axisHelper );
backgroundLight = new THREE.PointLight(0xffffcc, 0.5);
backgroundLight.position.set(-10,-30,-50);
scene.add(backgroundLight);
function add(mesh) {
var geometry, material;
if (mesh == 'cube') {
geometry = new THREE.BoxGeometry(1,1,1);
material = new THREE.MeshLambertMaterial({color: 0xCCFFCC});
cube = new THREE.Mesh(geometry, material);
cube.position.set(0,0,0);
objects.add(cube);
}
else if (mesh == 'sphere') {
geometry = new THREE.SphereGeometry(2,32,32);
material = new THREE.MeshLambertMaterial({color: 0xCCFFCC});
sphere = new THREE.Mesh(geometry, material);
sphere.position.set(0,0,10);
objects.add(sphere)
}
scene.add(objects);
}
function deleteObject() {
scene.remove(objects);
// objects.remove(cube);
// objects.remove(sphere);
}
function addControls(mesh) {
function updateGeometry (mesh, geometry) {
mesh.geometry.dispose();
mesh.geometry = geometry;
}
var data = {
width : 1,
height : 1,
depth : 1
};
function generateGeometry() {
updateGeometry(mesh,
new THREE.CubeGeometry(
data.width, data.height, data.depth
))
}
gui = new dat.GUI();
gui.add(data,'width', 0, 10).onChange(generateGeometry);
gui.add(data,'height', 0, 10).onChange(generateGeometry);
gui.add(data,'depth', 0, 10).onChange(generateGeometry);
}
function deleteControls () {
var parent = document.getElementsByClassName('ac')[0];
var child = document.getElementsByClassName('main')[0];
parent.removeChild(child);
}
var render = function () {
requestAnimationFrame( render );
renderer.render( scene, camera );
};
window.addEventListener( 'resize', function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}, false );
render();
</script>
</body>
</html>