-
Notifications
You must be signed in to change notification settings - Fork 5
/
three.js-displacement-gui.html
executable file
·91 lines (64 loc) · 2.18 KB
/
three.js-displacement-gui.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
<html>
<head>
<script src="three.min.js"></script>
<script type="text/javascript" src="dat.gui.min.js"></script>
</head>
<body>
</body>
<script>
// scene setup
var renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(document.body.clientWidth, document.body.clientHeight);
document.body.appendChild(renderer.domElement);
var scene = new THREE.Scene();
// CAMERA
var fov = 45; // camera field-of-view in degrees
var width = renderer.domElement.width;
var height = renderer.domElement.height;
var aspect = width / height; // view aspect ratio
var camera = new THREE.PerspectiveCamera( fov, aspect );
camera.position.z = -200;
camera.position.y = -400;
camera.lookAt(scene.position);
// LIGHTS
ambientLight = new THREE.AmbientLight( 0xffffff );
scene.add( ambientLight );
// SHADERS
var myTexture = new THREE.ImageUtils.loadTexture("US_heightmap_500px.png");
var shader = THREE.ShaderLib[ "normalmap" ];
var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
uniforms[ "enableDisplacement" ].value = true;
uniforms[ "enableDiffuse" ].value = true;
uniforms[ "tDisplacement" ].value = myTexture;
uniforms[ "tDiffuse" ].value = myTexture;
uniforms[ "uDisplacementScale" ].value = 50;
var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, wireframe: true };
var material = new THREE.ShaderMaterial( parameters );
// GEOMETRY
geometry = new THREE.PlaneGeometry(500, 281, 500, 281);
geometry.computeTangents();
var plane = new THREE.Mesh( geometry, material);
plane.rotation.y = Math.PI;
scene.add(plane);
// FUNCTIONS
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
}
requestAnimationFrame( animate );
// GUI
var initDisp = function() {
this.message = 'adjust displacement';
this.scale = 50;
}
window.onload = function() {
var disp = new initDisp();
var gui = new dat.GUI();
gui.add(disp, 'message');
controller = gui.add(disp, 'scale', -200, 200);
controller.onChange(function(value) {
uniforms[ "uDisplacementScale" ].value = value;
});
};
</script>
</html>