Initial
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* Abstract shader used for Adjust Darkness Level region behavior.
|
||||
* @abstract
|
||||
* @internal
|
||||
* @ignore
|
||||
*/
|
||||
class AbstractDarknessLevelRegionShader extends RegionShader {
|
||||
|
||||
/** @inheritDoc */
|
||||
static defaultUniforms = {
|
||||
...super.defaultUniforms,
|
||||
bottom: 0,
|
||||
top: 0,
|
||||
depthTexture: null
|
||||
};
|
||||
|
||||
/* ---------------------------------------- */
|
||||
|
||||
/**
|
||||
* The darkness level adjustment mode.
|
||||
* @type {number}
|
||||
*/
|
||||
mode = foundry.data.regionBehaviors.AdjustDarknessLevelRegionBehaviorType.MODES.OVERRIDE;
|
||||
|
||||
/* ---------------------------------------- */
|
||||
|
||||
/**
|
||||
* The darkness level modifier.
|
||||
* @type {number}
|
||||
*/
|
||||
modifier = 0;
|
||||
|
||||
/* ---------------------------------------- */
|
||||
|
||||
/**
|
||||
* Current darkness level of this mesh.
|
||||
* @type {number}
|
||||
*/
|
||||
get darknessLevel() {
|
||||
const M = foundry.data.regionBehaviors.AdjustDarknessLevelRegionBehaviorType.MODES;
|
||||
switch ( this.mode ) {
|
||||
case M.OVERRIDE: return this.modifier;
|
||||
case M.BRIGHTEN: return canvas.environment.darknessLevel * (1 - this.modifier);
|
||||
case M.DARKEN: return 1 - ((1 - canvas.environment.darknessLevel) * (1 - this.modifier));
|
||||
default: throw new Error("Invalid mode");
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------- */
|
||||
|
||||
/** @inheritDoc */
|
||||
_preRender(mesh, renderer) {
|
||||
super._preRender(mesh, renderer);
|
||||
const u = this.uniforms;
|
||||
u.bottom = canvas.masks.depth.mapElevation(mesh.region.bottom);
|
||||
u.top = canvas.masks.depth.mapElevation(mesh.region.top);
|
||||
if ( !u.depthTexture ) u.depthTexture = canvas.masks.depth.renderTexture;
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------- */
|
||||
|
||||
/**
|
||||
* Render the RegionMesh with darkness level adjustments.
|
||||
* @internal
|
||||
* @ignore
|
||||
*/
|
||||
class AdjustDarknessLevelRegionShader extends AbstractDarknessLevelRegionShader {
|
||||
|
||||
/** @override */
|
||||
static fragmentShader = `
|
||||
precision ${PIXI.settings.PRECISION_FRAGMENT} float;
|
||||
|
||||
uniform sampler2D depthTexture;
|
||||
uniform float darknessLevel;
|
||||
uniform float top;
|
||||
uniform float bottom;
|
||||
uniform vec4 tintAlpha;
|
||||
varying vec2 vScreenCoord;
|
||||
|
||||
void main() {
|
||||
vec2 depthColor = texture2D(depthTexture, vScreenCoord).rg;
|
||||
float depth = step(depthColor.g, top) * step(bottom, (254.5 / 255.0) - depthColor.r);
|
||||
gl_FragColor = vec4(darknessLevel, 0.0, 0.0, 1.0) * tintAlpha * depth;
|
||||
}
|
||||
`;
|
||||
|
||||
/* ---------------------------------------- */
|
||||
|
||||
/** @inheritDoc */
|
||||
static defaultUniforms = {
|
||||
...super.defaultUniforms,
|
||||
darknessLevel: 0
|
||||
};
|
||||
|
||||
/* ---------------------------------------- */
|
||||
|
||||
/** @inheritDoc */
|
||||
_preRender(mesh, renderer) {
|
||||
super._preRender(mesh, renderer);
|
||||
this.uniforms.darknessLevel = this.darknessLevel;
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------- */
|
||||
|
||||
/**
|
||||
* Render the RegionMesh with darkness level adjustments.
|
||||
* @internal
|
||||
* @ignore
|
||||
*/
|
||||
class IlluminationDarknessLevelRegionShader extends AbstractDarknessLevelRegionShader {
|
||||
|
||||
/** @override */
|
||||
static fragmentShader = `
|
||||
precision ${PIXI.settings.PRECISION_FRAGMENT} float;
|
||||
|
||||
uniform sampler2D depthTexture;
|
||||
uniform float top;
|
||||
uniform float bottom;
|
||||
uniform vec4 tintAlpha;
|
||||
varying vec2 vScreenCoord;
|
||||
|
||||
void main() {
|
||||
vec2 depthColor = texture2D(depthTexture, vScreenCoord).rg;
|
||||
float depth = step(depthColor.g, top) * step(bottom, (254.5 / 255.0) - depthColor.r);
|
||||
gl_FragColor = vec4(1.0) * tintAlpha * depth;
|
||||
}
|
||||
`;
|
||||
}
|
||||
65
resources/app/client/pixi/webgl/shaders/region/base.js
Normal file
65
resources/app/client/pixi/webgl/shaders/region/base.js
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* The shader used by {@link RegionMesh}.
|
||||
*/
|
||||
class RegionShader extends AbstractBaseShader {
|
||||
|
||||
/** @override */
|
||||
static vertexShader = `
|
||||
precision ${PIXI.settings.PRECISION_VERTEX} float;
|
||||
|
||||
attribute vec2 aVertexPosition;
|
||||
|
||||
uniform mat3 translationMatrix;
|
||||
uniform mat3 projectionMatrix;
|
||||
uniform vec2 canvasDimensions;
|
||||
uniform vec4 sceneDimensions;
|
||||
uniform vec2 screenDimensions;
|
||||
|
||||
varying vec2 vCanvasCoord; // normalized canvas coordinates
|
||||
varying vec2 vSceneCoord; // normalized scene coordinates
|
||||
varying vec2 vScreenCoord; // normalized screen coordinates
|
||||
|
||||
void main() {
|
||||
vec2 pixelCoord = aVertexPosition;
|
||||
vCanvasCoord = pixelCoord / canvasDimensions;
|
||||
vSceneCoord = (pixelCoord - sceneDimensions.xy) / sceneDimensions.zw;
|
||||
vec3 tPos = translationMatrix * vec3(aVertexPosition, 1.0);
|
||||
vScreenCoord = tPos.xy / screenDimensions;
|
||||
gl_Position = vec4((projectionMatrix * tPos).xy, 0.0, 1.0);
|
||||
}
|
||||
`;
|
||||
|
||||
/** @override */
|
||||
static fragmentShader = `
|
||||
precision ${PIXI.settings.PRECISION_FRAGMENT} float;
|
||||
|
||||
uniform vec4 tintAlpha;
|
||||
|
||||
void main() {
|
||||
gl_FragColor = tintAlpha;
|
||||
}
|
||||
`;
|
||||
|
||||
/* ---------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
static defaultUniforms = {
|
||||
canvasDimensions: [1, 1],
|
||||
sceneDimensions: [0, 0, 1, 1],
|
||||
screenDimensions: [1, 1],
|
||||
tintAlpha: [1, 1, 1, 1]
|
||||
};
|
||||
|
||||
/* ---------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
_preRender(mesh, renderer) {
|
||||
const uniforms = this.uniforms;
|
||||
uniforms.tintAlpha = mesh._cachedTint;
|
||||
const dimensions = canvas.dimensions;
|
||||
uniforms.canvasDimensions[0] = dimensions.width;
|
||||
uniforms.canvasDimensions[1] = dimensions.height;
|
||||
uniforms.sceneDimensions = dimensions.sceneRect;
|
||||
uniforms.screenDimensions = canvas.screenDimensions;
|
||||
}
|
||||
}
|
||||
84
resources/app/client/pixi/webgl/shaders/region/highlight.js
Normal file
84
resources/app/client/pixi/webgl/shaders/region/highlight.js
Normal file
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Shader for the Region highlight.
|
||||
* @internal
|
||||
* @ignore
|
||||
*/
|
||||
class HighlightRegionShader extends RegionShader {
|
||||
|
||||
/** @override */
|
||||
static vertexShader = `\
|
||||
precision ${PIXI.settings.PRECISION_VERTEX} float;
|
||||
|
||||
${this.CONSTANTS}
|
||||
|
||||
attribute vec2 aVertexPosition;
|
||||
|
||||
uniform mat3 translationMatrix;
|
||||
uniform mat3 projectionMatrix;
|
||||
uniform vec2 canvasDimensions;
|
||||
uniform vec4 sceneDimensions;
|
||||
uniform vec2 screenDimensions;
|
||||
uniform mediump float hatchThickness;
|
||||
|
||||
varying vec2 vCanvasCoord; // normalized canvas coordinates
|
||||
varying vec2 vSceneCoord; // normalized scene coordinates
|
||||
varying vec2 vScreenCoord; // normalized screen coordinates
|
||||
varying float vHatchOffset;
|
||||
|
||||
void main() {
|
||||
vec2 pixelCoord = aVertexPosition;
|
||||
vCanvasCoord = pixelCoord / canvasDimensions;
|
||||
vSceneCoord = (pixelCoord - sceneDimensions.xy) / sceneDimensions.zw;
|
||||
vec3 tPos = translationMatrix * vec3(aVertexPosition, 1.0);
|
||||
vScreenCoord = tPos.xy / screenDimensions;
|
||||
gl_Position = vec4((projectionMatrix * tPos).xy, 0.0, 1.0);
|
||||
vHatchOffset = (pixelCoord.x + pixelCoord.y) / (SQRT2 * 2.0 * hatchThickness);
|
||||
}
|
||||
`;
|
||||
|
||||
/* ---------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
static fragmentShader = `\
|
||||
precision ${PIXI.settings.PRECISION_FRAGMENT} float;
|
||||
|
||||
varying float vHatchOffset;
|
||||
|
||||
uniform vec4 tintAlpha;
|
||||
uniform float resolution;
|
||||
uniform bool hatchEnabled;
|
||||
uniform mediump float hatchThickness;
|
||||
|
||||
void main() {
|
||||
gl_FragColor = tintAlpha;
|
||||
if ( !hatchEnabled ) return;
|
||||
float x = abs(vHatchOffset - floor(vHatchOffset + 0.5)) * 2.0;
|
||||
float s = hatchThickness * resolution;
|
||||
float y0 = clamp((x + 0.5) * s + 0.5, 0.0, 1.0);
|
||||
float y1 = clamp((x - 0.5) * s + 0.5, 0.0, 1.0);
|
||||
gl_FragColor *= mix(0.3333, 1.0, y0 - y1);
|
||||
}
|
||||
`;
|
||||
|
||||
/* ---------------------------------------- */
|
||||
|
||||
/** @inheritDoc */
|
||||
static defaultUniforms = {
|
||||
...super.defaultUniforms,
|
||||
resolution: 1,
|
||||
hatchEnabled: false,
|
||||
hatchThickness: 1
|
||||
};
|
||||
|
||||
/** @inheritDoc */
|
||||
_preRender(mesh, renderer) {
|
||||
super._preRender(mesh, renderer);
|
||||
const uniforms = this.uniforms;
|
||||
uniforms.resolution = (renderer.renderTexture.current ?? renderer).resolution * mesh.worldTransform.a;
|
||||
const projection = renderer.projection.transform;
|
||||
if ( projection ) {
|
||||
const {a, b} = projection;
|
||||
uniforms.resolution *= Math.sqrt((a * a) + (b * b));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user