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.
- package/CHANGELOG.md +49 -1
- package/README.md +15 -4
- package/dist/Framework.d.ts +9 -3
- package/dist/Framework.d.ts.map +1 -1
- package/dist/api/IFramework.d.ts +7 -6
- package/dist/api/IFramework.d.ts.map +1 -1
- package/dist/api/_export.d.ts +2 -0
- package/dist/api/_export.d.ts.map +1 -1
- package/dist/api/camera/CameraFactory.d.ts +3 -2
- package/dist/api/camera/CameraFactory.d.ts.map +1 -1
- package/dist/api/camera/ICamera.d.ts +5 -0
- package/dist/api/camera/ICamera.d.ts.map +1 -1
- package/dist/api/camera/ICameraFactory.d.ts +5 -6
- package/dist/api/camera/ICameraFactory.d.ts.map +1 -1
- package/dist/api/camera/OrthographicCamera.d.ts +7 -0
- package/dist/api/camera/OrthographicCamera.d.ts.map +1 -1
- package/dist/api/camera/PerspectiveCamera.d.ts +30 -6
- package/dist/api/camera/PerspectiveCamera.d.ts.map +1 -1
- package/dist/api/content/ContentManager.d.ts +67 -2
- package/dist/api/content/ContentManager.d.ts.map +1 -1
- package/dist/api/content/IContentManager.d.ts +32 -1
- package/dist/api/content/IContentManager.d.ts.map +1 -1
- package/dist/api/content/ShaderModuleContent.d.ts.map +1 -1
- package/dist/api/data/Color.d.ts +5 -0
- package/dist/api/data/Color.d.ts.map +1 -1
- package/dist/api/geometry/BaseGeometry.d.ts +9 -0
- package/dist/api/geometry/BaseGeometry.d.ts.map +1 -1
- package/dist/api/geometry/GeometryBuilder.d.ts +3 -1
- package/dist/api/geometry/GeometryBuilder.d.ts.map +1 -1
- package/dist/api/geometry/IGeometryBuilder.d.ts +24 -0
- package/dist/api/geometry/IGeometryBuilder.d.ts.map +1 -1
- package/dist/api/geometry/_export-geometry.d.ts +1 -0
- package/dist/api/geometry/_export-geometry.d.ts.map +1 -1
- package/dist/api/input/InputManager.d.ts +4 -0
- package/dist/api/input/InputManager.d.ts.map +1 -1
- package/dist/api/lights/AmbientLight.d.ts +35 -0
- package/dist/api/lights/AmbientLight.d.ts.map +1 -0
- package/dist/api/lights/DirectionalLight.d.ts +40 -0
- package/dist/api/lights/DirectionalLight.d.ts.map +1 -0
- package/dist/api/lights/LightFactory.d.ts +18 -0
- package/dist/api/lights/LightFactory.d.ts.map +1 -0
- package/dist/api/lights/LightId.d.ts +6 -0
- package/dist/api/lights/LightId.d.ts.map +1 -0
- package/dist/api/lights/LightsBuffer.d.ts +70 -0
- package/dist/api/lights/LightsBuffer.d.ts.map +1 -0
- package/dist/api/lights/PointLight.d.ts +59 -0
- package/dist/api/lights/PointLight.d.ts.map +1 -0
- package/dist/api/lights/_export.d.ts +6 -0
- package/dist/api/lights/_export.d.ts.map +1 -0
- package/dist/api/loaders/ObjGeometryMapper.d.ts +19 -0
- package/dist/api/loaders/ObjGeometryMapper.d.ts.map +1 -0
- package/dist/api/loaders/_export.d.ts +1 -0
- package/dist/api/loaders/_export.d.ts.map +1 -1
- package/dist/api/loaders/obj/ObjLoader.d.ts +29 -0
- package/dist/api/loaders/obj/ObjLoader.d.ts.map +1 -0
- package/dist/api/loaders/obj/ObjTypes.d.ts +103 -0
- package/dist/api/loaders/obj/ObjTypes.d.ts.map +1 -0
- package/dist/api/loaders/obj/index.d.ts +3 -0
- package/dist/api/loaders/obj/index.d.ts.map +1 -0
- package/dist/api/material/ForwardBlinnPhongMaterial.d.ts +127 -0
- package/dist/api/material/ForwardBlinnPhongMaterial.d.ts.map +1 -0
- package/dist/api/material/IMaterial.d.ts +15 -6
- package/dist/api/material/IMaterial.d.ts.map +1 -1
- package/dist/api/material/IMaterialFactory.d.ts +17 -2
- package/dist/api/material/IMaterialFactory.d.ts.map +1 -1
- package/dist/api/material/InspectTextureMipsMaterial.d.ts +16 -7
- package/dist/api/material/InspectTextureMipsMaterial.d.ts.map +1 -1
- package/dist/api/material/MaterialFactory.d.ts +6 -0
- package/dist/api/material/MaterialFactory.d.ts.map +1 -1
- package/dist/api/material/SpriteMaterial.d.ts +51 -0
- package/dist/api/material/SpriteMaterial.d.ts.map +1 -0
- package/dist/api/material/UnlitMaterial.d.ts +28 -10
- package/dist/api/material/UnlitMaterial.d.ts.map +1 -1
- package/dist/api/material/_export.d.ts +2 -0
- package/dist/api/material/_export.d.ts.map +1 -1
- package/dist/api/meshes/IMesh.d.ts +35 -5
- package/dist/api/meshes/IMesh.d.ts.map +1 -1
- package/dist/api/meshes/IMeshFactory.d.ts +25 -4
- package/dist/api/meshes/IMeshFactory.d.ts.map +1 -1
- package/dist/api/meshes/Mesh.d.ts +58 -6
- package/dist/api/meshes/Mesh.d.ts.map +1 -1
- package/dist/api/meshes/MeshFactory.d.ts +7 -3
- package/dist/api/meshes/MeshFactory.d.ts.map +1 -1
- package/dist/api/meshes/QuadMesh.d.ts +5 -1
- package/dist/api/meshes/QuadMesh.d.ts.map +1 -1
- package/dist/api/meshes/SubMesh.d.ts +47 -0
- package/dist/api/meshes/SubMesh.d.ts.map +1 -0
- package/dist/api/meshes/_export.d.ts +1 -0
- package/dist/api/meshes/_export.d.ts.map +1 -1
- package/dist/api/rendering/render-pipelines/IForwardBlinnPhongRenderPipeline.d.ts +56 -0
- package/dist/api/rendering/render-pipelines/IForwardBlinnPhongRenderPipeline.d.ts.map +1 -0
- package/dist/api/rendering/render-pipelines/IRenderPipeline.d.ts +3 -0
- package/dist/api/rendering/render-pipelines/IRenderPipeline.d.ts.map +1 -1
- package/dist/api/rendering/render-pipelines/IRenderPipelineFactory.d.ts +29 -0
- package/dist/api/rendering/render-pipelines/IRenderPipelineFactory.d.ts.map +1 -1
- package/dist/api/rendering/render-pipelines/_export-render-pipelines.d.ts +1 -0
- package/dist/api/rendering/render-pipelines/_export-render-pipelines.d.ts.map +1 -1
- package/dist/api/scene/Scene.d.ts +104 -0
- package/dist/api/scene/Scene.d.ts.map +1 -0
- package/dist/api/shader/IShaderModuleLoader.d.ts +4 -1
- package/dist/api/shader/IShaderModuleLoader.d.ts.map +1 -1
- package/dist/api/shader/{shader-loader.d.ts → ShaderLoader.d.ts} +1 -1
- package/dist/api/shader/ShaderLoader.d.ts.map +1 -0
- package/dist/api/sprites/SpriteBatchDrawable.d.ts +1 -1
- package/dist/api/sprites/SpriteBatchDrawable.d.ts.map +1 -1
- package/dist/api/sprites/SpriteBatchMesh.d.ts +14 -1
- package/dist/api/sprites/SpriteBatchMesh.d.ts.map +1 -1
- package/dist/api/utilities/GeometryUtilities.d.ts.map +1 -1
- package/dist/api/window/WindowManager.d.ts +4 -0
- package/dist/api/window/WindowManager.d.ts.map +1 -1
- package/dist/core/rendering/vertex-buffer-layout.d.ts +1 -0
- package/dist/core/rendering/vertex-buffer-layout.d.ts.map +1 -1
- package/dist/index.js +2252 -260
- package/dist/webgl/WebGlGraphicsDevice.d.ts.map +1 -1
- package/dist/webgl/lights/WebGlAmbientLightFactory.d.ts +17 -0
- package/dist/webgl/lights/WebGlAmbientLightFactory.d.ts.map +1 -0
- package/dist/webgl/lights/WebGlLightsBuffer.d.ts +55 -0
- package/dist/webgl/lights/WebGlLightsBuffer.d.ts.map +1 -0
- package/dist/webgl/render-pipelines/AWebGlRenderPipeline.d.ts +3 -1
- package/dist/webgl/render-pipelines/AWebGlRenderPipeline.d.ts.map +1 -1
- package/dist/webgl/render-pipelines/WebGlMainRenderTargetRenderPipeline.d.ts +3 -0
- package/dist/webgl/render-pipelines/WebGlMainRenderTargetRenderPipeline.d.ts.map +1 -1
- package/dist/webgl/render-pipelines/WebGlRenderPipelineFactory.d.ts +3 -1
- package/dist/webgl/render-pipelines/WebGlRenderPipelineFactory.d.ts.map +1 -1
- package/dist/webgl/render-pipelines/inspect/WebGlInspectTextureMipsRenderPipeline.d.ts +3 -1
- package/dist/webgl/render-pipelines/inspect/WebGlInspectTextureMipsRenderPipeline.d.ts.map +1 -1
- package/dist/webgl/render-pipelines/material/WebGlBlinnPhongRenderPipeline.d.ts +88 -0
- package/dist/webgl/render-pipelines/material/WebGlBlinnPhongRenderPipeline.d.ts.map +1 -0
- package/dist/webgl/render-pipelines/material/WebGlUnlitRenderPipeline.d.ts +3 -1
- package/dist/webgl/render-pipelines/material/WebGlUnlitRenderPipeline.d.ts.map +1 -1
- package/dist/webgl/render-pipelines/sprite/WebGlSpriteRenderPipeline.d.ts +3 -1
- package/dist/webgl/render-pipelines/sprite/WebGlSpriteRenderPipeline.d.ts.map +1 -1
- package/dist/webgl/shader/WebGlShaderModule.d.ts +4 -1
- package/dist/webgl/shader/WebGlShaderModule.d.ts.map +1 -1
- package/dist/webgl/shader/WebGlShaderModuleLoader.d.ts +16 -5
- package/dist/webgl/shader/WebGlShaderModuleLoader.d.ts.map +1 -1
- package/dist/webgl/texture/WebGlTexture2D.d.ts +8 -1
- package/dist/webgl/texture/WebGlTexture2D.d.ts.map +1 -1
- package/dist/webgl/utilities/webgl-filter-anisotropic-utilities.d.ts +0 -1
- package/dist/webgl/utilities/webgl-filter-anisotropic-utilities.d.ts.map +1 -1
- package/package.json +4 -4
- package/shaders/glsl/forward_blinn_phong_material_fs.glsl +110 -0
- package/shaders/glsl/forward_blinn_phong_material_vs.glsl +32 -0
- package/shaders/glsl/sprite_fs.glsl +1 -1
- package/shaders/glsl/unlit_material_fs.glsl +1 -1
- package/src/Framework.ts +37 -13
- package/src/api/IFramework.ts +8 -6
- package/src/api/_export.ts +2 -0
- package/src/api/camera/CameraFactory.ts +6 -6
- package/src/api/camera/ICamera.ts +6 -0
- package/src/api/camera/ICameraFactory.ts +5 -10
- package/src/api/camera/OrthographicCamera.ts +23 -1
- package/src/api/camera/PerspectiveCamera.ts +60 -15
- package/src/api/content/ContentManager.ts +201 -5
- package/src/api/content/IContentManager.ts +33 -1
- package/src/api/content/ShaderModuleContent.ts +9 -0
- package/src/api/data/Color.ts +7 -1
- package/src/api/geometry/BaseGeometry.ts +23 -0
- package/src/api/geometry/GeometryBuilder.ts +14 -2
- package/src/api/geometry/IGeometryBuilder.ts +31 -1
- package/src/api/geometry/_export-geometry.ts +2 -1
- package/src/api/input/InputManager.ts +14 -5
- package/src/api/lights/AmbientLight.ts +53 -0
- package/src/api/lights/DirectionalLight.ts +60 -0
- package/src/api/lights/LightFactory.ts +21 -0
- package/src/api/lights/LightId.ts +9 -0
- package/src/api/lights/LightsBuffer.ts +87 -0
- package/src/api/lights/PointLight.ts +91 -0
- package/src/api/lights/_export.ts +5 -0
- package/src/api/loaders/ObjGeometryMapper.ts +28 -0
- package/src/api/loaders/_export.ts +2 -1
- package/src/api/loaders/obj/ObjLoader.ts +270 -0
- package/src/api/loaders/obj/ObjTypes.ts +124 -0
- package/src/api/loaders/obj/index.ts +5 -0
- package/src/api/material/ForwardBlinnPhongMaterial.ts +302 -0
- package/src/api/material/IMaterial.ts +16 -6
- package/src/api/material/IMaterialFactory.ts +19 -2
- package/src/api/material/InspectTextureMipsMaterial.ts +63 -24
- package/src/api/material/MaterialFactory.ts +12 -0
- package/src/api/material/SpriteMaterial.ts +83 -0
- package/src/api/material/UnlitMaterial.ts +101 -29
- package/src/api/material/_export.ts +3 -1
- package/src/api/meshes/IMesh.ts +45 -6
- package/src/api/meshes/IMeshFactory.ts +27 -4
- package/src/api/meshes/Mesh.ts +163 -41
- package/src/api/meshes/MeshFactory.ts +30 -6
- package/src/api/meshes/QuadMesh.ts +6 -2
- package/src/api/meshes/SubMesh.ts +71 -0
- package/src/api/meshes/_export.ts +1 -0
- package/src/api/rendering/render-pipelines/IForwardBlinnPhongRenderPipeline.ts +66 -0
- package/src/api/rendering/render-pipelines/IRenderPipeline.ts +4 -0
- package/src/api/rendering/render-pipelines/IRenderPipelineFactory.ts +39 -0
- package/src/api/rendering/render-pipelines/_export-render-pipelines.ts +2 -1
- package/src/api/scene/Scene.ts +264 -0
- package/src/api/shader/IShaderModuleLoader.ts +2 -1
- package/src/api/sprites/SpriteBatchDrawable.ts +12 -16
- package/src/api/sprites/SpriteBatchMesh.ts +23 -4
- package/src/api/utilities/GeometryUtilities.ts +2 -0
- package/src/api/window/WindowManager.ts +22 -2
- package/src/core/rendering/vertex-buffer-layout.ts +32 -1
- package/src/main.ts +0 -1
- package/src/webgl/WebGlGraphicsDevice.ts +0 -1
- package/src/webgl/lights/WebGlAmbientLightFactory.ts +35 -0
- package/src/webgl/lights/WebGlLightsBuffer.ts +215 -0
- package/src/webgl/render-pipelines/AWebGlRenderPipeline.ts +7 -1
- package/src/webgl/render-pipelines/WebGlMainRenderTargetRenderPipeline.ts +3 -0
- package/src/webgl/render-pipelines/WebGlRenderPipelineFactory.ts +24 -2
- package/src/webgl/render-pipelines/inspect/WebGlInspectTextureMipsRenderPipeline.ts +16 -6
- package/src/webgl/render-pipelines/material/WebGlBlinnPhongRenderPipeline.ts +300 -0
- package/src/webgl/render-pipelines/material/WebGlUnlitRenderPipeline.ts +16 -6
- package/src/webgl/render-pipelines/sprite/WebGlSpriteRenderPipeline.ts +11 -7
- package/src/webgl/shader/WebGlShaderModule.ts +11 -4
- package/src/webgl/shader/WebGlShaderModuleLoader.ts +29 -10
- package/src/webgl/texture/WebGlTexture2D.ts +19 -5
- package/src/webgl/utilities/webgl-filter-anisotropic-utilities.ts +9 -6
- package/dist/api/shader/shader-loader.d.ts.map +0 -1
- /package/src/api/shader/{shader-loader.ts → ShaderLoader.ts} +0 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import type {IMesh} from "../meshes/IMesh.ts";
|
|
2
|
+
import type {ITexture2D} from "../rendering/texture/ITexture2D.ts";
|
|
3
|
+
import type {ISampler} from "../rendering/sampler/ISampler.ts";
|
|
4
|
+
import {GeometryFormat} from "../geometry/GeometryFormat.ts";
|
|
5
|
+
import type {IUniformBuffer} from "../rendering/buffers/IUniformBuffer.ts";
|
|
6
|
+
import type {IIndexBuffer} from "../rendering/buffers/IIndexBuffer.ts";
|
|
7
|
+
import type {IVertexBuffer} from "../rendering/buffers/IVertexBuffer.ts";
|
|
8
|
+
import type {IFramework} from "../IFramework.ts";
|
|
9
|
+
import {BufferUsage} from "../rendering/buffers/BufferUsage.ts";
|
|
10
|
+
import {Color} from "../data/Color.ts";
|
|
11
|
+
import type {IPrimitiveState} from "../rendering/primitive/IPrimitiveState.ts";
|
|
12
|
+
import type {ICamera} from "../camera/ICamera.ts";
|
|
13
|
+
import type {LightsBuffer} from "../lights/LightsBuffer.ts";
|
|
14
|
+
import type {IForwardBlinnPhongRenderPipeline} from "../rendering/render-pipelines/IForwardBlinnPhongRenderPipeline.ts";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The additional material config options.
|
|
18
|
+
* @public
|
|
19
|
+
**/
|
|
20
|
+
export class ForwardBlinnPhongMaterialDescriptor {
|
|
21
|
+
|
|
22
|
+
/** The primitive state. */
|
|
23
|
+
public primitiveState?: IPrimitiveState;
|
|
24
|
+
|
|
25
|
+
/** The projection view buffer. */
|
|
26
|
+
public projectionViewBuffer!: IUniformBuffer;
|
|
27
|
+
|
|
28
|
+
/** The camera position buffer. */
|
|
29
|
+
public cameraPositionBuffer!: IUniformBuffer;
|
|
30
|
+
|
|
31
|
+
/** The lights buffer. The shader is compiled for its light counts. Can also be set later through `material.lightsBuffer`. */
|
|
32
|
+
public lightsBuffer?: LightsBuffer;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The Blinn-Phong material with forward calculated lighting.
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export class ForwardBlinnPhongMaterial {
|
|
41
|
+
|
|
42
|
+
private readonly _framework: IFramework;
|
|
43
|
+
|
|
44
|
+
private _lightsBuffer?: LightsBuffer;
|
|
45
|
+
private _diffuseColorCache: Color;
|
|
46
|
+
private _ambientColorCache: Color;
|
|
47
|
+
private _specularColorCache: Color;
|
|
48
|
+
private _shininessCache: number;
|
|
49
|
+
|
|
50
|
+
// std140: each vec3 starts on a 16-byte boundary, so pad each colour to 4 floats.
|
|
51
|
+
private _materialData = [
|
|
52
|
+
0.1, 0.1, 0.1, 0, // Ambient
|
|
53
|
+
0.8, 0.8, 0.8, 0, // Diffuse
|
|
54
|
+
0.5, 0.5, 0.5, 32, // Specular, shininess (the float fills the vec3's padding)
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
/** Created when the model buffer is assigned (the pipeline requires one). */
|
|
58
|
+
private _pipeline?: IForwardBlinnPhongRenderPipeline;
|
|
59
|
+
private readonly _primitiveState?: IPrimitiveState;
|
|
60
|
+
/** Texture and sampler set before the pipeline exists; applied when it is created. */
|
|
61
|
+
private _pendingTexture?: ITexture2D;
|
|
62
|
+
private _pendingSampler?: ISampler;
|
|
63
|
+
private _samplerSet = false;
|
|
64
|
+
private _projectionViewBuffer: IUniformBuffer;
|
|
65
|
+
private _camera?: ICamera;
|
|
66
|
+
private _cameraPositionBuffer: IUniformBuffer;
|
|
67
|
+
private _materialBuffer: IUniformBuffer;
|
|
68
|
+
private _modelBuffer?: IUniformBuffer;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The constructor.
|
|
72
|
+
* @param framework The framework.
|
|
73
|
+
* @param descriptor The material descriptor. Its projection view and camera position buffers are shared and are not disposed by this material.
|
|
74
|
+
* @throws Error when `descriptor`, `descriptor.projectionViewBuffer` or `descriptor.cameraPositionBuffer` is missing.
|
|
75
|
+
*/
|
|
76
|
+
public constructor(framework: IFramework, descriptor: ForwardBlinnPhongMaterialDescriptor) {
|
|
77
|
+
|
|
78
|
+
// Runtime check: JavaScript callers can omit the required buffers.
|
|
79
|
+
const missing = [
|
|
80
|
+
descriptor?.projectionViewBuffer ? undefined : "descriptor.projectionViewBuffer",
|
|
81
|
+
descriptor?.cameraPositionBuffer ? undefined : "descriptor.cameraPositionBuffer",
|
|
82
|
+
].filter(x => x !== undefined);
|
|
83
|
+
if (missing.length > 0) {
|
|
84
|
+
throw new Error(`ForwardBlinnPhongMaterial: ${missing.join(" and ")} ${missing.length > 1 ? "are" : "is"} required (e.g. camera.projectionViewBuffer and camera.positionBuffer).`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this._framework = framework;
|
|
88
|
+
const usage = BufferUsage.UNIFORM | BufferUsage.COPY_DST;
|
|
89
|
+
this._diffuseColorCache = this.diffuseColor.copy();
|
|
90
|
+
this._ambientColorCache = this.ambientColor.copy();
|
|
91
|
+
this._specularColorCache = this.specularColor.copy();
|
|
92
|
+
this._shininessCache = this.shininess;
|
|
93
|
+
this._projectionViewBuffer = descriptor.projectionViewBuffer;
|
|
94
|
+
this._cameraPositionBuffer = descriptor.cameraPositionBuffer;
|
|
95
|
+
this._materialBuffer = framework.bufferFactory.createUniformBuffer(this._materialData, usage);
|
|
96
|
+
this._primitiveState = descriptor.primitiveState;
|
|
97
|
+
this._lightsBuffer = descriptor.lightsBuffer;
|
|
98
|
+
// The pipeline is created once a model buffer is assigned (see modelBuffer).
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Creates the pipeline with the current bindings. */
|
|
102
|
+
private _createPipeline(modelBuffer: IUniformBuffer): IForwardBlinnPhongRenderPipeline {
|
|
103
|
+
const pipeline = this._framework.renderPipelineFactory.createForwardBlinnPhongRenderPipeline({
|
|
104
|
+
projectionViewBuffer: this._projectionViewBuffer,
|
|
105
|
+
modelBuffer,
|
|
106
|
+
materialBuffer: this._materialBuffer,
|
|
107
|
+
cameraPositionBuffer: this._cameraPositionBuffer,
|
|
108
|
+
lightsBuffer: this._lightsBuffer?.buffer,
|
|
109
|
+
maxDirectionalLights: this._lightsBuffer?.maxDirectionalLights,
|
|
110
|
+
maxPointLights: this._lightsBuffer?.maxPointLights,
|
|
111
|
+
primitiveState: this._primitiveState,
|
|
112
|
+
});
|
|
113
|
+
if (this._pendingTexture) {
|
|
114
|
+
pipeline.diffuseTexture = this._pendingTexture;
|
|
115
|
+
}
|
|
116
|
+
if (this._samplerSet) {
|
|
117
|
+
pipeline.diffuseTextureSampler = this._pendingSampler;
|
|
118
|
+
}
|
|
119
|
+
this._pipeline = pipeline;
|
|
120
|
+
return pipeline;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The ambient color of this material.
|
|
125
|
+
* Alpha is ignored. By default, it is (0.1,0.1,0.1).
|
|
126
|
+
*/
|
|
127
|
+
public ambientColor: Color = new Color(0.1, 0.1, 0.1);
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The diffuse color of this material.
|
|
131
|
+
* Alpha is ignored. By default, it is (0.8,0.8,0.8).
|
|
132
|
+
*/
|
|
133
|
+
public diffuseColor: Color = new Color(0.8, 0.8, 0.8);
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The specular color of this material (MTL `Ks`).
|
|
137
|
+
* Alpha is ignored. By default, it is (0.5,0.5,0.5).
|
|
138
|
+
*/
|
|
139
|
+
public specularColor: Color = new Color(0.5, 0.5, 0.5);
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The specular exponent (MTL `Ns`). Higher values give smaller, sharper highlights. By default, it is 32.
|
|
143
|
+
*/
|
|
144
|
+
public shininess = 32;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The camera. Setting it binds the camera's projection view and position buffers to the pipeline
|
|
148
|
+
* (used by the scene when its camera changes). Setting undefined keeps the last bound buffers.
|
|
149
|
+
* Undefined until set: the constructor only takes the buffers.
|
|
150
|
+
*/
|
|
151
|
+
public get camera(): ICamera | undefined {
|
|
152
|
+
return this._camera;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** @inheritDoc */
|
|
156
|
+
public set camera(camera: ICamera | undefined) {
|
|
157
|
+
if (camera === this._camera) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
this._camera = camera;
|
|
161
|
+
if (!camera) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (this._projectionViewBuffer !== camera.projectionViewBuffer) {
|
|
165
|
+
this._projectionViewBuffer = camera.projectionViewBuffer;
|
|
166
|
+
if (this._pipeline) {
|
|
167
|
+
this._pipeline.projectionViewBuffer = this._projectionViewBuffer;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (this._cameraPositionBuffer !== camera.positionBuffer) {
|
|
171
|
+
this._cameraPositionBuffer = camera.positionBuffer;
|
|
172
|
+
if (this._pipeline) {
|
|
173
|
+
this._pipeline.cameraPositionBuffer = this._cameraPositionBuffer;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** The lights buffer with the scene's lights. Shared, not disposed by this material. */
|
|
179
|
+
public get lightsBuffer(): LightsBuffer | undefined {
|
|
180
|
+
return this._lightsBuffer;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** The lights buffer with the scene's lights. Shared, not disposed by this material. */
|
|
184
|
+
public set lightsBuffer(lightsBuffer: LightsBuffer | undefined) {
|
|
185
|
+
this._lightsBuffer = lightsBuffer;
|
|
186
|
+
if (lightsBuffer && this._pipeline) {
|
|
187
|
+
// Switches to the shader variant for this buffer's light counts when they differ.
|
|
188
|
+
this._pipeline.setLightCounts(lightsBuffer.maxDirectionalLights, lightsBuffer.maxPointLights);
|
|
189
|
+
this._pipeline.lightsBuffer = lightsBuffer.buffer;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The texture to inspect.
|
|
195
|
+
*/
|
|
196
|
+
public get diffuseTexture(): ITexture2D {
|
|
197
|
+
return this._pipeline?.diffuseTexture ?? this._pendingTexture!;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The texture to inspect.
|
|
202
|
+
*/
|
|
203
|
+
public set diffuseTexture(value: ITexture2D) {
|
|
204
|
+
this._pendingTexture = value;
|
|
205
|
+
if (this._pipeline) {
|
|
206
|
+
this._pipeline.diffuseTexture = value;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The texture sampler.
|
|
212
|
+
*/
|
|
213
|
+
public get diffuseTextureSampler(): ISampler | undefined {
|
|
214
|
+
return this._pipeline ? this._pipeline.diffuseTextureSampler : this._pendingSampler;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The texture sampler.
|
|
219
|
+
*/
|
|
220
|
+
public set diffuseTextureSampler(value: ISampler | undefined) {
|
|
221
|
+
this._pendingSampler = value;
|
|
222
|
+
this._samplerSet = true;
|
|
223
|
+
if (this._pipeline) {
|
|
224
|
+
this._pipeline.diffuseTextureSampler = value;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* The model buffer (std140 `mat4`). Owned by the mesh, which assigns it before drawing; never disposed by this material.
|
|
230
|
+
* Undefined until assigned: rendering without one throws.
|
|
231
|
+
*/
|
|
232
|
+
public get modelBuffer(): IUniformBuffer | undefined {
|
|
233
|
+
return this._modelBuffer;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Binds the model buffer to the pipeline. */
|
|
237
|
+
public set modelBuffer(value: IUniformBuffer | undefined) {
|
|
238
|
+
if (value === this._modelBuffer) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
this._modelBuffer = value;
|
|
242
|
+
if (!value) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (this._pipeline) {
|
|
246
|
+
this._pipeline.modelBuffer = value;
|
|
247
|
+
} else {
|
|
248
|
+
this._createPipeline(value);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* The geometry format.
|
|
254
|
+
*/
|
|
255
|
+
public readonly geometryFormat = GeometryFormat.POS3_COLOR4_TEXTURECOORDS2_NORMAL3;
|
|
256
|
+
|
|
257
|
+
/** @inheritDoc */
|
|
258
|
+
public beforeRender(): void {
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
if (!this._diffuseColorCache.equals(this.diffuseColor)
|
|
262
|
+
|| !this._ambientColorCache.equals(this.ambientColor)
|
|
263
|
+
|| !this._specularColorCache.equals(this.specularColor)
|
|
264
|
+
|| this._shininessCache !== this.shininess) {
|
|
265
|
+
this._ambientColorCache.set(this.ambientColor);
|
|
266
|
+
this._diffuseColorCache.set(this.diffuseColor);
|
|
267
|
+
this._specularColorCache.set(this.specularColor);
|
|
268
|
+
this._shininessCache = this.shininess;
|
|
269
|
+
this._materialData[0] = this._ambientColorCache.r;
|
|
270
|
+
this._materialData[1] = this._ambientColorCache.g;
|
|
271
|
+
this._materialData[2] = this._ambientColorCache.b;
|
|
272
|
+
this._materialData[4] = this._diffuseColorCache.r;
|
|
273
|
+
this._materialData[5] = this._diffuseColorCache.g;
|
|
274
|
+
this._materialData[6] = this._diffuseColorCache.b;
|
|
275
|
+
this._materialData[8] = this._specularColorCache.r;
|
|
276
|
+
this._materialData[9] = this._specularColorCache.g;
|
|
277
|
+
this._materialData[10] = this._specularColorCache.b;
|
|
278
|
+
this._materialData[11] = this._shininessCache;
|
|
279
|
+
this._materialBuffer.update(this._materialData);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** @inheritDoc */
|
|
284
|
+
public renderMesh(mesh: IMesh): void {
|
|
285
|
+
this.render(mesh.vertexBuffer!, mesh.indexBuffer!, mesh.indexCount ?? -1, mesh.indexStart ?? 0);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** @inheritDoc */
|
|
289
|
+
public render(vertexBuffer: IVertexBuffer, indexBuffer: IIndexBuffer, indicesCount?: number, indicesOffset?: number): void {
|
|
290
|
+
if (!this._modelBuffer) {
|
|
291
|
+
throw new Error("ForwardBlinnPhongMaterial: no modelBuffer. Render it through a mesh (the mesh assigns its model buffer) or set material.modelBuffer.");
|
|
292
|
+
}
|
|
293
|
+
(this._pipeline ?? this._createPipeline(this._modelBuffer)).render(vertexBuffer, indexBuffer, indicesCount, indicesOffset);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** @inheritDoc */
|
|
297
|
+
public dispose(): void {
|
|
298
|
+
this._pipeline?.dispose();
|
|
299
|
+
this._pipeline = undefined;
|
|
300
|
+
this._materialBuffer.dispose();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
@@ -1,23 +1,31 @@
|
|
|
1
|
-
import {mat4} from "gl-matrix";
|
|
2
1
|
import type { IDisposable } from "../core/IDisposable.ts";
|
|
3
2
|
import {GeometryFormat} from "../geometry/GeometryFormat.ts";
|
|
4
3
|
import type { IMesh } from "../meshes/IMesh.ts";
|
|
5
4
|
import type { IVertexBuffer } from "../rendering/buffers/IVertexBuffer.ts";
|
|
6
5
|
import type { IIndexBuffer } from "../rendering/buffers/IIndexBuffer.ts";
|
|
6
|
+
import type { ICamera } from "../camera/ICamera.ts";
|
|
7
|
+
import type { IUniformBuffer } from "../rendering/buffers/IUniformBuffer.ts";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* The material interface.
|
|
11
|
+
* @public
|
|
10
12
|
*/
|
|
11
13
|
export interface IMaterial extends IDisposable {
|
|
12
14
|
|
|
13
15
|
/**
|
|
14
|
-
* The
|
|
16
|
+
* The camera this material renders with, for materials that use one (unlit, Blinn-Phong).
|
|
17
|
+
* A `Scene` assigns its camera to the materials of its meshes in `scene.update()`.
|
|
15
18
|
*/
|
|
16
|
-
|
|
19
|
+
camera?: ICamera;
|
|
17
20
|
|
|
18
21
|
/**
|
|
19
|
-
* The
|
|
22
|
+
* The model buffer (std140 `mat4`) for materials that use one (unlit, Blinn-Phong, inspect mips).
|
|
23
|
+
* A mesh owns it and assigns it before drawing, like the scene assigns its camera; materials never dispose it.
|
|
24
|
+
* Materials without the property (e.g. the sprite material) are left alone.
|
|
20
25
|
*/
|
|
26
|
+
modelBuffer?: IUniformBuffer;
|
|
27
|
+
|
|
28
|
+
/** The geometry format required by the material.*/
|
|
21
29
|
readonly geometryFormat: GeometryFormat;
|
|
22
30
|
|
|
23
31
|
/**
|
|
@@ -26,7 +34,7 @@ export interface IMaterial extends IDisposable {
|
|
|
26
34
|
beforeRender(): void;
|
|
27
35
|
|
|
28
36
|
/**
|
|
29
|
-
* Renders the material.
|
|
37
|
+
* Renders the mesh with this material, honouring the mesh's index range.
|
|
30
38
|
* @param mesh - The mesh.
|
|
31
39
|
*/
|
|
32
40
|
renderMesh(mesh: IMesh): void;
|
|
@@ -35,7 +43,9 @@ export interface IMaterial extends IDisposable {
|
|
|
35
43
|
* Renders the material.
|
|
36
44
|
* @param vertexBuffer - The vertex buffer.
|
|
37
45
|
* @param indexBuffer - The index buffer.
|
|
46
|
+
* @param indicesCount - The number of indices to draw. -1 or undefined draws all.
|
|
47
|
+
* @param indicesOffset - The first index to draw. Defaults to 0.
|
|
38
48
|
*/
|
|
39
|
-
render(vertexBuffer: IVertexBuffer, indexBuffer: IIndexBuffer): void;
|
|
49
|
+
render(vertexBuffer: IVertexBuffer, indexBuffer: IIndexBuffer, indicesCount?: number, indicesOffset?: number): void;
|
|
40
50
|
|
|
41
51
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {InspectTextureMipsMaterial} from "./InspectTextureMipsMaterial.ts";
|
|
2
|
+
import type {SpriteMaterial, SpriteMaterialDescriptor} from "./SpriteMaterial.ts";
|
|
2
3
|
import {UnlitMaterial, UnlitMaterialDescriptor} from "./UnlitMaterial.ts";
|
|
4
|
+
import type {ForwardBlinnPhongMaterial, ForwardBlinnPhongMaterialDescriptor} from "./ForwardBlinnPhongMaterial.ts";
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* The material factory.
|
|
@@ -14,8 +16,23 @@ export interface IMaterialFactory {
|
|
|
14
16
|
|
|
15
17
|
/**
|
|
16
18
|
* Creates an unlit material.
|
|
17
|
-
* @param
|
|
19
|
+
* @param descriptor The optional additional configuration options for material, such as rendering options.
|
|
18
20
|
* @returns The Unlit Material.
|
|
19
21
|
*/
|
|
20
|
-
createUnlitMaterial(
|
|
22
|
+
createUnlitMaterial(descriptor?: UnlitMaterialDescriptor): UnlitMaterial;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Creates a sprite material.
|
|
26
|
+
* @param descriptor The sprite material descriptor with projection view buffer, texture and sampler.
|
|
27
|
+
* @returns The Sprite Material.
|
|
28
|
+
*/
|
|
29
|
+
createSpriteMaterial(descriptor: SpriteMaterialDescriptor): SpriteMaterial;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Creates a Blinn-Phong material with forward calculated lighting.
|
|
33
|
+
* @param descriptor The configuration: the required projection view and camera position buffers, plus optional lights buffer and primitive state.
|
|
34
|
+
* @throws Error when a required buffer is missing.
|
|
35
|
+
* @returns The Forward Blinn-Phong Material.
|
|
36
|
+
*/
|
|
37
|
+
createForwardBlinnPhongMaterial(descriptor: ForwardBlinnPhongMaterialDescriptor): ForwardBlinnPhongMaterial;
|
|
21
38
|
}
|
|
@@ -17,24 +17,39 @@ export class InspectTextureMipsMaterial {
|
|
|
17
17
|
|
|
18
18
|
private readonly _framework: IFramework;
|
|
19
19
|
private _mipLevel = [0];
|
|
20
|
-
|
|
20
|
+
/** Created when the model buffer is assigned (the pipeline requires one). */
|
|
21
|
+
private _pipeline?: IInspectTextureMipsRenderPipeline;
|
|
22
|
+
/** Texture and sampler set before the pipeline exists; applied when it is created. */
|
|
23
|
+
private _pendingTexture?: ITexture2D;
|
|
24
|
+
private _pendingSampler?: ISampler;
|
|
25
|
+
private _samplerSet = false;
|
|
21
26
|
private _projectionViewBuffer: IUniformBuffer;
|
|
22
27
|
private _fragmentDataBuffer: IUniformBuffer;
|
|
23
|
-
private _modelBuffer
|
|
24
|
-
private _previousModelMatrix = mat4.create();
|
|
28
|
+
private _modelBuffer?: IUniformBuffer;
|
|
25
29
|
|
|
26
30
|
public constructor(framework: IFramework) {
|
|
27
31
|
this._framework = framework;
|
|
28
|
-
void this._framework;
|
|
29
32
|
const usage = BufferUsage.UNIFORM | BufferUsage.COPY_DST;
|
|
30
|
-
this._projectionViewBuffer = framework.bufferFactory.createUniformBuffer(
|
|
31
|
-
this._modelBuffer = framework.bufferFactory.createUniformBuffer(this._previousModelMatrix, usage);
|
|
33
|
+
this._projectionViewBuffer = framework.bufferFactory.createUniformBuffer(mat4.create(), usage);
|
|
32
34
|
this._fragmentDataBuffer = framework.bufferFactory.createUniformBuffer(this._mipLevel, usage);
|
|
33
|
-
|
|
35
|
+
// The pipeline is created once a model buffer is assigned (see modelBuffer).
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Creates the pipeline with the current bindings. */
|
|
39
|
+
private _createPipeline(modelBuffer: IUniformBuffer): IInspectTextureMipsRenderPipeline {
|
|
40
|
+
const pipeline = this._framework.renderPipelineFactory.createInspectTextureMipsRenderPipeline(
|
|
34
41
|
this._projectionViewBuffer,
|
|
35
|
-
|
|
42
|
+
modelBuffer,
|
|
36
43
|
this._fragmentDataBuffer,
|
|
37
44
|
);
|
|
45
|
+
if (this._pendingTexture) {
|
|
46
|
+
pipeline.spriteTexture = this._pendingTexture;
|
|
47
|
+
}
|
|
48
|
+
if (this._samplerSet) {
|
|
49
|
+
pipeline.textureSampler = this._pendingSampler;
|
|
50
|
+
}
|
|
51
|
+
this._pipeline = pipeline;
|
|
52
|
+
return pipeline;
|
|
38
53
|
}
|
|
39
54
|
|
|
40
55
|
/**
|
|
@@ -46,34 +61,60 @@ export class InspectTextureMipsMaterial {
|
|
|
46
61
|
* The texture to inspect.
|
|
47
62
|
*/
|
|
48
63
|
public get texture(): ITexture2D {
|
|
49
|
-
return this._pipeline.
|
|
64
|
+
return this._pipeline?.spriteTexture ?? this._pendingTexture!;
|
|
50
65
|
}
|
|
51
66
|
|
|
52
67
|
/**
|
|
53
68
|
* The texture to inspect.
|
|
54
69
|
*/
|
|
55
70
|
public set texture(value: ITexture2D) {
|
|
56
|
-
this.
|
|
71
|
+
this._pendingTexture = value;
|
|
72
|
+
if (this._pipeline) {
|
|
73
|
+
this._pipeline.spriteTexture = value;
|
|
74
|
+
}
|
|
57
75
|
}
|
|
58
76
|
|
|
59
77
|
/**
|
|
60
78
|
* The texture sampler.
|
|
61
79
|
*/
|
|
62
80
|
public get textureSampler(): ISampler | undefined {
|
|
63
|
-
return this._pipeline.textureSampler;
|
|
81
|
+
return this._pipeline ? this._pipeline.textureSampler : this._pendingSampler;
|
|
64
82
|
}
|
|
65
83
|
|
|
66
84
|
/**
|
|
67
85
|
* The texture sampler.
|
|
68
86
|
*/
|
|
69
87
|
public set textureSampler(value: ISampler | undefined) {
|
|
70
|
-
this.
|
|
88
|
+
this._pendingSampler = value;
|
|
89
|
+
this._samplerSet = true;
|
|
90
|
+
if (this._pipeline) {
|
|
91
|
+
this._pipeline.textureSampler = value;
|
|
92
|
+
}
|
|
71
93
|
}
|
|
72
94
|
|
|
73
95
|
/**
|
|
74
|
-
* The model
|
|
96
|
+
* The model buffer (std140 `mat4`). Owned by the mesh, which assigns it before drawing; never disposed by this material.
|
|
97
|
+
* Undefined until assigned: rendering without one throws.
|
|
75
98
|
*/
|
|
76
|
-
public
|
|
99
|
+
public get modelBuffer(): IUniformBuffer | undefined {
|
|
100
|
+
return this._modelBuffer;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Binds the model buffer to the pipeline. */
|
|
104
|
+
public set modelBuffer(value: IUniformBuffer | undefined) {
|
|
105
|
+
if (value === this._modelBuffer) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
this._modelBuffer = value;
|
|
109
|
+
if (!value) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (this._pipeline) {
|
|
113
|
+
this._pipeline.modelBuffer = value;
|
|
114
|
+
} else {
|
|
115
|
+
this._createPipeline(value);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
77
118
|
|
|
78
119
|
/**
|
|
79
120
|
* The geometry format.
|
|
@@ -86,28 +127,26 @@ export class InspectTextureMipsMaterial {
|
|
|
86
127
|
this._mipLevel[0] = this.mipLevel;
|
|
87
128
|
this._fragmentDataBuffer.update(this._mipLevel);
|
|
88
129
|
}
|
|
89
|
-
|
|
90
|
-
if (!mat4.equals(this._previousModelMatrix, this.modelMatrix)) {
|
|
91
|
-
mat4.copy(this._previousModelMatrix, this.modelMatrix);
|
|
92
|
-
this._modelBuffer.update(this._previousModelMatrix);
|
|
93
|
-
}
|
|
94
130
|
}
|
|
95
131
|
|
|
96
132
|
/** @inheritDoc */
|
|
97
133
|
public renderMesh(mesh: IMesh): void {
|
|
98
|
-
this.render(mesh.vertexBuffer!, mesh.indexBuffer
|
|
134
|
+
this.render(mesh.vertexBuffer!, mesh.indexBuffer!, mesh.indexCount ?? -1, mesh.indexStart ?? 0);
|
|
99
135
|
}
|
|
100
136
|
|
|
101
137
|
/** @inheritDoc */
|
|
102
|
-
public render(vertexBuffer: IVertexBuffer, indexBuffer: IIndexBuffer): void {
|
|
103
|
-
this.
|
|
138
|
+
public render(vertexBuffer: IVertexBuffer, indexBuffer: IIndexBuffer, indicesCount?: number, indicesOffset?: number): void {
|
|
139
|
+
if (!this._modelBuffer) {
|
|
140
|
+
throw new Error("InspectTextureMipsMaterial: no modelBuffer. Render it through a mesh (the mesh assigns its model buffer) or set material.modelBuffer.");
|
|
141
|
+
}
|
|
142
|
+
(this._pipeline ?? this._createPipeline(this._modelBuffer)).render(vertexBuffer, indexBuffer, indicesCount, indicesOffset);
|
|
104
143
|
}
|
|
105
144
|
|
|
106
145
|
/** @inheritDoc */
|
|
107
146
|
public dispose(): void {
|
|
108
|
-
this._pipeline
|
|
147
|
+
this._pipeline?.dispose();
|
|
148
|
+
this._pipeline = undefined;
|
|
109
149
|
this._projectionViewBuffer.dispose();
|
|
110
|
-
this._modelBuffer.dispose();
|
|
111
150
|
this._fragmentDataBuffer.dispose();
|
|
112
151
|
}
|
|
113
152
|
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import {InspectTextureMipsMaterial} from "./InspectTextureMipsMaterial.ts";
|
|
2
2
|
import type { IFramework } from "../IFramework.ts";
|
|
3
3
|
import type { IMaterialFactory } from "./IMaterialFactory.ts";
|
|
4
|
+
import {SpriteMaterial, type SpriteMaterialDescriptor} from "./SpriteMaterial.ts";
|
|
4
5
|
import {UnlitMaterial, UnlitMaterialDescriptor} from "./UnlitMaterial.ts";
|
|
6
|
+
import {ForwardBlinnPhongMaterial, type ForwardBlinnPhongMaterialDescriptor} from "./ForwardBlinnPhongMaterial.ts";
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* The material factory.
|
|
@@ -24,4 +26,14 @@ export class MaterialFactory implements IMaterialFactory {
|
|
|
24
26
|
public createUnlitMaterial(descriptor? : UnlitMaterialDescriptor): UnlitMaterial {
|
|
25
27
|
return new UnlitMaterial(this._framework, descriptor );
|
|
26
28
|
}
|
|
29
|
+
|
|
30
|
+
/** @inheritDoc */
|
|
31
|
+
public createSpriteMaterial(descriptor: SpriteMaterialDescriptor): SpriteMaterial {
|
|
32
|
+
return new SpriteMaterial(this._framework, descriptor);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** @inheritDoc */
|
|
36
|
+
public createForwardBlinnPhongMaterial(descriptor: ForwardBlinnPhongMaterialDescriptor): ForwardBlinnPhongMaterial {
|
|
37
|
+
return new ForwardBlinnPhongMaterial(this._framework, descriptor);
|
|
38
|
+
}
|
|
27
39
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { GeometryFormat } from "../geometry/GeometryFormat.ts";
|
|
2
|
+
import type { IMesh } from "../meshes/IMesh.ts";
|
|
3
|
+
import type { IVertexBuffer } from "../rendering/buffers/IVertexBuffer.ts";
|
|
4
|
+
import type { IIndexBuffer } from "../rendering/buffers/IIndexBuffer.ts";
|
|
5
|
+
import type { ITexture2D } from "../rendering/texture/ITexture2D.ts";
|
|
6
|
+
import type { ISampler } from "../rendering/sampler/ISampler.ts";
|
|
7
|
+
import type { IFramework } from "../IFramework.ts";
|
|
8
|
+
import type { IUniformBuffer } from "../rendering/buffers/IUniformBuffer.ts";
|
|
9
|
+
import type { ISpriteRenderPipeline } from "../rendering/render-pipelines/ISpriteRenderPipeline.ts";
|
|
10
|
+
import type { IMaterial } from "./IMaterial.ts";
|
|
11
|
+
|
|
12
|
+
/** The sprite material descriptor. */
|
|
13
|
+
export interface SpriteMaterialDescriptor {
|
|
14
|
+
|
|
15
|
+
/** The projection view buffer. Shared, it is not disposed by the material. */
|
|
16
|
+
projectionViewBuffer: IUniformBuffer;
|
|
17
|
+
|
|
18
|
+
/** The sprite texture. */
|
|
19
|
+
texture: ITexture2D;
|
|
20
|
+
|
|
21
|
+
/** The texture sampler. */
|
|
22
|
+
sampler: ISampler;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The sprite material.
|
|
27
|
+
*/
|
|
28
|
+
export class SpriteMaterial implements IMaterial {
|
|
29
|
+
|
|
30
|
+
private readonly _renderPipeline: ISpriteRenderPipeline;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The constructor.
|
|
34
|
+
* @param framework - The framework.
|
|
35
|
+
* @param descriptor - The sprite material descriptor.
|
|
36
|
+
*/
|
|
37
|
+
public constructor(framework: IFramework, descriptor: SpriteMaterialDescriptor) {
|
|
38
|
+
this._renderPipeline = framework.renderPipelineFactory.createSpriteRenderPipeline(descriptor.projectionViewBuffer);
|
|
39
|
+
this._renderPipeline.textureSampler = descriptor.sampler;
|
|
40
|
+
this._renderPipeline.spriteTexture = descriptor.texture;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** @inheritDoc */
|
|
44
|
+
public get geometryFormat(): GeometryFormat {
|
|
45
|
+
return this._renderPipeline.geometryFormat;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The sprite texture. */
|
|
49
|
+
public get texture(): ITexture2D {
|
|
50
|
+
return this._renderPipeline.spriteTexture;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The sprite texture. */
|
|
54
|
+
public set texture(value: ITexture2D) {
|
|
55
|
+
this._renderPipeline.spriteTexture = value;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** @inheritDoc */
|
|
59
|
+
public beforeRender(): void {
|
|
60
|
+
// Nothing to do
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** @inheritDoc */
|
|
64
|
+
public renderMesh(mesh: IMesh): void {
|
|
65
|
+
this.render(mesh.vertexBuffer!, mesh.indexBuffer!, mesh.indexCount ?? -1, mesh.indexStart ?? 0);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Renders the material.
|
|
70
|
+
* @param vertexBuffer - The vertex buffer.
|
|
71
|
+
* @param indexBuffer - The index buffer.
|
|
72
|
+
* @param indicesCount - The number of indices to draw. If -1 draws all indices.
|
|
73
|
+
* @param indicesOffset - Indices offset. Defaults to 0.
|
|
74
|
+
*/
|
|
75
|
+
public render(vertexBuffer: IVertexBuffer, indexBuffer: IIndexBuffer, indicesCount?: number, indicesOffset?: number): void {
|
|
76
|
+
this._renderPipeline.render(vertexBuffer, indexBuffer, indicesCount, indicesOffset);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** @inheritDoc */
|
|
80
|
+
public dispose(): void {
|
|
81
|
+
this._renderPipeline.dispose();
|
|
82
|
+
}
|
|
83
|
+
}
|