engine icon indicating copy to clipboard operation
engine copied to clipboard

Displacement Effect

Open defektu opened this issue 1 year ago • 29 comments

Fixes https://github.com/playcanvas/engine/issues/5554

@mvaligursky requested PR from #5554 to look into it.

Known issues with PR is in issues thread.

It might be a poor implementation in terms of engine's workflow.

LitShader-forward-proc & LitShader-ShadowPass_1_0-proc does not get #define DISPLACEMENT argument possibly because of my poor implementation.

Project file in #5554 works perfectly (probably due to shader chunk rebuilds when replacing a chunk)

Did not implemented checks for implementation cause of draft PR

I confirm I have read the contributing guidelines and signed the Contributor License Agreement.

defektu avatar Aug 17 '23 18:08 defektu

Would you have any way to use this? Engine example or Editor project?

mvaligursky avatar Aug 17 '23 19:08 mvaligursky

Scene for changing transformVS and engine example with use_local_engine

defektu avatar Aug 17 '23 19:08 defektu

I had a quick look, and I think:

  1. you should use heightMap for this instead of introducing new texture channel. The useDisplacement would then use this map as displacement instead of for parallax.
  2. I think you need something like this to have normals attribute added to the vertex shader: Screenshot 2023-08-18 at 11 34 13

Give these a go please and let me know if there are further issues - there might be with supplying UVs to the vertex shader too, but maybe not with the heightMap.

mvaligursky avatar Aug 18 '23 09:08 mvaligursky

Hi @defektu - any thoughts / progress on this? It'd be great to finish off if possible, thanks.

mvaligursky avatar Sep 11 '23 08:09 mvaligursky

Sorry @mvaligursky, nowadays are a bit rough workwise.

Using heights was possible but uv's and texture is not passing through to shadowpass. (vertex's are displacing but shadow pass won't in this case.)

Shadows are rendered through Standart material shader as far as I inspected. Build process won't take the litOption arguments (like texture_shadowMap) while shadows are processing (useHeights=null). Or I might doing something wrong.

I can explore any thoughts in my spare time.

defektu avatar Sep 11 '23 17:09 defektu

Today I noticed standart.js has // all other passes require only opacity part. So ShadowPass is not getting any info about anything except opacity.

defektu avatar Sep 12 '23 13:09 defektu

Yep, we'll need to adjust this to pass on the height if used as well.

mvaligursky avatar Sep 12 '23 13:09 mvaligursky

Also vertex_normals needed. But it does not pass into shadow pass as semantics as well Vertex shader attribute "vertex_normal" is not mapped to a semantic in shader definition, shader [Shader Id 2 LitShader-ShadowPass_1_0-proc]

defektu avatar Sep 12 '23 13:09 defektu

Shaderpass does not get any litOptions (turns out to be undefined) getShaderVariant passes this.shaderOptBuilder.updateMinRef if it is not FORWARD_PASS. So do you have any suggestions about this topic. Besides shadowPass it is working as intended (displacing the surface). image

defektu avatar Sep 12 '23 14:09 defektu

I suspect the StandardMaterialOptionsBuilder will need to be updated to generate required options in the shadow pass - probably in _updateSharedOptions, which I think is shared between forward and shadow pass, but not sure now based on you seeing litOptions being undefined.

Perhaps update the PR with what you have, using the height texture, and I'll see how I can fix the shadows using your example.

mvaligursky avatar Sep 12 '23 14:09 mvaligursky

Rolled back previous changes and added mesh-displacement example draft.

defektu avatar Sep 14 '23 19:09 defektu

Somehow I got it working with shared options. I will cleanup everything and send PR.

defektu avatar Sep 14 '23 21:09 defektu

Pushed working version with minimalOptions = false. It is working as intended like this. image

defektu avatar Sep 14 '23 21:09 defektu

Effect fully works for now but noticed env goes blank (weird frustrum culling behaviour causes that) Can't state if it is caused by PR or something else.

defektu avatar Sep 15 '23 21:09 defektu

I fixed the culling that few days ago .. you're just probably not up to date. Is it all ready for the review you reckon? Ping me when it is, I'll check it out, thanks!

