Skip to content

Commit 70704ff

Browse files
committed
feat: final changes / fixes for assignment
1 parent e1ddf8a commit 70704ff

7 files changed

+42
-39
lines changed

examples/canvas.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ function resizeCanvas() {
6060
function setBackgroundColor() {
6161
let backgroundColor = 0xffffff;
6262

63-
let darkTheme = parent.document.getElementById("theme-switch");
64-
darkTheme = darkTheme != null ? darkTheme.checked : false;
63+
let darkTheme = parent.document.getElementById("theme-switch-checkbox");
64+
darkTheme = darkTheme != null ? !darkTheme.checked : true;
6565
if (darkTheme) backgroundColor = 0x000000;
6666

6767
renderer.setClearColor(new THREE.Color(backgroundColor), 1);

index.html

+30-24
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<span class="space-fill"></span>
1616
<span id="home-button" class="nav-item" onclick="changeView('home')" style="display: none;">Back to Overview</span>
1717
<a href="./README.md" target="viewer-frame" class="nav-item" onclick="changeView('README.md')">About this Project</a>
18-
<span class="nav-item"><input type="checkbox" id="theme-switch"><label for="theme-switch" class="theme-switch-label"><i id="theme-switch-icon" class="fas fa-moon"></i></label></span>
18+
<span class="nav-item" id="theme-switch"><input type="checkbox" id="theme-switch-checkbox"><label for="theme-switch-checkbox" class="theme-switch-label"><i id="theme-switch-icon" class="fas fa-moon"></i></label></span>
1919
<a href="https://github.com/MatteoVoges/extending-three.js" class="nav-item"><i class="fa-brands fa-github"></i></a>
2020
</div>
2121

@@ -31,26 +31,34 @@
3131
</body>
3232

3333
<script>
34-
document.getElementById("viewer-frame").addEventListener("load", function() {
34+
const viewerFrame = document.getElementById("viewer-frame");
35+
const homeButton = document.getElementById("home-button");
36+
const examplesContainer = document.getElementById("examples");
37+
const themeSwitch = document.getElementById("theme-switch");
38+
const themeSwitchIcon = document.getElementById("theme-switch-icon");
39+
const themeSwitchCheckbox = document.getElementById("theme-switch-checkbox");
40+
41+
function applyPrimaryColor() {
3542
const primaryColor = "rgb(" + getComputedStyle(document.documentElement).getPropertyValue("--primary-color") + ")";
36-
for (const e of document.getElementById("viewer-frame").contentDocument.documentElement.getElementsByClassName("lil-gui")) {
43+
for (const e of document.getElementsByClassName("lil-gui")) {
3744
e.style.setProperty("--number-color", primaryColor);
3845
}
39-
});
40-
41-
document.getElementById("theme-switch").addEventListener("change", function() {
46+
}
4247

48+
function applyColorTheme() {
4349
let theme;
44-
if (this.checked) {
50+
if (themeSwitchCheckbox.checked) {
4551
// dark theme
46-
document.getElementById("theme-switch-icon").classList.remove("fa-moon");
47-
document.getElementById("theme-switch-icon").classList.add("fa-sun");
52+
themeSwitchIcon.classList.remove("fa-moon");
53+
themeSwitchIcon.classList.add("fa-sun");
4854
theme = {"background-color": "30, 30, 30", "dark-background-color": "11, 23, 32", "text-color": "255, 255, 255", "dark-text-color": "136, 136, 136"};
55+
themeSwitchCheckbox.checked = false;
4956
} else {
5057
// light theme
51-
document.getElementById("theme-switch-icon").classList.remove("fa-sun");
52-
document.getElementById("theme-switch-icon").classList.add("fa-moon");
58+
themeSwitchIcon.classList.remove("fa-sun");
59+
themeSwitchIcon.classList.add("fa-moon");
5360
theme = {"background-color": "255, 250, 240", "dark-background-color": "210, 210, 220", "text-color": "0, 0, 0", "dark-text-color": "100, 100, 100"};
61+
themeSwitchCheckbox.checked = true;
5462
}
5563

5664
// apply theme
@@ -59,28 +67,26 @@
5967
}
6068

6169
// change background color of renderer
62-
const viewerFrame = document.getElementById("viewer-frame");
6370
if (viewerFrame.contentWindow.setBackgroundColorCallback) viewerFrame.contentWindow.setBackgroundColorCallback();
64-
});
71+
}
72+
73+
viewerFrame.addEventListener("load", applyPrimaryColor);
74+
themeSwitch.addEventListener("click", applyColorTheme);
6575

6676
// apply theme on load
6777
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
68-
document.getElementById("theme-switch").checked = true;
69-
document.getElementById("theme-switch").dispatchEvent(new Event("change"));
78+
themeSwitchCheckbox.checked = true;
79+
applyColorTheme();
7080
}
7181

7282
function changeView(path) {
73-
const viewerFrame = document.getElementById("viewer-frame");
74-
const examples = document.getElementById("examples");
75-
const homeButton = document.getElementById("home-button");
76-
7783
if (path === "home") {
7884
viewerFrame.src = "";
7985
viewerFrame.style.display = "none";
8086
homeButton.style.display = "none";
81-
examples.style.display = "grid";
87+
examplesContainer.style.display = "grid";
8288
} else {
83-
examples.style.display = "none";
89+
examplesContainer.style.display = "none";
8490
viewerFrame.src = path;
8591
homeButton.style.display = "flex";
8692
viewerFrame.style.display = "unset";
@@ -89,7 +95,7 @@
8995
window.location.hash = path;
9096
}
9197

92-
const examples = {
98+
const exampleSources = {
9399
"Playground": "playground",
94100
"PBR - Interactive": "pbr-interactive",
95101
"PBR - Materials": "pbr-materials",
@@ -98,7 +104,7 @@
98104
}
99105

100106
// autogenerate examples
101-
for (const [name, src] of Object.entries(examples)) {
107+
for (const [name, src] of Object.entries(exampleSources)) {
102108
const example = document.createElement("div");
103109
example.className = "example";
104110
example.onclick = function() {changeView("examples/" + src + "/index.html")};
@@ -107,7 +113,7 @@
107113
<div>${name}</div>
108114
`;
109115

110-
document.getElementById("examples").appendChild(example);
116+
examplesContainer.appendChild(example);
111117
}
112118
</script>
113119

src/superquadricGeometry.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ class SuperquadricGeometry extends BufferGeometry {
8989
normal.normalize();
9090
normals.push(normal.x, normal.y, normal.z);
9191

92-
// uv
92+
// uv (calculate uv from coordinates / mapped sphere angles)
9393

94-
// calculate uv from coordinates / mapped sphere angles
9594
const latitude = Math.atan2(vertex.z, vertex.x);
9695
const longitude = Math.acos(vertex.y / vertex.length());
9796

@@ -153,7 +152,6 @@ class SuperquadricGeometry extends BufferGeometry {
153152
data.phiLength,
154153
data.thetaStart,
155154
data.thetaLength,
156-
data.post_uv,
157155
);
158156
}
159157
}

src/superquadricInstanceShader.glsl renamed to src/superquadricInstanceShader.glsl.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export const instancedVertexShader = /* glsl */`
12
precision highp float;
23
34
attribute float eta;
@@ -7,8 +8,6 @@ attribute float u;
78
attribute float epsilon1;
89
attribute float epsilon2;
910
10-
// attribute vec3 instancePosition;
11-
1211
varying vec3 vViewPosition;
1312
varying vec3 vNormal;
1413
varying vec3 vWorldPosition;
@@ -34,8 +33,6 @@ void main() {
3433
superquadricPosition.z = signed_pow(sin(eta), epsilon1) * signed_pow(sin(omega), epsilon2);
3534
if (u == 1.0) superquadricPosition.z = 0.0;
3635
37-
// superquadricPosition += instancePosition;
38-
3936
gl_Position = projectionMatrix * modelViewMatrix * instanceMatrix * vec4(superquadricPosition, 1.0);
4037
4138
@@ -54,4 +51,4 @@ void main() {
5451
vec4 mvPosition = modelViewMatrix * vec4(superquadricPosition, 1.0);
5552
vViewPosition = -mvPosition.xyz;
5653
vWorldPosition = (modelMatrix * vec4(superquadricPosition, 1.0)).xyz;
57-
}
54+
}`;

src/superquadricMaterial.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import * as THREE from "three";
22

3+
import { vertexShader } from "./superquadricShader.glsl.js";
4+
import { instancedVertexShader } from "./superquadricInstanceShader.glsl.js";
5+
6+
37
export class ExtendedSuperquadricBufferGeometry extends THREE.BufferGeometry {
48
constructor (widthSegments = 32, heightSegments = 16) {
59
super();
@@ -81,7 +85,6 @@ export class ExtendedSuperquadricBufferGeometry extends THREE.BufferGeometry {
8185
}
8286
}
8387

84-
const vertexShader = await fetch("/src/superquadricShader.glsl").then(response => response.text());
8588

8689
export class SuperquadricMaterial extends THREE.ShaderMaterial {
8790
constructor (parameters) {
@@ -104,8 +107,6 @@ export class SuperquadricMaterial extends THREE.ShaderMaterial {
104107
}
105108

106109

107-
const instancedVertexShader = await fetch("/src/superquadricInstanceShader.glsl").then(response => response.text());
108-
109110
export class SuperquadricInstanceMaterial extends THREE.ShaderMaterial {
110111
constructor (parameters) {
111112
super(parameters);

src/superquadricShader.glsl renamed to src/superquadricShader.glsl.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export const vertexShader = /* glsl */`
12
precision highp float;
23
34
attribute float eta;
@@ -50,4 +51,4 @@ void main() {
5051
vec4 mvPosition = modelViewMatrix * vec4(superquadricPosition, 1.0);
5152
vViewPosition = -mvPosition.xyz;
5253
vWorldPosition = (modelMatrix * vec4(superquadricPosition, 1.0)).xyz;
53-
}
54+
}`;

styles/main.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ body {
5454
fill: rgb(var(--primary-color));
5555
}
5656

57-
#theme-switch {
57+
#theme-switch-checkbox {
5858
display: none;
5959
}
6060

0 commit comments

Comments
 (0)