-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
90 lines (78 loc) · 3.06 KB
/
test.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Color Sphere</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="colorWheelCanvas"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/examples/js/controls/OrbitControls.js"></script>
<script>
const canvas = document.getElementById('colorWheelCanvas');
const renderer = new THREE.WebGLRenderer({ canvas });
renderer.setSize(window.innerWidth, window.innerHeight);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// Ensure OrbitControls is initialized
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.rotateSpeed = 0.3;
const geometry = new THREE.SphereGeometry(2, 64, 64);
const vertexShader = `
varying vec3 vPosition;
void main() {
vPosition = position;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;
const fragmentShader = `
varying vec3 vPosition;
void main() {
vec3 normalized = normalize(vPosition);
float hue = atan(normalized.z, normalized.x) / (2.0 * 3.14159265359) + 0.5;
float lightness = normalized.y * 0.5 + 0.5;
vec3 color = vec3(1.0 - lightness);
if (hue < 1.0 / 3.0) {
color = vec3(1.0 - hue * 3.0, hue * 3.0, 0.0);
} else if (hue < 2.0 / 3.0) {
color = vec3(0.0, 1.0 - (hue - 1.0 / 3.0) * 3.0, (hue - 1.0 / 3.0) * 3.0);
} else {
color = vec3((hue - 2.0 / 3.0) * 3.0, 0.0, 1.0 - (hue - 2.0 / 3.0) * 3.0);
}
color = mix(vec3(0.0), mix(color, vec3(1.0), normalized.y * 0.5 + 0.5), normalized.y * 0.5 + 0.5);
gl_FragColor = vec4(color, 1.0);
}
`;
const material = new THREE.ShaderMaterial({
vertexShader,
fragmentShader,
});
const sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>