chorama 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (256) hide show
  1. package/.configs/tsconfig.lib.json +43 -0
  2. package/.configs/tsconfig.website.json +34 -0
  3. package/.github/workflows/static.yml +88 -0
  4. package/.vscode/launch.json +29 -0
  5. package/.vscode/tasks.json +19 -0
  6. package/README.md +127 -0
  7. package/assets/images/disappointed.jpg +0 -0
  8. package/assets/images/skybox/grimmnight_back.png +0 -0
  9. package/assets/images/skybox/grimmnight_bottom.png +0 -0
  10. package/assets/images/skybox/grimmnight_front.png +0 -0
  11. package/assets/images/skybox/grimmnight_left.png +0 -0
  12. package/assets/images/skybox/grimmnight_right.png +0 -0
  13. package/assets/images/skybox/grimmnight_top.png +0 -0
  14. package/assets/images/skybox/miramar_back.png +0 -0
  15. package/assets/images/skybox/miramar_bottom.png +0 -0
  16. package/assets/images/skybox/miramar_front.png +0 -0
  17. package/assets/images/skybox/miramar_left.png +0 -0
  18. package/assets/images/skybox/miramar_right.png +0 -0
  19. package/assets/images/skybox/miramar_top.png +0 -0
  20. package/assets/images/uv.jpg +0 -0
  21. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_BaseColor.png +0 -0
  22. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_Normal.png +0 -0
  23. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_OcclusionRoughMetal.png +0 -0
  24. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_BaseColor.png +0 -0
  25. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_Normal.png +0 -0
  26. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_OcclusionRoughMetal.png +0 -0
  27. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_BaseColor.png +0 -0
  28. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_Normal.png +0 -0
  29. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_OcclusionRoughMetal.png +0 -0
  30. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_BaseColor.png +0 -0
  31. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_Normal.png +0 -0
  32. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_OcclusionRoughMetal.png +0 -0
  33. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_BaseColor.png +0 -0
  34. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_Normal.png +0 -0
  35. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_OcclusionRoughMetal.png +0 -0
  36. package/assets/models/gltf/flight_helmet/index.bin +0 -0
  37. package/assets/models/gltf/flight_helmet/index.gltf +705 -0
  38. package/assets/models/gltf/object.gltf +23 -0
  39. package/assets/models/gltf/pirate_girl/index.bin +0 -0
  40. package/assets/models/gltf/pirate_girl/index.gltf +2082 -0
  41. package/assets/models/obj/pirate_girl/pirate_girl.obj +18459 -0
  42. package/assets/models/obj/pirate_girl/pirate_girl.png +0 -0
  43. package/astro.config.mjs +45 -0
  44. package/content/guide/api-map.md +89 -0
  45. package/content/guide/camera-and-controls.md +98 -0
  46. package/content/guide/first-scene.md +176 -0
  47. package/content/guide/index.md +72 -0
  48. package/content/guide/installation.md +179 -0
  49. package/content/guide/materials-and-lighting.md +138 -0
  50. package/content/guide/plugins-and-render-pipeline.md +124 -0
  51. package/content/guide/render-targets-and-views.md +147 -0
  52. package/content/guide/scene-graph-and-transforms.md +113 -0
  53. package/content/guide/textures-and-assets.md +120 -0
  54. package/content/guide/troubleshooting.md +49 -0
  55. package/env.d.ts +19 -0
  56. package/examples/addons/rendergraph_gui.js +580 -0
  57. package/examples/camera/orthographic.js +120 -0
  58. package/examples/camera/perspective.js +138 -0
  59. package/examples/lights/directional.js +397 -0
  60. package/examples/lights/multiple_spot_lights.js +304 -0
  61. package/examples/lights/point.js +337 -0
  62. package/examples/lights/spot.js +366 -0
  63. package/examples/loader/gltf_material.js +111 -0
  64. package/examples/loader/gltfloader.js +78 -0
  65. package/examples/loader/objloader.js +95 -0
  66. package/examples/material/cullface.js +111 -0
  67. package/examples/material/materials.js +126 -0
  68. package/examples/material/standard/basic.js +164 -0
  69. package/examples/mesh/circle.js +117 -0
  70. package/examples/mesh/cuboid.js +151 -0
  71. package/examples/mesh/cylinder.js +139 -0
  72. package/examples/mesh/geometries.js +108 -0
  73. package/examples/mesh/meshTopology.js +103 -0
  74. package/examples/mesh/plane.js +117 -0
  75. package/examples/mesh/skinning.js +136 -0
  76. package/examples/mesh/uvsphere.js +113 -0
  77. package/examples/other/rotatingCube.js +93 -0
  78. package/examples/other/rotatingSphere.js +96 -0
  79. package/examples/rendertarget/basic_canvas.js +130 -0
  80. package/examples/rendertarget/depth_texture.js +130 -0
  81. package/examples/rendertarget/image_target.js +140 -0
  82. package/examples/rendertarget/multiple_views.js +158 -0
  83. package/examples/rendertarget/render_masks.js +173 -0
  84. package/examples/rendertarget/split_screen.js +123 -0
  85. package/examples/rendertarget/split_view.js +137 -0
  86. package/examples/skybox/skybox.js +111 -0
  87. package/examples/texture/arrays.js +156 -0
  88. package/examples/texture/textureWrap.js +118 -0
  89. package/examples/transform/propagation.js +92 -0
  90. package/package.json +55 -0
  91. package/rollup.config.js +66 -0
  92. package/scripts/stage-chorama.mjs +29 -0
  93. package/src/caches/cache.js +420 -0
  94. package/src/caches/index.js +2 -0
  95. package/src/caches/uniformbuffers.js +104 -0
  96. package/src/cameracontrols/index.js +258 -0
  97. package/src/constants/index.js +3 -0
  98. package/src/constants/mesh.js +197 -0
  99. package/src/constants/others.js +218 -0
  100. package/src/constants/texture.js +183 -0
  101. package/src/core/constants.js +14 -0
  102. package/src/core/extensions.js +42 -0
  103. package/src/core/index.js +7 -0
  104. package/src/core/layouts/index.js +4 -0
  105. package/src/core/layouts/meshvertex.js +60 -0
  106. package/src/core/layouts/uniform.js +21 -0
  107. package/src/core/layouts/uniformbuffer.js +15 -0
  108. package/src/core/layouts/vertexbuffer.js +43 -0
  109. package/src/core/limits.js +247 -0
  110. package/src/core/resources/blendparams.js +89 -0
  111. package/src/core/resources/framebuffer.js +127 -0
  112. package/src/core/resources/gpubuffer.js +32 -0
  113. package/src/core/resources/gpumesh.js +43 -0
  114. package/src/core/resources/gputexture.js +73 -0
  115. package/src/core/resources/index.js +5 -0
  116. package/src/core/shader.js +62 -0
  117. package/src/core/webgl/bindgroup.js +89 -0
  118. package/src/core/webgl/descriptors.js +104 -0
  119. package/src/core/webgl/index.js +5 -0
  120. package/src/core/webgl/renderpassencoder.js +96 -0
  121. package/src/core/webgl/renderpipeline.js +54 -0
  122. package/src/core/webgl/utils.js +371 -0
  123. package/src/core/webgl/webglrenderdevice.js +235 -0
  124. package/src/function.js +358 -0
  125. package/src/index.js +15 -0
  126. package/src/loader/gltf.js +2172 -0
  127. package/src/loader/index.js +3 -0
  128. package/src/loader/loader.js +174 -0
  129. package/src/loader/obj.js +188 -0
  130. package/src/loader/texture.js +85 -0
  131. package/src/loader/utils.js +16 -0
  132. package/src/material/basic.js +75 -0
  133. package/src/material/depth.js +73 -0
  134. package/src/material/index.js +8 -0
  135. package/src/material/lambert.js +73 -0
  136. package/src/material/material.js +106 -0
  137. package/src/material/normal.js +30 -0
  138. package/src/material/phong.js +86 -0
  139. package/src/material/raw.js +52 -0
  140. package/src/material/standard.js +221 -0
  141. package/src/math/index.js +3 -0
  142. package/src/math/transform.js +38 -0
  143. package/src/mesh/attribute/attribute.js +79 -0
  144. package/src/mesh/attribute/index.js +1 -0
  145. package/src/mesh/attributedata/index.js +1 -0
  146. package/src/mesh/attributedata/separate.js +180 -0
  147. package/src/mesh/builders/base.js +41 -0
  148. package/src/mesh/builders/circle.js +63 -0
  149. package/src/mesh/builders/cuboid.js +135 -0
  150. package/src/mesh/builders/cylinder.js +131 -0
  151. package/src/mesh/builders/index.js +7 -0
  152. package/src/mesh/builders/plane.js +73 -0
  153. package/src/mesh/builders/utils.js +20 -0
  154. package/src/mesh/builders/uvsphere.js +80 -0
  155. package/src/mesh/builders/wireframe.js +62 -0
  156. package/src/mesh/index.js +4 -0
  157. package/src/mesh/mesh.js +149 -0
  158. package/src/objects/bone.js +17 -0
  159. package/src/objects/camera/camera.js +56 -0
  160. package/src/objects/camera/index.js +2 -0
  161. package/src/objects/camera/projection.js +203 -0
  162. package/src/objects/debug/index.js +1 -0
  163. package/src/objects/debug/skeleton.js +28 -0
  164. package/src/objects/index.js +7 -0
  165. package/src/objects/light/ambient.js +20 -0
  166. package/src/objects/light/directional.js +29 -0
  167. package/src/objects/light/index.js +5 -0
  168. package/src/objects/light/point.js +32 -0
  169. package/src/objects/light/shadow/index.js +1 -0
  170. package/src/objects/light/shadow/shadow.js +67 -0
  171. package/src/objects/light/spot.js +56 -0
  172. package/src/objects/mesh.js +141 -0
  173. package/src/objects/object3d.js +167 -0
  174. package/src/objects/skybox.js +38 -0
  175. package/src/plugins/camera/camera.js +19 -0
  176. package/src/plugins/camera/index.js +2 -0
  177. package/src/plugins/camera/nodes/cameraview.js +46 -0
  178. package/src/plugins/camera/nodes/index.js +2 -0
  179. package/src/plugins/camera/nodes/opaquepass.js +79 -0
  180. package/src/plugins/index.js +6 -0
  181. package/src/plugins/light/index.js +2 -0
  182. package/src/plugins/light/light.js +23 -0
  183. package/src/plugins/light/nodes/index.js +1 -0
  184. package/src/plugins/light/nodes/light.js +127 -0
  185. package/src/plugins/meshmaterial/index.js +3 -0
  186. package/src/plugins/meshmaterial/meshmaterial.js +381 -0
  187. package/src/plugins/meshmaterial/nodes/index.js +1 -0
  188. package/src/plugins/meshmaterial/nodes/meshmaterial.js +50 -0
  189. package/src/plugins/meshmaterial/resources/index.js +1 -0
  190. package/src/plugins/meshmaterial/resources/meshmaterialpipelines.js +50 -0
  191. package/src/plugins/shadow/index.js +3 -0
  192. package/src/plugins/shadow/nodes/index.js +3 -0
  193. package/src/plugins/shadow/nodes/shadow.js +272 -0
  194. package/src/plugins/shadow/nodes/shadowOccluder.js +112 -0
  195. package/src/plugins/shadow/nodes/shadowOpaquePass.js +73 -0
  196. package/src/plugins/shadow/resources/ShadowMap.js +99 -0
  197. package/src/plugins/shadow/resources/index.js +2 -0
  198. package/src/plugins/shadow/resources/shadowpipelines.js +25 -0
  199. package/src/plugins/shadow/shadow.js +31 -0
  200. package/src/plugins/skeletonhelper/index.js +1 -0
  201. package/src/plugins/skeletonhelper/skeletonhelper.js +160 -0
  202. package/src/plugins/skybox/index.js +3 -0
  203. package/src/plugins/skybox/nodes/index.js +1 -0
  204. package/src/plugins/skybox/nodes/skybox.js +143 -0
  205. package/src/plugins/skybox/resources/index.js +2 -0
  206. package/src/plugins/skybox/resources/skyboxmesh.js +14 -0
  207. package/src/plugins/skybox/resources/skyboxpipeline.js +6 -0
  208. package/src/plugins/skybox/skybox.js +137 -0
  209. package/src/renderer/core/index.js +179 -0
  210. package/src/renderer/graph/index.js +3 -0
  211. package/src/renderer/graph/nodes.js +34 -0
  212. package/src/renderer/graph/rendergraph.js +182 -0
  213. package/src/renderer/index.js +5 -0
  214. package/src/renderer/plugin.js +36 -0
  215. package/src/renderer/renderer.js +179 -0
  216. package/src/renderer/views.js +28 -0
  217. package/src/rendertarget/canvastarget.js +30 -0
  218. package/src/rendertarget/image.js +132 -0
  219. package/src/rendertarget/index.js +3 -0
  220. package/src/rendertarget/rendertarget.js +89 -0
  221. package/src/shader/basicFragment.glsl +30 -0
  222. package/src/shader/basicVertex.glsl +87 -0
  223. package/src/shader/common/color.glsl +7 -0
  224. package/src/shader/common/common.glsl +25 -0
  225. package/src/shader/common/index.js +4 -0
  226. package/src/shader/common/light.glsl +437 -0
  227. package/src/shader/common/math.glsl +12 -0
  228. package/src/shader/debug/index.js +2 -0
  229. package/src/shader/debug/skeletonFragment.glsl +8 -0
  230. package/src/shader/debug/skeletonVertex.glsl +27 -0
  231. package/src/shader/depthFragment.glsl +37 -0
  232. package/src/shader/index.js +11 -0
  233. package/src/shader/lambertFragment.glsl +126 -0
  234. package/src/shader/normalFragment.glsl +25 -0
  235. package/src/shader/phongFragment.glsl +140 -0
  236. package/src/shader/skyboxFragment.glsl +16 -0
  237. package/src/shader/skyboxVertex.glsl +20 -0
  238. package/src/shader/standardFragment.glsl +274 -0
  239. package/src/texture/index.js +2 -0
  240. package/src/texture/sampler.js +111 -0
  241. package/src/texture/texture.js +234 -0
  242. package/src/utils/index.js +115 -0
  243. package/tsconfig.json +11 -0
  244. package/website/config/index.ts +1 -0
  245. package/website/config/navigation.ts +53 -0
  246. package/website/content.config.ts +92 -0
  247. package/website/layouts/DocLayout.astro +501 -0
  248. package/website/layouts/Example.astro +91 -0
  249. package/website/pages/examples/[...slug].astro +77 -0
  250. package/website/pages/examples/index.astro +98 -0
  251. package/website/pages/examples/samples/[...slug].astro +17 -0
  252. package/website/pages/guide/[slug].astro +30 -0
  253. package/website/pages/guide/index.astro +21 -0
  254. package/website/pages/index.astro +9 -0
  255. package/website/plugins/remark-link-base.js +23 -0
  256. package/website/utils/url.ts +30 -0
