forked from xeolabs/scenejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry_autoNormals.html
187 lines (148 loc) · 5.06 KB
/
geometry_autoNormals.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>SceneJS Example</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 {
margin: 0;
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
}
</style>
<script src="../api/latest/scenejs.min.js"></script>
<link href="css/styles.css" rel="stylesheet"/>
<body>
<div id="infoDark">
<a href="http://scenejs.org">SceneJS</a> - automatically-generated normal vectors
</div>
<script>
// Demonstrates how to make SceneJS automatically generate normal vectors for geometry nodes.
//
// In this example we create a wavey surface using sine and cosine functions. Instead of
// specifying the normal vectors for our geometry, we specify "auto", which causes
// SceneJS to generate the normals.
//
// This will have limitations for closed geometries: on a cylinder, for example,
// SceneJS does not calculate the normals for vertices along a seam using the vertices
// on the other side of the seam.
//
// This should be quite suitable for heightmaps however.
// Point SceneJS to the bundled plugins
SceneJS.setConfigs({
pluginPath:"../api/latest/plugins"
});
var meshData = createMeshData({
width:5,
height:5,
widthSegments:40,
heightSegments:40
});
SceneJS.createScene({
nodes:[
// Mouse-orbited camera, implemented by plugin at
// http://scenejs.org/api/latest/plugins/node/cameras/orbit.js
{
type:"cameras/orbit",
yaw:40,
pitch:-20,
zoom:10,
zoomSensitivity:1.0,
nodes:[
// Blue color
{
type:"material",
color:{ r:0.5, g:0.5, b:0.6 },
nodes:[
// Geometry node with automatic normals
{
type:"geometry",
primitive:"triangles",
positions:meshData.positions,
uv:meshData.uv,
indices:meshData.indices,
// Automatically create normals - only works with 'triangles' primitive
normals:"auto"
}
]
}
]
}
]
});
// Creates a wavey mesh, minus the normal vectors, which we'll get SceneJS to generate for us
function createMeshData(cfg) {
var positions = [];
var uvs = [];
var indices = [];
var width = cfg.width || 1.0;
var height = cfg.height || 1.0;
var widthSegments = cfg.widthSegments || 1;
var heightSegments = cfg.heightSegments || 1;
var ix, iz;
var halfWidth = width / 2;
var halfHeight = height / 2;
var gridX = widthSegments;
var gridZ = heightSegments;
var gridX1 = gridX + 1;
var gridZ1 = gridZ + 1;
var segWidth = width / gridX;
var segHeight = height / gridZ;
var x;
var y;
for (iz = 0; iz < gridZ1; iz++) {
for (ix = 0; ix < gridX1; ix++) {
x = ix * segWidth - halfWidth;
y = iz * segHeight - halfHeight;
positions.push(x);
positions.push(-y);
positions.push((Math.sin(x * 2) + Math.cos(y * 2)) * 0.3);
}
}
var a;
var b;
var c;
var d;
for (iz = 0; iz < gridZ; iz++) {
for (ix = 0; ix < gridX; ix++) {
a = ix + gridX1 * iz;
b = ix + gridX1 * ( iz + 1 );
c = ( ix + 1 ) + gridX1 * ( iz + 1 );
d = ( ix + 1 ) + gridX1 * iz;
indices.push(a);
indices.push(b);
indices.push(c);
indices.push(c);
indices.push(d);
indices.push(a);
// a
uvs.push(ix / gridX);
uvs.push(1 - iz / gridZ);
//b
uvs.push(ix / gridX);
uvs.push(1 - ( iz + 1 ) / gridZ);
//c
uvs.push(( ix + 1 ) / gridX);
uvs.push(1 - ( iz + 1 ) / gridZ);
//c
uvs.push(( ix + 1 ) / gridX);
uvs.push(1 - ( iz + 1 ) / gridZ);
//d
uvs.push(( ix + 1 ) / gridX, 1 - iz / gridZ);
uvs.push(1 - iz / gridZ);
//a
uvs.push(ix / gridX);
uvs.push(1 - iz / gridZ);
}
}
return {
positions:positions,
uv:uvs,
indices:indices
};
}
</script>
</body>
</html>