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

LitOptions class to hold backend options #4900

Merged
merged 25 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2653673
Created a LitOptions object to hold the backend options for the lit s…
GSterbrant Nov 30, 2022
53b8ad8
Fix lint
GSterbrant Dec 2, 2022
12f9b5f
Add deprecation callbacks for options.
GSterbrant Dec 7, 2022
f685056
Update documentation to reflect options split for onUpdateShader
GSterbrant Dec 7, 2022
9118128
Add exception for chunks in options which will be allowed to exist in…
GSterbrant Dec 7, 2022
ff049f1
Moved options documentation to their respective files and provide a l…
GSterbrant Dec 7, 2022
1106cbc
Since options allocate LitOptions, we don't need to create these opti…
GSterbrant Dec 7, 2022
02468c0
Add exception for pass and duplicate it between the lit and standard …
GSterbrant Dec 7, 2022
582e131
Fix lint
GSterbrant Dec 7, 2022
b9f0969
Fix tests.
GSterbrant Dec 7, 2022
be5622d
Fix unit test
GSterbrant Dec 7, 2022
24037ab
Fix
GSterbrant Dec 7, 2022
897bb92
Add getter and setter for pass to inform user to always set it on the…
GSterbrant Dec 8, 2022
fad152c
Fixed pass member name
GSterbrant Dec 8, 2022
951fa02
Use fog constants instead of string
GSterbrant Dec 8, 2022
42d0e62
Add missing full stops to some of the comments
GSterbrant Dec 8, 2022
0a29f67
Missing full stop
GSterbrant Dec 8, 2022
c63a8a0
Fix bad import
GSterbrant Dec 8, 2022
06b8623
Fixed docs
GSterbrant Dec 8, 2022
d82afab
Fixed litoptions returning pass
GSterbrant Dec 8, 2022
f6d9ccf
Alphabetical order of LitOptions index import.
GSterbrant Dec 8, 2022
7b475b9
Fixed deprecation for pass now renamed to _pass
GSterbrant Dec 8, 2022
4699f31
Fix types.
GSterbrant Dec 8, 2022
2ed3238
Add JS docs to the LitOptions and StandardMaterialOptions classes.
GSterbrant Dec 8, 2022
2cce3e0
Fix setting deprecated options in lit options and options respectively.
GSterbrant Dec 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/deprecated/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ import { SkinInstance } from '../scene/skin-instance.js';
import { StandardMaterial } from '../scene/materials/standard-material.js';
import { Batch } from '../scene/batching/batch.js';
import { getDefaultMaterial } from '../scene/materials/default-material.js';
import { Options } from '../scene/materials/options.js';
import { LitOptions } from '../scene/materials/lit-options.js';

import { Animation, Key, Node } from '../scene/animation/animation.js';
import { Skeleton } from '../scene/animation/skeleton.js';
Expand Down Expand Up @@ -785,6 +787,24 @@ _defineAlias('glossVertexColor', 'glossMapVertexColor');
_defineAlias('opacityVertexColor', 'opacityMapVertexColor');
_defineAlias('lightVertexColor', 'lightMapVertexColor');

function _defineOption(name) {
Object.defineProperty(Options.prototype, name, {
get: function () {
Debug.deprecated(`Getting pc.Options#${name} has been deprecated as the property has been moved to pc.Options.LitOptions.`);
return null;
mvaligursky marked this conversation as resolved.
Show resolved Hide resolved
},
set: function (value) {
Debug.deprecated(`Setting pc.Options#${name} has been deprecated as the property has been moved to pc.Options.LitOptions.`);
mvaligursky marked this conversation as resolved.
Show resolved Hide resolved
}
});
}

const tempOptions = new LitOptions();
const litOptionProperties = Object.getOwnPropertyNames(tempOptions);
for (const litOption in litOptionProperties) {
_defineOption(litOptionProperties[litOption]);
}

// ANIMATION