@@ -0,0 +1,126 @@
1
+ precision mediump float;
2
+ precision mediump sampler2DArray;
3
+
4
+ #include <math>
5
+ #include <common>
6
+ #include <light>
7
+
8
+ struct LambertMaterial {
9
+ vec4 color;
10
+ };
11
+
12
+ in vec3 v_position;
13
+ #ifdef VERTEX_UVS
14
+ in vec2 v_uv;
15
+ #endif
16
+ #ifdef VERTEX_NORMALS
17
+ in vec3 v_normal;
18
+ #endif
19
+
20
+ uniform MaterialBlock {
21
+ LambertMaterial material;
22
+ };
23
+ // Lights
24
+ uniform AmbientLightBlock {
25
+ AmbientLight ambient_light;
26
+ };
27
+ uniform DirectionalLightBlock {
28
+ DirectionalLights directional_lights;
29
+ };
30
+ uniform PointLightBlock {
31
+ PointLights point_lights;
32
+ };
33
+ uniform SpotLightBlock {
34
+ SpotLights spot_lights;
35
+ };
36
+
37
+ #ifdef MAX_SHADOW_CASTERS
38
+ uniform ShadowCasterBlock {
39
+ Shadow shadow_casters[MAX_SHADOW_CASTERS];
40
+ };
41
+ uniform sampler2DArray shadow_atlas;
42
+ #endif
43
+ uniform sampler2D mainTexture;
44
+ out vec4 fragment_color;
45
+
46
+ void main(){
47
+ vec3 base_color = material.color.rgb;
48
+
49
+ #ifdef VERTEX_UVS
50
+ vec4 sample_color = texture(mainTexture,v_uv);
51
+ base_color *= sample_color.rgb;
52
+ #endif
53
+ #ifdef VERTEX_NORMALS
54
+ vec3 normal = normalize(v_normal);
55
+ #else
56
+ #error "Mesh vertex normals are required for lighting."
57
+ #endif
58
+ float opacity = material.color.a;
59
+ int directional_light_count = min(directional_lights.count,MAX_DIRECTIONAL_LIGHTS);
60
+ int point_light_count = min(point_lights.count,MAX_POINT_LIGHTS);
61
+ int spot_light_count = min(spot_lights.count,MAX_SPOT_LIGHTS);
62
+
63
+ vec3 ambient = ambient_light.color.rgb * ambient_light.intensity;
64
+
65
+ vec3 total_exitance = vec3(0.0, 0.0, 0.0);
66
+ for (int i = 0; i < directional_light_count; i++) {
67
+ DirectionalLight light = directional_lights.lights[i];
68
+ //Remember you set the dir to negative because direction to light is the opposite direction of dir.
69
+ vec3 light_direction = -light.direction;
70
+ float brightness = calculate_brightness(normal, light_direction);
71
+ vec3 irradiance = light.color.rgb * brightness * light.intensity;
72
+
73
+ #ifdef MAX_SHADOW_CASTERS
74
+ if(light.shadow_index != -1){
75
+ Shadow shadow = shadow_casters[light.shadow_index];
76
+
77
+ irradiance *= shadow_contribution_2d(shadow, shadow_atlas, v_position, brightness);
78
+ }
79
+ #endif
80
+ total_exitance += base_color * irradiance;
81
+ }
82
+
83
+ for (int i = 0; i < point_light_count; i++) {
84
+ PointLight light = point_lights.lights[i];
85
+ vec3 distance_vector = light.position - v_position;
86
+ float distance = length(distance_vector);
87
+ vec3 direction = distance_vector / distance;
88
+ float attenuation = attenuate_point_light(distance, light.radius, light.intensity, light.decay);
89
+ float brightness = calculate_brightness(normal, direction);
90
+ vec3 irradiance = light.color.rgb * attenuation;
91
+
92
+ #ifdef MAX_SHADOW_CASTERS
93
+ if(light.shadow_index != -1){
94
+ Shadow shadow = shadow_casters[light.shadow_index];
95
+
96
+ irradiance *= shadow_contribution_cube(shadow, shadow_atlas, v_position, brightness);
97
+ }
98
+ #endif
99
+
100
+ total_exitance += base_color * brightness * irradiance;
101
+ }
102
+
103
+ for (int i = 0; i < spot_light_count; i++) {
104
+ SpotLight light = spot_lights.lights[i];
105
+ vec3 distance_vector = light.position - v_position;
106
+ float distance = length(distance_vector);
107
+ vec3 direction = distance_vector / distance;
108
+ float attenuation = attenuate_spot_light(light, direction, distance);
109
+ float brightness = calculate_brightness(normal, direction);
110
+ vec3 irradiance = light.color.rgb * attenuation;
111
+
112
+ #ifdef MAX_SHADOW_CASTERS
113
+ if(light.shadow_index != -1){
114
+ Shadow shadow = shadow_casters[light.shadow_index];
115
+
116
+ irradiance *= shadow_contribution_2d(shadow, shadow_atlas, v_position, brightness);
117
+ }
118
+ #endif
119
+
120
+ total_exitance += base_color * brightness * irradiance;
121
+ }
122
+
123
+ vec3 final_color = ambient * base_color + total_exitance;
124
+
125
+ fragment_color = vec4(final_color,opacity);
126
+ }
@@ -0,0 +1,25 @@
1
+ precision mediump float;
2
+ precision mediump sampler2DArray;
3
+
4
+ #include <common>
5
+
6
+ struct NormalMaterial {
7
+ vec4 padding;
8
+ };
9
+
10
+ in vec3 v_normal;
11
+
12
+ out vec4 fragment_color;
13
+
14
+ uniform MaterialBlock {
15
+ NormalMaterial material;
16
+ };
17
+
18
+ void main(){
19
+ #ifdef VERTEX_NORMALS
20
+ vec3 normal = normalize(v_normal);
21
+ #else
22
+ #error "Mesh vertex normals are required for lighting."
23
+ #endif
24
+ fragment_color = vec4(normal, 1.0);
25
+ }
@@ -0,0 +1,140 @@
1
+ precision mediump float;
2
+ precision mediump sampler2DArray;
3
+
4
+ #include <common>
5
+ #include <math>
6
+ #include <light>
7
+
8
+ struct PhongMaterial {
9
+ vec4 color;
10
+ float specularShininess;
11
+ float specularStrength;
12
+ };
13
+
14
+ in vec3 v_position;
15
+ #ifdef VERTEX_UVS
16
+ in vec2 v_uv;
17
+ #endif
18
+ in vec3 v_normal;
19
+ in vec3 cam_direction;
20
+
21
+ uniform MaterialBlock {
22
+ PhongMaterial material;
23
+ };
24
+ uniform AmbientLightBlock {
25
+ AmbientLight ambient_light;
26
+ };
27
+ uniform DirectionalLightBlock {
28
+ DirectionalLights directional_lights;
29
+ };
30
+ uniform PointLightBlock {
31
+ PointLights point_lights;
32
+ };
33
+ uniform SpotLightBlock {
34
+ SpotLights spot_lights;
35
+ };
36
+ #ifdef MAX_SHADOW_CASTERS
37
+ uniform ShadowCasterBlock {
38
+ Shadow shadow_casters[MAX_SHADOW_CASTERS];
39
+ };
40
+ uniform sampler2DArray shadow_atlas;
41
+ #endif
42
+ uniform sampler2D mainTexture;
43
+
44
+ out vec4 fragment_color;
45
+
46
+ void main(){
47
+ vec3 base_color = material.color.rgb;
48
+ #ifdef VERTEX_UVS
49
+ vec4 sample_color = texture(mainTexture,v_uv);
50
+ base_color *= sample_color.rgb;
51
+ #endif
52
+ #ifdef VERTEX_NORMALS
53
+ vec3 normal = normalize(v_normal);
54
+ #else
55
+ #error "Mesh vertex normals are required for lighting."
56
+ #endif
57
+ vec3 view_direction = normalize(cam_direction);
58
+ float opacity = material.color.a;
59
+ int directional_light_count = min(directional_lights.count,MAX_DIRECTIONAL_LIGHTS);
60
+ int point_light_count = min(point_lights.count,MAX_POINT_LIGHTS);
61
+ int spot_light_count = min(spot_lights.count,MAX_SPOT_LIGHTS);
62
+
63
+ vec3 total_exitance = vec3(0.0);
64
+ for (int i = 0; i < directional_light_count; i++) {
65
+ DirectionalLight light = directional_lights.lights[i];
66
+ vec3 reflection_direction = reflect(light.direction, normal);
67
+ vec3 irradiance = light.color.rgb;
68
+ //Remember you set the dir to negative because light direction is the opposite direction of dir.
69
+ float diffuse_brightness = calculate_brightness(normal,-light.direction);
70
+
71
+ #ifdef MAX_SHADOW_CASTERS
72
+ if(light.shadow_index != -1){
73
+ Shadow shadow = shadow_casters[light.shadow_index];
74
+
75
+ irradiance *= shadow_contribution_2d(shadow, shadow_atlas, v_position, diffuse_brightness);
76
+ }
77
+ #endif
78
+
79
+ vec3 diffuse = base_color * irradiance * diffuse_brightness * light.intensity;
80
+
81
+ float specular_brightness = calculate_brightness(reflection_direction,view_direction);
82
+ vec3 specular = pow(specular_brightness,material.specularShininess) * irradiance * material.specularStrength;
83
+
84
+ total_exitance += specular + diffuse;
85
+ }
86
+ for (int i = 0; i < point_light_count; i++) {
87
+ PointLight light = point_lights.lights[i];
88
+ vec3 distance_vector = light.position - v_position;
89
+ float distance = length(distance_vector);
90
+ vec3 direction = distance_vector / distance;
91
+ float attenuation = attenuate_point_light(distance, light.radius, light.intensity, light.decay);
92
+ float brightness = calculate_brightness(normal, direction);
93
+ vec3 irradiance = light.color.rgb * attenuation;
94
+
95
+ #ifdef MAX_SHADOW_CASTERS
96
+ if(light.shadow_index != -1){
97
+ Shadow shadow = shadow_casters[light.shadow_index];
98
+
99
+ irradiance *= shadow_contribution_cube(shadow, shadow_atlas, v_position, brightness);
100
+ }
101
+ #endif
102
+
103
+ vec3 reflection_direction = reflect(direction, normal);
104
+ vec3 diffuse_exitance = base_color * brightness * irradiance;
105
+
106
+ float specular_brightness = calculate_brightness(reflection_direction, -view_direction);
107
+ vec3 specular_exitance = pow(specular_brightness,material.specularShininess) * irradiance * material.specularStrength;
108
+ total_exitance += specular_exitance + diffuse_exitance;
109
+ }
110
+
111
+ for (int i = 0; i < spot_light_count; i++) {
112
+ SpotLight light = spot_lights.lights[i];
113
+ vec3 distance_vector = light.position - v_position;
114
+ float distance = length(distance_vector);
115
+ vec3 direction = distance_vector / distance;
116
+ float attenuation = attenuate_spot_light(light, direction, distance);
117
+ float brightness = calculate_brightness(normal, direction);
118
+ vec3 irradiance = light.color.rgb * attenuation;
119
+
120
+ #ifdef MAX_SHADOW_CASTERS
121
+ if(light.shadow_index != -1){
122
+ Shadow shadow = shadow_casters[light.shadow_index];
123
+
124
+ irradiance *= shadow_contribution_2d(shadow, shadow_atlas, v_position, brightness);
125
+ }
126
+ #endif
127
+
128
+ vec3 reflection_direction = reflect(direction, normal);
129
+ vec3 diffuse_exitance = base_color * brightness * irradiance;
130
+ float specular_brightness = calculate_brightness(reflection_direction, -view_direction);
131
+ vec3 specular_exitance = pow(specular_brightness,material.specularShininess) * irradiance * material.specularStrength;
132
+
133
+ total_exitance += specular_exitance + diffuse_exitance;
134
+ }
135
+
136
+ vec3 ambient = base_color * ambient_light.color.rgb * ambient_light.intensity;
137
+ vec3 final_color = ambient + total_exitance;
138
+
139
+ fragment_color = vec4(final_color, opacity);
140
+ }
@@ -0,0 +1,16 @@
1
+ precision mediump float;
2
+ precision mediump sampler2DArray;
3
+
4
+ #include <common>
5
+
6
+ in highp vec3 v_uv;
7
+
8
+ uniform float lerp;
9
+ uniform samplerCube day;
10
+ uniform samplerCube night;
11
+
12
+ out vec4 fragment_color;
13
+
14
+ void main(){
15
+ fragment_color = mix(texture(day, v_uv), texture(night,v_uv), lerp);
16
+ }
@@ -0,0 +1,20 @@
1
+ precision mediump float;
2
+
3
+ #include <common>
4
+
5
+ in vec3 position;
6
+
7
+ uniform CameraBlock {
8
+ Camera camera;
9
+ };
10
+ uniform mat4 model;
11
+
12
+ out highp vec3 v_uv;
13
+
14
+ void main(){
15
+ mat4 view = camera.view;
16
+ view[3].xyz = vec3(0.0);
17
+ v_uv = position;
18
+ gl_Position = camera.projection * view * model * vec4(position.xyz, 1.0);
19
+ gl_Position = gl_Position.xyww;
20
+ }
@@ -0,0 +1,274 @@
1
+ precision mediump float;
2
+ precision mediump sampler2DArray;
3
+
4
+ #include <math>
5
+ #include <color>
6
+ #include <common>
7
+ #include <light>
8
+
9
+ struct PBRInput {
10
+ float NdotL;
11
+ float NdotV;
12
+ float NdotH;
13
+ float HdotV;
14
+ };
15
+
16
+ struct PBRProperties {
17
+ vec3 normal;
18
+ vec3 albedo;
19
+ vec3 emissive;
20
+ float opacity;
21
+ float metallic;
22
+ float roughness;
23
+ float ambient_occlusion;
24
+ };
25
+
26
+ struct StandardMaterial {
27
+ vec4 color;
28
+ float metallic;
29
+ float roughness;
30
+ float ambient_occlusion_strength;
31
+ vec3 emissive_color;
32
+ float emissive_intensity;
33
+ };
34
+
35
+ in vec3 v_position;
36
+ #ifdef VERTEX_UVS
37
+ in vec2 v_uv;
38
+ #endif
39
+ #ifdef VERTEX_NORMALS
40
+ in vec3 v_normal;
41
+ #endif
42
+ #ifdef VERTEX_TANGENTS
43
+ in vec3 v_tangent;
44
+ #endif
45
+ in vec3 cam_direction;
46
+
47
+ uniform MaterialBlock {
48
+ StandardMaterial material;
49
+ };
50
+ uniform AmbientLightBlock {
51
+ AmbientLight ambient_light;
52
+ };
53
+ uniform DirectionalLightBlock {
54
+ DirectionalLights directional_lights;
55
+ };
56
+ uniform PointLightBlock {
57
+ PointLights point_lights;
58
+ };
59
+ uniform SpotLightBlock {
60
+ SpotLights spot_lights;
61
+ };
62
+ #ifdef MAX_SHADOW_CASTERS
63
+ uniform ShadowCasterBlock {
64
+ Shadow shadow_casters[MAX_SHADOW_CASTERS];
65
+ };
66
+ uniform sampler2DArray shadow_atlas;
67
+ #endif
68
+ uniform sampler2D mainTexture;
69
+ uniform sampler2D normal_texture;
70
+ uniform sampler2D occlusion_texture;
71
+ uniform sampler2D roughness_texture;
72
+ uniform sampler2D metallic_texture;
73
+ uniform sampler2D emissive_texture;
74
+
75
+ out vec4 fragment_color;
76
+
77
+ vec3 fresnel_schlick(float HdotV, vec3 F0){
78
+ return F0 + (1.0 - F0) * pow(clamp(1.0 - HdotV, 0.0, 1.0), 5.0);
79
+ }
80
+
81
+ // Also the Trowbridge-Rietz normal distribution function
82
+ float GGX_normal_distribution(float NdotH, float roughness){
83
+ float a = roughness * roughness;
84
+ float a2 = a * a;
85
+ float NdotH2 = NdotH * NdotH;
86
+ float denom = (NdotH2 * (a2 - 1.0) + 1.0);
87
+ denom = PI * denom * denom;
88
+
89
+ return a2 / denom;
90
+ }
91
+
92
+ float geometry_schlickGGX(float NdotV, float roughness){
93
+ float r = (roughness + 1.0);
94
+ float k = (r * r) / 8.0;
95
+ float denom = NdotV * (1.0 - k) + k;
96
+
97
+ return NdotV / denom;
98
+ }
99
+
100
+ float geometry_smith(float NdotV, float NdotL, float roughness){
101
+ float ggx2 = geometry_schlickGGX(NdotV, roughness);
102
+ float ggx1 = geometry_schlickGGX(NdotL, roughness);
103
+
104
+ return ggx1 * ggx2;
105
+ }
106
+
107
+ vec3 cook_torrance_BRDF(PBRProperties pbr_properties, PBRInput pbr_input){
108
+ vec3 F0 = mix(vec3(0.04), pbr_properties.albedo, pbr_properties.metallic);
109
+ vec3 F = fresnel_schlick(pbr_input.HdotV, F0);
110
+
111
+ float NDF = GGX_normal_distribution(pbr_input.NdotH, pbr_properties.roughness);
112
+ float G = geometry_smith(pbr_input.NdotV, pbr_input.NdotL, pbr_properties.roughness);
113
+
114
+ vec3 numerator = NDF * G * F;
115
+ float denominator = 4.0 * pbr_input.NdotV * pbr_input.NdotL + 0.0001;
116
+ vec3 specular = numerator / denominator;
117
+ vec3 kS = F;
118
+ vec3 kD = (vec3(1.0) - kS) * (1.0 - pbr_properties.metallic);
119
+
120
+ return (kD * pbr_properties.albedo / PI + specular);
121
+ }
122
+
123
+ PBRInput calculate_pbr_input(vec3 N, vec3 V, vec3 L, vec3 H){
124
+ PBRInput pbr_input;
125
+
126
+ pbr_input.NdotV = max(dot(N, V), 0.0);
127
+ pbr_input.NdotL = max(dot(N, L), 0.0);
128
+ pbr_input.NdotH = max(dot(N, H), 0.0);
129
+ pbr_input.HdotV = max(dot(H, V), 0.0);
130
+
131
+ return pbr_input;
132
+ }
133
+
134
+ PBRProperties calculate_pbr_properties(){
135
+ PBRProperties properties;
136
+
137
+ properties.albedo = material.color.rgb;
138
+ properties.emissive = material.emissive_color;
139
+ properties.opacity = material.color.a;
140
+ properties.metallic = material.metallic;
141
+ properties.roughness = material.roughness;
142
+ properties.ambient_occlusion = 1.0;
143
+
144
+ #ifdef VERTEX_UVS
145
+ vec4 albedo_texture_color = texture(mainTexture,v_uv);
146
+ properties.albedo *= quick_sRGB_to_linear(albedo_texture_color.rgb);
147
+ properties.opacity *= albedo_texture_color.a;
148
+
149
+ vec4 metallic_texture_color = texture(metallic_texture,v_uv);
150
+ properties.metallic *= metallic_texture_color.b;
151
+
152
+ vec4 roughness_texture_color = texture(roughness_texture,v_uv);
153
+ properties.roughness *= roughness_texture_color.g;
154
+
155
+ vec4 occlusion_texture_color = texture(occlusion_texture,v_uv);
156
+ properties.ambient_occlusion = mix(1.0,occlusion_texture_color.r, material.ambient_occlusion_strength);
157
+
158
+ vec4 emissive_texture_color = texture(emissive_texture,v_uv);
159
+ properties.emissive *= emissive_texture_color.rgb;
160
+ #endif
161
+
162
+ properties.metallic = clamp(properties.metallic, 0.0, 1.0);
163
+ properties.roughness = clamp(properties.roughness, 0.05, 1.0);
164
+
165
+ #if defined(VERTEX_TANGENTS)
166
+ #ifdef VERTEX_NORMALS
167
+ vec3 normal = normalize(v_normal);
168
+ #else
169
+ #error "Mesh vertex normals are required for lighting."
170
+ #endif
171
+ #ifdef VERTEX_UVS
172
+ vec3 tangent = normalize(v_tangent);
173
+ vec3 bitangent = cross(normal, tangent);
174
+ mat3 tangent_space = mat3(tangent, bitangent, normal);
175
+ vec3 surface_normal = texture(normal_texture, v_uv).rgb * 2.0 - 1.0;
176
+ properties.normal = tangent_space * surface_normal;
177
+ #else
178
+ properties.normal = normal;
179
+ #endif
180
+ #else
181
+ #ifdef VERTEX_NORMALS
182
+ properties.normal = v_normal;
183
+ #else
184
+ #error "Mesh vertex normals are required for lighting."
185
+ #endif
186
+ #endif
187
+
188
+ return properties;
189
+ }
190
+
191
+ void main(){
192
+ PBRProperties pbr_properties = calculate_pbr_properties();
193
+ vec3 N = pbr_properties.normal;
194
+ vec3 V = normalize(cam_direction);
195
+ int directional_light_count = min(directional_lights.count,MAX_DIRECTIONAL_LIGHTS);
196
+ int point_light_count = min(point_lights.count, MAX_POINT_LIGHTS);
197
+ int spot_light_count = min(spot_lights.count,MAX_SPOT_LIGHTS);
198
+
199
+ vec3 exitance = vec3(0.0);
200
+ for (int i = 0; i < directional_light_count; i++) {
201
+ DirectionalLight light = directional_lights.lights[i];
202
+ vec3 L = -light.direction;
203
+ vec3 H = normalize(L + V);
204
+ PBRInput pbr_input = calculate_pbr_input(N, V, L, H);
205
+ vec3 irradiance = light.color.rgb * light.intensity;
206
+
207
+ #ifdef MAX_SHADOW_CASTERS
208
+ if(light.shadow_index != -1){
209
+ Shadow shadow = shadow_casters[light.shadow_index];
210
+
211
+ irradiance *= shadow_contribution_2d(shadow, shadow_atlas, v_position, pbr_input.NdotL);
212
+ }
213
+ #endif
214
+ exitance += cook_torrance_BRDF(pbr_properties, pbr_input) * irradiance * pbr_input.NdotL;
215
+ }
216
+
217
+ for (int i = 0; i < point_light_count; i++) {
218
+ PointLight light = point_lights.lights[i];
219
+ vec3 normal = N;
220
+ vec3 distance_vector = light.position - v_position;
221
+ float distance = length(distance_vector);
222
+ vec3 L = distance_vector / distance;
223
+ vec3 H = normalize(L + V);
224
+ PBRInput pbr_input = calculate_pbr_input(N, V, L, H);
225
+ float attenuation = attenuate_point_light(distance, light.radius, light.intensity, light.decay);
226
+ vec3 irradiance = light.color.rgb * attenuation;
227
+
228
+ #ifdef MAX_SHADOW_CASTERS
229
+ if(light.shadow_index != -1){
230
+ Shadow shadow = shadow_casters[light.shadow_index];
231
+
232
+ irradiance *= shadow_contribution_cube(shadow, shadow_atlas, v_position, pbr_input.NdotL);
233
+ }
234
+ #endif
235
+
236
+ exitance += cook_torrance_BRDF(pbr_properties, pbr_input) * irradiance * pbr_input.NdotL;
237
+ }
238
+
239
+ for (int i = 0; i < spot_light_count; i++) {
240
+ SpotLight light = spot_lights.lights[i];
241
+ vec3 normal = N;
242
+ vec3 distance_vector = light.position - v_position;
243
+ float distance = length(distance_vector);
244
+ vec3 L = distance_vector / distance;
245
+ vec3 H = normalize(L + V);
246
+ PBRInput pbr_input = calculate_pbr_input(N, V, L, H);
247
+ float attenuation = attenuate_spot_light(light, L, distance);
248
+ vec3 irradiance = light.color.rgb * attenuation;
249
+
250
+ #ifdef MAX_SHADOW_CASTERS
251
+ if(light.shadow_index != -1){
252
+ Shadow shadow = shadow_casters[light.shadow_index];
253
+
254
+ irradiance *= shadow_contribution_2d(shadow, shadow_atlas, v_position, pbr_input.NdotL);
255
+ }
256
+ #endif
257
+
258
+ exitance += cook_torrance_BRDF(pbr_properties, pbr_input) * irradiance * pbr_input.NdotL;
259
+ }
260
+
261
+ vec3 emissive_exitance = pbr_properties.emissive * material.emissive_intensity;
262
+ vec3 ambient_exitance = pbr_properties.albedo * ambient_light.color.rgb * ambient_light.intensity * pbr_properties.ambient_occlusion;
263
+ vec3 final_color = emissive_exitance + ambient_exitance + exitance;
264
+
265
+ // tonemapping output
266
+ // this is temporary until a post processing step is introduced
267
+ final_color = final_color / (final_color + vec3(1.0));
268
+
269
+ // gamma correction
270
+ // this is temporary until a post processing step is introduced
271
+ final_color = quick_linear_to_sRGB(final_color);
272
+
273
+ fragment_color = vec4(final_color, pbr_properties.opacity);
274
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./texture.js"
2
+ export * from "./sampler.js"