ris-framework 0.1.0 → 0.2.0-dev.11

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 (217) hide show
  1. package/CHANGELOG.md +49 -1
  2. package/README.md +15 -4
  3. package/dist/Framework.d.ts +9 -3
  4. package/dist/Framework.d.ts.map +1 -1
  5. package/dist/api/IFramework.d.ts +7 -6
  6. package/dist/api/IFramework.d.ts.map +1 -1
  7. package/dist/api/_export.d.ts +2 -0
  8. package/dist/api/_export.d.ts.map +1 -1
  9. package/dist/api/camera/CameraFactory.d.ts +3 -2
  10. package/dist/api/camera/CameraFactory.d.ts.map +1 -1
  11. package/dist/api/camera/ICamera.d.ts +5 -0
  12. package/dist/api/camera/ICamera.d.ts.map +1 -1
  13. package/dist/api/camera/ICameraFactory.d.ts +5 -6
  14. package/dist/api/camera/ICameraFactory.d.ts.map +1 -1
  15. package/dist/api/camera/OrthographicCamera.d.ts +7 -0
  16. package/dist/api/camera/OrthographicCamera.d.ts.map +1 -1
  17. package/dist/api/camera/PerspectiveCamera.d.ts +30 -6
  18. package/dist/api/camera/PerspectiveCamera.d.ts.map +1 -1
  19. package/dist/api/content/ContentManager.d.ts +67 -2
  20. package/dist/api/content/ContentManager.d.ts.map +1 -1
  21. package/dist/api/content/IContentManager.d.ts +32 -1
  22. package/dist/api/content/IContentManager.d.ts.map +1 -1
  23. package/dist/api/content/ShaderModuleContent.d.ts.map +1 -1
  24. package/dist/api/data/Color.d.ts +5 -0
  25. package/dist/api/data/Color.d.ts.map +1 -1
  26. package/dist/api/geometry/BaseGeometry.d.ts +9 -0
  27. package/dist/api/geometry/BaseGeometry.d.ts.map +1 -1
  28. package/dist/api/geometry/GeometryBuilder.d.ts +3 -1
  29. package/dist/api/geometry/GeometryBuilder.d.ts.map +1 -1
  30. package/dist/api/geometry/IGeometryBuilder.d.ts +24 -0
  31. package/dist/api/geometry/IGeometryBuilder.d.ts.map +1 -1
  32. package/dist/api/geometry/_export-geometry.d.ts +1 -0
  33. package/dist/api/geometry/_export-geometry.d.ts.map +1 -1
  34. package/dist/api/input/InputManager.d.ts +4 -0
  35. package/dist/api/input/InputManager.d.ts.map +1 -1
  36. package/dist/api/lights/AmbientLight.d.ts +35 -0
  37. package/dist/api/lights/AmbientLight.d.ts.map +1 -0
  38. package/dist/api/lights/DirectionalLight.d.ts +40 -0
  39. package/dist/api/lights/DirectionalLight.d.ts.map +1 -0
  40. package/dist/api/lights/LightFactory.d.ts +18 -0
  41. package/dist/api/lights/LightFactory.d.ts.map +1 -0
  42. package/dist/api/lights/LightId.d.ts +6 -0
  43. package/dist/api/lights/LightId.d.ts.map +1 -0
  44. package/dist/api/lights/LightsBuffer.d.ts +70 -0
  45. package/dist/api/lights/LightsBuffer.d.ts.map +1 -0
  46. package/dist/api/lights/PointLight.d.ts +59 -0
  47. package/dist/api/lights/PointLight.d.ts.map +1 -0
  48. package/dist/api/lights/_export.d.ts +6 -0
  49. package/dist/api/lights/_export.d.ts.map +1 -0
  50. package/dist/api/loaders/ObjGeometryMapper.d.ts +19 -0
  51. package/dist/api/loaders/ObjGeometryMapper.d.ts.map +1 -0
  52. package/dist/api/loaders/_export.d.ts +1 -0
  53. package/dist/api/loaders/_export.d.ts.map +1 -1
  54. package/dist/api/loaders/obj/ObjLoader.d.ts +29 -0
  55. package/dist/api/loaders/obj/ObjLoader.d.ts.map +1 -0
  56. package/dist/api/loaders/obj/ObjTypes.d.ts +103 -0
  57. package/dist/api/loaders/obj/ObjTypes.d.ts.map +1 -0
  58. package/dist/api/loaders/obj/index.d.ts +3 -0
  59. package/dist/api/loaders/obj/index.d.ts.map +1 -0
  60. package/dist/api/material/ForwardBlinnPhongMaterial.d.ts +127 -0
  61. package/dist/api/material/ForwardBlinnPhongMaterial.d.ts.map +1 -0
  62. package/dist/api/material/IMaterial.d.ts +15 -6
  63. package/dist/api/material/IMaterial.d.ts.map +1 -1
  64. package/dist/api/material/IMaterialFactory.d.ts +17 -2
  65. package/dist/api/material/IMaterialFactory.d.ts.map +1 -1
  66. package/dist/api/material/InspectTextureMipsMaterial.d.ts +16 -7
  67. package/dist/api/material/InspectTextureMipsMaterial.d.ts.map +1 -1
  68. package/dist/api/material/MaterialFactory.d.ts +6 -0
  69. package/dist/api/material/MaterialFactory.d.ts.map +1 -1
  70. package/dist/api/material/SpriteMaterial.d.ts +51 -0
  71. package/dist/api/material/SpriteMaterial.d.ts.map +1 -0
  72. package/dist/api/material/UnlitMaterial.d.ts +28 -10
  73. package/dist/api/material/UnlitMaterial.d.ts.map +1 -1
  74. package/dist/api/material/_export.d.ts +2 -0
  75. package/dist/api/material/_export.d.ts.map +1 -1
  76. package/dist/api/meshes/IMesh.d.ts +35 -5
  77. package/dist/api/meshes/IMesh.d.ts.map +1 -1
  78. package/dist/api/meshes/IMeshFactory.d.ts +25 -4
  79. package/dist/api/meshes/IMeshFactory.d.ts.map +1 -1
  80. package/dist/api/meshes/Mesh.d.ts +58 -6
  81. package/dist/api/meshes/Mesh.d.ts.map +1 -1
  82. package/dist/api/meshes/MeshFactory.d.ts +7 -3
  83. package/dist/api/meshes/MeshFactory.d.ts.map +1 -1
  84. package/dist/api/meshes/QuadMesh.d.ts +5 -1
  85. package/dist/api/meshes/QuadMesh.d.ts.map +1 -1
  86. package/dist/api/meshes/SubMesh.d.ts +47 -0
  87. package/dist/api/meshes/SubMesh.d.ts.map +1 -0
  88. package/dist/api/meshes/_export.d.ts +1 -0
  89. package/dist/api/meshes/_export.d.ts.map +1 -1
  90. package/dist/api/rendering/render-pipelines/IForwardBlinnPhongRenderPipeline.d.ts +56 -0
  91. package/dist/api/rendering/render-pipelines/IForwardBlinnPhongRenderPipeline.d.ts.map +1 -0
  92. package/dist/api/rendering/render-pipelines/IRenderPipeline.d.ts +3 -0
  93. package/dist/api/rendering/render-pipelines/IRenderPipeline.d.ts.map +1 -1
  94. package/dist/api/rendering/render-pipelines/IRenderPipelineFactory.d.ts +29 -0
  95. package/dist/api/rendering/render-pipelines/IRenderPipelineFactory.d.ts.map +1 -1
  96. package/dist/api/rendering/render-pipelines/_export-render-pipelines.d.ts +1 -0
  97. package/dist/api/rendering/render-pipelines/_export-render-pipelines.d.ts.map +1 -1
  98. package/dist/api/scene/Scene.d.ts +104 -0
  99. package/dist/api/scene/Scene.d.ts.map +1 -0
  100. package/dist/api/shader/IShaderModuleLoader.d.ts +4 -1
  101. package/dist/api/shader/IShaderModuleLoader.d.ts.map +1 -1
  102. package/dist/api/shader/{shader-loader.d.ts → ShaderLoader.d.ts} +1 -1
  103. package/dist/api/shader/ShaderLoader.d.ts.map +1 -0
  104. package/dist/api/sprites/SpriteBatchDrawable.d.ts +1 -1
  105. package/dist/api/sprites/SpriteBatchDrawable.d.ts.map +1 -1
  106. package/dist/api/sprites/SpriteBatchMesh.d.ts +14 -1
  107. package/dist/api/sprites/SpriteBatchMesh.d.ts.map +1 -1
  108. package/dist/api/utilities/GeometryUtilities.d.ts.map +1 -1
  109. package/dist/api/window/WindowManager.d.ts +4 -0
  110. package/dist/api/window/WindowManager.d.ts.map +1 -1
  111. package/dist/core/rendering/vertex-buffer-layout.d.ts +1 -0
  112. package/dist/core/rendering/vertex-buffer-layout.d.ts.map +1 -1
  113. package/dist/index.js +2252 -260
  114. package/dist/webgl/WebGlGraphicsDevice.d.ts.map +1 -1
  115. package/dist/webgl/lights/WebGlAmbientLightFactory.d.ts +17 -0
  116. package/dist/webgl/lights/WebGlAmbientLightFactory.d.ts.map +1 -0
  117. package/dist/webgl/lights/WebGlLightsBuffer.d.ts +55 -0
  118. package/dist/webgl/lights/WebGlLightsBuffer.d.ts.map +1 -0
  119. package/dist/webgl/render-pipelines/AWebGlRenderPipeline.d.ts +3 -1
  120. package/dist/webgl/render-pipelines/AWebGlRenderPipeline.d.ts.map +1 -1
  121. package/dist/webgl/render-pipelines/WebGlMainRenderTargetRenderPipeline.d.ts +3 -0
  122. package/dist/webgl/render-pipelines/WebGlMainRenderTargetRenderPipeline.d.ts.map +1 -1
  123. package/dist/webgl/render-pipelines/WebGlRenderPipelineFactory.d.ts +3 -1
  124. package/dist/webgl/render-pipelines/WebGlRenderPipelineFactory.d.ts.map +1 -1
  125. package/dist/webgl/render-pipelines/inspect/WebGlInspectTextureMipsRenderPipeline.d.ts +3 -1
  126. package/dist/webgl/render-pipelines/inspect/WebGlInspectTextureMipsRenderPipeline.d.ts.map +1 -1
  127. package/dist/webgl/render-pipelines/material/WebGlBlinnPhongRenderPipeline.d.ts +88 -0
  128. package/dist/webgl/render-pipelines/material/WebGlBlinnPhongRenderPipeline.d.ts.map +1 -0
  129. package/dist/webgl/render-pipelines/material/WebGlUnlitRenderPipeline.d.ts +3 -1
  130. package/dist/webgl/render-pipelines/material/WebGlUnlitRenderPipeline.d.ts.map +1 -1
  131. package/dist/webgl/render-pipelines/sprite/WebGlSpriteRenderPipeline.d.ts +3 -1
  132. package/dist/webgl/render-pipelines/sprite/WebGlSpriteRenderPipeline.d.ts.map +1 -1
  133. package/dist/webgl/shader/WebGlShaderModule.d.ts +4 -1
  134. package/dist/webgl/shader/WebGlShaderModule.d.ts.map +1 -1
  135. package/dist/webgl/shader/WebGlShaderModuleLoader.d.ts +16 -5
  136. package/dist/webgl/shader/WebGlShaderModuleLoader.d.ts.map +1 -1
  137. package/dist/webgl/texture/WebGlTexture2D.d.ts +8 -1
  138. package/dist/webgl/texture/WebGlTexture2D.d.ts.map +1 -1
  139. package/dist/webgl/utilities/webgl-filter-anisotropic-utilities.d.ts +0 -1
  140. package/dist/webgl/utilities/webgl-filter-anisotropic-utilities.d.ts.map +1 -1
  141. package/package.json +4 -4
  142. package/shaders/glsl/forward_blinn_phong_material_fs.glsl +110 -0
  143. package/shaders/glsl/forward_blinn_phong_material_vs.glsl +32 -0
  144. package/shaders/glsl/sprite_fs.glsl +1 -1
  145. package/shaders/glsl/unlit_material_fs.glsl +1 -1
  146. package/src/Framework.ts +37 -13
  147. package/src/api/IFramework.ts +8 -6
  148. package/src/api/_export.ts +2 -0
  149. package/src/api/camera/CameraFactory.ts +6 -6
  150. package/src/api/camera/ICamera.ts +6 -0
  151. package/src/api/camera/ICameraFactory.ts +5 -10
  152. package/src/api/camera/OrthographicCamera.ts +23 -1
  153. package/src/api/camera/PerspectiveCamera.ts +60 -15
  154. package/src/api/content/ContentManager.ts +201 -5
  155. package/src/api/content/IContentManager.ts +33 -1
  156. package/src/api/content/ShaderModuleContent.ts +9 -0
  157. package/src/api/data/Color.ts +7 -1
  158. package/src/api/geometry/BaseGeometry.ts +23 -0
  159. package/src/api/geometry/GeometryBuilder.ts +14 -2
  160. package/src/api/geometry/IGeometryBuilder.ts +31 -1
  161. package/src/api/geometry/_export-geometry.ts +2 -1
  162. package/src/api/input/InputManager.ts +14 -5
  163. package/src/api/lights/AmbientLight.ts +53 -0
  164. package/src/api/lights/DirectionalLight.ts +60 -0
  165. package/src/api/lights/LightFactory.ts +21 -0
  166. package/src/api/lights/LightId.ts +9 -0
  167. package/src/api/lights/LightsBuffer.ts +87 -0
  168. package/src/api/lights/PointLight.ts +91 -0
  169. package/src/api/lights/_export.ts +5 -0
  170. package/src/api/loaders/ObjGeometryMapper.ts +28 -0
  171. package/src/api/loaders/_export.ts +2 -1
  172. package/src/api/loaders/obj/ObjLoader.ts +270 -0
  173. package/src/api/loaders/obj/ObjTypes.ts +124 -0
  174. package/src/api/loaders/obj/index.ts +5 -0
  175. package/src/api/material/ForwardBlinnPhongMaterial.ts +302 -0
  176. package/src/api/material/IMaterial.ts +16 -6
  177. package/src/api/material/IMaterialFactory.ts +19 -2
  178. package/src/api/material/InspectTextureMipsMaterial.ts +63 -24
  179. package/src/api/material/MaterialFactory.ts +12 -0
  180. package/src/api/material/SpriteMaterial.ts +83 -0
  181. package/src/api/material/UnlitMaterial.ts +101 -29
  182. package/src/api/material/_export.ts +3 -1
  183. package/src/api/meshes/IMesh.ts +45 -6
  184. package/src/api/meshes/IMeshFactory.ts +27 -4
  185. package/src/api/meshes/Mesh.ts +163 -41
  186. package/src/api/meshes/MeshFactory.ts +30 -6
  187. package/src/api/meshes/QuadMesh.ts +6 -2
  188. package/src/api/meshes/SubMesh.ts +71 -0
  189. package/src/api/meshes/_export.ts +1 -0
  190. package/src/api/rendering/render-pipelines/IForwardBlinnPhongRenderPipeline.ts +66 -0
  191. package/src/api/rendering/render-pipelines/IRenderPipeline.ts +4 -0
  192. package/src/api/rendering/render-pipelines/IRenderPipelineFactory.ts +39 -0
  193. package/src/api/rendering/render-pipelines/_export-render-pipelines.ts +2 -1
  194. package/src/api/scene/Scene.ts +264 -0
  195. package/src/api/shader/IShaderModuleLoader.ts +2 -1
  196. package/src/api/sprites/SpriteBatchDrawable.ts +12 -16
  197. package/src/api/sprites/SpriteBatchMesh.ts +23 -4
  198. package/src/api/utilities/GeometryUtilities.ts +2 -0
  199. package/src/api/window/WindowManager.ts +22 -2
  200. package/src/core/rendering/vertex-buffer-layout.ts +32 -1
  201. package/src/main.ts +0 -1
  202. package/src/webgl/WebGlGraphicsDevice.ts +0 -1
  203. package/src/webgl/lights/WebGlAmbientLightFactory.ts +35 -0
  204. package/src/webgl/lights/WebGlLightsBuffer.ts +215 -0
  205. package/src/webgl/render-pipelines/AWebGlRenderPipeline.ts +7 -1
  206. package/src/webgl/render-pipelines/WebGlMainRenderTargetRenderPipeline.ts +3 -0
  207. package/src/webgl/render-pipelines/WebGlRenderPipelineFactory.ts +24 -2
  208. package/src/webgl/render-pipelines/inspect/WebGlInspectTextureMipsRenderPipeline.ts +16 -6
  209. package/src/webgl/render-pipelines/material/WebGlBlinnPhongRenderPipeline.ts +300 -0
  210. package/src/webgl/render-pipelines/material/WebGlUnlitRenderPipeline.ts +16 -6
  211. package/src/webgl/render-pipelines/sprite/WebGlSpriteRenderPipeline.ts +11 -7
  212. package/src/webgl/shader/WebGlShaderModule.ts +11 -4
  213. package/src/webgl/shader/WebGlShaderModuleLoader.ts +29 -10
  214. package/src/webgl/texture/WebGlTexture2D.ts +19 -5
  215. package/src/webgl/utilities/webgl-filter-anisotropic-utilities.ts +9 -6
  216. package/dist/api/shader/shader-loader.d.ts.map +0 -1
  217. /package/src/api/shader/{shader-loader.ts → ShaderLoader.ts} +0 -0
