-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer.py
More file actions
238 lines (196 loc) · 6.46 KB
/
Copy pathviewer.py
File metadata and controls
238 lines (196 loc) · 6.46 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
from flask import Flask, render_template_string
app = Flask(__name__)
from flask import send_from_directory
@app.route('/results/<path:filename>')
def serve_results(filename):
return send_from_directory('results', filename)
@app.route('/')
def index():
HTML = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Textured Sphere with Normal & Displacement Map</title>
<style>
body {
margin: 0;
background: #222;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
color: #eee;
font-family: sans-serif;
}
#viewer-container {
width: 100%;
height: 95%;
border: 2px solid #444;
border-radius: 8px;
background: #111;
box-shadow: 0 0 15px rgba(0,0,0,0.7);
}
#controls {
margin-top: 10px;
width: 100%;
display: flex;
justify-content: space-around;
}
label {
display: flex;
flex-direction: column;
font-size: 14px;
user-select: none;
}
input[type=range] {
width: 150px;
}
</style>
</head>
<body>
<div id="viewer-container"></div>
<div id="controls">
<label>
Shape
<select id="shapeSelector">
<option value="sphere" selected>Sphere</option>
<option value="plane">Plane</option>
<option value="cube">Cube</option>
</select>
</label>
<label>
Normal Map Strength
<input type="range" id="normalScale" min="0" max="2" step="0.01" value="1" />
</label>
<label>
Extrusion
<input type="range" id="displacementScale" min="0" max="0.5" step="0.001" value="0.1" />
</label>
<label>
Metalness
<input type="range" id="metalness" min="0" max="1" step="0.01" value="0.5" />
</label>
<label>
Roughness
<input type="range" id="roughness" min="0" max="1" step="0.01" value="1" />
</label>
</div>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script>
let scene, camera, renderer, controls, mesh, material;
shapeSelector.value = 'sphere';
function createMesh(shape) {
if (mesh) {
scene.remove(mesh);
mesh.geometry.dispose();
mesh.material.dispose();
}
let geometry;
switch (shape) {
case 'plane':
geometry = new THREE.PlaneGeometry(2, 2, 128, 128);
break;
case 'cube':
geometry = new THREE.BoxGeometry(2, 2, 2, 128, 128, 128);
break;
case 'sphere':
default:
geometry = new THREE.SphereGeometry(1, 128, 128);
break;
}
material.side = THREE.DoubleSide;
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
}
function init() {
const container = document.getElementById('viewer-container');
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(
75,
container.clientWidth / container.clientHeight,
0.1,
1000
);
camera.position.set(0, 0, 3);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(container.clientWidth, container.clientHeight);
document.getElementById('viewer-container').appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, renderer.domElement);
const ambientLight = new THREE.AmbientLight(0xffffff, 0.3);
scene.add(ambientLight);
// Directional light from above
const directionalLight1 = new THREE.DirectionalLight(0xffffff, 1);
directionalLight1.position.set(0, 100, 0);
scene.add(directionalLight1);
// Directional light from below
const directionalLight2 = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight2.position.set(0, -100, 0);
scene.add(directionalLight2);
// Optional: Fill light from the front
const directionalLight3 = new THREE.DirectionalLight(0xffffff, 0.3);
directionalLight3.position.set(100, 0, 100);
scene.add(directionalLight3);
// Optional: Fill light from the front
const directionalLight4 = new THREE.DirectionalLight(0xffffff, 0.3);
directionalLight4.position.set(-100, 0, 100);
scene.add(directionalLight4);
// Optional: Fill light from the front
const directionalLight5 = new THREE.DirectionalLight(0xffffff, 0.3);
directionalLight5.position.set(100, 0, -100);
scene.add(directionalLight5);
// Optional: Fill light from the front
const directionalLight6 = new THREE.DirectionalLight(0xffffff, 0.3);
directionalLight6.position.set(-100, 0, -100);
scene.add(directionalLight6);
const loader = new THREE.TextureLoader();
const texture = loader.load('/results/texture.png');
const normalMap = loader.load('/results/normal_map.png');
const displacementMap = loader.load('/results/displacement_map.png');
material = new THREE.MeshStandardMaterial({
map: texture,
normalMap: normalMap,
normalScale: new THREE.Vector2(1, 1),
displacementMap: displacementMap,
displacementScale: 0.1,
metalness: 0.5,
roughness: 1
});
createMesh('sphere'); // default shape
document.getElementById('shapeSelector').addEventListener('change', (e) => {
createMesh(e.target.value);
});
document.getElementById('normalScale').addEventListener('input', (e) => {
let val = parseFloat(e.target.value);
material.normalScale.set(val, val);
});
document.getElementById('displacementScale').addEventListener('input', (e) => {
material.displacementScale = parseFloat(e.target.value);
});
document.getElementById('metalness').addEventListener('input', (e) => {
material.metalness = parseFloat(e.target.value);
});
document.getElementById('roughness').addEventListener('input', (e) => {
material.roughness = parseFloat(e.target.value);
});
}
function animate() {
requestAnimationFrame(animate);
if (mesh) {
mesh.rotation.y += 0.005;
}
controls.update();
renderer.render(scene, camera);
}
document.addEventListener('DOMContentLoaded', () => {
init();
animate();
});
</script>
</body>
</html>"""
return render_template_string(HTML)
if __name__ == '__main__':
app.run(debug=True)