threepipe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MaterialExtension.ts 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {IUniform, Object3D, Shader, WebGLRenderer} from 'three'
  2. import {IMaterial} from '../core'
  3. import {UiObjectConfig} from 'uiconfig.js'
  4. /**
  5. * Material extension interface
  6. * This is used to extend a three.js material satisfying the IMaterial interface, with extra uniforms, defines, shader code, etc.
  7. */
  8. export interface MaterialExtension{
  9. /**
  10. * Extra uniforms to copy to material
  11. */
  12. extraUniforms?: {[uniform: string]: IUniform};
  13. /**
  14. * Extra defines to copy to material
  15. */
  16. extraDefines?: Record<string, number|string>;
  17. /**
  18. * Custom callback to extend/modify/replace shader code and other shader properties
  19. * @param shader
  20. * @param material
  21. * @param renderer
  22. */
  23. shaderExtender?: (shader: Shader, material: IMaterial, renderer: WebGLRenderer) => void,
  24. /**
  25. * Extra code to add to the top of the fragment shader
  26. * Value can be a string or a function that returns a string
  27. */
  28. parsFragmentSnippet?: string | ((renderer?: WebGLRenderer, material?:IMaterial)=>string),
  29. /**
  30. * Extra code to add to the top of the vertex shader
  31. * Value can be a string or a function that returns a string
  32. */
  33. parsVertexSnippet?: string | ((renderer?: WebGLRenderer, material?:IMaterial)=>string),
  34. // customCacheKey?: string, // same as computeCacheKey
  35. /**
  36. * Custom cache key to use for this material extension.
  37. * A different cache key will cause the shader to be recompiled.
  38. * Check three.js docs for more info.
  39. * Value can be a string or a function that returns a string
  40. * This will only be checked if `material.needsUpdate` is `true`, not on every render.
  41. */
  42. computeCacheKey?: string | ((material: IMaterial) => string)
  43. /**
  44. * Custom callback to run code before the material is rendered
  45. * Executes from `material.onBeforeRender` for each material for each object it's rendered on.
  46. * @param object
  47. * @param material
  48. * @param renderer
  49. */
  50. onObjectRender?: (object: Object3D, material: IMaterial, renderer: WebGLRenderer) => void
  51. /**
  52. * Custom callback to run code after the material is rendered
  53. * Executes from `material.onAfterRender` for each material for each object it's rendered on.
  54. * @param object
  55. * @param material
  56. * @param renderer
  57. */
  58. onAfterRender?: (object: Object3D, material: IMaterial, renderer: WebGLRenderer) => void
  59. /**
  60. * Function to check if this material extension is compatible with the given material.
  61. * If not compatible, the material extension will not be applied.
  62. * This is only checked when the extension is registered.
  63. * @param material
  64. */
  65. isCompatible: (material: IMaterial) => boolean
  66. /**
  67. * List of shader properties updaters to run on the material.
  68. *
  69. */
  70. updaters?: IShaderPropertiesUpdater[]|(()=>IShaderPropertiesUpdater[])
  71. /**
  72. * Function to return the UI config for this material extension.
  73. * This is called once when the material extension is registered.
  74. * @param material
  75. */
  76. getUiConfig?: (material: IMaterial, refreshUi: UiObjectConfig['uiRefresh']) => UiObjectConfig | undefined
  77. updateVersion?: number
  78. // eslint-disable-next-line @typescript-eslint/naming-convention
  79. __setDirty?: () => void // set by MaterialExtender, this increments updateVersion, which ends up calling needsUpdate on all the materials with this extension
  80. uuid?: string
  81. setDirty?: ()=>void // this is set automatically if does not exists. calls __setDirty for all materials. //todo: also refresh UI.
  82. }
  83. export interface IShaderPropertiesUpdater {
  84. updateShaderProperties(material: {defines: Record<string, string | number | undefined>, uniforms: {[name: string]: IUniform}}): this;
  85. }