mvaligursky avatar Sep 15 '23 22:09 mvaligursky

Yes it is up and running @mvaligursky. UV transformations are not implemented. displacementOffset is nice to have and can be implemented by introducing new material attribute.

defektu avatar Sep 15 '23 23:09 defektu

Pushed new commit for displacementOffset too.

defektu avatar Sep 15 '23 23:09 defektu

Hi @mvaligursky, It's all ready to review.

Rebased branch with latest updates I accidentally force repushed everything again sorry about that. Culling is fixed with updates from origin. heightMap still affects when displacement is used, this causes some kind of weird artifacts with big displacements. I am suggesting seperating heightMapFactor and adding displacementScale.

Seperating displacementScale will give possibility to remove useDisplacement boolean. We can disable effect if displacementScale == 0. That might be better for UI cleanness with single slider to do everyting at once.

defektu avatar Sep 18 '23 12:09 defektu

I am suggesting seperating heightMapFactor and adding displacementScale.

Great idea! Perhaps we should call it just displacement, default it to 0?

mvaligursky avatar Sep 18 '23 13:09 mvaligursky

It seems several map types support some kind of factor or intensity map/value (e.g. normal, emissive). Can we consider some common naming for that?

willeastcott avatar Sep 18 '23 13:09 willeastcott

Yeah I was looking at it as well .. and saw

heightMapFactor
bumpiness
occludeSpecularIntensity
specularityFactorTint
metalness
iridescenceThickness

and didn't see any easy common naming we could easily move to.

mvaligursky avatar Sep 18 '23 13:09 mvaligursky

There is specularityFactor. Since effect uses heightMap, displacementFactor and displacementOffset is good?

defektu avatar Sep 18 '23 13:09 defektu

There is specularityFactor. Since effect uses heightMap, displacementFactor and displacementOffset is good?

yeah actually, I missed you added an offset too. Sounds good in that case to me.

mvaligursky avatar Sep 18 '23 13:09 mvaligursky

Hey @mvaligursky, Updated PR & ready for review.

Decided to keep useDisplacement attribute for code readability. It sets based on displacementFactor. Cleaned up mesh-displacement example.

I guess it is good to keep useDisplacement attribute in material parameters for debugging?

Not a code dev mainly so ignore commit chains <3

defektu avatar Sep 19 '23 00:09 defektu

Added draft docs for attributes.

Things to consider:

  • This effect uses UV0 and R channel of height map.
  • You can't offset or transform map yet.
  • If there is no heightMap, useDisplacement will break vertex chunk.

defektu avatar Sep 28 '23 08:09 defektu

Hey @mvaligursky and @slimbuck - can we sync on this PR? I'm keen to make a decision on what to do with it.

willeastcott avatar Oct 29 '23 12:10 willeastcott

Is there a way to change heightMap channel for displacement? Also will it apply tiling and offsets?

Another question, will it be viable/common when devs use displacement in combination with parallax? In such case the heightMap textures might be different or the same?

Maksims avatar Oct 29 '23 15:10 Maksims

Hi @Maksims, I think this vertex shader needs some special attributes for offsetting and tiling (which is totally possible). Concurrent options are bound to fragment shader. Don't think that back propagation is not possible unless changing where uniforms defined.

Not really experienced with engine, so this needs crosscheck.

defektu avatar Oct 31 '23 18:10 defektu

Back from service. Ready to do changes if needed.

defektu avatar Dec 16 '23 07:12 defektu

This should be a lot easier (very easy) to do using a ShaderMaterial for custom shaders - which would support skinned and instanced meshes too, see more info here: https://github.com/playcanvas/engine/issues/6835

Path to add it to the StandardMaterial is similar as before, but little simplified. But to add it to it I think we should ideally wait for https://github.com/KhronosGroup/glTF/issues/948 to shake out (could be a while!), to add exactly what is needed.

If you're keen to try and write an engine example using ShaderMaterial, that'd be a welcome addition. As that would likely be a separate PR, I'll close this one, but very much appreciate your time and efforts here @defektu .

mvaligursky avatar Aug 02 '24 13:08 mvaligursky