# misc/graphicsqualitygovernor (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/misc/graphicsqualitygovernor/

Decides graphics quality. (Decides whether to use the lite path from the setting and frame measurement, and picks the render pixel ratio cap)

## GraphicsQualityGovernor

Decides graphics quality. (Decides whether to use the lite path from the setting and frame measurement, and picks the render pixel ratio cap)
- Order for deciding the lite path (isLite): override (such as ?light in the URL) → setting low (always lite), high (never) → auto.
  In auto, it is the lite path if lite from the start (setAutoLiteDefault, such as touch devices) or if FrameProbe measured a lagging device.
- Calling update(frame seconds, maximum frame) every frame measures with FrameProbe only in auto. Returns true on the frame where the lite path changed.
  Once measured as a lagging device, it does not measure again. (Same as FrameProbe)
- Render pixel ratio cap: given with setRenderPixelRatioLimits(normal, lite path), and applyRenderPixelRatio(viewManager) applies it only when it changes. (0 means no limit)
- What to reduce on the lite path (shadows, outlines, effects, draw count) is decided by the game reading isLite.
- Does not import the engine or the browser. applyPageQuery reads the URL text from the current page, or the caller passes it with applySearchText(location.search).

Example: Decide the lite path from settings and frame measurement

```js
import { GraphicsQualityGovernor, GraphicsQuality } from "./build/vanilla.js";

const governor = new GraphicsQualityGovernor();
governor.setQuality(GraphicsQuality.auto);
governor.applySearchText("?mode=demo");
governor.setRenderPixelRatioLimits(2, 1);
const frameProbe = governor.getFrameProbe();
frameProbe.setSeconds(0.5, 1);
for (let frame = 0; frame < 120; ++frame) {
	const isChanged = governor.update(1 / 20, 60);
	if (isChanged) {
		console.log("lite", governor.isLite(), frame);
	}
}
console.log(governor.isLite(), governor.readMaxRenderPixelRatio());
```

- `constructor()` (constructor, since 0.4.0): Constructor. (Auto, no override, no render pixel ratio limit)
- `setQuality(quality)` (method, since 0.4.0): Sets the quality setting. (GraphicsQuality, auto for an unknown value)
  - `quality` string: A GraphicsQuality value.
- `getQuality()` (method, since 0.4.0): Returns the quality setting.
  - returns string: A GraphicsQuality value.
- `setLiteOverride(liteOverride)` (method, since 0.4.0): Sets the override. (true means always the lite path, false means never, null releases it)
  - `liteOverride` boolean | null: True for always lite, false for never, null to clear.
- `getLiteOverride()` (method, since 0.4.0): Returns the override.
  - returns boolean | null: The override. null if none.
- `applySearchText(searchText, liteKey = "light")` (method, since 0.4.0): Overrides from the URL query text. (Lite path if liteKey is present, never if its value is "0" or "false", unchanged if absent)
  - `searchText` string: location.search format. ("?light", "?a=1&light=0")
  - `liteKey` string: Query name to look for. ("light" by default)

  Example: Override the lite path with a URL query

  ```js
  import { GraphicsQualityGovernor } from "./build/vanilla.js";
  
  const governor = new GraphicsQualityGovernor();
  governor.applySearchText("?light");
  console.log(governor.getLiteOverride(), governor.isLite());
  governor.applySearchText("?a=1&light=0");
  console.log(governor.getLiteOverride(), governor.isLite());
  ```

- `applyPageQuery(liteKey = "light")` (method, since 0.4.0): Overrides from the query of the page URL. (passes location.search of the current page to applySearchText, unchanged where there is no URL)
  - The Rust version reads the URL query on the web and the environment variable VANILLA_<name> on desktop. This version runs only on Chrome so it reads only the URL.
  - `liteKey` string: Query name to look for. ("light" by default)

  Example: Overriding the lite path from the query of the page URL

  ```js
  import { GraphicsQualityGovernor } from "./build/vanilla.js";
  
  const governor = new GraphicsQualityGovernor();
  governor.applyPageQuery("light");
  console.log(governor.getLiteOverride());
  ```

- `setAutoLiteDefault(isAutoLiteDefault)` (method, since 0.4.0): Sets whether auto starts on the lite path. (When going lite even before measuring, like touch devices)
  - `isAutoLiteDefault` boolean: True to use the lite path from the start without measuring when automatic.
- `isAutoLiteDefault()` (method, since 0.4.0): Returns whether auto starts on the lite path.
  - returns boolean: True if the lite path is used from the start when automatic.
- `getFrameProbe()` (method, since 0.4.0): Returns the frame probe. (To change the wait seconds or the decision threshold, or to measureAgain in a heavy scene)
  - returns FrameProbe: The FrameProbe used by this governor.
- `isLite()` (method, since 0.4.0): Whether drawing on the lite path.
  - returns boolean: True if on the lite path.
- `update(frameSeconds, maximumFramePerSecond)` (method, since 0.4.0): One frame. (Measures frames if auto and not yet on the lite path, true on the frame where the lite path changed)
  - `frameSeconds` number: Interval since the last frame. (seconds)
  - `maximumFramePerSecond` number: Maximum frame of the engine setting. (0 means unlimited)
  - returns boolean: True if the lite path changed since the last update.
- `setRenderPixelRatioLimits(normalMaxRenderPixelRatio, liteMaxRenderPixelRatio)` (method, since 0.4.0): Sets the render pixel ratio cap. (Normal path and lite path, 0 means no limit)
  - `normalMaxRenderPixelRatio` number: Render pixel ratio limit on the normal path. (0 means no limit, 0 if negative)
  - `liteMaxRenderPixelRatio` number: Render pixel ratio limit on the lite path. (0 means no limit, 0 if negative)
- `readMaxRenderPixelRatio()` (method, since 0.4.0): Render pixel ratio cap of the current path.
  - returns number: Render pixel ratio limit for the current path. (0 means no limit)
- `applyRenderPixelRatio(viewManager)` (method, since 0.4.0): Applies the render pixel ratio cap. (Only when it differs from the ViewManager value, true if applied)
  - `viewManager` object: ViewManager
  - returns boolean: True if a new limit was applied. (false if it was the same and left as is)

  Example: Match the render pixel ratio limit every frame in a scene

  ```js
  import { Engine, EngineConfiguration, Scene, GraphicsQualityGovernor } from "./build/vanilla.js";
  
  const governor = new GraphicsQualityGovernor();
  governor.setRenderPixelRatioLimits(2, 1);
  
  class QualityScene extends Scene {
  	tick(timeDelta) {
  		super.tick(timeDelta);
  		const isChanged = governor.update(timeDelta, 60);
  		if (isChanged) {
  			console.log("lite", governor.isLite());
  		}
  		const engine = this.getEngine();
  		const viewManager = engine.getViewManager();
  		governor.applyRenderPixelRatio(viewManager);
  	}
  }
  
  const engineConfiguration = new EngineConfiguration();
  const engine = new Engine(engineConfiguration);
  engine.run(new QualityScene());
  ```

## GraphicsQuality (enum)

Graphics quality setting value.

- `auto` = `auto`: Automatic. (decided by frame measurement)
- `high` = `high`: High. (never the lite path)
- `low` = `low`: Low. (always the lite path)
