-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.js
121 lines (92 loc) · 4.11 KB
/
ui.js
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
export function createControls(sphere) {
let controlsDiv = document.createElement('div');
controlsDiv.style.margin = '20px';
let sphereDiv = document.createElement('div');
sphereDiv.classList.add('control-element');
let materialDiv = document.createElement('div');
materialDiv .classList.add('control-element');
let id = sphere.id;
controlsDiv.appendChild(sphereDiv);
controlsDiv.appendChild(materialDiv);
let positionXInput = createTextInput('Position X ', 'positionX'+id, 0);
let positionYInput = createTextInput('Position Y ', 'positionY'+id, 0);
let positionZInput = createTextInput('Position Z ', 'positionZ'+id, 0);
let radiusInput = createTextInput('Radius ', 'radius'+id, 3);
let colorInput = createColorInput('Base Color ', 'color'+id, '#ff0000');
let emissiveColorInput = createColorInput('Emissive Color ', 'eColor'+id, '#000000');
let emissiveStrengthInput = createTextInput(' Emissive Strength ', 'eStrength'+id, 0);
let roughnessInput = createTextInput(' Roughness ', 'roughness'+id, 1);
positionXInput.onchange = (() => updateSphere(sphere));
positionYInput.onchange = (() => updateSphere(sphere));
positionZInput.onchange = (() => updateSphere(sphere));
radiusInput.onchange = (() => updateSphere(sphere));
colorInput.onchange = (() => updateSphere(sphere));
emissiveColorInput.onchange = (() => updateSphere(sphere));
emissiveStrengthInput.onchange = (() => updateSphere(sphere));
roughnessInput.onchange = (() => updateSphere(sphere));
sphereDiv.appendChild(positionXInput);
sphereDiv.appendChild(positionYInput);
sphereDiv.appendChild(positionZInput);
sphereDiv.appendChild(radiusInput);
materialDiv.appendChild(colorInput);
materialDiv.appendChild(roughnessInput);
materialDiv.appendChild(emissiveColorInput);
materialDiv.appendChild(emissiveStrengthInput);
controlsDiv.classList.add("sphere-control");
let editDiv = document.getElementById("editing");
editDiv.appendChild(controlsDiv);
updateSphere(sphere);
}
function createTextInput(labelText, id, defaultValue) {
let label = document.createElement('label');
label.textContent = labelText;
let input = document.createElement('input');
input.id = id;
input.type = 'number';
input.value = defaultValue;
input.style.marginRight = '10px';
input.classList.add('controls-input');
label.appendChild(input);
return label;
}
function createColorInput(labelText, id, defaultValue) {
let label = document.createElement('label');
label.textContent = labelText;
let colorPicker = document.createElement('input');
colorPicker.id = id;
colorPicker.type = 'color';
colorPicker.value = defaultValue;
label.appendChild(colorPicker);
return label;
}
function updateSphere(sphere) {
let id = sphere.id;
let positionX = parseFloat(document.getElementById('positionX'+id).value);
let positionY = parseFloat(document.getElementById('positionY'+id).value);
let positionZ = parseFloat(document.getElementById('positionZ'+id).value);
let radius = parseFloat(document.getElementById('radius'+id).value);
let color = hexToRgb(document.getElementById('color'+id).value);
let emissiveColor = hexToRgb(document.getElementById('eColor'+id).value);
let emissiveStrength = parseFloat(document.getElementById('eStrength'+id).value);
let roughness = parseFloat(document.getElementById('roughness'+id).value);
sphere.position.x = positionX;
sphere.position.y = positionY;
sphere.position.z = positionZ;
sphere.radius = radius;
sphere.material.color = { x: color.r/255, y: color.g/255, z: color.b/255 };
sphere.material.roughness = roughness;
sphere.material.emissiveColor = { x: emissiveColor.r, y: emissiveColor.g, z: emissiveColor.b };
sphere.material.emissiveStrength = emissiveStrength;
}
function hexToRgb(hex) {
hex = hex.replace(/^#/, '');
let int = parseInt(hex, 16);
let red = (int >> 16) & 255;
let green = (int >> 8) & 255;
let blue = int & 255;
return {
r: red,
g: green,
b: blue
};
}