@@ -1,9 +1,11 @@
1
+ import type { Scene } from "../scene/Scene.ts";
1
2
  import type { IShaderModuleLoader } from "../shader/IShaderModuleLoader.ts";
2
3
  import type { IShaderModule } from "../shader/IShaderModule.ts";
3
4
  import type { ITexture2D } from "../rendering/texture/ITexture2D.ts";
4
5
  import {TextureDescriptor} from "../rendering/texture/TextureDescriptor.ts";
5
6
  import {ContentConfig} from "./ContentConfig.ts";
6
7
  import type { IKtx2Texture } from "ris-ktx2";
8
+ import type {IMesh} from "../meshes/IMesh.ts";
7
9
 
8
10
  /**
9
11
  * The content manager.
@@ -26,9 +28,10 @@ export interface IContentManager {
26
28
  /**
27
29
  * Loads a shader module.
28
30
  * @param shaderAssetId - The id of a shader asset.
31
+ * @param keyValueDictionary - If present, tries to find key in shader such that it replaced ##DIRECTIONAL_LIGHT_COUNT## by given value for key DIRECTIONAL_LIGHST_COUNT.
29
32
  * @returns The shader module.
30
33
  */
31
- loadShaderModule(shaderAssetId: string): IShaderModule;
34
+ loadShaderModule(shaderAssetId: string, keyValueDictionary?: { [id:string]: number}): IShaderModule;
32
35
 
