-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepth_displacement_2.html
172 lines (150 loc) · 6.59 KB
/
depth_displacement_2.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
<!DOCTYPE html>
<html>
<head>
<title>Three.js Depth Map Displacement with Bulge Effect</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="displacementScale">Displacement Scale: </label>
<input type="range" id="displacementScale" min="0" max="1" step="0.01" value="0.1">
<span id="displacementScaleValue">0.1</span>
</div>
<div class="slider-container">
<label for="bulgeIntensity">Bulge Intensity: </label>
<input type="range" id="bulgeIntensity" min="0" max="0.5" step="0.01" value="0.1">
<span id="bulgeIntensityValue">0.1</span>
</div>
<div class="slider-container">
<label for="mouseSensitivity">Mouse Sensitivity: </label>
<input type="range" id="mouseSensitivity" min="0.0001" max="0.001" step="0.0001" value="0.0005">
<span id="mouseSensitivityValue">0.0005</span>
</div>
<div class="slider-container">
<label for="meshSize">Mesh Size: </label>
<input type="range" id="meshSize" min="0.1" max="5" step="0.1" value="2">
<span id="meshSizeValue">2</span>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
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);
let mesh;
let displacementScale = 0.1;
let bulgeIntensity = 0.1;
let mouseSensitivity = 0.0005;
let meshSize = 2;
let mouseX = 0, mouseY = 0;
const loader = new THREE.TextureLoader();
const colorTexture = loader.load('depth_sim.png');
const depthTexture = loader.load('depth_sim_depth_map', createMesh);
const vertexShader = `
varying vec2 vUv;
varying vec3 vViewPosition;
uniform sampler2D depthMap;
uniform float displacementScale;
void main() {
vUv = uv;
vec4 depth = texture2D(depthMap, uv);
vec3 newPosition = position + normal * depth.r * displacementScale;
vec4 mvPosition = modelViewMatrix * vec4(newPosition, 1.0);
gl_Position = projectionMatrix * mvPosition;
vViewPosition = -mvPosition.xyz;
}
`;
const fragmentShader = `
varying vec2 vUv;
varying vec3 vViewPosition;
uniform sampler2D colorTexture;
uniform sampler2D depthMap;
uniform vec2 mousePos;
uniform float bulgeIntensity;
void main() {
vec2 uv = vUv;
// Apply bulge effect
float bulge = bulgeIntensity * (1.0 - length(vViewPosition.xy));
uv += vViewPosition.xy * bulge;
// Apply mouse effect
vec2 mouseOffset = (mousePos - 0.5) * 0.1;
uv += mouseOffset * (1.0 - texture2D(depthMap, vUv).r);
vec4 texColor = texture2D(colorTexture, uv);
gl_FragColor = texColor;
}
`;
function createMesh() {
if (mesh) scene.remove(mesh);
const geometry = new THREE.PlaneGeometry(meshSize, meshSize, 200, 200);
const material = new THREE.ShaderMaterial({
uniforms: {
colorTexture: { value: colorTexture },
depthMap: { value: depthTexture },
displacementScale: { value: displacementScale },
bulgeIntensity: { value: bulgeIntensity },
mousePos: { value: new THREE.Vector2(0.5, 0.5) }
},
vertexShader: vertexShader,
fragmentShader: fragmentShader,
side: THREE.DoubleSide
});
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
}
camera.position.z = 1.5;
document.getElementById('displacementScale').addEventListener('input', function(e) {
displacementScale = parseFloat(e.target.value);
document.getElementById('displacementScaleValue').textContent = displacementScale.toFixed(2);
if (mesh) mesh.material.uniforms.displacementScale.value = displacementScale;
});
document.getElementById('bulgeIntensity').addEventListener('input', function(e) {
bulgeIntensity = parseFloat(e.target.value);
document.getElementById('bulgeIntensityValue').textContent = bulgeIntensity.toFixed(2);
if (mesh) mesh.material.uniforms.bulgeIntensity.value = bulgeIntensity;
});
document.getElementById('mouseSensitivity').addEventListener('input', function(e) {
mouseSensitivity = parseFloat(e.target.value);
document.getElementById('mouseSensitivityValue').textContent = mouseSensitivity.toFixed(4);
});
document.getElementById('meshSize').addEventListener('input', function(e) {
meshSize = parseFloat(e.target.value);
document.getElementById('meshSizeValue').textContent = meshSize.toFixed(1);
createMesh();
});
document.addEventListener('mousemove', onDocumentMouseMove);
function onDocumentMouseMove(event) {
mouseX = event.clientX / window.innerWidth;
mouseY = 1 - (event.clientY / window.innerHeight);
if (mesh) mesh.material.uniforms.mousePos.value.set(mouseX, mouseY);
}
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', function() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>