forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvr_utils.js
232 lines (213 loc) · 5.1 KB
/
vr_utils.js
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.ui.VRUtils');
shaka.ui.VRUtils = class {
/**
* @param {number} resolution
* @return {{vertices: !Array.<number>, textureCoords: !Array.<number>,
* indices: !Array.<number>}}
*/
static generateSphere(resolution) {
/** @type {!Array.<number>} */
const vertices = [];
/** @type {!Array.<number>} */
const textureCoords = [];
/** @type {!Array.<number>} */
const indices = [];
for (let i = 0; i <= resolution; i++) {
const v = i / resolution;
const phi = v * Math.PI;
const sinPhi = Math.sin(phi);
const cosPhi = Math.cos(phi);
for (let j = 0; j <= resolution; j++) {
const u = j / resolution;
const theta = u * Math.PI * 2;
const sinTheta = Math.sin(theta);
const cosTheta = Math.cos(theta);
const x = -1 * cosTheta * sinPhi;
const y = cosPhi;
const z = sinTheta * sinPhi;
vertices.push(x, y, z);
textureCoords.push(u);
textureCoords.push(v);
}
}
for (let i = 0; i < resolution; i++) {
for (let j = 0; j < resolution; j++) {
const a = i * (resolution + 1) + j;
const b = a + 1;
const c = (i + 1) * (resolution + 1) + j;
const d = c + 1;
indices.push(a, c, b);
indices.push(b, c, d);
}
}
return {vertices, textureCoords, indices};
}
/**
* @return {{vertices: !Array.<number>, textureCoords: !Array.<number>,
* indices: !Array.<number>}}
*/
static generateCube() {
/** @type {!Array.<number>} */
const vertices = [
// order : left top back right bottom front
// Front face 3
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
// Back face 2
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
// Top face 6
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// Bottom face 1
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
// Right face 4
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
// Left face 5
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
];
/** @type {!Array.<number>} */
const textureCoords = [
// Left Face
2 / 3, 0.5,
1 / 3, 0.5,
1 / 3, 0.0,
2 / 3, 0.0,
// Top Face
2 / 3, 0.5,
2 / 3, 0.0,
1.0, 0.0,
1.0, 0.5,
// Back Face
1.0, 1.0,
2 / 3, 1.0,
2 / 3, 0.5,
1.0, 0.5,
// Right Face
0.0, 0.5,
0.0, 0.0,
1 / 3, 0.0,
1 / 3, 0.5,
// Bottom Face
0.0, 0.5,
1 / 3, 0.5,
1 / 3, 1.0,
0.0, 1.0,
// Front Face
1 / 3, 1.0,
1 / 3, 0.5,
2 / 3, 0.5,
2 / 3, 1.0,
];
/** @type {!Array.<number>} */
const indices = [
// Front face
0, 1, 2,
0, 2, 3,
// Back face
4, 5, 6,
4, 6, 7,
// Top face
8, 9, 10,
8, 10, 11,
// Bottom face
12, 13, 14,
12, 14, 15,
// Right face
16, 17, 18,
16, 18, 19,
// Left face
20, 21, 22,
20, 22, 23,
];
return {vertices, textureCoords, indices};
}
};
/**
* Sphere vertex shader.
*
* @constant {string}
*/
shaka.ui.VRUtils.VERTEX_SPHERE_SHADER =
`attribute vec4 a_vPosition;
// Per-vertex texture coordinate info
attribute vec2 a_TexCoordinate;
uniform mat4 u_VPMatrix;
// Passed into the fragment shader.
varying vec2 v_TexCoordinate;
varying vec3 pass_position;
void main()
{
gl_Position = u_VPMatrix * a_vPosition;
// Pass through texture coord
v_TexCoordinate = a_TexCoordinate;
pass_position = a_vPosition.xyz;
}`;
/**
* Sphere fragment shader.
*
* @constant {string}
*/
shaka.ui.VRUtils.FRAGMENT_SPHERE_SHADER =
`precision highp float;
#define PI 3.141592653589793238462643383279
varying vec2 v_TexCoordinate;
varying vec3 pass_position;
uniform sampler2D uSampler;
void main(void) {
highp float xValue =
(PI + atan(pass_position.z, pass_position.x)) / (2.0 * PI);
vec2 tc = vec2(xValue, v_TexCoordinate.t);
tc = vec2(tc.x , tc.y);
highp vec4 texelColor =
texture2D(uSampler, tc);
gl_FragColor = vec4(texelColor.rgb, texelColor.a);
}`;
/**
* Cube vertex shader.
*
* @constant {string}
*/
shaka.ui.VRUtils.VERTEX_CUBE_SHADER =
`attribute vec4 aVertexPosition;
attribute vec2 aTextureCoord;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
varying highp vec2 vTextureCoord;
varying highp vec3 vLighting;
void main(void) {
gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
vTextureCoord = aTextureCoord;
}`;
/**
* Cube fragment shader.
*
* @constant {string}
*/
shaka.ui.VRUtils.FRAGMENT_CUBE_SHADER =
`varying highp vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
highp vec4 texelColor = texture2D(uSampler, vTextureCoord);
gl_FragColor = vec4(texelColor.rgb , texelColor.a);
}`;