-
Notifications
You must be signed in to change notification settings - Fork 7
/
18-postprocessing-contrast.html
211 lines (175 loc) · 5.4 KB
/
18-postprocessing-contrast.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - post processing: contrast</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: contrast<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 contrastShader = {
uniforms: {
"tDiffuse": { type: "t", value: null },
"contrast": { type: "f", value: 1.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 contrast;",
"void main() {",
"vec3 original_color = texture2D(tDiffuse, vUv).rgb;",
"vec3 grey = vec3(0.5);",
"gl_FragColor = vec4(grey + contrast * (original_color - grey) ,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 guiParams = {
enabled: true,
contrast: 0.0,
}
var contrastEffect;
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 ) );
contrastEffect = new THREE.ShaderPass( contrastShader );
composer.addPass( contrastEffect );
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();
}
function update() {
requestAnimationFrame( update );
stats.update();
contrastEffect.uniforms.contrast.value = guiParams.contrast;
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) {
contrastEffect.enabled = value;
});
gui.add(guiParams,'contrast',0,5);
}
init();
buildGui();
update();
render();
</script>
</body>
</html>