-
Notifications
You must be signed in to change notification settings - Fork 0
/
depth_sim.html
212 lines (186 loc) · 8.4 KB
/
depth_sim.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
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
<!DOCTYPE html>
<html>
<head>
<title>Three.js Cat Depth Effect with Advanced Controls</title>
<style>
body { margin: 0; padding: 0; overflow: hidden; }
canvas { display: block; }
#controls {
position: absolute;
top: 10px;
left: 10px;
background: rgba(0,0,0,0.5);
color: white;
padding: 10px;
border-radius: 5px;
}
.slider-container {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div id="controls">
<div class="slider-container">
<label for="bulgeIntensity">Bulge Intensity: </label>
<input type="range" id="bulgeIntensity" min="0" max="0.1" step="0.001" value="0.0">
<span id="bulgeIntensityValue">0.0</span>
</div>
<div class="slider-container">
<label for="mouseEffect">Mouse Effect: </label>
<input type="range" id="mouseEffect" min="0" max="0.5" step="0.01" value="0.035">
<span id="mouseEffectValue">0.035</span>
</div>
<div class="slider-container">
<label for="swirlIntensity">Swirl Intensity: </label>
<input type="range" id="swirlIntensity" min="0" max="0.1" step="0.001" value="0.0">
<span id="swirlIntensityValue">0.0</span>
</div>
<div class="slider-container">
<label for="depthIntensity">Depth Intensity: </label>
<input type="range" id="depthIntensity" min="0" max="1.0" step="0.001" value="0.23">
<span id="depthIntensityValue">0.23</span>
</div>
<div class="slider-container">
<label for="meshDistance">Mesh Distance: </label>
<input type="range" id="meshDistance" min="0.1" max="5" step="0.01" value="1.56">
<span id="meshDistanceValue">1.56</span>
</div>
<div class="slider-container">
<label for="meshSize">Mesh Size: </label>
<input type="range" id="meshSize" min="1" max="10" step="0.1" value="5.7">
<span id="meshSizeValue">5.7</span>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Set up the scene, camera, and renderer
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Load the cat image texture
const loader = new THREE.TextureLoader();
const texture = loader.load(
'depth_sim.png',
function(texture) {
console.log('Texture loaded successfully');
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
},
undefined,
function(error) {
console.error('Error loading texture:', error);
console.error(error.target.src); // This will show the attempted load path
}
);
// Define shaders
const vertexShader = `
varying vec2 vUv;
varying vec3 vPosition;
void main() {
vUv = uv;
vPosition = position;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;
const fragmentShader = `
uniform sampler2D textureSampler;
uniform vec2 mousePos;
uniform float bulgeIntensity;
uniform float mouseEffect;
uniform float swirlIntensity;
uniform float depthIntensity;
varying vec2 vUv;
varying vec3 vPosition;
void main() {
vec2 uv = vUv;
// Create a adjustable static bulge effect
float bulge = bulgeIntensity * (1.0 - distance(vPosition.xy, vec2(0.0)));
// Add adjustable mouse-based distortion
vec2 mouseOffset = (mousePos - 0.5) * mouseEffect;
uv += vPosition.xy * bulge - mouseOffset * (1.0 - distance(vPosition.xy, vec2(0.0)));
// Add an adjustable swirl effect
float angle = atan(vPosition.y, vPosition.x);
float radius = length(vPosition.xy);
float swirl = swirlIntensity * sin(10.0 * angle) * smoothstep(0.0, 0.5, radius);
uv += vec2(swirl * vPosition.y, -swirl * vPosition.x);
// Sample the texture
vec4 texColor = texture2D(textureSampler, uv);
// Adjustable depth effect
float depth = smoothstep(1.0, 0.0, length(vPosition.xy));
texColor.rgb *= mix(1.0 - depthIntensity, 1.0 + depthIntensity * 0.2, depth);
gl_FragColor = texColor;
}
`;
// Create a custom shader material
const material = new THREE.ShaderMaterial({
uniforms: {
textureSampler: { value: texture },
mousePos: { value: new THREE.Vector2(0.5, 0.5) },
bulgeIntensity: { value: 0.0 },
mouseEffect: { value: 0.035 },
swirlIntensity: { value: 0.0 },
depthIntensity: { value: 0.23 }
},
vertexShader: vertexShader,
fragmentShader: fragmentShader
});
// Create initial geometry
let geometry = new THREE.PlaneGeometry(5.7, 5.7, 200, 200);
// Create a plane mesh with the geometry and material
let plane = new THREE.Mesh(geometry, material);
scene.add(plane);
camera.position.z = 1.56;
// Mouse move event listener
function onMouseMove(event) {
material.uniforms.mousePos.value.x = event.clientX / window.innerWidth;
material.uniforms.mousePos.value.y = 1 - (event.clientY / window.innerHeight); // Invert Y
}
window.addEventListener('mousemove', onMouseMove);
// Slider event listeners
document.getElementById('bulgeIntensity').addEventListener('input', function(e) {
material.uniforms.bulgeIntensity.value = parseFloat(e.target.value);
document.getElementById('bulgeIntensityValue').textContent = e.target.value;
});
document.getElementById('mouseEffect').addEventListener('input', function(e) {
material.uniforms.mouseEffect.value = parseFloat(e.target.value);
document.getElementById('mouseEffectValue').textContent = e.target.value;
});
document.getElementById('swirlIntensity').addEventListener('input', function(e) {
material.uniforms.swirlIntensity.value = parseFloat(e.target.value);
document.getElementById('swirlIntensityValue').textContent = e.target.value;
});
document.getElementById('depthIntensity').addEventListener('input', function(e) {
material.uniforms.depthIntensity.value = parseFloat(e.target.value);
document.getElementById('depthIntensityValue').textContent = e.target.value;
});
document.getElementById('meshDistance').addEventListener('input', function(e) {
camera.position.z = parseFloat(e.target.value);
document.getElementById('meshDistanceValue').textContent = e.target.value;
});
document.getElementById('meshSize').addEventListener('input', function(e) {
const size = parseFloat(e.target.value);
scene.remove(plane);
geometry.dispose();
geometry = new THREE.PlaneGeometry(size, size, 200, 200);
plane = new THREE.Mesh(geometry, material);
scene.add(plane);
document.getElementById('meshSizeValue').textContent = e.target.value;
});
// Render loop
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
// Start animation
animate();
// Handle window resize
window.addEventListener('resize', function() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>