Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility of engine v1 with data format of engine v2 #7066

Merged
merged 10 commits into from
Nov 4, 2024
22 changes: 22 additions & 0 deletions src/deprecated/compatibility-v2-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// add StandardMaterial parameter types the engine v2 supports to avoid validation warnings
export function __adjustStandardMaterialParameterTypes(types) {
types.useGamma = 'boolean';
types.aoIntensity = 'number';
}

const _tintProperties = [
'ambientTint',
'emissiveTint',
'diffuseTint',
'sheenTint'
];

// in the engine v2 material json data, the tints were removed and are assumed to be always true
// for the engine v1, force them to be true
export function __adjustStandardMaterialData(data) {
_tintProperties.forEach((prop) => {
if (data[prop] === undefined) {
data[prop] = true;
}
});
}
25 changes: 25 additions & 0 deletions src/deprecated/compatibility-v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { StandardMaterial } from '../scene/materials/standard-material.js';

// useGammaTonemap is renamed to useGamma
Object.defineProperty(StandardMaterial.prototype, 'useGamma', {
get: function () {
return this.useGammaTonemap;
},
set: function (value) {
this.useGammaTonemap = value;
}
});

// ignore aoIntensity as it's not supported
Object.defineProperty(StandardMaterial.prototype, 'aoIntensity', {
get: function () {
return 1;
},
set: function (value) {
}
});

// dummy export to avoid the module being removed by rollup
// TODO: remove this export when the module has other exports
export function __dummyFunction() {
}
4 changes: 4 additions & 0 deletions src/framework/parsers/material/json-standard-material.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { SPECULAR_BLINN, SPECULAR_PHONG } from '../../../scene/constants.js';
import { StandardMaterial } from '../../../scene/materials/standard-material.js';
import { StandardMaterialValidator } from '../../../scene/materials/standard-material-validator.js';
import { standardMaterialParameterTypes } from '../../../scene/materials/standard-material-parameters.js';
import { __adjustStandardMaterialData } from '../../../deprecated/compatibility-v2-utils.js';

/**
* Convert incoming JSON data into a {@link StandardMaterial}.
Expand Down Expand Up @@ -164,6 +165,9 @@ class JsonStandardMaterialParser {
}
}

// data migration for engine v2 data compatibility
__adjustStandardMaterialData(data);

return data;
}

Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,5 +395,9 @@ export { XrViews } from './framework/xr/xr-views.js';
// BACKWARDS COMPATIBILITY
export * from './deprecated/deprecated.js';

// ENGINE V2 DATA COMPATIBILITY
export * from './deprecated/compatibility-v2.js';
export * from './deprecated/compatibility-v2-utils.js';

// EXTRAS
export * from './extras/index.js';
5 changes: 5 additions & 0 deletions src/scene/materials/standard-material-parameters.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { __adjustStandardMaterialParameterTypes } from '../../deprecated/compatibility-v2-utils.js';

function _textureParameter(name, channel = true, vertexColor = true) {
const result = {};
result[`${name}Map`] = 'texture';
Expand Down Expand Up @@ -164,6 +166,9 @@ const standardMaterialParameterTypes = {
// msdfVertexColorChannel
};

// handle engine v2 compatibility
__adjustStandardMaterialParameterTypes(standardMaterialParameterTypes);

const standardMaterialTextureParameters = [];
for (const key in standardMaterialParameterTypes) {
const type = standardMaterialParameterTypes[key];
Expand Down