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

scene-background.md 6.4KB

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