export const anim = {
Expand Down
102 changes: 102 additions & 0 deletions src/scene/materials/lit-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {
BLEND_NONE, GAMMA_NONE
} from '../constants.js';

class LitOptions {
mvaligursky marked this conversation as resolved.
Show resolved Hide resolved
constructor() {
this.hasTangents = false;
this.chunks = [];

this.pass = 0;
GSterbrant marked this conversation as resolved.
Show resolved Hide resolved
this.alphaTest = false;
this.forceFragmentPrecision = false;
this.blendType = BLEND_NONE;
this.separateAmbient = false;
this.screenSpace = false;
this.skin = false;
this.useInstancing = false;
this.useMorphPosition = false;
this.useMorphNormal = false;
this.useMorphTextureBased = false;

this.nineSlicedMode = false;

this.clusteredLightingEnabled = true;

this.clusteredLightingCookiesEnabled = false;
this.clusteredLightingShadowsEnabled = false;
this.clusteredLightingShadowType = 0;
this.clusteredLightingAreaLightsEnabled = false;

this.vertexColors = false;
this.lightMapEnabled = false;
this.useLightMapVertexColors = false;
this.dirLightMapEnabled = false;
this.heightMapEnabled = false;
this.normalMapEnabled = false;
this.clearCoatNormalMapEnabled = false;
this.aoMapEnabled = false;
this.useAoVertexColors = false;
this.diffuseMapEnabled = false;

this.useAmbientTint = false;
this.customFragmentShader = null;
this.pixelSnap = false;

this.useClearCoatNormalMap = false;
this.useDiffuseMap = false;
this.useAoMap = false;

this.detailModes = 0;
this.shadingModel = 0;
this.ambientSH = false;
this.fastTbn = false;
this.twoSidedLighting = false;
this.occludeSpecular = false;
this.occludeSpecularFloat = false;

this.useMsdf = false;
this.msdfTextAttribute = 0;

this.alphaToCoverage = false;
this.opacityFadesSpecular = false;

this.cubeMapProjection = false;

this.occludeDirect = false;
this.conserveEnergy = false;
this.useSpecular = false;
this.useSpecularityFactor = false;
this.useSpecularColor = false;
this.enableGGXSpecular = false;
this.fresnelModel = 0;
this.useRefraction = false;
this.useClearCoat = false;
this.useSheen = false;
this.useIridescence = false;
this.useMetalness = false;
this.useDynamicRefraction = false;

this.fog = 'none';
mvaligursky marked this conversation as resolved.
Show resolved Hide resolved
this.gamma = GAMMA_NONE;
this.toneMap = -1;
this.fixSeams = false;

this.reflectionSource = null;
this.reflectionEncoding = null;
this.ambientSource = 'constant';
mvaligursky marked this conversation as resolved.
Show resolved Hide resolved
this.ambientEncoding = null;

// TODO: add a test for if non skybox cubemaps have rotation (when this is supported) - for now assume no non-skybox cubemap rotation
this.skyboxIntensity = 1.0;
this.useCubeMapRotation = false;

this.lightMapWithoutAmbient = false;

this.lights = [];
this.noShadow = false;
this.lightMaskDynamic = 0x0;
}
}

export { LitOptions };
7 changes: 7 additions & 0 deletions src/scene/materials/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Options {
mvaligursky marked this conversation as resolved.
Show resolved Hide resolved
constructor() {
this.chunks = [];
}
}

export { Options };
12 changes: 7 additions & 5 deletions src/scene/materials/standard-material-options-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
} from '../constants.js';
import { _matTex2D } from '../shader-lib/programs/standard.js';

import { LitOptions } from './lit-options.js';

