-
Notifications
You must be signed in to change notification settings - Fork 5
/
hypTilingRhombicDodec4.html
235 lines (191 loc) · 7.4 KB
/
hypTilingRhombicDodec4.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
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
233
234
235
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hyperbolic tiling</title>
<!--
Hyperbolic space, 12 tones of christmas, with webVR support. WASD + Arrows to move, space or p to play/pause.
A websperience by Vi Hart, Andrea Hawksley, and Henry Segerman, using the hyperbolic space prototype developed at a hyperbolic webVR "hackalot" with Vi Hart, Mike Stay, Henry Segerman, Andrea Hawksley, and Andrew Lutomirski, with help from Marc ten Bosch's 4d graphics shader, Jeff Week's Curved Spaces, Mozilla's webVR framework for THREEjs, etc.
http://vihart.com
http://andreahawksley.com
http://segerman.org/
http://reperiendi.wordpress.com/
https://github.com/hawksley
http://www.geometrygames.org/CurvedSpaces/
http://www.marctenbosch.com
https://github.com/MozVR/vr-web-examples/tree/master/threejs-vr-boilerplate
Music:
https://soundcloud.com/vihartvihart/the-12-tones-of-christmas
-->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000;
color: #fff;
margin: 0px;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
<!-- <audio id='music' src="media/12tonesofchristmas.ogg" autoplay /> -->
</body>
<script type="x-shader/x-vertex" id="vertexShader">
// This shader moves vertices around
// input
uniform float time; // global time in seconds
uniform mat4 translation; // dodecahedral
uniform mat4 boost;
vec3 ChooseColor( in vec3 nBase )
{
float r = nBase.x;
float g = nBase.y;
float b = nBase.z;
float norm = sqrt(r*r+g*g+b*b);
r /= norm;
g /= norm;
b /= norm;
// float sin_a = 0.973255;
// float cos_a = 0.229727;
// float rot_r = cos_a * r + sin_a * g;
// float rot_g = -sin_a * r + cos_a * g;
return vec3(r*0.5 + 0.5,g*0.5 + 0.5,b*0.5 + 0.5);
}
// Quaternion Multiplication
vec4 quatMult( in vec4 p, in vec4 q )
{
vec4 r;
r.w = + p.w*q.w - p.x*q.x - p.y*q.y - p.z*q.z;
r.x = + p.w*q.x + p.x*q.w + p.y*q.z - p.z*q.y;
r.y = + p.w*q.y - p.x*q.z + p.y*q.w + p.z*q.x;
r.z = + p.w*q.z + p.x*q.y - p.y*q.x + p.z*q.w;
return r;
}
vec4 quatInv( in vec4 p )
{
vec4 r;
r.x = -p.x;
r.y = -p.y;
r.z = -p.z;
r.w = p.w;
return r;
}
// Project the vector p to the 3-space perpendicular to q
vec4 projVecPerp( in vec4 p, in vec4 q )
{
vec4 r;
float pDotq = p.w*q.w + p.x*q.x + p.y*q.y + p.z*q.z;
float qDotq = q.w*q.w + q.x*q.x + q.y*q.y + q.z*q.z;
float foo = pDotq / qDotq;
r.x = p.x - foo * q.x;
r.y = p.y - foo * q.y;
r.z = p.z - foo * q.z;
r.w = p.w - foo * q.w;
return r;
}
// Hopf fibration coloring
// returns a color based on the 4D normal
vec3 HopfColor( in vec4 nBase )
{
vec4 n = quatMult( nBase, vec4(0,1,0,0) ); //makes hypTiling monkeys have nicer colours
// compute the color
float y = n.x;
float u = n.y;
float v = n.z;
float x = n.w;
float r = 2. * (u*x + v*y);
float g = 2. * (u*y - v*x);
float b = x*x + y*y - u*u - v*v;
return vec3(r*0.5 + 0.5,g*0.5 + 0.5,b*0.5 + 0.5);
}
// output
varying vec3 vColor; // this shader computes the color of each vertex
vec4 projectToHyperboloid(vec4 v) {
float scaleFactor = sqrt(v.w * v.w - v.x * v.x - v.y * v.y - v.z * v.z);
vec4 result;
result.x = v.x / scaleFactor;
result.y = v.y / scaleFactor;
result.z = v.z / scaleFactor;
result.w = v.w / scaleFactor;
return result;
}
////// scaling regular euclidean polyhedron with face center at dist 1 from center so that it projects onto hyperboloid
////// correctly:
////// for cube, tiling {4,3,n}
////// polyhedron center d
////// pi/4 ---------. pi/2 face center
////// "-_ |
////// "-_ |
////// "-_. pi/n edge center
////// scale so that in Klein model 1 becomes d.
////// hyp dist d, cosh(d) = cd = cos(pi/n) / sin(pi/4) by hyperbolic law of cosines.
////// In Klein model, d becomes tanh(arccosh(cd)) = sqrt(cd*cd-1)/cd
////// For dodec, centerAngle = 0.55357435889704525151 = ArcCos[(1/2) Sqrt[(5/2) + 11 Sqrt[5]/10] / ((3 + Sqrt[5])/4)]
// float meshScalingFactor( float edgeAngle, float centerAngle ) {
// float d = Math.cos(edgeAngle) / Math.sin(centerAngle);
// return Math.sqrt(d*d-1.0)/d;
// }
void main()
{
//use S^3 quaternion pullback to get consistent texturing
vec4 p3sphere = normalize( vec4(position.zyx, 1.0) );
vec4 n3sphere = vec4( normal.zyx, 0.0);
n3sphere = projVecPerp( n3sphere, p3sphere );
n3sphere = quatMult( n3sphere, quatInv( p3sphere ) );
n3sphere = normalize(n3sphere);
vColor = HopfColor(n3sphere);
// base position
// float scaling = meshScalingFactor( 0.523, 0.785 );
// float scaling = 0.57735; /// {4,3,6}
// float scaling = 0.66874; /// {5,3,4}
// float scaling = 0.760072; /// {5,3,5}
// float scaling = 0.619712; /// {4,3,7}
float scaling = 0.707107; // {rhombic dodec, 4}
vec4 p = projectToHyperboloid( vec4(position.zyx * scaling, 1.0) ); //0.57735 = tanh(arccosh(sqrt(3/2)))
// scale cube of (face center) rad 1 so it maps to correct cube in Klein model on w=1,
// Klein model euclidean dist from 0 = tanh(hyp dist from 0)
p = boost * translation * p;
// compute the color from the normal
// vColor = ChooseColor(-normal.zyx);
// take the final 3D position and project it onto the screen
gl_Position = projectionMatrix * modelViewMatrix * p;
}
</script>
<script type="x-shader/x-vertex" id="fragmentShader">
// this gets called once per pixel
varying vec3 vColor;
void main()
{
// just use the color we computed and assign it to this pixel
gl_FragColor = vec4( vColor, 1. );
}
</script>
<!--
three.js 3d library
-->
<script src="js/lib/three.min.js"></script>
<!--
library for fast quaternion rotation
-->
<script src="js/lib/gl-matrix.js"></script>
<script src="js/hypMath.js"></script>
<!--
VRControlsHyperbolic.js acquires positional information from connected VR devices and applies the transformations to a three.js camera object. Also deals with hyperbolic and parabolic motions.
-->
<script src="js/vr/VRControlsHyperbolic.js"></script>
<!--
VREffect.js handles stereo camera setup and rendering.
-->
<script src="js/vr/VREffect.js"></script>
<script src="js/loaders/OBJLoader.js"></script>
<!--
dodec_no_id.js stores hyperbolic translation matrices to make copies of the dodecahedron.
-->
<script src="js/rhombicDodec4.js"></script>
<!--
<script src="js/534Reflect.js"></script>
-->
<script src="js/hypTilingRhombicDodec.js" type="text/javascript" id="mainCode"></script>
<!-- // <script src="js/hypTiling.js" type="text/javascript" id="mainCode"></script> -->
</html>