threepipe
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1 рік тому
1 рік тому
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ---
  2. prev:
  3. text: 'Render Pipeline'
  4. link: './render-pipeline'
  5. next:
  6. text: 'UI Configuration'
  7. link: './ui-config'
  8. ---
  9. # Material Extension
  10. Threepipe includes a Material extension system along with a material manager.
  11. The material manager is used to register materials and material extensions.
  12. The material extensions can extend any material in the scene, or any plugin/pass with additional uniforms, defines, shader snippets and provides hooks.
  13. The material extensions are automatically applied to all materials in the scene that are compatible,
  14. when the extension is registered or when the material(the object it's assigned to) is added to the scene.
  15. Threepipe includes several built-in materials like [PhysicalMaterial](https://threepipe.org/docs/classes/PhysicalMaterial.html), [UnlitMaterial](https://threepipe.org/docs/classes/UnlitMaterial.html), [ExtendedShaderMaterial](https://threepipe.org/docs/classes/ExtendedShaderMaterial.html), [LegacyPhongMaterial](https://threepipe.org/docs/classes/LegacyPhongMaterial.html), that include support for extending the material. Any existing three.js material can be made extendable, check the `ShaderPass2` class for a simple example that adds support for material extension to three.js ShaderPass.
  16. Several Plugins create and register material extensions to add different kinds of rendering features over the standard three.js materials like [ClearcoatTintPlugin](https://threepipe.org/docs/classes/ClearcoatTintPlugin.html), [SSAOPlugin](https://threepipe.org/docs/classes/SSAOPlugin.html), [CustomBumpMapPlugin](https://threepipe.org/docs/classes/CustomBumpMapPlugin.html), [AnisotropyPlugin](https://threepipe.org/docs/classes/AnisotropyPlugin.html), [FragmentClippingExtensionPlugin](https://threepipe.org/docs/classes/FragmentClippingExtensionPlugin.html), etc. They also provide uiConfig that can be used to dynamically generate UI or the material extensions.
  17. Some plugins also expose their material extensions to be used by other passes/plugins to access properties like buffers, synced uniforms, defines etc. Like [GBufferPlugin](https://threepipe.org/docs/classes/GBufferPlugin.html), [DepthBufferPlugin](https://threepipe.org/docs/classes/DepthBufferPlugin.html), [NormalBufferPlugin](https://threepipe.org/docs/classes/NormalBufferPlugin.html), etc.
  18. The material extensions must follow the [MaterialExtension](https://threepipe.org/docs/interfaces/MaterialExtension.html) interface.
  19. Many plugins create their own material extensions either for the scene materials or shader passes(like the screen pass). Some plugins like `DepthBufferPlugin` also provides helper material extensions for other custom plugins to fetch value in the depth buffer.
  20. A sample material extension
  21. ```typescript
  22. const extension: MaterialExtension = {
  23. shaderExtender: (shader)=> {
  24. // change the shader properties like shader.fragmentShader, etc
  25. // similar to onBeforeCompile
  26. },
  27. parsFragmentSnippet: ` // add some code before the main function in the fragment shader
  28. uniform sampler2D tTexture;
  29. uniform float opacity;
  30. `,
  31. extraUniforms: {
  32. tTexture: ()=>({value: getTexture()}),
  33. opacity: {value: 1}
  34. // add additional uniforms, these can be IUniform or functions that return IUniform
  35. },
  36. extraDefines: {
  37. ['DEPTH_PACKING']: BasicDepthPacking,
  38. ['SOME_DEFINE']: ()=>"1",
  39. // add additional defines, these can be values or functions that return values
  40. },
  41. priority: 100, // priority when using multiple extensions on the same material
  42. isCompatible: (material) => material.isMeshBasicMaterial, // check if the material is compatible with this extension,
  43. computeCacheKey: (material) => material.uuid, // a custom cache key for the material extension. Shader is recompiled when this is changed
  44. onObjectRender: (object: Object3D, material: IMaterial) => {
  45. // called when some object is rendererd which has a material with this extension.
  46. },
  47. // uiConfig
  48. // check more properties and hooks in the MaterialExtension interface
  49. }
  50. // The extension can be registered to all the materials using the MaterialManager
  51. viewer.assetManager.materialManager.registerMaterialExtension(extension)
  52. // or register it on a single material (like the Screen Pass)
  53. viewer.renderManager.screenPass.material.registerMaterialExtensions([extension])
  54. ```
  55. [//]: # (todo add example)