33
36
  /**
34
37
  * Loads a texture2D.
@@ -38,4 +41,33 @@ export interface IContentManager {
38
41
  * @returns The Texture2D.
39
42
  */
40
43
  loadTexture2DAsync(path: string, desc?: TextureDescriptor, contentConfig?: ContentConfig): Promise<ITexture2D>;
44
+
45
+ /**
46
+ * Loads a mesh from a model, typically.
47
+ * Currently, OBJ(WAVEFRONT) is supported.
48
+ * The mesh has one submesh per `usemtl` range, all sharing one vertex and index buffer.
49
+ * By default, each submesh gets an unlit material with the .mtl `Kd` as diffuse colour and `d` as alpha.
50
+ * @param path The path or url to the model.
51
+ * @param scene The scene the mesh adds itself to; `scene.render()` draws it. The materials use `scene.camera`
52
+ * (assigned on `scene.update()` when it is set later).
53
+ * @return The mesh.
54
+ */
55
+ loadMeshAsync(path: string, scene: Scene): Promise<IMesh>;
56
+
57
+ /**
58
+ * Loads an OBJ (Wavefront) mesh with lit Blinn-Phong materials.
59
+ * Like {@link loadMeshAsync}, the mesh has one submesh per `usemtl` range sharing one vertex and index buffer,
60
+ * and ranges with the same material name share one material. `scene.render()` draws it.
61
+ * The .mtl values map onto `ForwardBlinnPhongMaterial`:
62
+ * - `Kd` → `diffuseColor` (with `d` as alpha), white when missing.
63
+ * - `Ka` → `ambientColor`, the diffuse colour when missing.
64
+ * - `Ks` → `specularColor` and `Ns` → `shininess`, the material defaults (0.5 grey, 32) when missing.
65
+ * Without an .mtl file every submesh gets a white material with those defaults.
66
+ * @param path The path or url to the .obj file. A relative `mtllib` is resolved against it.
67
+ * @param scene The scene the mesh adds itself to. Every material uses the scene's lights and `scene.camera`
68
+ * (assigned on `scene.update()` when it is set later).
69
+ * @return The mesh.
70
+ */
71
+ loadBlinnPhongMeshAsync(path: string, scene: Scene): Promise<IMesh>;
72
+
41
73
  }
