threepipe
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

scene-background.md 6.3KB

11 месяцев назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ---
  2. prev:
  3. text: 'GLTF Fat/Mesh Lines'
  4. link: './gltf-mesh-lines'
  5. next: false
  6. aside: false
  7. ---
  8. # Setting Background Color and Images
  9. The Background in `threepipe` is managed by the `scene` in the `viewer`.
  10. It can be set to a color(using [`scene.setBackgroundColor`](https://threepipe.org/docs/classes/RootScene.html#setBackgroundColor)), a flat 2d or environment map texture(using [`scene.background`](https://threepipe.org/docs/classes/RootScene.html#background)), or both.
  11. When both are set, the color is multiplied with the texture, so it can be used to tint the background.
  12. The final intensity(brightness) of the background can also be controlled with the [`scene.backgroundIntensity`](https://threepipe.org/docs/classes/RootScene.html#backgroundintensity) property on the scene.
  13. ## Background Color
  14. To set the background color, use the `setBackgroundColor` method on the scene. This will set a solid color as the background.
  15. ```javascript
  16. viewer.scene.setBackgroundColor(0x000000); // Set background to black
  17. ```
  18. ::: info
  19. The color passed in can be a number (`0xff00ff`), a string(`'#ff00ff'`, `'rgba(255, 0, 255, 1)'`), or a `Color` object.
  20. It will be converted to a `Color` object internally and set to the `backgroundColor` property of the scene.
  21. :::
  22. To remove the background color, set it to `null`:
  23. ```javascript
  24. viewer.scene.setBackgroundColor(null); // Remove background color
  25. ```
  26. To get the current background color, access the [`backgroundColor`](https://threepipe.org/docs/classes/RootScene.html#backgroundcolor) property of the scene:
  27. ```javascript
  28. const currentColor = viewer.scene.backgroundColor; // Get current background color
  29. console.log('#' + currentColor.getHexString()); // Log color in hex format
  30. ```
  31. ## Background Texture
  32. An Image/Texture can be set as the background using the `viewer.setBackgroundMap` function by passing in a url, `IAsset`, `File` object or a loaded `Texture`.
  33. ```javascript
  34. viewer.setBackgroundMap('https://example.com/path/to/texture.jpg'); // Set background texture
  35. // or load an env map as background
  36. viewer.setBackgroundMap('https://example.com/path/to/envmap.hdr', {
  37. setEnvironment: true // if set to true, the environment map will also be set as the scene's background
  38. });
  39. // or set the background property directly
  40. viewer.scene.background = viewer.load<ITexture>('https://example.com/path/to/texture.jpg')
  41. ```
  42. If a color is set, it will be multiplied with the texture, allowing you to tint the background.
  43. To remove the background texture, simply set it to `null`:
  44. ```javascript
  45. viewer.setBackgroundMap(null)
  46. // or
  47. viewer.scene.background = null;
  48. ```
  49. ::: tip
  50. When setting a flat texture as the background, it will be stretched to fill the entire viewport.
  51. This can be changed by either setting the `mapping` property of the texture to `EquirectangularReflectionMapping`. (it is set automatically when loading an appropriate HDR/EXR texture)
  52. :::
  53. ## Transparent Background
  54. To set a transparent background, you can set both the `scene.background` and `scene.backgroundColor` to `null`.
  55. ```javascript
  56. viewer.scene.background = null; // Remove background texture
  57. viewer.scene.setBackgroundColor(null); // Remove background color
  58. ```
  59. ::: info RGBM Rendering Mode
  60. `ThreeViewer` uses `rgbm` rendering by default, which doesn't directly support transparent backgrounds.
  61. To use transparent background with `rgbm` mode, `clipBackground` function in `ViewerRenderManager.screenPass` is used.
  62. This clips/removes the background color from the final render, allowing for a transparent background effect.
  63. :::
  64. ## Clip Background
  65. In many cases, you may want to render the background color so that it can be used by plugins/post-processing effects like bloom, reflections, etc but composite the render with transparency over other content.
  66. This can be achieved by setting the `clipBackground` property of `ScreenPass` in the `ViewerRenderManager`. This renders the background color and texture normally, but clips it from the final render, allowing for a transparent background effect.
  67. ```javascript
  68. viewer.renderManager.screenPass.clipBackground = true; // Enable clipping of background
  69. ```
  70. Note that this is also automatically set when using `rgbm` rendering mode, and `null` for the background. But it will render the background first as black and clip it.
  71. So, when using `rgbm`, set the background color to an ideal color for the scene and use `clipBackground` to remove it.
  72. ## Background Intensity
  73. The intensity of the background can be controlled using the `backgroundIntensity` property of the scene. This property is a multiplier for the background color and texture.
  74. This allows you to adjust the brightness of the background without changing the color or texture itself and even achieve greater than 100% brightness backgrounds for HDR rendering
  75. ```javascript
  76. viewer.scene.backgroundIntensity = 0.5; // Set background intensity to 50%
  77. viewer.scene.backgroundIntensity = 5; // Set background intensity to 500%
  78. ```
  79. ## Pan/Scale Background
  80. To adjust the position or scale of the background texture, you can modify the `offset`, `center`, `repeat`, `rotation` properties of the texture. This allows you to pan or scale the background texture as needed.
  81. ```javascript
  82. const backgroundTexture = viewer.scene.background;
  83. if (backgroundTexture) {
  84. backgroundTexture.offset.set(0.1, 0.1); // Move texture by 10% in both x and y directions
  85. backgroundTexture.repeat.set(2, 2); // Repeat texture twice in both x and y directions
  86. backgroundTexture.rotation = Math.PI / 4; // Rotate texture by 45 degrees
  87. }
  88. ```
  89. For equirect textures, `rotation` can be set to rotate the texture around the y-axis, which can be useful for adjusting the orientation of the environment map.
  90. ```javascript
  91. if (backgroundTexture && backgroundTexture.mapping === EquirectangularReflectionMapping) {
  92. backgroundTexture.rotation = Math.PI / 2; // Rotate environment map by 90 degrees
  93. }
  94. ```
  95. [//]: # (TODO - add example how to adjust background like css background-position, background-size, etc)
  96. ## Serialization
  97. The background color and texture can be serialized to JSON along with the scene.
  98. Since the scene object is part of the viewer configuration when saving a glb with config, it is also saved and applied when loading the glb.
  99. The serialization supports colors, image urls, embedded textures with formats that require external loaders like HDR, EXR, etc.