-
-
Notifications
You must be signed in to change notification settings - Fork 310
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
pbr support transmission #2478
Merged
Merged
pbr support transmission #2478
Changes from 32 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
dd09252
feat: shaderlab pbr support refraction
hhhhkrx 1fc002e
fix: disableMacro
hhhhkrx adfdfec
fix: name
hhhhkrx 62844aa
Merge branch 'dev/1.4' of github.com:galacean/engine into refraction
hhhhkrx ffa2912
fix: refraction mode
hhhhkrx 1fc63b2
fix: keep iridescenceRange unified
hhhhkrx bab9582
fix: refraction
hhhhkrx 174e0e7
fix: refraction
hhhhkrx 47fb197
fix: refraction mode
hhhhkrx f9b748c
fix: refraction
hhhhkrx 4ef6387
Merge branch 'dev/1.4' of github.com:galacean/engine into refraction
hhhhkrx 76b40b1
fix: refraction
hhhhkrx 3abe5c1
fix: refraction
hhhhkrx b81bbeb
fix: refraction
hhhhkrx bcc9daf
fix: refraction
hhhhkrx b9b9558
fix: refraction
hhhhkrx 9d8a37e
Merge branch 'dev/1.4' of github.com:galacean/engine into refraction
hhhhkrx 84e2f0d
fix: refraction mode
hhhhkrx e977cbc
fix: refraction mode
hhhhkrx 4040c26
fix: refraction shader
hhhhkrx 420566d
fix: refraction
hhhhkrx 4a3ce3e
fix: refraction mode
hhhhkrx 6b19600
fix: refraction
hhhhkrx 129ac02
fix: refraction value
hhhhkrx 1ff952d
fix: refraction
hhhhkrx 0093886
Merge branch 'dev/1.4' of github.com:galacean/engine into refraction
hhhhkrx cf77ec4
feat: transmission
hhhhkrx 2764f86
feat: transmission
hhhhkrx 141095a
fix: transmission name
hhhhkrx 36c994c
fix: refraction name
hhhhkrx 2928dcc
fix: refraction mode
hhhhkrx 26c7fd5
fix: refraction mode
hhhhkrx dd6e0ec
fix: refraction mode
hhhhkrx e9f3480
fix: specular name
hhhhkrx d12ff04
fix: specular name
hhhhkrx 9338ec2
fix: diffuseColor mix refraction
hhhhkrx 730548b
fix: diffuse and specular light
hhhhkrx 0a776c4
fix: btdf
hhhhkrx ce75450
refactor: opt code
GuoLei1990 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { MathUtil, Vector2, Vector3, Vector4, Color } from "@galacean/engine-math"; | ||
import { Engine } from "../Engine"; | ||
import { ShaderProperty } from "../shader"; | ||
import { ShaderMacro, ShaderProperty } from "../shader"; | ||
import { Shader } from "../shader/Shader"; | ||
import { Texture2D } from "../texture/Texture2D"; | ||
import { PBRBaseMaterial } from "./PBRBaseMaterial"; | ||
import { RefractionMode } from "./enums/Refraction"; | ||
import { RenderQueueType } from "../shader/enums/RenderQueueType"; | ||
|
||
/** | ||
* PBR (Metallic-Roughness Workflow) Material. | ||
|
@@ -18,19 +20,34 @@ export class PBRMaterial extends PBRBaseMaterial { | |
private static _anisotropyInfoProp = ShaderProperty.getByName("material_AnisotropyInfo"); | ||
private static _anisotropyTextureProp = ShaderProperty.getByName("material_AnisotropyTexture"); | ||
|
||
private _anisotropyRotation: number = 0; | ||
|
||
private static _iridescenceInfoProp = ShaderProperty.getByName("material_IridescenceInfo"); | ||
private static _iridescenceThicknessTextureProp = ShaderProperty.getByName("material_IridescenceThicknessTexture"); | ||
private static _iridescenceTextureProp = ShaderProperty.getByName("material_IridescenceTexture"); | ||
private _iridescenceRange = new Vector2(100, 400); | ||
|
||
private _sheenEnabled = false; | ||
private static _sheenColorProp = ShaderProperty.getByName("material_SheenColor"); | ||
private static _sheenRoughnessProp = ShaderProperty.getByName("material_SheenRoughness"); | ||
private static _sheenTextureProp = ShaderProperty.getByName("material_SheenTexture"); | ||
private static _sheenRoughnessTextureProp = ShaderProperty.getByName("material_SheenRoughnessTexture"); | ||
|
||
private static _transmissionMacro: ShaderMacro = ShaderMacro.getByName("MATERIAL_ENABLE_TRANSMISSION"); | ||
private static _thicknessMacro: ShaderMacro = ShaderMacro.getByName("MATERIAL_HAS_THICKNESS"); | ||
private static _absorptionMacro: ShaderMacro = ShaderMacro.getByName("MATERIAL_HAS_ABSORPTION"); | ||
private static _thicknessTextureMacro: ShaderMacro = ShaderMacro.getByName("MATERIAL_HAS_THICKNESS_TEXTURE"); | ||
private static _transmissionTextureMacro: ShaderMacro = ShaderMacro.getByName("MATERIAL_HAS_TRANSMISSION_TEXTURE"); | ||
private static _transmissionProp = ShaderProperty.getByName("material_Transmission"); | ||
private static _transmissionTextureProp = ShaderProperty.getByName("material_TransmissionTexture"); | ||
private static _attenuationColorProp = ShaderProperty.getByName("material_AttenuationColor"); | ||
private static _attenuationDistanceProp = ShaderProperty.getByName("material_AttenuationDistance"); | ||
private static _thicknessProp = ShaderProperty.getByName("material_Thickness"); | ||
private static _thicknessTextureProp = ShaderProperty.getByName("material_ThicknessTexture"); | ||
|
||
private _refractionMode: RefractionMode; | ||
private _anisotropyRotation: number = 0; | ||
private _iridescenceRange = new Vector2(100, 400); | ||
private _sheenEnabled = false; | ||
private _absorptionEnabled = true; | ||
private _lastRenderQueueType = RenderQueueType.Opaque; | ||
|
||
/** | ||
* Index Of Refraction. | ||
* @defaultValue `1.5` | ||
|
@@ -289,6 +306,139 @@ export class PBRMaterial extends PBRBaseMaterial { | |
} | ||
} | ||
|
||
/** | ||
* Refraction switch. | ||
* @remarks Use refractionMode to set the refraction shape. | ||
*/ | ||
get refractionMode(): RefractionMode { | ||
return this._refractionMode; | ||
} | ||
|
||
set refractionMode(value: RefractionMode) { | ||
if (value !== this._refractionMode) { | ||
this._refractionMode = value; | ||
this._setRefractionMode(value); | ||
} | ||
} | ||
|
||
override set isTransparent(value: boolean) { | ||
super.isTransparent = value; | ||
if (this.transmission > 0) { | ||
// If transmission enabled, always use transparent queue to ensure get correct opaque texture | ||
this.renderState.renderQueueType = RenderQueueType.Transparent; | ||
} | ||
} | ||
|
||
override set alphaCutoff(value: number) { | ||
super.alphaCutoff = value; | ||
if (this.transmission > 0) { | ||
// If transmission enabled, always use transparent queue to ensure get correct opaque texture | ||
this.renderState.renderQueueType = RenderQueueType.Transparent; | ||
} | ||
} | ||
|
||
/** | ||
* Transmission factor. | ||
* @defaultValue `0.0` | ||
*/ | ||
get transmission(): number { | ||
return this.shaderData.getFloat(PBRMaterial._transmissionProp); | ||
} | ||
|
||
set transmission(value: number) { | ||
value = MathUtil.clamp(value, 0, 1); | ||
if (!!this.shaderData.getFloat(PBRMaterial._transmissionProp) !== !!value) { | ||
if (value > 0) { | ||
this.shaderData.enableMacro(PBRMaterial._transmissionMacro); | ||
this.renderState.renderQueueType = RenderQueueType.Transparent; | ||
} else { | ||
this.shaderData.disableMacro(PBRMaterial._transmissionMacro); | ||
} | ||
} | ||
this.shaderData.setFloat(PBRMaterial._transmissionProp, value); | ||
} | ||
|
||
/** | ||
* Transmission texture. | ||
* @remarks Use red channel, and multiply 'transmission'. | ||
*/ | ||
get transmissionTexture(): Texture2D { | ||
return <Texture2D>this.shaderData.getTexture(PBRMaterial._transmissionTextureProp); | ||
} | ||
|
||
set transmissionTexture(value: Texture2D) { | ||
this.shaderData.setTexture(PBRMaterial._transmissionTextureProp, value); | ||
if (value) { | ||
this.shaderData.enableMacro(PBRMaterial._transmissionTextureMacro); | ||
} else { | ||
this.shaderData.disableMacro(PBRMaterial._transmissionTextureMacro); | ||
} | ||
} | ||
|
||
/** | ||
* Attenuation color. | ||
* @defaultValue `[1,1,1]` | ||
*/ | ||
get attenuationColor(): Color { | ||
return this.shaderData.getColor(PBRMaterial._attenuationColorProp); | ||
} | ||
|
||
set attenuationColor(value: Color) { | ||
const attenuationColor = this.shaderData.getColor(PBRMaterial._attenuationColorProp); | ||
if (value !== attenuationColor) { | ||
attenuationColor.copyFrom(value); | ||
} | ||
} | ||
|
||
/** | ||
* Attenuation distance, greater than 0.0. | ||
* @defaultValue `infinity` | ||
*/ | ||
get attenuationDistance(): number { | ||
return this.shaderData.getFloat(PBRMaterial._attenuationDistanceProp); | ||
} | ||
|
||
set attenuationDistance(value: number) { | ||
value = Math.max(0, value); | ||
this.shaderData.setFloat(PBRMaterial._attenuationDistanceProp, value); | ||
} | ||
|
||
/** | ||
* Thickness, greater than or equal to 0.0. | ||
* @defaultValue `0.0` | ||
*/ | ||
get thickness(): number { | ||
return this.shaderData.getFloat(PBRMaterial._thicknessProp); | ||
} | ||
|
||
set thickness(value: number) { | ||
value = Math.max(0, value); | ||
if (!!this.shaderData.getFloat(PBRMaterial._thicknessProp) !== !!value) { | ||
if (value > 0) { | ||
this.shaderData.enableMacro(PBRMaterial._thicknessMacro); | ||
} else { | ||
this.shaderData.disableMacro(PBRMaterial._thicknessMacro); | ||
} | ||
} | ||
this.shaderData.setFloat(PBRMaterial._thicknessProp, value); | ||
} | ||
|
||
/** | ||
* Thickness texture. | ||
* @remarks Use green channel, and multiply 'thickness', range is 0.0 to 1.0. | ||
*/ | ||
get thicknessTexture(): Texture2D { | ||
return <Texture2D>this.shaderData.getTexture(PBRMaterial._thicknessTextureProp); | ||
} | ||
|
||
set thicknessTexture(value: Texture2D) { | ||
this.shaderData.setTexture(PBRMaterial._thicknessTextureProp, value); | ||
if (value) { | ||
this.shaderData.enableMacro(PBRMaterial._thicknessTextureMacro); | ||
} else { | ||
this.shaderData.disableMacro(PBRMaterial._thicknessTextureMacro); | ||
} | ||
} | ||
/** | ||
* Create a pbr metallic-roughness workflow material instance. | ||
* @param engine - Engine to which the material belongs | ||
|
@@ -304,8 +454,20 @@ export class PBRMaterial extends PBRBaseMaterial { | |
shaderData.setVector4(PBRMaterial._iridescenceInfoProp, new Vector4(0, 1.3, 100, 400)); | ||
const sheenColor = new Color(0, 0, 0); | ||
shaderData.setColor(PBRMaterial._sheenColorProp, sheenColor); | ||
this.refractionMode = RefractionMode.Plane; | ||
shaderData.setFloat(PBRMaterial._transmissionProp, 0); | ||
shaderData.setFloat(PBRMaterial._thicknessProp, 0); | ||
shaderData.setFloat(PBRMaterial._attenuationDistanceProp, Infinity); | ||
const attenuationColor = new Color(1, 1, 1); | ||
shaderData.setColor(PBRMaterial._attenuationColorProp, attenuationColor); | ||
shaderData.enableMacro(PBRMaterial._absorptionMacro); | ||
|
||
// @ts-ignore | ||
this._iridescenceRange._onValueChanged = this._onIridescenceRangeChanged.bind(this); | ||
this._iridescenceRange._onValueChanged = () => { | ||
const iridescenceInfo = this.shaderData.getVector4(PBRMaterial._iridescenceInfoProp); | ||
iridescenceInfo.z = this._iridescenceRange.x; | ||
iridescenceInfo.w = this._iridescenceRange.y; | ||
}; | ||
// @ts-ignore | ||
sheenColor._onValueChanged = () => { | ||
const enableSheen = sheenColor.r + sheenColor.g + sheenColor.b > 0; | ||
|
@@ -318,12 +480,18 @@ export class PBRMaterial extends PBRBaseMaterial { | |
} | ||
} | ||
}; | ||
} | ||
|
||
private _onIridescenceRangeChanged(): void { | ||
const iridescenceInfo = this.shaderData.getVector4(PBRMaterial._iridescenceInfoProp); | ||
iridescenceInfo.z = this._iridescenceRange.x; | ||
iridescenceInfo.w = this._iridescenceRange.y; | ||
// @ts-ignore | ||
attenuationColor._onValueChanged = () => { | ||
const enableAbsorption = attenuationColor.r + attenuationColor.g + attenuationColor.b > 0; | ||
if (enableAbsorption !== this._absorptionEnabled) { | ||
this._absorptionEnabled = enableAbsorption; | ||
if (enableAbsorption) { | ||
this.shaderData.enableMacro(PBRMaterial._absorptionMacro); | ||
} else { | ||
this.shaderData.disableMacro(PBRMaterial._absorptionMacro); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
|
@@ -334,4 +502,18 @@ export class PBRMaterial extends PBRBaseMaterial { | |
this.cloneTo(dest); | ||
return dest; | ||
} | ||
|
||
private _setRefractionMode(refractionMode: RefractionMode): void { | ||
switch (refractionMode) { | ||
case RefractionMode.Sphere: | ||
this.shaderData.enableMacro("REFRACTION_MODE", "SPHERE"); | ||
break; | ||
case RefractionMode.Plane: | ||
this.shaderData.enableMacro("REFRACTION_MODE", "PLANE"); | ||
break; | ||
case RefractionMode.Thin: | ||
this.shaderData.enableMacro("REFRACTION_MODE", "THIN"); | ||
break; | ||
} | ||
} | ||
Comment on lines
+528
to
+540
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Refraction mode macro setting.
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Refraction mode. | ||
*/ | ||
export enum RefractionMode { | ||
/** Refraction shape is sphere. */ | ||
Sphere, | ||
/** Refraction shape is plane. */ | ||
Plane, | ||
/** Refraction shape is thin. */ | ||
Thin | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { PBRMaterial, Texture2D } from "@galacean/engine-core"; | ||
import { GLTFMaterialParser } from "../parser/GLTFMaterialParser"; | ||
import { registerGLTFExtension } from "../parser/GLTFParser"; | ||
import { GLTFParserContext, GLTFParserType } from "../parser/GLTFParserContext"; | ||
import { GLTFExtensionMode, GLTFExtensionParser } from "./GLTFExtensionParser"; | ||
import { IKHRMaterialsTransmission } from "./GLTFExtensionSchema"; | ||
@registerGLTFExtension("KHR_materials_transmission", GLTFExtensionMode.AdditiveParse) | ||
class KHR_materials_transmission extends GLTFExtensionParser { | ||
override additiveParse(context: GLTFParserContext, material: PBRMaterial, schema: IKHRMaterialsTransmission): void { | ||
const { transmissionFactor = 0, transmissionTexture } = schema; | ||
material.transmission = transmissionFactor; | ||
|
||
if (transmissionTexture) { | ||
GLTFMaterialParser._checkOtherTextureTransform(transmissionTexture, "Transmission texture"); | ||
|
||
context.get<Texture2D>(GLTFParserType.Texture, transmissionTexture.index).then((texture) => { | ||
material.transmissionTexture = texture; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { PBRMaterial, Texture2D } from "@galacean/engine-core"; | ||
import { Color } from "@galacean/engine-math"; | ||
import { GLTFMaterialParser } from "../parser/GLTFMaterialParser"; | ||
import { registerGLTFExtension } from "../parser/GLTFParser"; | ||
import { GLTFParserContext, GLTFParserType } from "../parser/GLTFParserContext"; | ||
import { GLTFExtensionMode, GLTFExtensionParser } from "./GLTFExtensionParser"; | ||
import { IKHRMaterialsVolume } from "./GLTFExtensionSchema"; | ||
|
||
@registerGLTFExtension("KHR_materials_volume", GLTFExtensionMode.AdditiveParse) | ||
class KHR_materials_volume extends GLTFExtensionParser { | ||
override additiveParse(context: GLTFParserContext, material: PBRMaterial, schema: IKHRMaterialsVolume): void { | ||
const { thicknessFactor = 0, thicknessTexture, attenuationDistance = Infinity, attenuationColor } = schema; | ||
material.thickness = thicknessFactor; | ||
material.attenuationDistance = attenuationDistance; | ||
|
||
if (attenuationColor) { | ||
material.attenuationColor.set( | ||
Color.linearToGammaSpace(attenuationColor[0]), | ||
Color.linearToGammaSpace(attenuationColor[1]), | ||
Color.linearToGammaSpace(attenuationColor[2]), | ||
undefined | ||
); | ||
} | ||
|
||
if (thicknessTexture) { | ||
GLTFMaterialParser._checkOtherTextureTransform(thicknessTexture, "Thickness texture"); | ||
|
||
context.get<Texture2D>(GLTFParserType.Texture, thicknessTexture.index).then((texture) => { | ||
material.thicknessTexture = texture; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use bind this is more readability