@@ -6,6 +6,9 @@ import inspectTextureMipsVS from "../../../shaders/glsl/inspect_texture_mips_vs.
6
6
  import inspectTextureMipsFS from "../../../shaders/glsl/inspect_texture_mips_fs.glsl?raw";
7
7
  import unlitMaterialVS from "../../../shaders/glsl/unlit_material_vs.glsl?raw";
8
8
  import unlitMaterialFS from "../../../shaders/glsl/unlit_material_fs.glsl?raw";
9
+ import forwardBlinnPhongVS from "../../../shaders/glsl/forward_blinn_phong_material_vs.glsl?raw";
10
+ import forwardBlinnPhongFS from "../../../shaders/glsl/forward_blinn_phong_material_fs.glsl?raw";
11
+
9
12
 
10
13
  /**
11
14
  * The api should look something like this for internal ones.
@@ -43,5 +46,11 @@ export const ShaderModuleContent : {[id: string]: any} = {
43
46
  "vertex": unlitMaterialVS,
44
47
  "fragment": unlitMaterialFS,
45
48
  "reflection": {}
49
+ },
50
+
51
+ "forward_blinn_phong_material": {
52
+ "vertex": forwardBlinnPhongVS,
53
+ "fragment": forwardBlinnPhongFS,
54
+ "reflection": {}
46
55
  }
47
56
  }
@@ -222,7 +222,13 @@ export class Color implements Iterable<number>{
222
222
  o
223
223
  }
224
224
 
225
-
225
+ /**
226
+ * Copy self and return new instance with same values.
227
+ * @return Color
228
+ * */
229
+ public copy(): Color {
230
+ return new Color(this.r, this.g, this.b, this.a);
231
+ }
226
232
  }
227
233
 
228
234
 
@@ -2,11 +2,34 @@ import { GeometryFormat } from "./GeometryFormat.ts";
2
2
  import {VertexFormat} from "./VertexFormat.ts";
3
3
  import type {IGeometry} from "../index.ts";
4
4
 
