-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSounddiffuser.html
More file actions
245 lines (211 loc) · 10.7 KB
/
Copy pathSounddiffuser.html
File metadata and controls
245 lines (211 loc) · 10.7 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Parametric Diffuser Designer</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<style>
body { margin: 0; font-family: 'Segoe UI', sans-serif; height: 100vh; display: flex; flex-direction: column; background: #111; color: #eee; overflow: hidden; }
#canvas-container { flex: 1; position: relative; background: #000; }
#controls { height: 45%; overflow-y: auto; padding: 15px; background: #1a1a1a; display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 15px; border-top: 1px solid #333; }
.group { background: #252525; padding: 12px; border-radius: 8px; border: 1px solid #333; }
h3 { margin-top: 0; color: #00d1ff; font-size: 0.9em; text-transform: uppercase; }
label { display: block; font-size: 0.75em; color: #888; margin-top: 8px; }
input, select { width: 100%; padding: 8px; margin: 4px 0; background: #111; border: 1px solid #444; color: #fff; border-radius: 4px; box-sizing: border-box; }
.btn { background: #007bff; border: none; color: white; padding: 10px; border-radius: 4px; cursor: pointer; width: 100%; font-weight: bold; margin-top: 10px; }
/* Color UI */
#active-colors { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
.color-chip { width: 24px; height: 24px; border-radius: 4px; border: 1px solid #555; cursor: pointer; position: relative; }
.color-chip:hover::after { content: '×'; position: absolute; top: -5px; right: -5px; background: red; color: white; border-radius: 50%; width: 14px; height: 14px; font-size: 10px; text-align: center; }
/* AR UI */
#ar-ui { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: none; pointer-events: none; z-index: 1000; }
#ar-instructions { position: absolute; top: 15%; width: 80%; left: 10%; text-align: center; background: rgba(0,0,0,0.7); padding: 15px; border-radius: 10px; font-size: 0.9em; }
#close-ar { position: absolute; bottom: 40px; left: 50%; transform: translateX(-50%); pointer-events: auto; background: #ff4444; padding: 15px 40px; border-radius: 40px; font-weight: bold; box-shadow: 0 4px 10px rgba(0,0,0,0.5); }
.depth-row { display: flex; gap: 5px; }
</style>
</head>
<body>
<div id="canvas-container">
<div id="ar-ui">
<div id="ar-instructions">Slowly move phone to find a wall. When the green ring appears, tap to place.</div>
<button id="close-ar" class="btn">EXIT AR</button>
</div>
</div>
<div id="controls">
<div class="group">
<h3>1. Size & Blocks (Inches)</h3>
<label>Total Width</label><input type="number" id="width" value="48" oninput="updateModel()">
<label>Total Height</label><input type="number" id="height" value="24" oninput="updateModel()">
<label>Block Scale</label><input type="number" id="blockSize" value="2" oninput="updateModel()">
</div>
<div class="group">
<h3>2. Diffusion Depths</h3>
<div class="depth-row">
<input type="number" class="depth-val" value="1" oninput="updateModel()" title="Depth">
<input type="number" class="depth-weight" value="50" oninput="updateModel()" title="Weight %">
</div>
<div class="depth-row">
<input type="number" class="depth-val" value="6" oninput="updateModel()" title="Depth">
<input type="number" class="depth-weight" value="50" oninput="updateModel()" title="Weight %">
</div>
<label>Pattern Type</label>
<select id="pattern" onchange="updateModel()">
<option value="random">Random</option>
<option value="checker">Checkerboard</option>
<option value="linear">Horizontal Linear</option>
</select>
</div>
<div class="group">
<h3>3. Colors</h3>
<div style="display:flex; gap:5px;">
<input type="color" id="colorPicker" value="#00d1ff">
<button class="btn" onclick="addColor()" style="margin:0; width:60px;">+</button>
</div>
<div id="active-colors"></div>
<button id="ar-launch" class="btn" style="background:#e91e63;">ENTER AR PREVIEW</button>
</div>
</div>
<script>
let scene, camera, renderer, controls, diffuserGroup, reticle;
let hitTestSource = null, hitTestSourceRequested = false, isPlaced = false;
let colors = ['#ffffff', '#00d1ff', '#222222'];
init();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(65, window.innerWidth / (window.innerHeight * 0.55), 0.1, 1000);
camera.position.set(0, 0, 60);
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight * 0.55);
renderer.xr.enabled = true;
document.getElementById('canvas-container').appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
scene.add(new THREE.HemisphereLight(0xffffff, 0x444444, 1.2));
const sun = new THREE.DirectionalLight(0xffffff, 0.5);
sun.position.set(10, 20, 10);
scene.add(sun);
diffuserGroup = new THREE.Group();
scene.add(diffuserGroup);
// Placement Reticle
reticle = new THREE.Mesh(
new THREE.RingGeometry(1.5, 2, 32).rotateX(-Math.PI / 2),
new THREE.MeshBasicMaterial({ color: 0x00ff00 })
);
reticle.matrixAutoUpdate = false;
reticle.visible = false;
scene.add(reticle);
renderColorChips();
updateModel();
animate();
document.getElementById('ar-launch').addEventListener('click', startAR);
document.getElementById('close-ar').addEventListener('click', endAR);
}
function addColor() {
colors.push(document.getElementById('colorPicker').value);
renderColorChips();
updateModel();
}
function renderColorChips() {
const container = document.getElementById('active-colors');
container.innerHTML = colors.map((c, i) => `
<div class="color-chip" style="background:${c}" onclick="removeColor(${i})"></div>
`).join('');
}
function removeColor(index) {
if(colors.length > 1) {
colors.splice(index, 1);
renderColorChips();
updateModel();
}
}
async function startAR() {
const session = await navigator.xr.requestSession('immersive-ar', {
requiredFeatures: ['hit-test'],
optionalFeatures: ['dom-overlay'],
domOverlay: { root: document.getElementById('ar-ui') }
});
renderer.xr.setSession(session);
document.getElementById('ar-ui').style.display = 'block';
isPlaced = false;
session.addEventListener('select', () => {
if (reticle.visible) {
isPlaced = true;
document.getElementById('ar-instructions').innerHTML = "<b>LOCKED</b><br>Move around to see the depths!";
}
});
}
function endAR() {
renderer.xr.getSession().end();
document.getElementById('ar-ui').style.display = 'none';
isPlaced = false;
reticle.visible = false;
}
function updateModel() {
while(diffuserGroup.children.length > 0) diffuserGroup.remove(diffuserGroup.children[0]);
const W = parseFloat(document.getElementById('width').value) || 12;
const H = parseFloat(document.getElementById('height').value) || 12;
const B = parseFloat(document.getElementById('blockSize').value) || 1;
const pattern = document.getElementById('pattern').value;
const dVals = Array.from(document.querySelectorAll('.depth-val')).map(v => parseFloat(v.value));
const dWeights = Array.from(document.querySelectorAll('.depth-weight')).map(v => parseFloat(v.value));
const geo = new THREE.BoxGeometry(B * 0.97, B * 0.97, 1);
for(let r=0; r < Math.floor(H/B); r++) {
for(let c=0; c < Math.floor(W/B); c++) {
// Depth Weighted Selection
const rand = Math.random() * 100;
let cum = 0, depth = dVals[0];
for(let i=0; i<dWeights.length; i++) {
cum += dWeights[i];
if(rand <= cum) { depth = dVals[i]; break; }
}
// Color Selection based on Pattern
let colorIdx = 0;
if(pattern === 'random') colorIdx = Math.floor(Math.random() * colors.length);
else if(pattern === 'checker') colorIdx = (r + c) % colors.length;
else colorIdx = Math.floor((c / (W/B)) * colors.length);
const mat = new THREE.MeshStandardMaterial({
color: colors[colorIdx % colors.length],
roughness: 0.6,
metalness: 0.2
});
const mesh = new THREE.Mesh(geo, mat);
mesh.position.set((c*B)-(W/2)+(B/2), (r*B)-(H/2)+(B/2), depth/2);
mesh.scale.z = depth;
diffuserGroup.add(mesh);
}
}
}
function animate() {
renderer.setAnimationLoop(render);
}
function render(timestamp, frame) {
if (frame) {
const session = renderer.xr.getSession();
const referenceSpace = renderer.xr.getReferenceSpace();
if (!hitTestSourceRequested && session) {
session.requestReferenceSpace('viewer').then(space => {
session.requestHitTestSource({ space: space }).then(source => { hitTestSource = source; });
});
hitTestSourceRequested = true;
}
if (hitTestSource && !isPlaced) {
const hitResults = frame.getHitTestResults(hitTestSource);
if (hitResults.length > 0) {
const hit = hitResults[0];
const pose = hit.getPose(referenceSpace);
reticle.visible = true;
reticle.matrix.fromArray(pose.transform.matrix);
diffuserGroup.position.setFromMatrixPosition(reticle.matrix);
diffuserGroup.quaternion.setFromRotationMatrix(new THREE.Matrix4().extractRotation(reticle.matrix));
diffuserGroup.rotateX(Math.PI / 2); // Align to vertical wall
}
}
}
controls.update();
renderer.render(scene, camera);
}
</script>
</body>
</html>