master
ブランチ master
ShaderPlayground / src / gui / newshader.cc
14.9 KB、580行 生 Blame 履歴
1/*
2# 076 License
3
4Copyright (c) 076 Studio LLC
5
6Permission is hereby granted to any person obtaining a copy of the software
7Shader Playground (the "Software") to use, modify, merge, copy, publish, distribute,
8sublicense, and/or sell copies of the Software, subject to the following conditions:
9
10 1. **Origin Attribution**:
11 - You must not misrepresent the origin of the Software; you must not claim
12 you created the original Software.
13 - If the Software is used in a product, you must either:
14 a. Provide clear attribution in the product's documentation, user interface,
15 or other visible areas, **OR**
16 b. Pay the original developers a fee they specify in writing.
17 2. **Usage Restriction**:
18 - The Software, or any derivative works, dependencies, or libraries
19 incorporating it, must not be used for censorship or to suppress freedom of
20 speech, expression, or creativity. Prohibited uses include, but are not
21 limited to:
22 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
23 ideas, or objective reality.
24 - Tools or systems designed to restrict access to information or
25 artistic works.
26 3. **Notice Preservation**:
27 - This license and the above copyright notice must remain intact in all copies
28 of the source code.
29
30THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
31INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
32PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
33HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
34CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
35OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37#include <gui/newshader.hh>
38#include <database/locale.hh>
39#include <util/vector.hh>
40#include <imgui.h>
41
42namespace gui {
43 db::ShaderData NewShader::Make(ShaderType type) {
44 db::ShaderData newShader = {};
45 auto curLang = m_Info->i18n->GetCurrentLanguage().code;
46 newShader.id = 0;
47 if (curLang == "ja_JP") newShader.name = "(名無し)";
48 else newShader.name = "(Unnamed)";
49 newShader.clearColor.rgba = { 0.f, 0.f, 0.f, 1.f };
50 newShader.description = "";
51
52 newShader.vertexShader.id = 0;
53 newShader.vertexShader.type = db::ShaderCodeType::GlslVertex;
54 newShader.vertexShader.filename = "";
55
56 newShader.fragmentShader.id = 0;
57 newShader.fragmentShader.type = db::ShaderCodeType::GlslFragment;
58 newShader.fragmentShader.filename = "";
59
60 newShader.luaCode.id = 0;
61 newShader.luaCode.type = db::ShaderCodeType::Lua;
62 newShader.luaCode.filename = "";
63
64 if (type == ShaderType::Scene) {
65 newShader.vertexShader.code = getDefaultVertScene();
66 newShader.fragmentShader.code = getDefaultFragScene();
67 newShader.luaCode.code = getDefaultLuaScene();
68 } else if (type == ShaderType::Simple3D) {
69 newShader.vertexShader.code = getDefaultVert3D();
70 newShader.fragmentShader.code = getDefaultFrag3D();
71 newShader.luaCode.code = getDefaultLua3D();
72 } else {
73 newShader.vertexShader.code = getDefaultVert2D();
74 newShader.fragmentShader.code = getDefaultFrag2D();
75 newShader.luaCode.code = getDefaultLua2D();
76 }
77
78 return newShader;
79 }
80
81 ///////////////////////////
82
83 string NewShader::getDefaultVert2D() {
84 return R"(#version 460 core
85
86layout(location = 0) in vec2 aPosition;
87layout(location = 1) in vec3 aColor;
88out vec3 vColor;
89
90void main() {
91 vColor = aColor;
92 gl_Position = vec4(aPosition, 0.0, 1.0);
93})";
94 }
95
96 string NewShader::getDefaultFrag2D() {
97 return R"(#version 460 core
98
99in vec3 vColor;
100out vec4 out_color;
101
102void main() {
103 out_color = vec4(vColor, 1.0);
104})";
105 }
106
107 string NewShader::getDefaultLua2D() {
108 return R"(local stride = le.sizeof("vec2") + le.sizeof("vec3")
109
110local mesh = {
111 vertices = {
112 0, .5, 1, 0, 0,
113 -.5, -.5, 0, 1, 0,
114 .5, -.5, 0, 0, 1
115 },
116 indices = { 0, 1, 2 },
117
118 attributes = {
119 {
120 location = 0,
121 size = 2,
122 stride = stride,
123 offset = 0
124 }, -- position
125 {
126 location = 1,
127 size = 3,
128 stride = stride,
129 offset = le.sizeof("vec2")
130 } -- uv
131 }
132}
133
134le.gfx.add_mesh(mesh)
135le.gfx.set_position(0, 0, 0, 0)
136
137function init()
138end
139
140function update()
141end)";
142 }
143
144 string NewShader::getDefaultVert3D() {
145 return R"(#version 460 core
146
147layout(location = 0) in vec3 aPosition;
148layout(location = 1) in vec3 aColor;
149
150out vec3 vColor;
151
152uniform mat4 uModel;
153uniform mat4 uView;
154uniform mat4 uProjection;
155
156void main() {
157 vColor = aColor;
158 gl_Position = uProjection * uView * uModel * vec4(aPosition, 1.0);
159})";
160 }
161
162 string NewShader::getDefaultFrag3D() {
163 return R"(#version 460 core
164
165in vec3 vColor;
166out vec4 out_color;
167
168void main() {
169 out_color = vec4(vColor, 1.0);
170})";
171 }
172
173 string NewShader::getDefaultLua3D() {
174 return R"(local offset = le.sizeof("vec3")
175local stride = offset * 2
176
177local mesh = {
178 vertices = {
179 -- position -- uv
180 -- mae
181 -0.5, -0.5, -0.5, 1,0,0,
182 0.5, -0.5, -0.5, 0,1,0,
183 0.5, 0.5, -0.5, 0,0,1,
184 -0.5, 0.5, -0.5, 1,1,0,
185 -- usiro
186 -0.5, -0.5, 0.5, 1,0,1,
187 0.5, -0.5, 0.5, 0,1,1,
188 0.5, 0.5, 0.5, 1,1,1,
189 -0.5, 0.5, 0.5, 1,0,1,
190 },
191
192 indices = {
193 0,1,2, 2,3,0, -- mae
194 1,5,6, 6,2,1, -- migi
195 5,4,7, 7,6,5, -- usiro
196 4,0,3, 3,7,4, -- hidari
197 3,2,6, 6,7,3, -- ue
198 4,5,1, 1,0,4 -- sita
199 },
200
201 attributes = {
202 { location = 0, size = 3, stride = stride, offset = 0 },
203 { location = 1, size = 3, stride = stride, offset = offset }
204 }
205}
206
207le.gfx.add_mesh(mesh)
208le.gfx.set_position(0, 0, 0, 1)
209le.gfx.add_mesh(mesh)
210le.gfx.set_position(1, -8, -2, -11)
211le.gfx.add_mesh(mesh)
212le.gfx.set_position(2, 8, -2, -11)
213
214function init()
215end
216
217function update()
218 local t = le.sys.get_time()
219
220 le.gfx.set_rotation(0, t*0.7, t*1.0, t*0.4)
221 le.gfx.set_rotation(1, t*0.2, t*0.8, t*1.4)
222 le.gfx.set_rotation(2, t*0.4, t*0.3, t*0.8)
223
224 local reso = le.sys.get_resolution()
225 local aspect = reso.x / reso.y
226
227 local view = le.math.look_at(
228 0, 0, 4, -- eye
229 0, 0, 0, -- target
230 0, 1, 0 -- up
231 )
232
233 local projection = le.math.perspective(
234 45.0,
235 aspect,
236 0.1,
237 100.0
238 )
239
240 le.gfx.set_uniform_mat4("uView", view)
241 le.gfx.set_uniform_mat4("uProjection", projection)
242end)";
243 }
244
245 string NewShader::getDefaultVertScene() {
246 return R"(#version 460 core
247
248layout(location = 0) in vec3 aPosition;
249layout(location = 1) in vec3 aNormal;
250layout(location = 2) in vec3 aColor;
251
252out vec3 vFragPos;
253out vec3 vNormal;
254out vec3 vColor;
255out vec4 vFragPosLightSpace;
256
257uniform mat4 uModel;
258uniform mat4 uView;
259uniform mat4 uProjection;
260uniform mat4 uLightSpace;
261
262void main() {
263 vec4 worldPos = uModel * vec4(aPosition, 1.0);
264
265 vFragPos = worldPos.xyz;
266 vNormal = mat3(transpose(inverse(uModel))) * aNormal;
267 vColor = aColor;
268 vFragPosLightSpace = uLightSpace * worldPos;
269
270 gl_Position = uProjection * uView * worldPos;
271})";
272 }
273
274 string NewShader::getDefaultFragScene() {
275 return R"(#version 460 core
276
277in vec3 vFragPos;
278in vec3 vNormal;
279in vec3 vColor;
280in vec4 vFragPosLightSpace;
281
282out vec4 out_color;
283
284uniform vec3 uLightPos;
285uniform vec3 uLightColor;
286uniform sampler2D uShadowMap;
287
288void main() {
289 vec3 norm = normalize(vNormal);
290 vec3 lightDir = normalize(uLightPos - vFragPos);
291
292 float diff = max(dot(norm, lightDir), 0.0);
293
294 vec3 projCoords = vFragPosLightSpace.xyz / vFragPosLightSpace.w;
295 projCoords = projCoords * 0.5 + 0.5;
296
297 float closestDepth = texture(uShadowMap, projCoords.xy).r;
298 float currentDepth = projCoords.z;
299
300 float bias = max(0.05 * (1.0 - dot(norm, lightDir)), 0.005);
301
302 float shadow =
303 (projCoords.z > 1.0)
304 ? 0.0
305 : (currentDepth - bias > closestDepth ? 1.0 : 0.0);
306
307 vec3 ambient = 0.2 * vColor;
308 vec3 diffuse = (1.0 - shadow) * diff * uLightColor * vColor;
309
310 vec3 viewDir = normalize(-vFragPos);
311 vec3 reflectDir = reflect(-lightDir, norm);
312 float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
313 vec3 specular = (1.0 - shadow) * 0.3 * spec * uLightColor;
314
315 vec3 result = ambient + diffuse + specular;
316
317 out_color = vec4(result, 1.0);
318})";
319 }
320
321 string NewShader::getDefaultLuaScene() {
322 return R"(local posSize = le.sizeof("vec3")
323local normalSize = le.sizeof("vec3")
324local colorSize = le.sizeof("vec3")
325
326local stride = posSize + normalSize + colorSize
327
328local sphere = {
329 vertices = {},
330
331 indices = {},
332
333 attributes = {
334 { location = 0, size = 3, stride = stride, offset = 0 },
335 { location = 1, size = 3, stride = stride, offset = posSize },
336 { location = 2, size = 3, stride = stride, offset = posSize + normalSize }
337 }
338}
339
340local plane = {
341 vertices = {
342 -20, -2, -20, 0,1,0, 0.4, 0.4, 0.4,
343 20, -2, -20, 0,1,0, 0.4, 0.4, 0.4,
344 20, -2, 20, 0,1,0, 0.4, 0.4, 0.4,
345 -20, -2, 20, 0,1,0, 0.4, 0.4, 0.4,
346 },
347 indices = {
348 0, 1, 2,
349 0, 2, 3,
350 },
351 attributes = {
352 { location = 0, size = 3, stride = stride, offset = 0 },
353 { location = 1, size = 3, stride = stride, offset = posSize },
354 { location = 2, size = 3, stride = stride, offset = posSize + normalSize }
355 }
356}
357
358local pyramid = {
359 vertices = {
360 0.0, 0.8, 0.0, 0.0, 0.447, 0.894, 1,1,1,
361 -0.5, -0.2, 0.5, 0.0, 0.447, 0.894, 1,0,0,
362 0.5, -0.2, 0.5, 0.0, 0.447, 0.894, 0,1,0,
363
364 0.0, 0.8, 0.0, 0.894, 0.447, 0.0, 1,1,1,
365 0.5, -0.2, 0.5, 0.894, 0.447, 0.0, 0,1,0,
366 0.5, -0.2, -0.5, 0.894, 0.447, 0.0, 0,0,1,
367
368 0.0, 0.8, 0.0, 0.0, 0.447, -0.894, 1,1,1,
369 0.5, -0.2, -0.5, 0.0, 0.447, -0.894, 0,0,1,
370 -0.5, -0.2, -0.5, 0.0, 0.447, -0.894, 1,1,0,
371
372 0.0, 0.8, 0.0, -0.894, 0.447, 0.0, 1,1,1,
373 -0.5, -0.2, -0.5, -0.894, 0.447, 0.0, 1,1,0,
374 -0.5, -0.2, 0.5, -0.894, 0.447, 0.0, 1,0,0,
375
376 -0.5, -0.2, 0.5, 0,-1,0, 1,0,0,
377 0.5, -0.2, 0.5, 0,-1,0, 0,1,0,
378 0.5, -0.2, -0.5, 0,-1,0, 0,0,1,
379 -0.5, -0.2, -0.5, 0,-1,0, 1,1,0,
380 },
381
382 indices = {
383 0,1,2,
384 3,4,5,
385 6,7,8,
386 9,10,11,
387
388 12,13,14,
389 12,14,15
390 },
391
392 attributes = {
393 { location = 0, size = 3, stride = stride, offset = 0 },
394 { location = 1, size = 3, stride = stride, offset = posSize },
395 { location = 2, size = 3, stride = stride, offset = posSize + normalSize }
396 }
397}
398
399local cube = {
400 vertices = {
401 -- position --normal -- uv
402 -- mae
403 -0.5, -0.5, -0.5, 0, 0, -1, 1,0,0,
404 0.5, -0.5, -0.5, 0, 0, -1, 0,1,0,
405 0.5, 0.5, -0.5, 0, 0, -1, 0,0,1,
406 -0.5, 0.5, -0.5, 0, 0, -1, 1,1,0,
407 -- usiro
408 -0.5, -0.5, 0.5, 0, 0, 1, 1,0,1,
409 0.5, -0.5, 0.5, 0, 0, 1, 0,1,1,
410 0.5, 0.5, 0.5, 0, 0, 1, 1,1,1,
411 -0.5, 0.5, 0.5, 0, 0, 1, 1,0,1,
412 -- (migi)
413 0.5, -0.5, -0.5, 1, 0, 0, 0,1,0,
414 0.5, -0.5, 0.5, 1, 0, 0, 0,1,1,
415 0.5, 0.5, 0.5, 1, 0, 0, 1,1,1,
416 0.5, 0.5, -0.5, 1, 0, 0, 0,0,1,
417 -- hidari
418 -0.5, -0.5, 0.5, -1, 0, 0, 1,0,1,
419 -0.5, -0.5, -0.5, -1, 0, 0, 1,0,0,
420 -0.5, 0.5, -0.5, -1, 0, 0, 1,1,0,
421 -0.5, 0.5, 0.5, -1, 0, 0, 1,0,1,
422 -- ue
423 -0.5, 0.5, -0.5, 0, 1, 0, 1,1,0,
424 0.5, 0.5, -0.5, 0, 1, 0, 0,0,1,
425 0.5, 0.5, 0.5, 0, 1, 0, 1,1,1,
426 -0.5, 0.5, 0.5, 0, 1, 0, 1,0,1,
427 -- sita
428 -0.5, -0.5, -0.5, 0, -1, 0, 1,0,0,
429 0.5, -0.5, -0.5, 0, -1, 0, 0,1,0,
430 0.5, -0.5, 0.5, 0, -1, 0, 0,1,1,
431 -0.5, -0.5, 0.5, 0, -1, 0, 1,0,1,
432 },
433
434 indices = {
435 0,1,2, 2,3,0,
436 4,5,6, 6,7,4,
437 8,9,10, 10,11,8,
438 12,13,14, 14,15,12,
439 16,17,18, 18,19,16,
440 20,21,22, 22,23,20
441 },
442
443 attributes = {
444 { location = 0, size = 3, stride = stride, offset = 0 },
445 { location = 1, size = 3, stride = stride, offset = posSize },
446 { location = 2, size = 3, stride = stride, offset = posSize + normalSize }
447 }
448}
449
450local radius = .5
451local stacks = 16
452local slices = 24
453
454for i = 0, stacks do
455 local phi = math.pi * i / stacks
456 local y = radius * math.cos(phi)
457 local ringRadius = radius * math.sin(phi)
458
459 for j = 0, slices -1 do
460 local theta = 2 * math.pi * j / slices
461
462 local x = ringRadius * math.cos(theta)
463 local z = ringRadius * math.sin(theta)
464
465 local len = math.sqrt(x*x + y*y + z*z)
466 local nx = x / len
467 local ny = y / len
468 local nz = z / len
469
470 local r = (x + radius) / (2 * radius)
471 local g = (y + radius) / (2 * radius)
472 local b = (z + radius) / (2 * radius)
473
474 table.insert(sphere.vertices, x)
475 table.insert(sphere.vertices, y)
476 table.insert(sphere.vertices, z)
477
478 table.insert(sphere.vertices, nx)
479 table.insert(sphere.vertices, ny)
480 table.insert(sphere.vertices, nz)
481
482 table.insert(sphere.vertices, r)
483 table.insert(sphere.vertices, g)
484 table.insert(sphere.vertices, b)
485 end
486end
487
488for i = 0, stacks - 1 do
489 for j = 0, slices - 1 do
490 local nextJ = (j + 1) % slices
491
492 local a = i * slices + j
493 local b = (i + 1) * slices + j
494 local c = i * slices + nextJ
495 local d = (i + 1) * slices + nextJ
496
497 table.insert(sphere.indices, a)
498 table.insert(sphere.indices, b)
499 table.insert(sphere.indices, c)
500
501 table.insert(sphere.indices, c)
502 table.insert(sphere.indices, b)
503 table.insert(sphere.indices, d)
504 end
505end
506
507le.gfx.add_mesh(cube)
508le.gfx.set_position(0, 0, 0, -1)
509le.gfx.add_mesh(cube)
510le.gfx.set_position(1, -8, -2, -11)
511le.gfx.add_mesh(cube)
512le.gfx.set_position(2, 8, -2, -11)
513
514le.gfx.add_mesh(pyramid)
515le.gfx.set_position(3, 8, 2, -11)
516le.gfx.set_scale(3, 2, 2, 2)
517
518le.gfx.add_mesh(sphere)
519le.gfx.set_position(4, -2, 1, -3)
520
521le.gfx.add_mesh(plane)
522le.gfx.set_position(5, 0, -2, 0)
523
524function init()
525end
526
527function update()
528 local t = le.sys.get_time()
529 local zs = -3 + math.sin(t * 1.5) * 6
530 le.gfx.set_position(4, -2.3, -1, zs)
531
532 local size = 8
533 local speed = 0.8
534 local phase = (t * speed) % (4 * math.pi)
535 local cubeX, cubeY
536
537 if phase < math.pi then
538 cubeX = -size/2 + (phase / math.pi) * size
539 cubeY = size/2
540 elseif phase < 2 * math.pi then
541 cubeX = size/2
542 cubeY = size/2 - ((phase - math.pi) / math.pi) * size
543 elseif phase < 3 * math.pi then
544 cubeX = size/2 - ((phase - 2*math.pi) / math.pi) * size
545 cubeY = -size/2
546 else
547 cubeX = -size/2
548 cubeY = -size/2 + ((phase - 3*math.pi) / math.pi) * size
549 end
550
551 le.gfx.set_position(2, cubeX, cubeY, -11)
552
553 le.gfx.set_rotation(0, t*0.7, t*1.0, t*0.4)
554 le.gfx.set_rotation(1, t*0.2, t*0.8, t*1.4)
555 le.gfx.set_rotation(2, t*0.4, t*0.5, t*0.8)
556 le.gfx.set_rotation(3, 0, t*0.8, -.5)
557
558 local reso = le.sys.get_resolution()
559 local aspect = reso.x / reso.y
560
561 local view = le.math.look_at(
562 0, 0, 4, -- eye
563 0, 0, 0, -- target
564 0, 1, 0 -- up
565 )
566
567 local projection = le.math.perspective(
568 45.0,
569 aspect,
570 0.1,
571 100.0
572 )
573
574 le.gfx.set_uniform_mat4("uView", view)
575 le.gfx.set_uniform_mat4("uProjection", projection)
576 le.gfx.set_uniform_vec3("uLightPos", 3, 5, 2)
577 le.gfx.set_uniform_vec3("uLightColor", 1, 1, 1)
578end)";
579 }
580} // namespace gui