5
+
5
6
  /**
6
7
  * The default IGeometry implementation. GeometryBuilder fills it with vertex data.
7
8
  */
8
9
  export class BaseGeometry implements IGeometry {
9
10
 
11
+ /**
12
+ * Initializes a new instance of the BaseGeometry class.
13
+ * @param positions The optional positions of the geometry.
14
+ * @param indices The optional indices of the geometry.
15
+ * @param normals The optional normals of the geometry.
16
+ * @param textureCoords The optional texture coordinates of the geometry.
17
+ * @param colors The optional colors of the geometry.
18
+ */
19
+ constructor(positions?: number[], indices?: number[], normals?: number[], textureCoords?: number[], colors?: number[]) {
20
+ this.positions = positions;
21
+ this.indices = indices;
22
+ this.normals = normals;
23
+ this.textureCoords = textureCoords;
24
+ this.colors = colors;
25
+
26
+ if(this.indices && this.indices.length > 0) {
27
+ this.vertexCount = this.indices.length / 3;
28
+ } else if(this.positions && this.positions.length > 0) {
29
+ this.vertexCount = this.positions.length / 3;
30
+ }
31
+ }
32
+
10
33
  /** @inheritdoc */
11
34
  public vertexCount: number = 0;
12
35
 
@@ -1,6 +1,6 @@
1
1
  import { vec2 } from "gl-matrix"
2
2
  import { BaseGeometry } from "./BaseGeometry.ts";
3
- import type {IGeometry, IGeometryBuilder} from "../index.ts";
3
+ import type {IGeometry, IGeometryBuilder, GeometryDescriptor} from "../index.ts";
4
4
 
5
5
  /**
6
6
  * The GeometryBuilder class provides methods for creating various types of geometries, such as quads, with specified parameters like scale and winding order.
@@ -55,4 +55,16 @@ export class GeometryBuilder implements IGeometryBuilder {
55
55
 
56
56
  return geometry;
57
57
  }
58
- }
58
+
59
+ /** @inheritdoc */
60
+ public create(descriptor: GeometryDescriptor): IGeometry {
61
+ const vertexCount = descriptor.positions.length / 3;
62
+ return new BaseGeometry(
63
+ [...descriptor.positions],
64
+ descriptor.indices ? [...descriptor.indices] : undefined,
65
+ descriptor.normals ? [...descriptor.normals] : new Array(vertexCount * 3).fill(0),
66
+ descriptor.textureCoords ? [...descriptor.textureCoords] : new Array(vertexCount * 2).fill(0),
67
+ descriptor.colors ? [...descriptor.colors] : new Array(vertexCount * 4).fill(1),
68
+ );
69
+ }
70
+ }
@@ -1,6 +1,28 @@
1
1
  import type { IGeometry } from "./IGeometry.ts";
2
2
  import {vec2} from "gl-matrix";
3
3
 
4
+ /**
5
+ * The geometry descriptor.
6
+ * @public
7
+ */
8
+ export interface GeometryDescriptor {
9
+
10
+ /** Positions, 3 per vertex. */
11
+ readonly positions: number[];
12
+
13
+ /** Triangle indices, 3 per triangle. Optional for non-indexed geometry. */
14
+ readonly indices?: number[];
15
+
16
+ /** Normals, 3 per vertex. */
17
+ readonly normals?: number[];
18
+
19
+ /** Texture coordinates, 2 per vertex. */
20
+ readonly textureCoords?: number[];
21
+
22
+ /** RGBA colours, 4 per vertex. */
23
+ readonly colors?: number[];
24
+ }
25
+
4
26
  /**
5
27
  * The geometry builder.
6
28
  */
@@ -13,5 +35,13 @@ export interface IGeometryBuilder {
13
35
  * @returns The geometry.
14
36
  */
15
37
  quadGeometry(scale?: vec2, counterClockwise?: boolean): IGeometry;
16
-
38
+
39
+ /**
40
+ * Creates geometry from a plain descriptor.
41
+ * Attributes a material's geometry format needs but the descriptor lacks are filled with defaults:
42
+ * white colours, zero texture coordinates and zero normals.
43
+ * @param descriptor - The geometry descriptor. Its arrays are copied.
44
+ * @returns The geometry.
45
+ */
46
+ create(descriptor: GeometryDescriptor): IGeometry;
17
47
  }
@@ -1,4 +1,5 @@
1
1
  export * from "./IGeometryBuilder.ts";
2
2
  export * from "./IGeometry.ts";
3
3
  export * from "./VertexFormat.ts";
4
- export * from "./GeometryFormat.ts";
4
+ export * from "./GeometryFormat.ts";
5
+ export * from "./BaseGeometry.ts";
@@ -63,6 +63,8 @@ export class InputManager implements IInputManager {
63
63
  private readonly _touchCapabilities: TouchPanelCapabilities;
64
64
  private readonly _contacts: TouchContact[];
65
65
  private _touchListening = false;
66
+ /** Aborting it removes every canvas listener added in initialize(). */
67
+ private _listeners = new AbortController();
66
68
  /** Set in update, consumed in afterUpdate. Ageing before a publish would hide the press. */
67
69
  private _touchPublished = false;
68
70
 
@@ -120,6 +122,13 @@ export class InputManager implements IInputManager {
120
122
  return this._touchCapabilities;
121
123
  }
122
124
 
125
+ /** Removes the canvas listeners. Called from `Framework.dispose()`. */
126
+ public dispose(): void {
127
+ this._listeners.abort();
128
+ this._listeners = new AbortController();
129
+ this._touchListening = false;
130
+ }
131
+
123
132
  /** @inheritDoc */
124
133
  public initialize(): void {
125
134
 
@@ -133,20 +142,20 @@ export class InputManager implements IInputManager {
133
142
  canvas.addEventListener("mousemove", (e: MouseEvent) => {
134
143
  this._mousePosition[0] = e.clientX;
135
144
  this._mousePosition[1] = e.clientY;
136
- });
145
+ }, {signal: this._listeners.signal});
137
146
 
138
147
  canvas.addEventListener("mousedown", (e: MouseEvent) => {
139
148
  const idx = e.button;
140
149
  const btn = this._mouseButtonMap[idx] ?? MouseButton.NONE;
141
150
  this._mouseButtonDown[btn] = true;
142
- })
151
+ }, {signal: this._listeners.signal})
143
152
 
