-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
167 lines (148 loc) · 5.52 KB
/
main.lua
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
function lovr.load()
controllerModels = {}
photos = {
lovr.graphics.newMaterial(lovr.graphics.newTexture('country.jpg', { mipmaps = false })),
lovr.graphics.newMaterial(lovr.graphics.newTexture('windmill.jpg', { mipmaps = false }))
}
sphere = {
position = lovr.math.newVec3(0, 1.1, -1),
translater = 0,
translateThreshold = 1.3,
radius = .15,
posRelativeToHead = lovr.math.newVec3(),
yOverride = 1.1
}
constants = {
headOffset = lovr.math.newVec3(0, -0.5, -0.6),
grey = 0xd3d3d3,
white = 0xffffff
}
currentPhoto = 1
rimLightShader = lovr.graphics.newShader(createRimLightShader())
highlightShader = lovr.graphics.newShader(createHighlightShader())
repositionSelectionSphere(5)
end
-- App cycle
function lovr.update(dt)
handlePhotoSelection()
repositionSelectionSphere(dt)
end
function handlePhotoSelection()
for i, hand in ipairs(lovr.headset.getHands()) do
local handPos = lovr.math.vec3(lovr.headset.getPose(hand))
handInSphere = sphereDistanceTest(handPos, sphere.radius)
if handInSphere then
if lovr.headset.wasPressed(hand, 'trigger') then
cyclePhoto()
end
end
end
end
function repositionSelectionSphere(variator)
local headPos = lovr.math.vec3(lovr.headset.getPose())
local sphereNearHead = sphereDistanceTest(headPos, sphere.translateThreshold)
if not sphereNearHead then
local v = lovr.math.vec3(mat4(lovr.headset.getPose()):translate(constants.headOffset))
sphere.posRelativeToHead:set(v.x, sphere.yOverride, v.z)
sphere.translater = math.min(sphere.translater + variator * .05, 1)
end
end
-- Rendering
function lovr.draw()
lovr.graphics.skybox(photos[currentPhoto]:getTexture())
drawSphere()
drawControllers()
end
function drawSphere()
local color = handInSphere and constants.white or constants.grey
local ease = quadraticEaseInOut(sphere.translater)
sphere.position:lerp(sphere.posRelativeToHead, ease)
lovr.graphics.setColor(color)
lovr.graphics.setShader(rimLightShader)
lovr.graphics.setBlendMode('alpha')
lovr.graphics.setCullingEnabled(true)
lovr.graphics.setWinding('clockwise')
-- Draw the selection sphere
lovr.graphics.sphere(photos[nextPhoto()], sphere.position, sphere.radius)
lovr.graphics.setWinding('counterclockwise')
-- Draw the sphere again but only render the specular highlight
lovr.graphics.setDepthTest(nil, false)
lovr.graphics.setShader(highlightShader)
lovr.graphics.sphere(photos[nextPhoto()], sphere.position, sphere.radius)
lovr.graphics.setDepthTest(nil, true)
lovr.graphics.setBlendMode()
lovr.graphics.setCullingEnabled(false)
lovr.graphics.setShader()
lovr.graphics.setColor(constants.white)
end
function drawControllers()
for i, hand in ipairs(lovr.headset.getHands()) do
controllerModels[hand] = controllerModels[hand] or lovr.headset.newModel(hand)
if controllerModels[hand] then
local x, y, z, angle, ax, ay, az = lovr.headset.getPose(hand)
controllerModels[hand]:draw(x, y, z, 1, angle, ax, ay, az)
end
end
end
-- Update and Draw helpers
function cyclePhoto()
currentPhoto = nextPhoto()
end
function nextPhoto()
return (currentPhoto % #photos) + 1
end
function sphereDistanceTest(pos, dist)
local d = pos - sphere.position
return (d.x * d.x + d.y * d.y + d.z * d.z) < (dist ^ 2)
end
function quadraticEaseInOut(val)
return val < 0.5 and 2 * val * val or 1 - math.pow(-2 * val + 2, 2) / 2
end
-- Shader
function createRimLightShader()
return [[
out vec3 FragmentPos;
out vec3 CameraPos;
out vec3 Normal;
vec4 position(mat4 projection, mat4 transform, vec4 vertex) {
Normal = (lovrNormalMatrix * -lovrNormal).xyz;
FragmentPos = (lovrModel * vertex).xyz;
CameraPos = -lovrView[3].xyz * mat3(lovrView);
return projection * transform * vertex;
}
]], [[
in vec3 Normal;
in vec3 FragmentPos;
in vec3 CameraPos;
vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
vec3 toCam = normalize(CameraPos - FragmentPos);
float rimAmt = 1.0 - max(0.0, dot(normalize(Normal), toCam));
rimAmt = pow(rimAmt, 3.);
return mix(texture(image, uv) * graphicsColor, vec4(1.), rimAmt);
}
]]
end
function createHighlightShader()
return [[
out vec3 FragmentPos;
out vec3 CameraPos;
out vec3 Normal;
vec4 position(mat4 projection, mat4 transform, vec4 vertex) {
Normal = (lovrNormalMatrix * -lovrNormal).xyz;
FragmentPos = (lovrModel * vertex).xyz;
CameraPos = -lovrView[3].xyz * mat3(lovrView);
return projection * transform * vertex;
}
]], [[
in vec3 Normal;
in vec3 FragmentPos;
in vec3 CameraPos;
vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
vec3 lightDir = normalize(vec3(.4, 2, -.1));
vec3 viewDir = normalize(CameraPos - FragmentPos);
vec3 reflectDir = reflect(-lightDir, normalize(Normal));
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 25.);
return vec4(1., 1., 1., spec * .75);
}
]]
end