-
Notifications
You must be signed in to change notification settings - Fork 0
/
toonshader.html
138 lines (109 loc) · 3.65 KB
/
toonshader.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="libs/three.js"></script>
<script src="libs/OrbitControls.js"></script>
<script src="libs/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
<script src="libs/shaderloader/ShaderLoader.min.js" type="text/javascript" charset="utf-8"></script>
<script data-src="shader/toon.vert" type="x-shader/x-vertex" data-name="toon"></script>
<script data-src="shader/toon.frag" type="x-shader/x-fragment" data-name="toon"></script>
<style>
html {
height: 100%;
}
body {
height: 100%;
margin: 0px;
}
#viewer {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="viewer"></div>
<script type="text/javascript">
var scene, camera, renderer, controls, clock;
var torus;
var toon_vs, toon_fs;
var viewerWidth, viewerHeight;
var lightDirection = new THREE.Vector3(5.0, 5.0, 0.0);
$(window).load(function() {
SHADER_LOADER.load(
function(data) {
toon_vs = data.toon.vertex;
toon_fs = data.toon.fragment;
viewerWidth = $("#viewer").innerWidth();
viewerHeight = $("#viewer").innerHeight();
initViewer();
animate();
}
);
});
$(window).resize(function() {
camera.aspect = viewerWidth / viewerHeight;
camera.updateProjectionMatrix();
renderer.setSize(viewerWidth, viewerHeight);
});
function initViewer() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, viewerWidth / viewerHeight, 0.1, 1000);
renderer = new THREE.WebGLRenderer();
clock = new THREE.Clock;
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', render);
renderer.setClearColor(0xEEEEEE, 1.0);
renderer.clear();
renderer.setSize(viewerWidth, viewerHeight);
$("#viewer").append(renderer.domElement);
camera.position.z = 5;
addHelpers();
drawScene();
}
function addHelpers() {
var axes = new THREE.AxisHelper(50);
axes.position.set(0, 0, 0);
scene.add(axes);
var gridXZ = new THREE.GridHelper(100, 10);
gridXZ.setColors(new THREE.Color(0xFFC0CB), new THREE.Color(0x8f8f8f));
gridXZ.position.set(0, 0, 0);
scene.add(gridXZ);
var mI = 3,mJ = 3, s = 0.4;
for (var i=0; i<mI; i++)
for ( var j=0; j<mJ; j++) {
var lightDirectionArrow = new THREE.ArrowHelper(lightDirection.clone().normalize(), new THREE.Vector3(-4, -4+(i-mI/2)*s, (j-mJ/2)*s), 2, 0x00000000);
scene.add(lightDirectionArrow);
}
}
function drawScene() {
var uniforms = {
uLightDir: {
type: "v3",
value: lightDirection.clone()
}
};
var shaderMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: toon_vs,
fragmentShader: toon_fs
});
torus = new THREE.Mesh(new THREE.TorusGeometry(2, 0.5, 16, 50), shaderMaterial);
torus.scale.set(1, 1, 1);
torus.position.set(0, 0, 0);
scene.add(torus);
}
function animate() {
requestAnimationFrame(animate);
var delta = clock.getDelta();
torus.rotation.x -= delta * 0.5;
torus.rotation.y += delta * 0.5;
render();
}
function render() {
renderer.render(scene, camera);
}
</script>
</body>
</html>