ris-framework 0.1.0-dev.9 → 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,11 +1,13 @@
1
+ import type {Scene} from "../scene/Scene.ts";
1
2
  import type { IMeshFactory } from "./IMeshFactory.ts";
2
3
  import type { IGeometry } from "../geometry/IGeometry.ts";
3
- import {GeometryFormat} from "../geometry/GeometryFormat.ts";
4
4
  import type { IMesh } from "./IMesh.ts";
5
5
  import {QuadMesh} from "./QuadMesh.ts";
6
6
  import type { IFramework } from "../IFramework.ts";
7
7
  import {Mesh} from "./Mesh.ts";
8
8
  import {vec2} from "gl-matrix";
9
+ import type { IMaterial } from "../material/IMaterial.ts";
10
+ import type { SubMeshDescriptor } from "./IMeshFactory.ts";
9
11
 
10
12
  /**
11
13
  * The implementation of the mesh factory interface.
@@ -20,17 +22,39 @@ export class MeshFactory implements IMeshFactory {
20
22
  }
21
23
 
22
24
  /** @inheritDoc */
23
- public create(geometry: IGeometry, geometryFormat: GeometryFormat): IMesh {
24
- const mesh = new Mesh(this._framework);
25
- mesh.setGeometry(geometry, geometryFormat);
25
+ public create(scene: Scene, geometry: IGeometry, material: IMaterial): IMesh {
26
+ const mesh = new Mesh(this._framework, scene, material);
27
+ mesh.setGeometry(geometry);
26
28
  return mesh;
27
29
  }
28
30
 
29
31
  /** @inheritDoc */
30
- public createQuadMesh(isUpdatable: boolean, scale?: vec2): QuadMesh {
31
- const quadMesh = new QuadMesh(this._framework, isUpdatable, scale);
32
+ public createWithSubMeshes(scene: Scene, geometry: IGeometry, subMeshes: SubMeshDescriptor[]): IMesh {
33
+ if (subMeshes.length == 0) {
34
+ throw new Error("createWithSubMeshes needs at least one submesh.");
35
+ }
36
+ const format = subMeshes[0].material.geometryFormat;
37
+ if (subMeshes.some(s => s.material.geometryFormat != format)) {
38
+ throw new Error("All submesh materials must use the same geometry format, as they share one vertex buffer.");
39
+ }
40
+
41
+ const mesh = new Mesh(this._framework, scene);
42
+ mesh.setGeometry(geometry, format);
43
+ for (const subMesh of subMeshes) {
44
+ mesh.addSubMesh(subMesh.material, subMesh.indexStart, subMesh.indexCount);
45
+ }
46
+ return mesh;
47
+ }
48
+
49
+ /** @inheritDoc */
50
+ public createQuadMesh(_isUpdatable: boolean, _scale?: vec2): QuadMesh {
51
+
52
+ throw new Error("Meshes not implemented.");
53
+ /* const quadMesh = new QuadMesh(this._framework, isUpdatable, scale);
32
54
  quadMesh.initialize();
33
55
  return quadMesh;
56
+
57
+ */
34
58
  }
35
59
 
36
60
  }
@@ -3,6 +3,8 @@ import {Rect} from "../data/Rect.ts";
3
3
  import type { IFramework } from "../IFramework.ts";
4
4
  import {vec2} from "gl-matrix";
5
5
  import {BufferUsage} from "../rendering/buffers/BufferUsage.ts";
6
+ import type {IMaterial} from "../material/IMaterial.ts";
7
+ import type {Scene} from "../scene/Scene.ts";
6
8
 
7
9
  /**
8
10
  * The mesh of a quad.
@@ -16,11 +18,13 @@ export class QuadMesh extends Mesh {
16
18
  /**
17
19
  * The constructor.
18
20
  * @param framework The framework.
21
+ * @param scene The scene the quad adds itself to.
22
+ * @param material The material.
19
23
  * @param _isUpdatable Makes mesh updatable.
20
24
  * @param scale The scale factor for geometry. If not set, it is set to (1,1).
21
25
  */
22
- public constructor(framework: IFramework, private _isUpdatable: boolean, scale?: vec2) {
23
- super(framework);
26
+ public constructor(framework: IFramework, scene: Scene, material: IMaterial, private _isUpdatable: boolean, scale?: vec2) {
27
+ super(framework, scene, material);
24
28
  if(_isUpdatable) {
25
29
  this._bufferUsage = BufferUsage.VERTEX | BufferUsage.COPY_DST;
26
30
  }
@@ -0,0 +1,71 @@
1
+ import type { mat4, vec3 } from "gl-matrix";
2
+ import type { IMesh } from "./IMesh.ts";
3
+ import type { Mesh } from "./Mesh.ts";
4
+ import type { IUniformBuffer } from "../rendering/buffers/IUniformBuffer.ts";
5
+ import type { IMaterial } from "../material/IMaterial.ts";
6
+ import type { IVertexBuffer } from "../rendering/buffers/IVertexBuffer.ts";
7
+ import type { IIndexBuffer } from "../rendering/buffers/IIndexBuffer.ts";
8
+
9
+ /**
10
+ * A part of a mesh drawn with its own material.
11
+ * It shares the parent's vertex and index buffers and draws only its index range.
12
+ */
13
+ export class SubMesh implements IMesh {
14
+
15
+ /**
16
+ * The constructor.
17
+ * @param parent The mesh that owns the buffers.
18
+ * @param material The material of this submesh.
19
+ * @param indexStart The first index of the range.
20
+ * @param indexCount The number of indices in the range.
21
+ */
22
+ public constructor(
23
+ public readonly parent: Mesh,
24
+ public material: IMaterial,
25
+ public readonly indexStart: number,
26
+ public readonly indexCount: number) {
27
+ }
28
+
29
+ /** The parent's vertex buffer. */
30
+ public get vertexBuffer(): IVertexBuffer | undefined {
31
+ return this.parent.vertexBuffer;
32
+ }
33
+
34
+ /** The parent's index buffer. */
35
+ public get indexBuffer(): IIndexBuffer | undefined {
36
+ return this.parent.indexBuffer;
37
+ }
38
+
39
+ /** The parent's position; setting it sets the parent's. */
40
+ public get position(): vec3 { return this.parent.position; }
41
+ public set position(value: vec3) { this.parent.position = value; }
42
+
43
+ /** The parent's rotation; setting it sets the parent's. */
44
+ public get rotation(): vec3 { return this.parent.rotation; }
45
+ public set rotation(value: vec3) { this.parent.rotation = value; }
46
+
47
+ /** The parent's scale; setting it sets the parent's. */
48
+ public get scale(): vec3 { return this.parent.scale; }
49
+ public set scale(value: vec3) { this.parent.scale = value; }
50
+
51
+ /** The parent's model matrix. */
52
+ public get modelMatrix(): mat4 {
53
+ return this.parent.modelMatrix;
54
+ }
55
+
56
+ /** The parent's model buffer. */
57
+ public get modelBuffer(): IUniformBuffer {
58
+ return this.parent.modelBuffer;
59
+ }
60
+
61
+ /** Draws this submesh with its material, using the parent's transform (model buffer). */
62
+ public render(): void {
63
+ this.parent.__assignModelBuffer(this.material);
64
+ this.material.beforeRender();
65
+ this.material.renderMesh(this);
66
+ }
67
+
68
+ /** Does nothing: the buffers belong to the parent and the material to the caller. */
69
+ public dispose(): void {
70
+ }
71
+ }
@@ -1,6 +1,7 @@
1
1
  export * from "./IMeshFactory.ts";
2
2
  export * from "./IMesh.ts"
3
3
  export * from "./Mesh.ts"
4
+ export * from "./SubMesh.ts"
4
5
  export * from "./QuadMesh.ts";
5
6
  export * from "./MeshParameters.ts"
6
7
  export * from "./MeshFactory.ts";
@@ -0,0 +1,66 @@
1
+ import type { ITexture2D } from "../texture/ITexture2D.ts";
2
+ import type { ISampler } from "../sampler/ISampler.ts";
3
+ import type { IRenderPipeline } from "./IRenderPipeline.ts";
4
+ import type { IUniformBuffer } from "../buffers/IUniformBuffer.ts";
5
+
6
+ /**
7
+ * The Blinn-Phong render pipeline with forward lighting.
8
+ */
9
+ export interface IForwardBlinnPhongRenderPipeline extends IRenderPipeline {
10
+
11
+ /**
12
+ * The diffuse texture.
13
+ */
14
+ diffuseTexture: ITexture2D;
15
+
16
+ /**
17
+ * The texture sampler.
18
+ */
19
+ diffuseTextureSampler?: ISampler;
20
+
21
+ /**
22
+ * The projection view uniform buffer.
23
+ * Contains a single matrix representing the projection view matrix.
24
+ */
25
+ projectionViewBuffer: IUniformBuffer;
26
+
27
+ /**
28
+ * The world matrix uniform buffer.
29
+ * Contains a single matrix representing the world matrix.
30
+ */
31
+ modelBuffer: IUniformBuffer;
32
+
33
+ /**
34
+ * The material buffer that contains the material properties.
35
+ * - vec3 ambientColor
36
+ * - vec3 diffuseColor
37
+ * - vec3 specularColor
38
+ * - float shininess
39
+ */
40
+ materialBuffer: IUniformBuffer;
41
+
42
+ /** The camera position buffer: std140 `vec3` world-space eye, e.g. `camera.positionBuffer`. */
43
+ cameraPositionBuffer: IUniformBuffer;
44
+
45
+ /**
46
+ * The lights buffer, `LightsBuffer.buffer`, bound to the shader's `LightsBuffer` block.
47
+ * - vec3 ambientLight
48
+ * - DirectionalLight[DIRECTIONAL_LIGHTS_COUNT] { vec3 light; vec3 direction; }
49
+ * - PointLight[POINT_LIGHTS_COUNT] { vec3 light; float constant; vec3 position; float linear; float quadratic; }
50
+ */
51
+ lightsBuffer: IUniformBuffer;
52
+
53
+ /** The directional light slots the shader was compiled with. */
54
+ readonly maxDirectionalLights: number;
55
+
56
+ /** The point light slots the shader was compiled with. */
57
+ readonly maxPointLights: number;
58
+
59
+ /**
60
+ * Sets the light slot counts. They must match the lights buffer (`LightsBuffer.maxDirectionalLights`/`maxPointLights`).
61
+ * When they change after initialization the shader variant for the new counts is loaded.
62
+ * @param maxDirectionalLights The directional light slots.
63
+ * @param maxPointLights The point light slots.
64
+ */
65
+ setLightCounts(maxDirectionalLights: number, maxPointLights: number): void;
66
+ }
@@ -4,6 +4,7 @@ import type { IPrimitiveState } from "../primitive/IPrimitiveState.ts";
4
4
  import type { IVertexBufferLayout } from "../buffers/IVertexBufferLayout.ts";
5
5
  import type { IIndexBuffer } from "../buffers/IIndexBuffer.ts";
6
6
  import type { IVertexBuffer } from "../buffers/IVertexBuffer.ts";
7
+ import type { GeometryFormat } from "../../geometry/GeometryFormat.ts";
7
8
 
8
9
  /**
9
10
  * The render pipeline interface.
@@ -27,6 +28,9 @@ export interface IRenderPipeline extends IDisposable {
27
28
  */
28
29
  readonly vertexBufferLayouts: IVertexBufferLayout[];
29
30
 
31
+ /** The geometry format required by the render pipeline. */
32
+ readonly geometryFormat: GeometryFormat;
33
+
30
34
  /**
31
35
  * Initializes the render pipeline.
32
36
  * This method is called by the renderer when the pipeline is added to the renderer.
@@ -5,6 +5,38 @@ import type { ISpriteRenderPipeline } from "./ISpriteRenderPipeline.ts";
5
5
  import type { IInspectTextureMipsRenderPipeline } from "./IInspectTextureMipsRenderPipeline.ts";
6
6
  import type { IUnlitRenderPipeline } from "./IUnlitRenderPipeline.ts";
7
7
  import type { IPrimitiveState } from "../primitive/IPrimitiveState.ts";
8
+ import type { IForwardBlinnPhongRenderPipeline } from "./IForwardBlinnPhongRenderPipeline.ts";
9
+
10
+ /** The descriptor for the forward Blinn-Phong render pipeline. */
11
+ export interface ForwardBlinnPhongRenderPipelineDescriptor {
12
+
13
+ /** The projection view buffer, e.g. `camera.projectionViewBuffer`. */
14
+ projectionViewBuffer: IUniformBuffer;
15
+
16
+ /** The world buffer. */
17
+ modelBuffer: IUniformBuffer;
18
+
19
+ /** The camera position buffer, e.g. `camera.positionBuffer`: std140 `vec3` world-space eye. */
20
+ cameraPositionBuffer: IUniformBuffer;
21
+
22
+ /** The material buffer (ambient and diffuse color). */
23
+ materialBuffer: IUniformBuffer;
24
+
25
+ /**
26
+ * The lights buffer, e.g. `lightsBuffer.buffer`.
27
+ * Optional: it can be set later through `pipeline.lightsBuffer`.
28
+ */
29
+ lightsBuffer?: IUniformBuffer;
30
+
31
+ /** Directional light slots to compile the shader with; must match the lights buffer. Defaults to `DEFAULT_DIRECTIONAL_LIGHTS_COUNT`. */
32
+ maxDirectionalLights?: number;
33
+
34
+ /** Point light slots to compile the shader with; must match the lights buffer. Defaults to `DEFAULT_POINT_LIGHTS_COUNT`. */
35
+ maxPointLights?: number;
36
+
37
+ /** The primitive state. */
38
+ primitiveState?: IPrimitiveState;
39
+ }
8
40
 
9
41
  /**
10
42
  * The pipeline factory.
@@ -49,4 +81,11 @@ export interface IRenderPipelineFactory {
49
81
  primitiveState?: IPrimitiveState
50
82
  ): IUnlitRenderPipeline;
51
83
 
84
+ /**
85
+ * Creates the forward Blinn-Phong render pipeline.
86
+ * @param descriptor - The buffers and primitive state for the pipeline.
87
+ * @returns The forward Blinn-Phong render pipeline.
88
+ */
89
+ createForwardBlinnPhongRenderPipeline(descriptor: ForwardBlinnPhongRenderPipelineDescriptor): IForwardBlinnPhongRenderPipeline;
90
+
52
91
  }
@@ -3,4 +3,5 @@ export * from "./IMainRenderTargetRenderPipeline.ts";
3
3
  export * from "./IRenderPipelineFactory.ts";
4
4
  export * from "./IRenderPipeline.ts";
5
5
  export * from "./IInspectTextureMipsRenderPipeline.ts"
6
- export * from "./IUnlitRenderPipeline.ts"
6
+ export * from "./IUnlitRenderPipeline.ts"
7
+ export * from "./IForwardBlinnPhongRenderPipeline.ts"
@@ -0,0 +1,264 @@
1
+ import {AmbientLight} from "../lights/AmbientLight.ts";
2
+ import {DirectionalLight} from "../lights/DirectionalLight.ts";
3
+ import {PointLight} from "../lights/PointLight.ts";
4
+ import type {IDisposable} from "../core/IDisposable.ts";
5
+ import type {LightsBuffer} from "../lights/LightsBuffer.ts";
6
+ import type {IFramework} from "../IFramework.ts";
7
+ import type {IMesh} from "../meshes/IMesh.ts";
8
+ import type {GameTime} from "../time/GameTime.ts";
9
+ import type {ICamera} from "../camera/ICamera.ts";
10
+ import type {IMaterial} from "../material/IMaterial.ts";
11
+
12
+ /** Anything a scene can hold: a mesh, a light (ambient, directional, point) or a camera. */
13
+ export type SceneItem = IMesh | AmbientLight | DirectionalLight | PointLight | ICamera;
14
+
15
+ /**
16
+ * The scene. Meshes, lights and cameras given a scene in their constructor add themselves through {@link add};
17
+ * meshes remove themselves when disposed. Call {@link update} once per frame and {@link render} in the render listener.
18
+ */
19
+ export class Scene implements IDisposable {
20
+
21
+ private readonly _meshes: IMesh[] = [];
22
+ private _activeAmbientLight: AmbientLight;
23
+ private _disposed = false;
24
+ private _activeCamera?: ICamera;
25
+
26
+ /**
27
+ * The camera the scene renders with; undefined until one is set or added.
28
+ * Setting it assigns it to the materials of every mesh (and submesh) whose camera differs;
29
+ * {@link update} also assigns it to meshes and submeshes added later.
30
+ */
31
+ public get camera(): ICamera | undefined {
32
+ return this._activeCamera;
33
+ }
34
+
35
+ public set camera(value: ICamera | undefined) {
36
+ this._activeCamera = value;
37
+ if (!value) {
38
+ return;
39
+ }
40
+ for (const mesh of this._meshes) {
41
+ Scene._assignCameraToMesh(mesh, value);
42
+ }
43
+ }
44
+
45
+ /** The ambient lights registered with this scene. */
46
+ public readonly ambientLights: AmbientLight[] = [];
47
+
48
+ /**
49
+ * The constructor.
50
+ * @param framework The framework.
51
+ */
52
+ public constructor(framework: IFramework) {
53
+
54
+ // Assigned through add() by the ambient light constructor; set here for the type checker.
55
+ this._activeAmbientLight = framework.lightsFactory.createAmbientLight(undefined, this);
56
+
57
+ this.__forwardLightsBuffer = framework.lightsFactory.__createLightsBuffer({
58
+ ambientLight: this._activeAmbientLight,
59
+ maxDirectionalLights: 3,
60
+ maxPointLights: 5
61
+ });
62
+
63
+ }
64
+
65
+ /**
66
+ * The lights buffer used for forward shading.
67
+ * @internal
68
+ */
69
+ __forwardLightsBuffer: LightsBuffer;
70
+
71
+ /** The meshes in this scene, in the order they were added. */
72
+ public get meshes(): readonly IMesh[] {
73
+ return this._meshes;
74
+ }
75
+
76
+ /** The directional lights in this scene. */
77
+ public get directionalLights(): readonly DirectionalLight[] {
78
+ return this.__forwardLightsBuffer.directionalLights;
79
+ }
80
+
81
+ /** The point lights in this scene. */
82
+ public get pointLights(): readonly PointLight[] {
83
+ return this.__forwardLightsBuffer.pointLights;
84
+ }
85
+
86
+ /**
87
+ * Gets the active ambient light.
88
+ */
89
+ public get activeAmbientLight(): AmbientLight {
90
+ return this._activeAmbientLight;
91
+ }
92
+
93
+ /** Sets the active ambient light; it is added to {@link ambientLights} if needed. */
94
+ public set activeAmbientLight(value: AmbientLight) {
95
+ this._addAmbientLight(value);
96
+ this._activeAmbientLight = value;
97
+ this.__forwardLightsBuffer.ambientLight = value;
98
+ }
99
+
100
+ /**
101
+ * Adds a mesh, a light or a camera. Adding an item that is already in the scene does nothing.
102
+ * - Mesh: drawn by {@link render}; gets the scene camera.
103
+ * - Ambient light: added to {@link ambientLights} (use {@link activeAmbientLight} to make it the lit one).
104
+ * - Directional / point light: added to the lights buffer.
105
+ * - Camera: becomes the scene {@link camera}.
106
+ * Constructors given a scene (meshes, lights, the perspective camera) call this themselves.
107
+ * @param item The item to add.
108
+ * @throws Error when all slots for a light kind are in use (3 directional, 5 point), or for an unsupported item.
109
+ */
110
+ public add(item: SceneItem): void {
111
+ if (item instanceof AmbientLight) {
112
+ this._addAmbientLight(item);
113
+ } else if (item instanceof DirectionalLight || item instanceof PointLight) {
114
+ this.__forwardLightsBuffer.add(item);
115
+ } else if (Scene._isCamera(item)) {
116
+ this.camera = item;
117
+ } else if (Scene._isMesh(item)) {
118
+ if (!this._meshes.includes(item)) {
119
+ this._meshes.push(item);
120
+ if (this._activeCamera) {
121
+ Scene._assignCameraToMesh(item, this._activeCamera);
122
+ }
123
+ }
124
+ } else {
125
+ throw new Error("Scene.add: expected a mesh, an AmbientLight, DirectionalLight, PointLight or a camera.");
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Removes a mesh, a light or a camera. The mesh is not disposed.
131
+ * Removing the active camera clears {@link camera}.
132
+ * @param item The item to remove.
133
+ * @returns True when the item was in the scene (for a camera: was the active camera).
134
+ * @throws Error when removing the active ambient light (set another {@link activeAmbientLight} first), or for an unsupported item.
135
+ */
136
+ public remove(item: SceneItem): boolean {
137
+ if (item instanceof AmbientLight) {
138
+ if (item === this._activeAmbientLight) {
139
+ throw new Error("Scene.remove: cannot remove the active ambient light; set scene.activeAmbientLight to another light first.");
140
+ }
141
+ return Scene._removeFrom(this.ambientLights, item);
142
+ }
143
+ if (item instanceof DirectionalLight || item instanceof PointLight) {
144
+ return this.__forwardLightsBuffer.remove(item);
145
+ }
146
+ if (Scene._isCamera(item)) {
147
+ if (item !== this._activeCamera) {
148
+ return false;
149
+ }
150
+ this._activeCamera = undefined;
151
+ return true;
152
+ }
153
+ if (Scene._isMesh(item)) {
154
+ return Scene._removeFrom(this._meshes, item);
155
+ }
156
+ throw new Error("Scene.remove: expected a mesh, an AmbientLight, DirectionalLight, PointLight or a camera.");
157
+ }
158
+
159
+ /**
160
+ * Adds a directional or point light.
161
+ * @deprecated Use {@link add}.
162
+ * @throws Error when all slots for that kind are in use (3 directional, 5 point).
163
+ */
164
+ public addLight(light: DirectionalLight | PointLight): void {
165
+ this.add(light);
166
+ }
167
+
168
+ /**
169
+ * Removes a directional or point light.
170
+ * @deprecated Use {@link remove}.
171
+ * @returns True when the light was in the scene.
172
+ */
173
+ public removeLight(light: DirectionalLight | PointLight): boolean {
174
+ return this.remove(light);
175
+ }
176
+
177
+ private _addAmbientLight(ambientLight: AmbientLight): void {
178
+ if (!this.ambientLights.includes(ambientLight)) {
179
+ this.ambientLights.push(ambientLight);
180
+ }
181
+ }
182
+
183
+ private static _removeFrom<T>(list: T[], item: T): boolean {
184
+ const index = list.indexOf(item);
185
+ if (index < 0) {
186
+ return false;
187
+ }
188
+ list.splice(index, 1);
189
+ return true;
190
+ }
191
+
192
+ /** Cameras have the camera buffers and updateBuffers(); meshes and lights do not. */
193
+ private static _isCamera(item: unknown): item is ICamera {
194
+ const camera = item as Partial<ICamera> | null;
195
+ return typeof camera === "object" && camera !== null
196
+ && "projectionViewBuffer" in camera && "viewMatrix" in camera && typeof camera.updateBuffers === "function";
197
+ }
198
+
199
+ /** Meshes have render() and a material slot or submeshes. */
200
+ private static _isMesh(item: unknown): item is IMesh {
201
+ const mesh = item as Partial<IMesh> | null;
202
+ return typeof mesh === "object" && mesh !== null && typeof mesh.render === "function"
203
+ && ("material" in mesh || "subMeshes" in mesh);
204
+ }
205
+
206
+ /**
207
+ * Updates the scene: assigns {@link camera} to the mesh materials that don't have it yet, and uploads changed lights.
208
+ * @param _time The game time.
209
+ * @public
210
+ */
211
+ update(_time: GameTime): void {
212
+ // Also covers meshes whose submeshes were added after the mesh registered itself.
213
+ if (this._activeCamera) {
214
+ for (const mesh of this._meshes) {
215
+ Scene._assignCameraToMesh(mesh, this._activeCamera);
216
+ }
217
+ }
218
+ this.__forwardLightsBuffer.update();
219
+ }
220
+
221
+ private static _assignCameraToMesh(mesh: IMesh, camera: ICamera): void {
222
+ Scene._assignCamera(mesh.material, camera);
223
+ for (const subMesh of mesh.subMeshes ?? []) {
224
+ Scene._assignCamera(subMesh.material, camera);
225
+ }
226
+ }
227
+
228
+ private static _assignCamera(material: IMaterial | undefined, camera: ICamera): void {
229
+ // Only materials that have a camera property; the setter rebinds buffers, so skip when unchanged.
230
+ // A camera that is not initialized yet (e.g. added from its constructor) has no buffers;
231
+ // it is assigned by the next update() once initialize() created them.
232
+ if (!camera.projectionViewBuffer || !camera.positionBuffer) {
233
+ return;
234
+ }
235
+ if (material && "camera" in material && material.camera !== camera) {
236
+ material.camera = camera;
237
+ }
238
+ }
239
+
240
+ /**
241
+ * Renders the meshes.
242
+ */
243
+ render(): void {
244
+
245
+ for (const mesh of this._meshes) {
246
+ mesh.render();
247
+ }
248
+
249
+ }
250
+
251
+ /** Disposes the meshes still in the scene and the lights buffer. Calling it twice does nothing. */
252
+ public dispose(): void {
253
+ if (this._disposed) {
254
+ return;
255
+ }
256
+ this._disposed = true;
257
+ // Each mesh removes itself, so iterate a copy.
258
+ for (const mesh of this._meshes.slice()) {
259
+ mesh.dispose();
260
+ }
261
+ this._meshes.length = 0;
262
+ this.__forwardLightsBuffer.dispose();
263
+ }
264
+ }
@@ -8,8 +8,9 @@ export interface IShaderModuleLoader {
8
8
  /**
9
9
  * Loads a shader module.
10
10
  * @param shaderModuleId - The shader module id.
11
+ * @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.
11
12
  * @returns The shader module.
12
13
  */
13
- load(shaderModuleId: string): IShaderModule;
14
+ load(shaderModuleId: string, keyValueDictionary?: {[id:string]: number}): IShaderModule;
14
15
 
15
16
  }
@@ -1,12 +1,12 @@
1
1
  import type {
2
2
  IFramework,
3
- ISpriteRenderPipeline,
4
3
  ITexture2D,
5
4
  IUniformBuffer,
6
5
  Color,
7
6
  IDisposable, ISampler
8
7
  } from "../index.ts";
9
8
  import {SpriteBatchMesh} from "./SpriteBatchMesh.ts";
9
+ import type {SpriteMaterial} from "../material/SpriteMaterial.ts";
10
10
  import type {vec2, vec3} from "gl-matrix";
11
11
 
12
12
  /**
@@ -19,7 +19,7 @@ export class SpriteBatchDrawable implements IDisposable {
19
19
  private readonly _sampler: ISampler;
20
20
  private _maxBatchSize: number;
21
21
  private _drawingMesh: SpriteBatchMesh = null!;
22
- private _renderPipeline: ISpriteRenderPipeline = null!;
22
+ private _material: SpriteMaterial = null!;
23
23
 
24
24
  private _needsResize = false;
25
25
 
@@ -54,13 +54,13 @@ export class SpriteBatchDrawable implements IDisposable {
54
54
 
55
55
  /** Initialize the drawable */
56
56
  public initialize(): void {
57
- this._renderPipeline = this._framework
58
- .renderPipelineFactory
59
- .createSpriteRenderPipeline(this.projectionViewBuffer);
60
- this._renderPipeline.spriteTexture = this._texture;
61
- this._renderPipeline.textureSampler = this._sampler;
57
+ this._material = this._framework.materialFactory.createSpriteMaterial({
58
+ projectionViewBuffer: this.projectionViewBuffer,
59
+ texture: this._texture,
60
+ sampler: this._sampler,
61
+ });
62
62
 
63
- this._drawingMesh = new SpriteBatchMesh(this._framework, this._maxBatchSize);
63
+ this._drawingMesh = new SpriteBatchMesh(this._framework, this._material, this._maxBatchSize);
64
64
  this._drawingMesh.initialize();
65
65
  }
66
66
 
@@ -115,14 +115,10 @@ export class SpriteBatchDrawable implements IDisposable {
115
115
  }
116
116
 
117
117
  // Draw mesh.
118
- const indicesOffset = this._fromInstance * 6;
119
- const indicesCount = this._toInstance * 6 - indicesOffset;
118
+ this._drawingMesh.indicesOffset = this._fromInstance * 6;
119
+ this._drawingMesh.indicesCount = this._toInstance * 6 - this._drawingMesh.indicesOffset;
120
120
  this._drawingMesh.applyChanges();
121
- this._renderPipeline.render(
122
- this._drawingMesh.vertexBuffer!,
123
- this._drawingMesh.indexBuffer!,
124
- indicesCount,
125
- indicesOffset);
121
+ this._drawingMesh.draw();
126
122
  this._fromInstance = this._toInstance;
127
123
  }
128
124
 
@@ -140,7 +136,7 @@ export class SpriteBatchDrawable implements IDisposable {
140
136
 
141
137
  /** @inheritDoc */
142
138
  public dispose(): void {
143
- this._renderPipeline.dispose();
139
+ this._material.dispose();
144
140
  this._drawingMesh.dispose();
145
141
  }
146
142
  }