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

[1.0] Fix rim lighting #851

Merged
merged 3 commits into from
Nov 4, 2021
Merged
Changes from 1 commit
Commits
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
180 changes: 83 additions & 97 deletions packages/three-vrm-materials-mtoon/src/MToonMaterial.ts
Original file line number Diff line number Diff line change
@@ -281,26 +281,7 @@ export class MToonMaterial extends THREE.ShaderMaterial {
public set ignoreVertexColor(value: boolean) {
this._ignoreVertexColor = value;

this._updateShaderCode();
}

/**
* Give the `renderer.capabilities.isWebGL2` here.
* Needed from Three.js r133.
*/
private _isWebGL2 = false;

/**
* Give the `renderer.capabilities.isWebGL2` here.
* Needed from Three.js r133.
*/
public get isWebGL2(): boolean {
return this._isWebGL2;
}
public set isWebGL2(value: boolean) {
this._isWebGL2 = value;

this._updateShaderCode();
this.needsUpdate = true;
}

private _debugMode: MToonMaterialDebugMode = MToonMaterialDebugMode.None;
@@ -311,7 +292,7 @@ export class MToonMaterial extends THREE.ShaderMaterial {
set debugMode(m: MToonMaterialDebugMode) {
this._debugMode = m;

this._updateShaderCode();
this.needsUpdate = true;
}

private _outlineWidthMode: MToonMaterialOutlineWidthMode = MToonMaterialOutlineWidthMode.None;
@@ -322,7 +303,7 @@ export class MToonMaterial extends THREE.ShaderMaterial {
set outlineWidthMode(m: MToonMaterialOutlineWidthMode) {
this._outlineWidthMode = m;

this._updateShaderCode();
this.needsUpdate = true;
}

private _isOutline = false;
@@ -333,7 +314,7 @@ export class MToonMaterial extends THREE.ShaderMaterial {
set isOutline(b: boolean) {
this._isOutline = b;

this._updateShaderCode();
this.needsUpdate = true;
}

/**
@@ -344,7 +325,7 @@ export class MToonMaterial extends THREE.ShaderMaterial {
}

constructor(parameters: MToonMaterialParameters = {}) {
super();
super({ vertexShader, fragmentShader });

// override depthWrite with transparentWithZWrite
if (parameters.transparentWithZWrite) {
@@ -420,7 +401,83 @@ export class MToonMaterial extends THREE.ShaderMaterial {
this.setValues(parameters);

// == update shader stuff ======================================================================
this._updateShaderCode();
this.onBeforeCompile = (shader, renderer) => {
/**
* Will be needed to determine whether we should inline convert sRGB textures or not.
* See: https://github.com/mrdoob/three.js/pull/22551
*/
const isWebGL2 = renderer.capabilities.isWebGL2;

const useUvInVert = this.outlineWidthMultiplyTexture !== null;
const useUvInFrag =
this.map !== null ||
this.shadeMultiplyTexture !== null ||
this.shadingShiftTexture !== null ||
this.rimMultiplyTexture !== null ||
this.uvAnimationMaskTexture !== null;

const threeRevision = parseInt(THREE.REVISION, 10);

const defines =
Object.entries({
// Temporary compat against shader change @ Three.js r126
// See: #21205, #21307, #21299
THREE_VRM_THREE_REVISION: threeRevision,

OUTLINE: this._isOutline,
MTOON_USE_UV: useUvInVert || useUvInFrag, // we can't use `USE_UV` , it will be redefined in WebGLProgram.js
MTOON_UVS_VERTEX_ONLY: useUvInVert && !useUvInFrag,
USE_SHADEMULTIPLYTEXTURE: this.shadeMultiplyTexture !== null,
USE_SHADINGSHIFTTEXTURE: this.shadingShiftTexture !== null,
USE_MATCAPTEXTURE: this.matcapTexture !== null,
USE_RIMMULTIPLYTEXTURE: this.rimMultiplyTexture !== null,
USE_OUTLINEWIDTHMULTIPLYTEXTURE: this.outlineWidthMultiplyTexture !== null,
USE_UVANIMATIONMASKTEXTURE: this.uvAnimationMaskTexture !== null,
IGNORE_VERTEX_COLOR: this._ignoreVertexColor === true,
DEBUG_NORMAL: this._debugMode === 'normal',
DEBUG_LITSHADERATE: this._debugMode === 'litShadeRate',
DEBUG_UV: this._debugMode === 'uv',
OUTLINE_WIDTH_WORLD: this._outlineWidthMode === MToonMaterialOutlineWidthMode.WorldCoordinates,
OUTLINE_WIDTH_SCREEN: this._outlineWidthMode === MToonMaterialOutlineWidthMode.ScreenCoordinates,
})
.filter(([token, macro]) => !!macro)
.map(([token, macro]) => `#define ${token} ${macro}`)
.join('\n') + '\n';

// -- texture encodings ----------------------------------------------------------------------
const encodings =
(this.matcapTexture !== null
? getTexelDecodingFunction(
'matcapTextureTexelToLinear',
getTextureEncodingFromMap(this.matcapTexture, isWebGL2),
) + '\n'
: '') +
(this.shadeMultiplyTexture !== null
? getTexelDecodingFunction(
'shadeMultiplyTextureTexelToLinear',
getTextureEncodingFromMap(this.shadeMultiplyTexture, isWebGL2),
) + '\n'
: '') +
(this.rimMultiplyTexture !== null
? getTexelDecodingFunction(
'rimMultiplyTextureTexelToLinear',
getTextureEncodingFromMap(this.rimMultiplyTexture, isWebGL2),
) + '\n'
: '');

// -- generate shader code -------------------------------------------------------------------
shader.vertexShader = defines + shader.vertexShader;
shader.fragmentShader = defines + encodings + shader.fragmentShader;

// -- compat ---------------------------------------------------------------------------------

// COMPAT
// Three.js r132 introduces new shader chunks <normal_pars_fragment> and <alphatest_pars_fragment>
if (threeRevision < 132) {
shader.fragmentShader = shader.fragmentShader.replace('#include <normal_pars_fragment>', '');
shader.fragmentShader = shader.fragmentShader.replace('#include <alphatest_pars_fragment>', '');
}
};
}

/**
@@ -498,7 +555,7 @@ export class MToonMaterial extends THREE.ShaderMaterial {
this.isOutline = source.isOutline;

// == update shader stuff ======================================================================
this._updateShaderCode();
this.needsUpdate = true;

return this;
}
@@ -512,75 +569,4 @@ export class MToonMaterial extends THREE.ShaderMaterial {
dst.value.copy(src.value.matrix);
}
}

private _updateShaderCode(): void {
const useUvInVert = this.outlineWidthMultiplyTexture !== null;
const useUvInFrag =
this.map !== null ||
this.shadeMultiplyTexture !== null ||
this.shadingShiftTexture !== null ||
this.rimMultiplyTexture !== null ||
this.uvAnimationMaskTexture !== null;

const threeRevision = parseInt(THREE.REVISION, 10);

this.defines = {
// Temporary compat against shader change @ Three.js r126
// See: #21205, #21307, #21299
THREE_VRM_THREE_REVISION: threeRevision,

OUTLINE: this._isOutline,
MTOON_USE_UV: useUvInVert || useUvInFrag, // we can't use `USE_UV` , it will be redefined in WebGLProgram.js
MTOON_UVS_VERTEX_ONLY: useUvInVert && !useUvInFrag,
USE_SHADEMULTIPLYTEXTURE: this.shadeMultiplyTexture !== null,
USE_SHADINGSHIFTTEXTURE: this.shadingShiftTexture !== null,
USE_MATCAPTEXTURE: this.matcapTexture !== null,
USE_RIMMULTIPLYTEXTURE: this.rimMultiplyTexture !== null,
USE_OUTLINEWIDTHMULTIPLYTEXTURE: this.outlineWidthMultiplyTexture !== null,
USE_UVANIMATIONMASKTEXTURE: this.uvAnimationMaskTexture !== null,
IGNORE_VERTEX_COLOR: this._ignoreVertexColor === true,
DEBUG_NORMAL: this._debugMode === 'normal',
DEBUG_LITSHADERATE: this._debugMode === 'litShadeRate',
DEBUG_UV: this._debugMode === 'uv',
OUTLINE_WIDTH_WORLD: this._outlineWidthMode === MToonMaterialOutlineWidthMode.WorldCoordinates,
OUTLINE_WIDTH_SCREEN: this._outlineWidthMode === MToonMaterialOutlineWidthMode.ScreenCoordinates,
};

// == texture encodings ========================================================================
const encodings =
(this.matcapTexture !== null
? getTexelDecodingFunction(
'matcapTextureTexelToLinear',
getTextureEncodingFromMap(this.matcapTexture, this.isWebGL2),
) + '\n'
: '') +
(this.shadeMultiplyTexture !== null
? getTexelDecodingFunction(
'shadeMultiplyTextureTexelToLinear',
getTextureEncodingFromMap(this.shadeMultiplyTexture, this.isWebGL2),
) + '\n'
: '') +
(this.rimMultiplyTexture !== null
? getTexelDecodingFunction(
'rimMultiplyTextureTexelToLinear',
getTextureEncodingFromMap(this.rimMultiplyTexture, this.isWebGL2),
) + '\n'
: '');

// == generate shader code =====================================================================
this.vertexShader = vertexShader;
this.fragmentShader = encodings + fragmentShader;

// == compat ===================================================================================

// COMPAT
// Three.js r132 introduces new shader chunks <normal_pars_fragment> and <alphatest_pars_fragment>
if (threeRevision < 132) {
this.fragmentShader = this.fragmentShader.replace('#include <normal_pars_fragment>', '');
this.fragmentShader = this.fragmentShader.replace('#include <alphatest_pars_fragment>', '');
}

// == set needsUpdate flag =====================================================================
this.needsUpdate = true;
}
}