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.

IMaterial.ts 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import type {Color, Event, IUniform, Material, MaterialParameters, Shader} from 'three'
  2. import type {IDisposable, IJSONSerializable} from 'ts-browser-helpers'
  3. import type {MaterialExtension} from '../materials'
  4. import type {ChangeEvent, IUiConfigContainer} from 'uiconfig.js'
  5. import type {SerializationMetaType} from '../utils'
  6. import type {IObject3D} from './IObject'
  7. import {ISetDirtyCommonOptions} from './IObject'
  8. import type {ITexture} from './ITexture'
  9. import type {IImportResultUserData} from '../assetmanager'
  10. export type IMaterialParameters = MaterialParameters & {customMaterialExtensions?: MaterialExtension[]}
  11. export type IMaterialEventTypes = 'dispose' | 'materialUpdate' | 'beforeRender' | 'beforeCompile' | 'afterRender' | 'textureUpdate' | 'beforeDeserialize'
  12. export type IMaterialEvent<T extends string = IMaterialEventTypes> = Event & {
  13. type: T
  14. bubbleToObject?: boolean
  15. bubbleToParent?: boolean
  16. material?: IMaterial
  17. texture?: ITexture
  18. oldTexture?: ITexture
  19. uiChangeEvent?: ChangeEvent
  20. }
  21. export interface IMaterialSetDirtyOptions extends ISetDirtyCommonOptions{
  22. /**
  23. * @default true
  24. */
  25. bubbleToObject?: boolean,
  26. /**
  27. * @default true
  28. */
  29. needsUpdate?: boolean,
  30. [key: string]: any
  31. }
  32. export interface IMaterialUserData extends IImportResultUserData{
  33. uuid?: string // adding to userdata also, so that its saved in gltf
  34. /**
  35. * Automatically dispose material when not used by any object in the scene
  36. * @default true
  37. */
  38. disposeOnIdle?: boolean
  39. renderToGBuffer?: boolean
  40. /**
  41. * Same as {@link renderToGBuffer} but for depth only, not normal or flags etc
  42. */
  43. renderToDepth?: boolean
  44. // only for materials that have envMapIntensity
  45. separateEnvMapIntensity?: boolean // default: false
  46. cloneId?: string
  47. cloneCount?: number
  48. __envIntensity?: number // temp storage for envMapIntensity while rendering
  49. __isVariation?: boolean
  50. inverseAlphaMap?: boolean // only for physical material right now
  51. /**
  52. * See {@link MaterialManager.dispose} as {@link BaseGroundPlugin._refreshMaterial}
  53. */
  54. runtimeMaterial?: boolean
  55. /**
  56. * See {@link GBufferPlugin}
  57. */
  58. gBufferData?: {
  59. materialId?: number
  60. /**
  61. * @default true
  62. */
  63. tonemapEnabled?: boolean
  64. [key: string]: any
  65. }
  66. /**
  67. * Force a depth value in GBuffer.
  68. * This is useful to force center values like 0 to the depth.
  69. */
  70. forcedLinearDepth?: number // todo uiconfig for this in imaterial?
  71. /**
  72. * General flag to disable multiple plugins on the material at once, like SSAO, SSR, Bloom etc.
  73. */
  74. pluginsDisabled?: boolean // todo uiconfig for this in imaterial?
  75. // todo: move these to respective plugins
  76. /**
  77. * For SSCSPlugin
  78. */
  79. sscsDisabled?: boolean
  80. /**
  81. * For SSRPlugin
  82. */
  83. ssreflDisabled?: boolean
  84. /**
  85. * For SSRPlugin
  86. */
  87. ssreflNonPhysical?: boolean
  88. [key: string]: any
  89. // legacy, to be removed
  90. /**
  91. * @deprecated
  92. */
  93. setDirty?: (options?: IMaterialSetDirtyOptions) => void
  94. /**
  95. * @deprecated Use {@link postTonemap.tonemapEnabled} instead. This is kept because used in old files.
  96. */
  97. postTonemap?: boolean
  98. }
  99. export interface IMaterial<E extends IMaterialEvent = IMaterialEvent, ET = IMaterialEventTypes> extends Material<E, ET>, IJSONSerializable, IDisposable, IUiConfigContainer {
  100. constructor: {
  101. TYPE: string
  102. TypeSlug: string
  103. MaterialProperties?: Record<string, any>
  104. MaterialTemplate?: IMaterialTemplate
  105. }
  106. assetType: 'material'
  107. setDirty(options?: IMaterialSetDirtyOptions): void;
  108. // clone?: ()=> any;
  109. needsUpdate: boolean;
  110. // toJSON same as three.js Material.toJSON
  111. // toJSON(meta?: any): any;
  112. // copyProps should be just setValues
  113. setValues(parameters: Material|(MaterialParameters&{type?:string}), allowInvalidType?: boolean, clearCurrentUserData?: boolean): this;
  114. toJSON(meta?: SerializationMetaType, _internal?: boolean): any;
  115. fromJSON(json: any, meta?: SerializationMetaType, _internal?: boolean): this | null;
  116. extraUniformsToUpload: Record<string, IUniform>
  117. materialExtensions: MaterialExtension[]
  118. registerMaterialExtensions: (customMaterialExtensions: MaterialExtension[]) => void;
  119. unregisterMaterialExtensions: (customMaterialExtensions: MaterialExtension[]) => void;
  120. /**
  121. * Managed internally, do not change manually
  122. */
  123. generator?: IMaterialGenerator
  124. /**
  125. * Managed internally, do not change manually
  126. */
  127. appliedMeshes: Set<IObject3D>
  128. lastShader?: Shader
  129. // Note: for userData: add _ in front of for private use, which is preserved while cloning but not serialisation, and __ for private use, which is not preserved while cloning and serialisation
  130. userData: IMaterialUserData
  131. /**
  132. * Disposes the material from the GPU.
  133. * Set force to false if not sure the material is used by any object in the scene.
  134. * // todo add check for visible in scene also? or is that overkill
  135. * @param force - when true, same as three.js dispose. when false, only disposes if disposeOnIdle not false and not used by any object in the scene. default: true
  136. */
  137. dispose(force?: boolean): void
  138. // optional from subclasses, added here for autocomplete
  139. flatShading?: boolean
  140. map?: ITexture | null
  141. alphaMap?: ITexture | null
  142. envMap?: ITexture | null
  143. envMapIntensity?: number
  144. aoMap?: ITexture | null
  145. lightMap?: ITexture | null
  146. normalMap?: ITexture | null
  147. bumpMap?: ITexture | null
  148. displacementMap?: ITexture | null
  149. aoMapIntensity?: number
  150. lightMapIntensity?: number
  151. roughnessMap?: ITexture | null
  152. metalnessMap?: ITexture | null
  153. roughness?: number
  154. metalness?: number
  155. transmissionMap?: ITexture | null
  156. transmission?: number
  157. color?: Color
  158. wireframe?: boolean
  159. linewidth?: number
  160. isRawShaderMaterial?: boolean
  161. isPhysicalMaterial?: boolean
  162. isUnlitMaterial?: boolean
  163. isGBufferMaterial?: boolean
  164. // [key: string]: any
  165. }
  166. export type IMaterialGenerator<T extends IMaterial = IMaterial> = (params: any)=>T
  167. export interface IMaterialTemplate<T extends IMaterial = IMaterial, TP = any>{
  168. templateUUID?: string,
  169. name: string,
  170. typeSlug?: string,
  171. alias?: string[], // alternate names
  172. materialType: string,
  173. generator?: IMaterialGenerator<T>,
  174. params?: TP
  175. }