forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webgl_materials_wireframe.html
171 lines (105 loc) · 3.99 KB
/
webgl_materials_wireframe.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - materials - wireframe</title>
<meta charset="utf-8">
<style>
body {
margin: 0px;
background-color: #000000;
overflow: hidden;
}
</style>
</head>
<body>
<script src="../build/three.min.js"></script>
<script type="x-shader/x-vertex" id="vertexShader">
attribute vec3 center;
varying vec3 vCenter;
void main() {
vCenter = center;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
</script>
<script type="x-shader/x-fragment" id="fragmentShader">
#extension GL_OES_standard_derivatives : enable
varying vec3 vCenter;
float edgeFactorTri() {
vec3 d = fwidth( vCenter.xyz );
vec3 a3 = smoothstep( vec3( 0.0 ), d * 1.5, vCenter.xyz );
return min( min( a3.x, a3.y ), a3.z );
}
void main() {
gl_FragColor.rgb = mix( vec3( 1.0 ), vec3( 0.2 ), edgeFactorTri() );
gl_FragColor.a = 1.0;
}
</script>
<script>
var camera, scene, renderer;
var meshTris, meshLines, meshMixed;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
camera.position.z = 800;
scene = new THREE.Scene();
var size = 150;
var geometryLines = new THREE.BoxGeometry( size, size, size );
var geometryTris = new THREE.BoxGeometry( size, size, size );
// wireframe using gl.LINES
var materialLines = new THREE.MeshBasicMaterial( { wireframe: true } );
meshLines = new THREE.Mesh( geometryLines, materialLines );
meshLines.position.x = -150;
scene.add( meshLines );
//
var vertexShader = document.getElementById( 'vertexShader' ).textContent;
var fragmentShader = document.getElementById( 'fragmentShader' ).textContent;
// wireframe using gl.TRIANGLES (interpreted as triangles)
var attributesTris = { center: { type: 'v3', boundTo: 'faceVertices', value: [] } };
var valuesTris = attributesTris.center.value;
setupAttributes( geometryTris, valuesTris );
var materialTris = new THREE.ShaderMaterial( { uniforms: {}, attributes: attributesTris, vertexShader: vertexShader, fragmentShader: fragmentShader } );
meshTris = new THREE.Mesh( geometryTris, materialTris );
meshTris.position.x = 150;
scene.add( meshTris );
// wireframe using gl.TRIANGLES (mixed triangles and quads)
var mixedGeometry = new THREE.SphereGeometry( size / 2, 32, 16 );
var attributesMixed = { center: { type: 'v3', boundTo: 'faceVertices', value: [] } };
var valuesMixed = attributesMixed.center.value;
setupAttributes( mixedGeometry, valuesMixed );
var materialMixed = new THREE.ShaderMaterial( { uniforms: {}, attributes: attributesMixed, vertexShader: vertexShader, fragmentShader: fragmentShader } );
meshMixed = new THREE.Mesh( mixedGeometry, materialMixed );
meshMixed.position.x = -150;
scene.add( meshMixed );
// renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// events
window.addEventListener( 'resize', onWindowResize, false );
}
function setupAttributes( geometry, values ) {
for( var f = 0; f < geometry.faces.length; f ++ ) {
values[ f ] = [ new THREE.Vector3( 1, 0, 0 ), new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 0, 1 ) ];
}
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
meshLines.rotation.x += 0.005;
meshLines.rotation.y += 0.01;
meshTris.rotation.x += 0.005;
meshTris.rotation.y += 0.01;
if ( meshMixed ) {
meshMixed.rotation.x += 0.005;
meshMixed.rotation.y += 0.01;
}
renderer.render( scene, camera );
}
</script>
</body>
</html>