-
Notifications
You must be signed in to change notification settings - Fork 7
/
18-postprocessing-sobel.html
235 lines (195 loc) · 6.33 KB
/
18-postprocessing-sobel.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - post processing: luminance</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px; width: 100%;
color: #222;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: center;
}
a {
color: #000;
text-decoration: none;
}
a:hover {
color: #0080ff;
}
</style>
</head>
<body>
<div id="info">
<a href="https://github.com/robertoranon/int3D" target="_blank">Interactive 3D Graphics 2017 code</a> - post processing: luminance<br />
</div>
<script src="lib/three.min.js"></script>
<script src="lib/stats.min.js"></script>
<script src="lib/OrbitControls.js"></script>
<script src='lib/dat.gui.min.js'></script>
<script src='lib/OBJLoader.js'></script>
<script src="lib/EffectComposer.js"></script>
<script src="lib/CopyShader.js"></script>
<script src="lib/ShaderPass.js"></script>
<script src="lib/RenderPass.js"></script>
<script src="lib/GammaCorrectionShader.js"></script>
<!-- three.js code -->
<script>
var sobelShader = {
uniforms: {
"tDiffuse": { type: "t", value: null },
"width": { type: "f", value: 0.0 },
"height": { type: "f", value: 0.0 },
},
vertexShader: [
"varying vec2 vUv;",
"void main() {",
"vUv = uv;",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragmentShader: [
"uniform sampler2D tDiffuse;",
"varying vec2 vUv;",
"uniform float width;",
"uniform float height;",
"void main(void)",
"{",
"float step_w = 1.0/width;",
"float step_h = 1.0/height;",
"vec2 offset[9];",
"offset[0] = vec2(-step_w, -step_h);",
"offset[1] = vec2(0.0, -step_h);",
"offset[2] = vec2(step_w, -step_h);",
"offset[3] = vec2(-step_w, 0.0);",
"offset[4] = vec2(0.0, 0.0);",
"offset[5] = vec2(step_w, 0.0);",
"offset[6] = vec2(-step_w, step_h);",
"offset[7] = vec2(0.0, step_h);",
"offset[8] = vec2(step_w, step_h);",
"float sample[9];",
"for( int i=0; i<9; i++ )",
"sample[i]= length(texture2D(tDiffuse, vUv + offset[i]).rgb);",
"float horizEdge = sample[0] + (2.0*sample[1]) + sample[2] - (sample[6] + (2.0*sample[7]) + sample[8]);",
"float vertEdge = sample[0] + (2.0*sample[3]) + sample[5] - (sample[2] + (2.0*sample[5]) + sample[8]);",
"gl_FragColor.rgb = 1.0 - vec3(sqrt((horizEdge * horizEdge) + (vertEdge * vertEdge)));",
"gl_FragColor.a = 1.0;",
"}",
].join("\n")
};
var renderer = new THREE.WebGLRenderer( { antialias: true } );
var camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 1000 );
var controls = new THREE.OrbitControls( camera, renderer.domElement );
var scene = new THREE.Scene();
var sobelEffect;
var guiParams = {
enabled: true,
}
var normalMap = loadTexture( "textures/normal.jpg" );
var loader = new THREE.CubeTextureLoader();
loader.setPath( 'textures/cubemap/' );
var textureCube = loader.load( [
'posx.png', 'negx.png',
'posy.png', 'negy.png',
'posz.png', 'negz.png'
] );
scene.background = textureCube;
textureCube.minFilter = THREE.LinearMipMapLinearFilter;
var ourMaterial = new THREE.MeshStandardMaterial({
envMap: textureCube,
roughness: 0.5,
normalMap: normalMap,
normalScale: new THREE.Vector2(1,1),
color: new THREE.Color(0.8,0.8,0.8),
metalness: 1,
});
var loader = new THREE.OBJLoader();
loader.load( "models/ninjaHead_Low.obj", function ( group ) {
geometry = group.children[ 0 ].geometry;
geometry.center();
mesh = new THREE.Mesh( geometry, ourMaterial );
mesh.scale.multiplyScalar( 0.1 );
scene.add( mesh );
} );
var gui;
var stats = new Stats();
function loadTexture(file) {
var texture = new THREE.TextureLoader().load( file , function ( texture ) {
texture.minFilter = THREE.LinearMipMapLinearFilter;
texture.anisotropy = renderer.getMaxAnisotropy();
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.offset.set( 0, 0 );
texture.needsUpdate = true;
render();
} )
return texture;
}
function init() {
renderer.setClearColor( 0xf0f0f0 );
camera.position.set( 0, 0, 10 );
scene.add( camera );
document.body.appendChild( renderer.domElement );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.gammaInput = true;
renderer.gammaOutput = false;
controls.minDistance = 1;
controls.maxDistance = 100;
controls.enablePan = false;
controls.update();
window.addEventListener( 'resize', onResize, false );
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
document.body.appendChild( stats.domElement );
composer = new THREE.EffectComposer( renderer );
composer.addPass( new THREE.RenderPass( scene, camera ) );
sobelEffect = new THREE.ShaderPass( sobelShader );
sobelEffect.uniforms.width.value = window.innerWidth;
sobelEffect.uniforms.height.value = window.innerHeight;
composer.addPass( sobelEffect );
passthrough = new THREE.ShaderPass( THREE.GammaCorrectionShader);
passthrough.renderToScreen = true;
composer.addPass( passthrough );
}
function onResize() {
renderer.setSize( window.innerWidth, window.innerHeight );
camera.aspect = ( window.innerWidth / window.innerHeight );
camera.updateProjectionMatrix();
sobelEffect.uniforms.width.value = window.innerWidth;
sobelEffect.uniforms.height.value = window.innerHeight;
}
function update() {
requestAnimationFrame( update );
stats.update();
render();
}
function render() {
composer.render();
}
function clearGui() {
if ( gui ) gui.destroy();
gui = new dat.GUI();
gui.open();
}
function buildGui() {
clearGui();
gui.add(guiParams,'enabled').onChange( function (value) {
sobelEffect.enabled = value;
});
}
init();
buildGui();
update();
render();
</script>
</body>
</html>