Skip to content

Commit 4d2057f

Browse files
dakerfloryst
authored andcommitted
feat(PBR): add for support for combined RM/ORM texture
fixes #2961
1 parent 284ef73 commit 4d2057f

File tree

4 files changed

+99
-40
lines changed

4 files changed

+99
-40
lines changed

Sources/IO/Geometry/GLTFImporter/Reader.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,25 +252,18 @@ async function createPropertyFromGLTFMaterial(model, material, actor) {
252252
}
253253
}
254254

255+
// Handle metallic-roughness texture (metallicRoughnessTexture)
255256
if (pbr.metallicRoughnessTexture) {
256257
const extensions = pbr.metallicRoughnessTexture.extensions;
257258
const tex = pbr.metallicRoughnessTexture.texture;
258259
const sampler = tex.sampler;
259-
const metallicImage = await loadImage(tex.source, 'b');
260-
const metallicTex = createVTKTextureFromGLTFTexture(
261-
metallicImage,
260+
const rmImage = await loadImage(tex.source);
261+
const rmTex = createVTKTextureFromGLTFTexture(
262+
rmImage,
262263
sampler,
263264
extensions
264265
);
265-
property.setMetallicTexture(metallicTex);
266-
267-
const roughnessImage = await loadImage(tex.source, 'g');
268-
const roughnessTex = createVTKTextureFromGLTFTexture(
269-
roughnessImage,
270-
sampler,
271-
extensions
272-
);
273-
property.setRoughnessTexture(roughnessTex);
266+
property.setRMTexture(rmTex);
274267
}
275268

276269
// Handle ambient occlusion texture (occlusionTexture)

Sources/Rendering/Core/Property/index.d.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { vtkObject } from '../../../interfaces';
2-
import { RGBColor } from '../../../types';
2+
import { Nullable, RGBColor } from '../../../types';
33
import { Interpolation, Representation, Shading } from './Constants';
44
import { vtkTexture } from '../../Core/Texture';
55

@@ -14,6 +14,8 @@ export interface IPropertyInitialValues {
1414
normalTexture?: vtkTexture;
1515
ambientOcclusionTexture?: vtkTexture;
1616
emissionTexture?: vtkTexture;
17+
RMTexture?: vtkTexture;
18+
ORMTexture?: vtkTexture;
1719
edgeColor?: RGBColor;
1820
ambient?: number;
1921
diffuse?: number;
@@ -216,32 +218,42 @@ export interface vtkProperty extends vtkObject {
216218
/**
217219
* Get the diffuse texture.
218220
*/
219-
getDiffuseTexture(): vtkTexture;
221+
getDiffuseTexture(): Nullable<vtkTexture>;
220222

221223
/**
222224
* Get the metallic texture.
223225
*/
224-
getMetallicTexture(): vtkTexture;
226+
getMetallicTexture(): Nullable<vtkTexture>;
227+
228+
/**
229+
* Get the roughness & metallic texture.
230+
*/
231+
getRMTexture(): Nullable<vtkTexture>;
232+
233+
/**
234+
* Get the occlusion, roughness & metallic texture.
235+
*/
236+
getORMTexture(): Nullable<vtkTexture>;
225237

226238
/**
227239
* Get the roughness texture.
228240
*/
229-
getRoughnessTexture(): vtkTexture;
241+
getRoughnessTexture(): Nullable<vtkTexture>;
230242

231243
/**
232244
* Get the normal texture.
233245
*/
234-
getNormalTexture(): vtkTexture;
246+
getNormalTexture(): Nullable<vtkTexture>;
235247

236248
/**
237249
* Get the ambient occlusion texture.
238250
*/
239-
getAmbientOcclusionTexture(): vtkTexture;
251+
getAmbientOcclusionTexture(): Nullable<vtkTexture>;
240252

241253
/**
242254
* Get the emission texture.
243255
*/
244-
getEmissionTexture(): vtkTexture;
256+
getEmissionTexture(): Nullable<vtkTexture>;
245257

246258
/**
247259
* Set the ambient lighting coefficient.
@@ -500,6 +512,18 @@ export interface vtkProperty extends vtkObject {
500512
*/
501513
setRoughnessTexture(roughnessTexture: vtkTexture): boolean;
502514

515+
/**
516+
* Set the roughness & metallic texture.
517+
* @param {vtkTexture} RMTexture
518+
*/
519+
setRMTexture(RMTexture: vtkTexture): boolean;
520+
521+
/**
522+
* Set the occlusion, roughness & metallic texture.
523+
* @param {vtkTexture} ORMTexture
524+
*/
525+
setORMTexture(ORMTexture: vtkTexture): boolean;
526+
503527
/**
504528
* Set the normal texture.
505529
* @param {vtkTexture} normalTexture

Sources/Rendering/Core/Property/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ const DEFAULT_VALUES = {
127127

128128
shading: false,
129129
materialName: null,
130+
131+
ORMTexture: null,
132+
RMTexture: null,
130133
};
131134

132135
// ----------------------------------------------------------------------------
@@ -162,6 +165,8 @@ export function extend(publicAPI, model, initialValues = {}) {
162165
'normalTexture',
163166
'ambientOcclusionTexture',
164167
'emissionTexture',
168+
'ORMTexture',
169+
'RMTexture',
165170
]);
166171
macro.setGetArray(
167172
publicAPI,

Sources/Rendering/WebGPU/CellArrayMapper/index.js

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ function vtkWebGPUCellArrayMapper(publicAPI, model) {
819819
code = fDesc.getCode();
820820

821821
const actor = model.WebGPUActor.getRenderable();
822+
const property = actor.getProperty();
822823

823824
const checkDims = (texture) => {
824825
if (!texture) return false;
@@ -827,15 +828,16 @@ function vtkWebGPUCellArrayMapper(publicAPI, model) {
827828
};
828829

829830
const usedTextures = [];
831+
const diffuseTexture = property.getDiffuseTexture?.();
830832

831833
if (
832-
actor.getProperty().getDiffuseTexture?.()?.getImageLoaded() ||
834+
diffuseTexture?.getImageLoaded() ||
833835
actor.getTextures()[0] ||
834836
model.colorTexture
835837
) {
836838
if (
837839
// Chained or statements here are questionable
838-
checkDims(actor.getProperty().getDiffuseTexture?.()) ||
840+
checkDims(diffuseTexture) ||
839841
checkDims(actor.getTextures()[0]) ||
840842
checkDims(model.colorTexture)
841843
) {
@@ -844,38 +846,65 @@ function vtkWebGPUCellArrayMapper(publicAPI, model) {
844846
);
845847
}
846848
}
847-
if (actor.getProperty().getRoughnessTexture?.()?.getImageLoaded()) {
848-
if (checkDims(actor.getProperty().getRoughnessTexture())) {
849+
850+
const ormTexture = property.getORMTexture?.();
851+
const rmTexture = property.getRMTexture?.();
852+
const roughnessTexture = property.getRoughnessTexture?.();
853+
const metallicTexture = property.getMetallicTexture?.();
854+
const ambientOcclusionTexture = property.getAmbientOcclusionTexture?.();
855+
const emissionTexture = property.getEmissionTexture?.();
856+
const normalTexture = property.getNormalTexture?.();
857+
858+
// ORM texture support: if present, sample R/G/B for AO/Roughness/Metallic
859+
if (ormTexture?.getImageLoaded()) {
860+
if (checkDims(ormTexture)) {
849861
usedTextures.push(
850-
'_roughnessMap = textureSample(RoughnessTexture, RoughnessTextureSampler, input.tcoordVS);'
862+
'_ambientOcclusionMap = textureSample(ORMTexture, ORMTextureSampler, input.tcoordVS).rrra;',
863+
'_roughnessMap = textureSample(ORMTexture, ORMTextureSampler, input.tcoordVS).ggga;',
864+
'_metallicMap = textureSample(ORMTexture, ORMTextureSampler, input.tcoordVS).bbba;'
851865
);
852866
}
853-
}
854-
if (actor.getProperty().getMetallicTexture?.()?.getImageLoaded()) {
855-
if (checkDims(actor.getProperty().getMetallicTexture())) {
867+
} else if (rmTexture?.getImageLoaded()) {
868+
if (checkDims(rmTexture)) {
856869
usedTextures.push(
857-
'_metallicMap = textureSample(MetallicTexture, MetallicTextureSampler, input.tcoordVS);'
870+
'_roughnessMap = textureSample(RMTexture, RMTextureSampler, input.tcoordVS).ggga;',
871+
'_metallicMap = textureSample(RMTexture, RMTextureSampler, input.tcoordVS).bbba;'
858872
);
859873
}
860-
}
861-
if (actor.getProperty().getNormalTexture?.()?.getImageLoaded()) {
862-
if (checkDims(actor.getProperty().getNormalTexture())) {
863-
usedTextures.push(
864-
'_normalMap = textureSample(NormalTexture, NormalTextureSampler, input.tcoordVS);'
865-
);
874+
} else {
875+
if (roughnessTexture?.getImageLoaded()) {
876+
if (checkDims(roughnessTexture)) {
877+
usedTextures.push(
878+
'_roughnessMap = textureSample(RoughnessTexture, RoughnessTextureSampler, input.tcoordVS);'
879+
);
880+
}
881+
}
882+
if (metallicTexture?.getImageLoaded()) {
883+
if (checkDims(metallicTexture)) {
884+
usedTextures.push(
885+
'_metallicMap = textureSample(MetallicTexture, MetallicTextureSampler, input.tcoordVS);'
886+
);
887+
}
888+
}
889+
if (ambientOcclusionTexture?.getImageLoaded()) {
890+
if (checkDims(ambientOcclusionTexture)) {
891+
usedTextures.push(
892+
'_ambientOcclusionMap = textureSample(AmbientOcclusionTexture, AmbientOcclusionTextureSampler, input.tcoordVS);'
893+
);
894+
}
866895
}
867896
}
868-
if (actor.getProperty().getAmbientOcclusionTexture?.()?.getImageLoaded()) {
869-
if (checkDims(actor.getProperty().getAmbientOcclusionTexture())) {
897+
if (emissionTexture?.getImageLoaded()) {
898+
if (checkDims(emissionTexture)) {
870899
usedTextures.push(
871-
'_ambientOcclusionMap = textureSample(AmbientOcclusionTexture, AmbientOcclusionTextureSampler, input.tcoordVS);'
900+
'_emissionMap = textureSample(EmissionTexture, EmissionTextureSampler, input.tcoordVS);'
872901
);
873902
}
874903
}
875-
if (actor.getProperty().getEmissionTexture?.()?.getImageLoaded()) {
876-
if (checkDims(actor.getProperty().getEmissionTexture())) {
904+
if (normalTexture?.getImageLoaded()) {
905+
if (checkDims(normalTexture)) {
877906
usedTextures.push(
878-
'_emissionMap = textureSample(EmissionTexture, EmissionTextureSampler, input.tcoordVS);'
907+
'_normalMap = textureSample(NormalTexture, NormalTextureSampler, input.tcoordVS);'
879908
);
880909
}
881910
}
@@ -1146,6 +1175,14 @@ function vtkWebGPUCellArrayMapper(publicAPI, model) {
11461175
const pair = ['Diffuse', model.colorTexture];
11471176
textures.push(pair);
11481177
}
1178+
if (actor.getProperty().getORMTexture?.()) {
1179+
const pair = ['ORM', actor.getProperty().getORMTexture()];
1180+
textures.push(pair);
1181+
}
1182+
if (actor.getProperty().getRMTexture?.()) {
1183+
const pair = ['RM', actor.getProperty().getRMTexture()];
1184+
textures.push(pair);
1185+
}
11491186
if (actor.getProperty().getRoughnessTexture?.()) {
11501187
const pair = ['Roughness', actor.getProperty().getRoughnessTexture()];
11511188
textures.push(pair);

0 commit comments

Comments
 (0)