const arraysEqual = (a, b) => {
if (a.length !== b.length) {
return false;
Expand Down Expand Up @@ -44,15 +46,15 @@ class StandardMaterialOptionsBuilder {

// Minimal options for Depth and Shadow passes
updateMinRef(options, scene, stdMat, objDefs, staticLightList, pass, sortedLights) {
options.litOptions = {};
options.litOptions = new LitOptions();
this._updateSharedOptions(options, scene, stdMat, objDefs, pass);
this._updateMinOptions(options, stdMat);
this._updateUVOptions(options, stdMat, objDefs, true);
options.litOptions.chunks = options.chunks;
}

updateRef(options, scene, stdMat, objDefs, staticLightList, pass, sortedLights) {
options.litOptions = {};
options.litOptions = new LitOptions();
this._updateSharedOptions(options, scene, stdMat, objDefs, pass);
this._updateEnvOptions(options, stdMat, scene);
this._updateMaterialOptions(options, stdMat);
Expand Down Expand Up @@ -120,13 +122,13 @@ class StandardMaterialOptionsBuilder {

// All texture related lit options
options.litOptions.lightMapEnabled = options.lightMap;
options.litOptions.lightMapVertexColors = options.lightVertexColors;
options.litOptions.useLightMapVertexColors = options.lightVertexColors;
options.litOptions.dirLightMapEnabled = options.dirLightMap;
options.litOptions.heightMapEnabled = options.heightMap;
options.litOptions.normalMapEnabled = options.normalMap;
options.litOptions.clearCoatNormalMapEnabled = options.clearCoatNormalMap;
options.litOptions.aoMapEnabled = options.aoMap;
options.litOptions.aoVertexColors = options.aoVertexColors;
options.litOptions.useAoVertexColors = options.aoVertexColors;
options.litOptions.diffuseMapEnabled = options.diffuseMap;
}

Expand Down Expand Up @@ -248,7 +250,7 @@ class StandardMaterialOptionsBuilder {
options.sheenGlossinessTint = 1;

// LIT OPTIONS
options.litOptions.ambientTint = options.ambientTint;
options.litOptions.useAmbientTint = options.ambientTint;
options.litOptions.customFragmentShader = stdMat.customFragmentShader;
options.litOptions.pixelSnap = stdMat.pixelSnap;

Expand Down
6 changes: 3 additions & 3 deletions src/scene/shader-lib/program-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ProgramLibrary {

const device = this._device;
def = generator.createShaderDefinition(device, options);
def.name = `${name}-pass:${options.pass}`;
def.name = `${name}-pass:${options.litOptions?.pass}`;
mvaligursky marked this conversation as resolved.
Show resolved Hide resolved
this.definitionsCache.set(key, def);
}
return def;
Expand Down Expand Up @@ -151,7 +151,7 @@ class ProgramLibrary {
let opt = {};
if (name === "standard") {
// For standard material saving all default values is overkill, so we store only diff
const defaultMat = this._getDefaultStdMatOptions(options.pass);
const defaultMat = this._getDefaultStdMatOptions(options.litOptions?.pass);

for (const p in options) {
if ((options.hasOwnProperty(p) && defaultMat[p] !== options[p]) || p === "pass")
Expand Down Expand Up @@ -234,7 +234,7 @@ class ProgramLibrary {
// back into the loaded options
if (cache[i].name === "standard") {
const opt = cache[i].options;
const defaultMat = this._getDefaultStdMatOptions(opt.pass);
const defaultMat = this._getDefaultStdMatOptions(opt.litOptions?.pass);
for (const p in defaultMat) {
if (defaultMat.hasOwnProperty(p) && opt[p] === undefined)
opt[p] = defaultMat[p];
Expand Down
12 changes: 6 additions & 6 deletions src/scene/shader-lib/programs/lit-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ class LitShader {
}
}

const useAo = options.aoMapEnabled || options.aoVertexColors;
const useAo = options.aoMapEnabled || options.useAoVertexColors;

if (useAo) {
code += chunks.aoDiffuseOccPS;
Expand Down Expand Up @@ -876,11 +876,11 @@ class LitShader {
code += chunks.combinePS;

// lightmap support
if (options.lightMapEnabled || options.lightMapVertexColors) {
if (options.lightMapEnabled || options.useLightMapVertexColors) {
code += (options.useSpecular && options.dirLightMapEnabled) ? chunks.lightmapDirAddPS : chunks.lightmapAddPS;
}

const addAmbient = (!options.lightMapEnabled && !options.lightMapVertexColors) || options.lightMapWithoutAmbient;
const addAmbient = (!options.lightMapEnabled && !options.useLightMapVertexColors) || options.lightMapWithoutAmbient;

if (addAmbient) {
if (options.ambientSource === 'ambientSH') {
Expand All @@ -895,7 +895,7 @@ class LitShader {
}
}

if (options.ambientTint && !useOldAmbient) {
if (options.useAmbientTint && !useOldAmbient) {
code += "uniform vec3 material_ambient;\n";
}

Expand Down Expand Up @@ -1017,15 +1017,15 @@ class LitShader {
}
}

if (options.ambientTint && !useOldAmbient) {
if (options.useAmbientTint && !useOldAmbient) {
code += " dDiffuseLight *= material_ambient;\n";
}

if (useAo && !options.occludeDirect) {
code += " occludeDiffuse();\n";
}

if (options.lightMapEnabled || options.lightMapVertexColors) {
if (options.lightMapEnabled || options.useLightMapVertexColors) {
code += " addLightMap();\n";
}

Expand Down
5 changes: 3 additions & 2 deletions src/scene/shader-lib/programs/standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import { ShaderPass } from '../../shader-pass.js';
import { LitShader } from './lit-shader.js';
import { ChunkBuilder } from '../chunk-builder.js';
import { ChunkUtils } from '../chunk-utils.js';
import { Options } from '../../materials/options.js';

const _matTex2D = [];

const standard = {
// Shared Standard Material option structures
optionsContext: {},
optionsContextMin: {},
optionsContext: new Options(),
optionsContextMin: new Options(),

/** @type { Function } */
generateKey: function (options) {
Expand Down