spatial_material.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. .. _doc_spatial_material:
  2. Spatial Material
  3. ================
  4. Introduction
  5. ------------
  6. ``SpatialMaterial`` is a default 3D material that aims to provide most of the features
  7. artists look for in a material, without the need for writing shader code. However,
  8. it can be converted to shader code if additional functionality is needed.
  9. This tutorial explains most parameters present in ``SpatialMaterial``.
  10. There are 4 ways to add a ``SpatialMaterial`` to an object. It can be added in
  11. the *Material* property of the mesh. It can be added in the *Material* property of
  12. the node using the mesh (such as a MeshInstance node), the *Material Override* property
  13. of the node using the mesh, and the *Material Overlay*.
  14. .. image:: img/add_material.png
  15. If you add a material to the mesh itself, every time that mesh is used it will have that
  16. material. If you add a material to the node using the mesh, the material will only be used
  17. by that node, it will also override the material property of the mesh. If a material is
  18. added in the *Material Override* property of the node, it will only be used by that node.
  19. It will also override the regular material property of the node and the material property of
  20. the mesh.
  21. The *Material Overlay* property will render a material **over** the current one being used by the
  22. mesh. As an example, this can be used to put a transparent shield effect on a mesh.
  23. Flags
  24. -----
  25. Spatial materials have many flags determining the general usage of a material.
  26. .. image:: img/spatial_material1.png
  27. Transparent
  28. ~~~~~~~~~~~
  29. In Godot, materials are not transparent unless specifically configured to be.
  30. The main reason behind this is that transparent materials are rendered
  31. using a different technique (sorted from back to front and rendered in order).
  32. This technique is less efficient (many state changes happen) and makes
  33. the materials unusable with many mid- and post-processing effects
  34. (such as SSAO, SSR, etc.) that require perfectly opaque geometry.
  35. For this reason, materials in Godot are assumed opaque unless
  36. specified otherwise. The main settings that enable transparency are:
  37. * Transparent flag (this one)
  38. * Blend mode set to other than "Mix"
  39. * Enabling distance or proximity fade
  40. Use Shadow to Opacity
  41. ~~~~~~~~~~~~~~~~~~~~~
  42. Lighting modifies the alpha so shadowed areas are opaque and non-shadowed
  43. areas are transparent. Useful for overlaying shadows onto a camera feed in AR.
  44. Unshaded
  45. ~~~~~~~~
  46. In most cases it is common for materials to be affected by lighting (shaded).
  47. However, in some cases you might want to show just the albedo (color) and
  48. ignore the rest. Toggling this flag on will remove all shading and display
  49. pure, unlit color.
  50. .. image:: img/spatial_material26.png
  51. Vertex Lighting
  52. ~~~~~~~~~~~~~~~
  53. Godot has a more or less uniform cost per pixel thanks to depth pre-pass. All
  54. lighting calculations are made by running the lighting shader on every pixel.
  55. As these calculations are costly, performance can be brought down considerably
  56. in some corner cases such as drawing several layers of transparency (which is
  57. common in particle systems). Switching to per-vertex lighting may help in these
  58. cases.
  59. Additionally, on low-end or mobile devices, switching to vertex lighting
  60. can considerably increase rendering performance.
  61. .. image:: img/spatial_material2.png
  62. Keep in mind that when vertex lighting is enabled, only directional lighting
  63. can produce shadows (for performance reasons).
  64. No Depth Test
  65. ~~~~~~~~~~~~~
  66. In order for close objects to appear over far away objects, depth testing
  67. is performed. Disabling it has the result of objects appearing over
  68. (or under) everything else.
  69. Disabling this makes the most sense for drawing indicators in world space,
  70. and works very well with the *Render Priority* property of Material
  71. (see the bottom of this page).
  72. .. image:: img/spatial_material3.png
  73. Use Point Size
  74. ~~~~~~~~~~~~~~~
  75. This option is only effective when the geometry rendered is made of points
  76. (generally it's made of triangles when imported from 3D DCCs). If so, then
  77. those points can be resized (see below).
  78. World Triplanar
  79. ~~~~~~~~~~~~~~~
  80. When using triplanar mapping (see below, in the UV1 and UV2 settings),
  81. triplanar is computed in object local space. This option makes triplanar work
  82. in world space.
  83. Fixed Size
  84. ~~~~~~~~~~
  85. This causes the object to be rendered at the same size no matter the distance.
  86. This is useful mostly for indicators (no depth test and high render priority)
  87. and some types of billboards.
  88. Do Not Receive Shadows
  89. ~~~~~~~~~~~~~~~~~~~~~~
  90. Makes the object not receive any kind of shadow that would otherwise
  91. be cast onto it.
  92. Disable Ambient Light
  93. ~~~~~~~~~~~~~~~~~~~~~
  94. Makes the object not receive any kind of ambient lighting that would
  95. otherwise light it.
  96. Ensure Correct Normals
  97. ~~~~~~~~~~~~~~~~~~~~~~
  98. Fixes normals when non-uniform scaling is used.
  99. Vertex Color
  100. ------------
  101. This setting allows choosing what is done by default to vertex colors that come
  102. from your 3D modelling application. By default, they are ignored.
  103. .. image:: img/spatial_material4.png
  104. Use as Albedo
  105. ~~~~~~~~~~~~~
  106. Choosing this option means vertex color is used as albedo color.
  107. Is sRGB
  108. ~~~~~~~
  109. Most 3D DCCs will likely export vertex colors as sRGB, so toggling this
  110. option on will help them look correct.
  111. Parameters
  112. -----------
  113. ``SpatialMaterial`` also has several configurable parameters to tweak
  114. many aspects of the rendering:
  115. .. image:: img/spatial_material5.png
  116. Diffuse Mode
  117. ~~~~~~~~~~~~
  118. Specifies the algorithm used by diffuse scattering of light when hitting
  119. the object. The default is *Burley*. Other modes are also available:
  120. * **Burley:** Default mode, the original Disney Principled PBS diffuse algorithm.
  121. * **Lambert:** Is not affected by roughness.
  122. * **Lambert Wrap:** Extends Lambert to cover more than 90 degrees when
  123. roughness increases. Works great for hair and simulating cheap
  124. subsurface scattering. This implementation is energy conserving.
  125. * **Oren Nayar:** This implementation aims to take microsurfacing into account
  126. (via roughness). Works well for clay-like materials and some types of cloth.
  127. * **Toon:** Provides a hard cut for lighting, with smoothing affected by roughness.
  128. It is recommended you disable sky contribution from your environment's
  129. ambient light settings or disable ambient light in the spatial material
  130. to achieve a better effect.
  131. .. image:: img/spatial_material6.png
  132. Specular Mode
  133. ~~~~~~~~~~~~~
  134. Specifies how the specular blob will be rendered. The specular blob
  135. represents the shape of a light source reflected in the object.
  136. * **ShlickGGX:** The most common blob used by PBR 3D engines nowadays.
  137. * **Blinn:** Common in previous-generation engines.
  138. Not worth using nowadays, but left here for the sake of compatibility.
  139. * **Phong:** Same as above.
  140. * **Toon:** Creates a toon blob, which changes size depending on roughness.
  141. * **Disabled:** Sometimes the blob gets in the way. Begone!
  142. .. image:: img/spatial_material7.png
  143. Blend Mode
  144. ~~~~~~~~~~
  145. Controls the blend mode for the material. Keep in mind that any mode
  146. other than *Mix* forces the object to go through the transparent pipeline.
  147. * **Mix:** Default blend mode, alpha controls how much the object is visible.
  148. * **Add:** Object is blended additively, nice for flares or some
  149. fire-like effects.
  150. * **Sub:** Object is subtracted.
  151. * **Mul:** Object is multiplied.
  152. .. image:: img/spatial_material8.png
  153. Cull Mode
  154. ~~~~~~~~~
  155. Determines which side of the object is not drawn when backfaces are rendered:
  156. * **Back:** The back of the object is culled when not visible (default).
  157. * **Front:** The front of the object is culled when not visible.
  158. * **Disabled:** Used for objects that are double-sided (no culling is performed).
  159. .. note::
  160. By default, Blender has backface culling disabled on materials and will
  161. export materials to match how they render in Blender. This means that
  162. materials in Godot will have their cull mode set to **Disabled**. This can
  163. decrease performance since backfaces will be rendered, even when they are
  164. being culled by other faces. To resolve this, enable **Backface Culling** in
  165. Blender's Materials tab, then export the scene to glTF again.
  166. Depth Draw Mode
  167. ~~~~~~~~~~~~~~~
  168. Specifies when depth rendering must take place.
  169. * **Opaque Only (default):** Depth is only drawn for opaque objects.
  170. * **Always:** Depth draw is drawn for both opaque and transparent objects.
  171. * **Never:** No depth draw takes place
  172. (do not confuse this with the No Depth Test option above).
  173. * **Depth Pre-Pass:** For transparent objects, an opaque pass is made first
  174. with the opaque parts, then transparency is drawn above.
  175. Use this option with transparent grass or tree foliage.
  176. .. image:: img/material_depth_draw.png
  177. Line Width
  178. ~~~~~~~~~~
  179. When drawing lines, specify the width of the lines being drawn.
  180. This option is not available on most modern hardware.
  181. Point Size
  182. ~~~~~~~~~~
  183. When drawing points, specify the point size in pixels.
  184. Billboard Mode
  185. ~~~~~~~~~~~~~~
  186. Enables billboard mode for drawing materials. This controls how the object
  187. faces the camera:
  188. * **Disabled:** Billboard mode is disabled.
  189. * **Enabled:** Billboard mode is enabled, the object's -Z axis will always
  190. face the camera.
  191. * **Y-Billboard:** The object's X axis will always be aligned with the camera.
  192. * **Particles:** Most suited for particle systems, because it allows
  193. specifying animation options.
  194. .. image:: img/spatial_material9.png
  195. The above options are only enabled for Particle Billboard.
  196. Billboard Keep Scale
  197. ~~~~~~~~~~~~~~~~~~~~
  198. Enables scaling a mesh in billboard mode.
  199. Grow
  200. ~~~~
  201. Grows the object vertices in the direction pointed by their normals:
  202. .. image:: img/spatial_material10.png
  203. This is commonly used to create cheap outlines. Add a second material pass,
  204. make it black and unshaded, reverse culling (Cull Front), and add some grow:
  205. .. image:: img/spatial_material11.png
  206. Use Alpha Scissor
  207. ~~~~~~~~~~~~~~~~~
  208. When transparency other than ``0`` or ``1`` is not needed, it's possible to
  209. set a threshold to prevent the object from rendering semi-transparent pixels.
  210. .. image:: img/spatial_material12.png
  211. This renders the object via the opaque pipeline, which is faster and allows it
  212. to use mid- and post-process effects such as SSAO, SSR, etc.
  213. Material colors, maps and channels
  214. ----------------------------------
  215. Besides the parameters, what defines materials themselves are the colors,
  216. textures, and channels. Godot supports an extensive list of them. They are
  217. described in detail below:
  218. Albedo
  219. ~~~~~~
  220. *Albedo* is the base color for the material, on which all the other settings
  221. operate. When set to *Unshaded*, this is the only color that is visible. In
  222. previous versions of Godot, this channel was named *Diffuse*. The change
  223. of name mainly happened because, in PBR (Physically Based Rendering), this color affects many
  224. more calculations than just the diffuse lighting path.
  225. Albedo color and texture can be used together as they are multiplied.
  226. *Alpha channel* in albedo color and texture is also used for the
  227. object transparency. If you use a color or texture with *alpha channel*,
  228. make sure to either enable transparency or *alpha scissoring* for it to work.
  229. Metallic
  230. ~~~~~~~~
  231. Godot uses a metallic model over competing models due to its simplicity.
  232. This parameter defines how reflective the material is. The more reflective, the
  233. less diffuse/ambient light affects the material and the more light is reflected.
  234. This model is called "energy-conserving".
  235. The *Specular* parameter is a general amount for the reflectivity (unlike
  236. *Metallic*, this is not energy-conserving, so leave it at ``0.5`` and don't touch
  237. it unless you need to).
  238. The minimum internal reflectivity is ``0.04``, so it's impossible to make a
  239. material completely unreflective, just like in real life.
  240. .. image:: img/spatial_material13.png
  241. Roughness
  242. ~~~~~~~~~
  243. *Roughness* affects the way reflection happens. A value of ``0`` makes it a
  244. perfect mirror while a value of ``1`` completely blurs the reflection (simulating
  245. natural microsurfacing). Most common types of materials can be achieved with
  246. the right combination of *Metallic* and *Roughness*.
  247. .. image:: img/spatial_material14.png
  248. Emission
  249. ~~~~~~~~
  250. *Emission* specifies how much light is emitted by the material (keep in mind this
  251. does not include light surrounding geometry unless :ref:`doc_gi_probes` are used).
  252. This value is added to the resulting final image and is not affected by other
  253. lighting in the scene.
  254. .. image:: img/spatial_material15.png
  255. Normal map
  256. ~~~~~~~~~~
  257. Normal mapping allows you to set a texture that represents finer shape detail.
  258. This does not modify geometry, only the incident angle for light. In Godot,
  259. only the red and green channels of normal maps are used for better compression
  260. and wider compatibility.
  261. .. image:: img/spatial_material16.png
  262. .. note::
  263. Godot requires the normal map to use the X+, Y+ and Z+ coordinates, this is
  264. known as OpenGL style. If you've imported a material made to be used with
  265. another engine it may be DirectX style, in which case the normal map needs to
  266. be converted so its Y axis is flipped.
  267. More information about normal maps (including a coordinate order table for
  268. popular engines) can be found
  269. `here <http://wiki.polycount.com/wiki/Normal_Map_Technical_Details>`__.
  270. Rim
  271. ~~~
  272. Some fabrics have small micro-fur that causes light to scatter around it. Godot
  273. emulates this with the *Rim* parameter. Unlike other rim lighting implementations,
  274. which just use the emission channel, this one actually takes light into account
  275. (no light means no rim). This makes the effect considerably more believable.
  276. .. image:: img/spatial_material17.png
  277. Rim size depends on roughness, and there is a special parameter to specify how
  278. it must be colored. If *Tint* is ``0``, the color of the light is used for the
  279. rim. If *Tint* is ``1``, then the albedo of the material is used. Using
  280. intermediate values generally works best.
  281. Clearcoat
  282. ~~~~~~~~~
  283. *This feature is only available when using the GLES3 backend.*
  284. The *Clearcoat* parameter is used to add a secondary pass of transparent coat
  285. to the material. This is common in car paint and toys. In practice, it's a
  286. smaller specular blob added on top of the existing material.
  287. The effect is extremely subtle in Godot 3 releases, and may require specific
  288. lighting or looking at a material a specific way to notice a difference.
  289. This can be seen in the image below where clearcoat is turned on in the
  290. right.
  291. .. image:: img/clearcoat_comparison.png
  292. .. note:: The effect will be more noticeable in Godot 4.
  293. Anisotropy
  294. ~~~~~~~~~~
  295. *This feature is only available when using the GLES3 backend.*
  296. This changes the shape of the specular blob and aligns it to tangent space.
  297. Anisotropy is commonly used with hair, or to make materials such as brushed
  298. aluminum more realistic. It works especially well when combined with flowmaps.
  299. .. image:: img/spatial_material18.png
  300. Ambient Occlusion
  301. ~~~~~~~~~~~~~~~~~~
  302. It is possible to specify a baked ambient occlusion map. This map affects how
  303. much ambient light reaches each surface of the object (it does not affect direct
  304. light by default). While it is possible to use Screen-Space Ambient Occlusion
  305. (SSAO) to generate ambient occlusion, nothing beats the quality of a well-baked
  306. AO map. It is recommended to bake ambient occlusion whenever possible.
  307. .. image:: img/spatial_material19.png
  308. Depth
  309. ~~~~~
  310. *This feature is only available when using the GLES3 backend.*
  311. Setting a depth map on a material produces a ray-marched search to emulate the
  312. proper displacement of cavities along the view direction. This is not real
  313. added geometry, but an illusion of depth. It may not work for complex objects,
  314. but it produces a realistic depth effect for textures. For best results,
  315. *Depth* should be used together with normal mapping.
  316. .. image:: img/spatial_material20.png
  317. Subsurface Scattering
  318. ~~~~~~~~~~~~~~~~~~~~~
  319. *This feature is only available when using the GLES3 backend.*
  320. This effect emulates light that penetrates an object's surface, is scattered,
  321. and then comes out. It is useful to create realistic skin, marble, colored
  322. liquids, etc.
  323. .. image:: img/spatial_material21.png
  324. Transmission
  325. ~~~~~~~~~~~~
  326. This controls how much light from the lit side (visible to light) is transferred
  327. to the dark side (opposite from the light). This works well for thin objects
  328. such as plant leaves, grass, human ears, etc.
  329. .. image:: img/spatial_material22.png
  330. Refraction
  331. ~~~~~~~~~~~
  332. *This feature is only available when using the GLES3 backend.*
  333. When refraction is enabled, it supersedes alpha blending, and Godot attempts to
  334. fetch information from behind the object being rendered instead. This allows
  335. distorting the transparency in a way similar to refraction in real life.
  336. .. image:: img/spatial_material23.png
  337. Detail
  338. ~~~~~~
  339. Godot allows using secondary albedo and normal maps to generate a detail
  340. texture, which can be blended in many ways. By combining this with secondary
  341. UV or triplanar modes, many interesting textures can be achieved.
  342. .. image:: img/spatial_material24.png
  343. There are several settings that control how detail is used.
  344. Mask: The detail mask is a black and white image used to control where the
  345. blending takes place on a texture. White is for the detail textures, Black
  346. is for the regular material textures, different shades of gray are for
  347. partial blending of the material textures and detail textures.
  348. Blend Mode: These four modes control how the textures are blended together.
  349. - Mix: Combines pixel values of both textures. At black, only show the material texture,
  350. at white, only show the detail texture. Values of gray create a smooth blend between
  351. the two.
  352. - Add: Adds pixel values of one Texture with the other. Unlike mix mode
  353. both textures are completely mixed at white parts of a mask and not at gray
  354. parts. The original texture is mostly unchanged at black
  355. - Sub: Subtracts pixel values of one texture with the other. The second
  356. texture is completely subtracted at white parts of a mask with only a little
  357. subtraction in black parts, gray parts being different levels of subtraction
  358. based on the exact texture.
  359. - Mul: Multiplies the RGB channel numbers for each pixel from the top texture
  360. with the values for the corresponding pixel from the bottom texture.
  361. Albedo: This is where you put an albedo texture you want to blend. If nothing
  362. is in this slot it will be interpreted as white by default.
  363. Normal: This is where you put a normal texture you want to blend. If nothing is
  364. in this slot it will be interpreted as a flat normal map. This can still be used
  365. even if the material does not have normal map enabled.
  366. UV1 and UV2
  367. ~~~~~~~~~~~~
  368. Godot supports two UV channels per material. Secondary UV is often useful for
  369. ambient occlusion or emission (baked light). UVs can be scaled and offset,
  370. which is useful when using repeating textures.
  371. Triplanar Mapping
  372. ~~~~~~~~~~~~~~~~~
  373. Triplanar mapping is supported for both UV1 and UV2. This is an alternative way
  374. to obtain texture coordinates, sometimes called "Autotexture". Textures are
  375. sampled in X, Y and Z and blended by the normal. Triplanar mapping can be
  376. performed in either world space or object space.
  377. In the image below, you can see how all primitives share the same material with
  378. world triplanar, so the brick texture continues smoothly between them.
  379. .. image:: img/spatial_material25.png
  380. Proximity and distance fade
  381. ----------------------------
  382. Godot allows materials to fade by proximity to each other as well as depending
  383. on the distance from the viewer. Proximity fade is useful for effects such as
  384. soft particles or a mass of water with a smooth blending to the shores. Distance
  385. fade is useful for light shafts or indicators that are only present after a
  386. given distance.
  387. Keep in mind enabling these enables alpha blending, so abusing them for an
  388. entire scene is usually not a good idea.
  389. .. image:: img/spatial_material_proxfade.gif
  390. Render priority
  391. ---------------
  392. The rendering order of objects can be changed, although this is mostly
  393. useful for transparent objects (or opaque objects that perform depth draw
  394. but no color draw, such as cracks on the floor).