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

Displacement Effect #5573

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
cb69ae3
feature(Displacement): Init
defektu Aug 17, 2023
bab5a3d
fix(displacement): rollback & heightMap replacement
defektu Sep 14, 2023
778ed42
fix(displacement): working state with temporary flag
defektu Sep 14, 2023
7f7b099
chore(mesh-displacement): updated mesh-displacement example
defektu Sep 15, 2023
8571a0c
fix(standart-material): displacement effect fully works now.
defektu Sep 15, 2023
4321fb1
feature(displacement-effect): displacement offset
defektu Sep 15, 2023
70eb2f7
fix(standart-material): lint
defektu Sep 16, 2023
0771893
feature(Displacement): Init
defektu Aug 17, 2023
c6daa4c
fix(displacement): rollback & heightMap replacement
defektu Sep 14, 2023
480edb2
fix(displacement): working state with temporary flag
defektu Sep 14, 2023
a9fa42c
chore(mesh-displacement): updated mesh-displacement example
defektu Sep 15, 2023
744516c
fix(standart-material): displacement effect fully works now.
defektu Sep 15, 2023
e21a60b
feature(displacement-effect): displacement offset
defektu Sep 15, 2023
60f59ec
fix(standart-material): lint
defektu Sep 16, 2023
eb7ae50
Merge branch 'displacement_effect' of https://github.com/defektu/engi…
defektu Sep 18, 2023
8289413
feat(displacement): displacementFactor added
defektu Sep 18, 2023
3738ca2
chore(mesh-displacement.tsx): added displacementFactor variable
defektu Sep 19, 2023
312bed2
fix(types-fixup): no fixup needed
defektu Sep 19, 2023
535f0b2
chore(displacement) updated example & couple changes
defektu Sep 19, 2023
d919105
fix(types): lint
defektu Sep 19, 2023
b5c4d73
fix(displcamenet) removed lit arguments
defektu Sep 19, 2023
20b5b0c
Updated property explanations.
defektu Sep 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions examples/src/examples/graphics/mesh-displacement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class MeshDisplacementExample {
<LabelGroup text='Height Map Factor'>
<SliderInput binding={new BindingTwoWay()} link={{ observer: data, path: 'script.useDisplacement.heightMapFactor' }} min={-2.0} max={2.0}/>
</LabelGroup>
<LabelGroup text='Displacement Offset'>
<SliderInput binding={new BindingTwoWay()} link={{ observer: data, path: 'script.useDisplacement.displacementOffset' }} min={-2.0} max={2.0}/>
</LabelGroup>
</Panel>
<Panel headerText='Point light'>
defektu marked this conversation as resolved.
Show resolved Hide resolved
<LabelGroup text='Enabled'>
Expand Down Expand Up @@ -145,7 +148,8 @@ class MeshDisplacementExample {
},
useDisplacement: {
enabled: true,
heightMapFactor: -0.25
heightMapFactor: -0.25,
displacementOffset: 0.2
}
});

Expand Down Expand Up @@ -187,6 +191,13 @@ class MeshDisplacementExample {
material.heightMap = assets.height.resource;
material.heightMapFactor = data.get('script.useDisplacement.heightMapFactor');
material.useDisplacement = data.get('script.useDisplacement.enabled');
material.displacementOffset = data.get('script.useDisplacement.displacementOffset');
const tiling = new pc.Vec2(2, 2);
material.heightMapTiling = tiling;
material.diffuseMapTiling = tiling;
material.metalnessMapTiling = tiling;
material.glossMapTiling = tiling;
material.normalMapTiling = tiling;
material.update();

const sphere = createSphere(0, 2.5, 0, material, true);
Expand Down Expand Up @@ -265,9 +276,11 @@ class MeshDisplacementExample {

let resizeControlPanel = true;
app.on("update", function (dt) {
// update material options
// update material options
material.heightMapFactor = data.get('script.useDisplacement.heightMapFactor');
material.useDisplacement = data.get('script.useDisplacement.enabled');
material.displacementOffset = data.get('script.useDisplacement.displacementOffset');

material.update();

// resize control panel to fit the content better
Expand Down
2 changes: 1 addition & 1 deletion src/scene/materials/standard-material-options-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class StandardMaterialOptionsBuilder {
options.litOptions.useDynamicRefraction = stdMat.useDynamicRefraction;

options.litOptions.useDisplacement = stdMat.useDisplacement;
defektu marked this conversation as resolved.
Show resolved Hide resolved

options.litOptions.displacementOffset = stdMat.displacementOffset;
}

_updateEnvOptions(options, stdMat, scene) {
Expand Down
1 change: 1 addition & 0 deletions src/scene/materials/standard-material-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const standardMaterialParameterTypes = {
heightMapFactor: 'number',

useDisplacement: "boolean",
displacementOffset: "number",

alphaToCoverage: 'boolean',
alphaTest: 'number',
Expand Down
5 changes: 5 additions & 0 deletions src/scene/materials/standard-material.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,11 @@
this._setParameter('material_heightMapFactor', getUniform('heightMapFactor'));
}

if (this.useDisplacement) {
this._setParameter('material_displacementOffset', this.displacementOffset);
console.log(this.displacementOffset)
defektu marked this conversation as resolved.
Show resolved Hide resolved
}

const isPhong = this.shadingModel === SPECULAR_PHONG;

// set overridden environment textures
Expand All @@ -813,7 +818,7 @@
} else if (this.sphereMap) {
this._setParameter('texture_sphereMap', this.sphereMap);
}

Check failure on line 821 in src/scene/materials/standard-material.js

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
this._setParameter('material_reflectivity', this.reflectivity);

// remove unused params
Expand Down
3 changes: 2 additions & 1 deletion src/scene/shader-lib/chunks/common/vert/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ mat4 getModelMatrix() {
#ifdef DISPLACEMENT
uniform sampler2D texture_heightMap;
uniform float material_heightMapFactor;
uniform float material_displacementOffset;
#endif
vec4 getPosition() {
dModelMatrix = getModelMatrix();

defektu marked this conversation as resolved.
Show resolved Hide resolved
#ifdef DISPLACEMENT
vec4 dMapSample = texture2D(texture_heightMap, vertex_texCoord0.xy);
float displacer = dMapSample.r * material_heightMapFactor * 10.;
float displacer = dMapSample.r * material_heightMapFactor * 10. + material_displacementOffset;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the *10 part here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can be removed when material_heightMapFactor is replaced with new attribute.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be more useful for vertex displacement to be implemented as an overridable shader chunk instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure, you are the guys to call the shot.

vec3 localPos = vertex_position + vertex_normal * displacer;
#else
vec3 localPos = vertex_position;
Expand Down
1 change: 1 addition & 0 deletions utils/types-fixup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ const standardMaterialProps = [
['cubeMap', 'Texture|null'],
['cubeMapProjection', 'number'],
['cubeMapProjectionBox', 'BoundingBox'],
['displacementOffset', 'number'],
['diffuse', 'Color'],
['diffuseDetailMap', 'Texture|null'],
['diffuseDetailMapChannel', 'string'],
Expand Down