144
153
  canvas.addEventListener("mouseup", (e: MouseEvent) => {
145
154
  const idx = e.button;
146
155
  const btn = this._mouseButtonMap[idx] ?? MouseButton.NONE;
147
156
  this._mouseButtonDown[btn] = false;
148
157
  this._mouseButtonReleased[btn] = true;
149
- });
158
+ }, {signal: this._listeners.signal});
150
159
 
151
160
  canvas.addEventListener("wheel", (e: WheelEvent) => {
152
161
 
@@ -167,7 +176,7 @@ export class InputManager implements IInputManager {
167
176
  vec2.set(this._scrollWheelPosition, x, y);
168
177
 
169
178
  e.preventDefault();
170
- })
179
+ }, {signal: this._listeners.signal})
171
180
  }
172
181
 
173
182
  private _handleTouchEvents(canvas: HTMLCanvasElement): void {
@@ -179,7 +188,7 @@ export class InputManager implements IInputManager {
179
188
  // Pan and pinch-zoom start on this canvas only. The rest of the page is left alone.
180
189
  canvas.style.touchAction = "none";
181
190
 
182
- const options = InputManager._touchListenerOptions;
191
+ const options: AddEventListenerOptions = {...InputManager._touchListenerOptions, signal: this._listeners.signal};
183
192
  canvas.addEventListener("touchstart", this._onTouchStart, options);
184
193
  canvas.addEventListener("touchmove", this._onTouchMove, options);
185
194
  canvas.addEventListener("touchend", this._onTouchEnd, options);
@@ -0,0 +1,53 @@
1
+ import {Color} from "../data/Color.ts";
2
+ import {createLightId} from "./LightId.ts";
3
+ import type {Scene} from "../scene/Scene.ts";
4
+
5
+ /** The descriptor for an ambient light. All fields are optional. */
6
+ export interface AmbientLightDescriptor {
7
+ /** The unique id. Assigned automatically when omitted. */
8
+ id?: number;
9
+
10
+ /** The optional name for a light. */
11
+ name?: string;
12
+
13
+ /** The light colour. Alpha is ignored. Defaults to black. */
14
+ color?: Color;
15
+
16
+ /** Multiplies the colour. Defaults to 1. */
17
+ intensity?: number;
18
+ }
19
+
20
+ /**
21
+ * The ambient light. Plain data: it has no GPU resources.
22
+ * Put it into a {@link LightsBuffer} to use it in shaders.
23
+ * @public
24
+ */
25
+ export class AmbientLight {
26
+
27
+ /**
28
+ * The constructor.
29
+ * @param descriptor The optional descriptor. The colour is copied.
30
+ * @param scene The optional scene to register with.
31
+ */
32
+ public constructor(descriptor?: AmbientLightDescriptor, scene?: Scene) {
33
+
34
+ this.id = descriptor?.id ?? createLightId();
35
+ this.name = descriptor?.name;
36
+ this.color = descriptor?.color?.copy() ?? Color.black();
37
+ this.intensity = descriptor?.intensity ?? 1;
38
+
39
+ scene?.add(this);
40
+ }
41
+
42
+ /** The unique id of this light. */
43
+ public readonly id: number;
44
+
45
+ /** The optional name for a light. */
46
+ public name?: string;
47
+
48
+ /** The light color. Alpha is ignored. */
49
+ public color: Color;
50
+
51
+ /** Multiplies {@link color} when written to the lights buffer. */
52
+ public intensity: number;
53
+ }
@@ -0,0 +1,60 @@
1
+ import {vec3} from "gl-matrix";
2
+ import {Color} from "../data/Color.ts";
3
+ import {createLightId} from "./LightId.ts";
4
+ import type {Scene} from "../scene/Scene.ts";
5
+
6
+ /** The descriptor for a directional light. All fields are optional. */
7
+ export interface DirectionalLightDescriptor {
8
+ /** The unique id. Assigned automatically when omitted. */
9
+ id?: number;
10
+
11
+ /** The optional name for a light. */
12
+ name?: string;
13
+
14
+ /** The light colour. Alpha is ignored. Defaults to white. */
15
+ color?: Color;
16
+
17
+ /** Multiplies the colour. Defaults to 1. */
18
+ intensity?: number;
19
+
20
+ /** The direction in which the light travels. Defaults to straight down, (0, -1, 0). */
21
+ direction?: vec3;
22
+ }
23
+
24
+ /**
25
+ * The directional light. Plain data: it has no GPU resources.
26
+ * @public
27
+ */
28
+ export class DirectionalLight {
29
+
30
+ /**
31
+ * The constructor.
32
+ * @param descriptor The optional descriptor. Colour and direction are copied.
33
+ * @param scene The optional scene; the light adds itself via {@link Scene.add}.
34
+ * @throws Error when the scene has no free directional light slot left (the light is then not in the scene).
35
+ */
36
+ public constructor(descriptor?: DirectionalLightDescriptor, scene?: Scene) {
37
+ this.id = descriptor?.id ?? createLightId();
38
+ this.name = descriptor?.name;
39
+ this.color = descriptor?.color?.copy() ?? Color.white();
40
+ this.intensity = descriptor?.intensity ?? 1;
41
+ this.direction = descriptor?.direction ? vec3.clone(descriptor.direction) : vec3.fromValues(0, -1, 0);
42
+
43
+ scene?.add(this);
44
+ }
45
+
46
+ /** The unique id of this light. */
47
+ public readonly id: number;
48
+
49
+ /** The optional name for a light. */
50
+ public name?: string;
51
+
52
+ /** The light colour. Alpha is ignored. */
53
+ public color: Color;
54
+
55
+ /** Multiplies {@link color} when written to the lights buffer. */
56
+ public intensity: number;
57
+
58
+ /** The direction in which the light travels. */
59
+ public direction: vec3;
60
+ }
@@ -0,0 +1,21 @@
1
+ import type {AmbientLight, AmbientLightDescriptor} from "./AmbientLight.ts";
2
+ import type {Scene} from "../scene/Scene.ts";
3
+ import type {LightsBuffer, LightsBufferDescriptor} from "./LightsBuffer.ts";
4
+
5
+ /** The light factory */
6
+ export interface AmbientLightFactory {
7
+
8
+ /**
9
+ * Creates the ambient light.
10
+ * @param desc The descriptor for ambient light. The colour defaults to a dim grey; the id is assigned automatically.
11
+ * @param scene The optional scene to register the light with.
12
+ */
13
+ createAmbientLight(desc?: AmbientLightDescriptor, scene?: Scene): AmbientLight;
14
+
15
+ /**
16
+ * Creates a lights buffer, which owns the uniform buffer with all lights for shaders.
17
+ * @param desc The optional descriptor with the initial lights and directional light count.
18
+ */
19
+ __createLightsBuffer(desc?: LightsBufferDescriptor): LightsBuffer;
20
+
21
+ }
@@ -0,0 +1,9 @@
1
+ let nextLightId = 1;
2
+
3
+ /**
4
+ * Returns a new unique light id. Light constructors call this when no id is given.
5
+ * @internal
6
+ */
7
+ export function createLightId(): number {
8
+ return nextLightId++;
9
+ }
@@ -0,0 +1,87 @@
1
+ import type {AmbientLight} from "./AmbientLight.ts";
2
+ import type {DirectionalLight} from "./DirectionalLight.ts";
3
+ import type {PointLight} from "./PointLight.ts";
4
+ import type {IUniformBuffer} from "../rendering/buffers/IUniformBuffer.ts";
5
+ import type {IDisposable} from "../core/IDisposable.ts";
6
+
7
+ /** The number of directional lights the lights buffer and the Blinn-Phong shader hold by default. */
8
+ export const DEFAULT_DIRECTIONAL_LIGHTS_COUNT = 3;
9
+
10
+ /** The number of point lights the lights buffer and the Blinn-Phong shader hold by default. */
11
+ export const DEFAULT_POINT_LIGHTS_COUNT = 3;
12
+
13
+ /** The descriptor for a lights buffer. */
14
+ export interface LightsBufferDescriptor {
15
+
16
+ /** The ambient light. Defaults to a black ambient light. */
17
+ ambientLight?: AmbientLight;
18
+
19
+ /** The initial directional lights. More than {@link maxDirectionalLights} throws. */
20
+ directionalLights?: DirectionalLight[];
21
+
22
+ /**
23
+ * The number of directional light slots. Must match `DIRECTIONAL_LIGHTS_COUNT` in the shader.
24
+ * Defaults to {@link DEFAULT_DIRECTIONAL_LIGHTS_COUNT}.
25
+ */
26
+ maxDirectionalLights?: number;
27
+
28
+ /** The initial point lights. More than {@link maxPointLights} throws. */
29
+ pointLights?: PointLight[];
30
+
31
+ /**
32
+ * The number of point light slots. Must match `POINT_LIGHTS_COUNT` in the shader.
33
+ * Defaults to {@link DEFAULT_POINT_LIGHTS_COUNT}.
34
+ */
35
+ maxPointLights?: number;
36
+ }
37
+
38
+ /**
39
+ * Holds the lights of a scene and owns the uniform buffer that shaders read them from.
40
+ * Assign it to lit materials, e.g. `ForwardBlinnPhongMaterial.lightsBuffer`.
41
+ *
42
+ * std140 layout of the `LightsBuffer` block:
43
+ * Every light colour is written premultiplied by its intensity, so the layout has no intensity field.
44
+ * - `vec3 u_ambientLight` at offset 0, padded to 16 bytes.
45
+ * - `DirectionalLight u_directionalLight[N]` from offset 16, 32 bytes each:
46
+ * `vec3 light` at +0 and `vec3 direction` at +16.
47
+ * - `PointLight u_pointLight[M]` after the directional lights, 48 bytes each:
48
+ * `vec3 light` at +0, `float constant` at +12, `vec3 position` at +16, `float linear` at +28, `float quadratic` at +32.
49
+ */
50
+ export interface LightsBuffer extends IDisposable {
51
+
52
+ /** The ambient light written to the buffer. */
53
+ ambientLight: AmbientLight;
54
+
55
+ /** The directional lights written to the buffer. Read-only; use {@link add} and {@link remove}. Unused slots are black. */
56
+ readonly directionalLights: readonly DirectionalLight[];
57
+
58
+ /** The number of directional light slots in the buffer. */
59
+ readonly maxDirectionalLights: number;
60
+
61
+ /** The point lights written to the buffer. Read-only; use {@link add} and {@link remove}. Unused slots are black. */
62
+ readonly pointLights: readonly PointLight[];
63
+
64
+ /** The number of point light slots in the buffer. */
65
+ readonly maxPointLights: number;
66
+
67
+ /**
68
+ * The uniform buffer holding the lights for shaders.
69
+ * NOTE: Intended to be used internally, never set this value manually.
70
+ */
71
+ readonly buffer: IUniformBuffer;
72
+
73
+ /**
74
+ * Adds a directional or point light. Adding a light that is already in the buffer does nothing.
75
+ * @throws Error when all slots for that kind of light are in use.
76
+ */
77
+ add(light: DirectionalLight | PointLight): void;
78
+
79
+ /**
80
+ * Removes a directional or point light.
81
+ * @returns True when the light was in the buffer.
82
+ */
83
+ remove(light: DirectionalLight | PointLight): boolean;
84
+
85
+ /** Writes the lights to {@link buffer} if anything changed since the last update. Call once per frame before rendering. */
86
+ update(): void;
87
+ }
@@ -0,0 +1,91 @@
1
+ import {vec3} from "gl-matrix";
2
+ import {Color} from "../data/Color.ts";
3
+ import {createLightId} from "./LightId.ts";
4
+ import type {Scene} from "../scene/Scene.ts";
5
+
6
+ /** The descriptor for a point light. All fields are optional. */
7
+ export interface PointLightDescriptor {
8
+ /** The unique id. Assigned automatically when omitted. */
9
+ id?: number;
10
+
11
+ /** The optional name for a light. */
12
+ name?: string;
13
+
14
+ /** The light colour. Alpha is ignored. Defaults to white. */
15
+ color?: Color;
16
+
17
+ /** Multiplies the colour. Defaults to 1. */
18
+ intensity?: number;
19
+
20
+ /** World-space position. Defaults to the origin. */
21
+ position?: vec3;
22
+
23
+ /** Constant attenuation term. Defaults to 1. */
24
+ constant?: number;
25
+
26
+ /** Linear attenuation term. Defaults to 0.09 (about 50 units of range). */
27
+ linear?: number;
28
+
29
+ /** Quadratic attenuation term. Defaults to 0.032 (about 50 units of range). */
30
+ quadratic?: number;
31
+ }
32
+
33
+ /**
34
+ * The point light. Plain data: it has no GPU resources.
35
+ * Attenuation is `1 / (constant + linear * d + quadratic * d²)` for distance `d`.
36
+ * @public
37
+ */
38
+ export class PointLight {
39
+
40
+ /** The default constant attenuation. */
41
+ public static readonly DEFAULT_CONSTANT = 1;
42
+
43
+ /** The default linear attenuation. */
44
+ public static readonly DEFAULT_LINEAR = 0.09;
45
+
46
+ /** The default quadratic attenuation. */
47
+ public static readonly DEFAULT_QUADRATIC = 0.032;
48
+
49
+ /**
50
+ * The constructor.
51
+ * @param descriptor The optional descriptor. Colour and position are copied.
52
+ * @param scene The optional scene; the light adds itself via {@link Scene.add}.
53
+ * @throws Error when the scene has no free point light slot left (the light is then not in the scene).
54
+ */
55
+ public constructor(descriptor?: PointLightDescriptor, scene?: Scene) {
56
+ this.id = descriptor?.id ?? createLightId();
57
+ this.name = descriptor?.name;
58
+ this.color = descriptor?.color?.copy() ?? Color.white();
59
+ this.intensity = descriptor?.intensity ?? 1;
60
+ this.position = descriptor?.position ? vec3.clone(descriptor.position) : vec3.create();
61
+ this.constant = descriptor?.constant ?? PointLight.DEFAULT_CONSTANT;
62
+ this.linear = descriptor?.linear ?? PointLight.DEFAULT_LINEAR;
63
+ this.quadratic = descriptor?.quadratic ?? PointLight.DEFAULT_QUADRATIC;
64
+
65
+ scene?.add(this);
66
+ }
67
+
68
+ /** The unique id of this light. */
69
+ public readonly id: number;
70
+
71
+ /** The optional name for a light. */
72
+ public name?: string;
73
+
74
+ /** The light colour. Alpha is ignored. */
75
+ public color: Color;
76
+
77
+ /** Multiplies {@link color} when written to the lights buffer. */
78
+ public intensity: number;
79
+
80
+ /** World-space position of the light. */
81
+ public position: vec3;
82
+
83
+ /** Constant attenuation term. */
84
+ public constant: number;
85
+
86
+ /** Linear attenuation term. */
87
+ public linear: number;
88
+
89
+ /** Quadratic attenuation term. */
90
+ public quadratic: number;
91
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./AmbientLight.ts";
2
+ export * from "./DirectionalLight.ts";
3
+ export * from "./PointLight.ts";
4
+ export * from "./LightsBuffer.ts";
5
+ export * from "./LightFactory.ts";
@@ -0,0 +1,28 @@
1
+ import type {GeometryDescriptor} from "../geometry/IGeometryBuilder.ts";
2
+ import type {ObjGeometryDefinition} from "./obj/index.ts";
3
+
4
+ /**
5
+ * Maps plain OBJ geometry definitions from {@link ObjLoader} onto the framework's {@link GeometryDescriptor}.
6
+ * Pass the result to `geometryBuilder.create`.
7
+ * Not part of the public API: `content.loadMeshAsync` and `content.loadBlinnPhongMeshAsync` use it.
8
+ * @internal
9
+ */
10
+ export class ObjGeometryMapper {
11
+
12
+ /**
13
+ * Creates a geometry descriptor from an OBJ geometry definition.
14
+ * @param definition The geometry definition, e.g. `result.geometry` from `ObjLoader.loadAsync`.
15
+ * @param useMaterialColors True keeps the loader's per-vertex `Kd` colours; false (the default) uses white,
16
+ * for when each submesh's material carries the colour instead.
17
+ * @returns The descriptor. It references the definition's arrays; `geometryBuilder.create` copies them.
18
+ */
19
+ public static toDescriptor(definition: ObjGeometryDefinition, useMaterialColors = false): GeometryDescriptor {
20
+ return {
21
+ positions: definition.positions,
22
+ indices: definition.indices,
23
+ normals: definition.normals,
24
+ textureCoords: definition.textureCoords,
25
+ colors: useMaterialColors ? definition.colors : undefined,
26
+ };
27
+ }
28
+ }
@@ -1,2 +1,3 @@
1
1
  export * from "./IImageLoader.ts"
2
- export * from "./ImageLoader.ts"
2
+ export * from "./ImageLoader.ts"
3
+ export * from "./obj/index.ts"