-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBumpMapToNormalMap.html
79 lines (68 loc) · 1.66 KB
/
BumpMapToNormalMap.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
<!DOCTYPE html>
<html>
<head>
<title>BumpMapToNormalMap</title>
<meta charset="utf-8">
<style>
body {
background-color: #323232;
}
canvas {
position: absolute;
left: 50%;
top:50%;
transform: translate(-50%, -50%);
background-color: white;
}
</style>
</head>
<body>
<script src="./lib/playground.js"></script>
<script type="text/javascript">
let scene = new Scene(800, 600);
document.body.appendChild(scene.canvas);
let bumpMap;
let normalMap;
let rendered;
window.onload = function() {
init();
render();
}
function init() {
bumpMap = new FieldNode(new ScalarField(400, 300));
normalMap = new FieldNode(new VectorField(400, 300));
rendered = new FieldNode(new ScalarField(400, 300));
normalMap.setPosition(400, 0);
rendered.setPosition(400,300);
normalMap.field = bumpMap.field.getGradient();
scene.addChild(bumpMap);
scene.addChild(normalMap);
scene.addChild(rendered);
}
function render() {
if (scene.isMouseDown) {
bumpMap.field.setValue((p, v) => {
let value = 0;
let dis = p.distanceTo(scene.mouse);
if(dis < Math.PI*10) {
value = Math.cos(dis/10) + 1;
}
return Math.max(value * 1, v);
});
}
normalMap.field = bumpMap.field.getGradient();
normalMap.field.setValue((p, v) => {
return v.multiplyScalar(-1);
});
rendered.field.setValue((p) => {
let nv = normalMap.field.value[p.y][p.x];
let lv = new Vector(scene.mouse.x - 600, scene.mouse.y - 450);
lv.multiplyScalar(100/lv.length);
return (nv.x * lv.x + nv.y * lv.y)/100;
});
scene.update();
window.requestAnimationFrame(render);
}
</script>
</body>
</html>