# Example of usage

Here is an example of how to use stable-diffusion-for-unity on the specific subject of 2D game background images.&#x20;

## Summary

* Simple level design Modeling of Game System
* Image generation by Depth2Img&#x20;
* Background image creation by Text2Img if necessary
* Image editing + adjustment of generated images by Img2Img

## Preparation

We will generate image by Depth2Img using Depth obtained from the camera information in Unity.

So we need to set the camera and the Canvas for this.

* Resolution for Game View\
  At this time, we assume a side-scrolling game so set 1920x540 to generate a large width.<br>

  <div><figure><img src="/files/4BMYtPyJJTF8s67Ct1IS" alt=""><figcaption></figcaption></figure> <figure><img src="/files/YjLTY64IEonOQ1FDTp6C" alt=""><figcaption></figcaption></figure></div>
* Camera settings
  * Set camera for depth. \
    Remove the UI check on Rendering > Culling Musk so that the RawImage does not affect the Depth.

    <div align="left"><figure><img src="/files/jax6tfA9ARpaAbKonZbQ" alt=""><figcaption></figcaption></figure></div>
  * Change the projection to Orthographic

    <div align="left"><figure><img src="/files/EpBnJ2yGSYsIHhIZW7qa" alt=""><figcaption></figcaption></figure></div>
  * Adjust ClippingPlanes Near/Far\
    Set the difference between Near<->Far to a smaller value. This will make it easier to reflect the Depth.

    <div align="left"><figure><img src="/files/y1DyYaqPFK3UyoKigWnB" alt=""><figcaption></figcaption></figure></div>
* Create Canvas and RawImage\
  ![](/files/oRlxFxtOPGf5opJIg3Xw)
* Chnage the Canvas's RenderMode to WorldSpace

  <div align="left"><figure><img src="/files/DK47doGBginJRgJfdswY" alt=""><figcaption></figcaption></figure></div>
* Adjust the size of the Canvas and RawImage to fit the camera

  <figure><img src="/files/FofSVpQnONWtwNPvUgc7" alt=""><figcaption></figcaption></figure>
* Attach Text2ImageWithControlNet.cs to RawImage and set the size for 1920x540<br>

  <div align="left"><figure><img src="/files/fdrgNWZzhlrlGuZqRZWs" alt=""><figcaption></figcaption></figure></div>

## Simple level design Modeling of Game System

Create the base shape of the stage for the game. At this time, we are using ProBuilder for modeling.

<figure><img src="/files/jpGHCLT5ZvjKfgMiHb2f" alt=""><figcaption></figcaption></figure>

## Image Generation with Depth2Img

Note: ControlNet requires a large amount of VRAM. If you cannot generate images due to lack of VRAM, you may need to reduce the resolution.

Generate images according to the shape of the created 3D model.&#x20;

Set the parameters in Text2ImageWithControlNet.cs and generate the image from Generate.<br>

<div align="left"><figure><img src="/files/JiTm398Kvrqv2MIeuXgY" alt=""><figcaption></figcaption></figure></div>

By selecting Depth/Normal in ControlType, it is possible to use the information from the camera position in Unity to generate an image that conforms to the 3D model shape in Unity.<br>

| Input                            | Depth                            | Normal                           |
| -------------------------------- | -------------------------------- | -------------------------------- |
| ![](/files/EUI8KVC30RLBJCeDohOq) | ![](/files/fNs9W9zOQdfcT7QkdlyC) | ![](/files/tbmMuPu1K4vYllSBN1vH) |

Depth and Normal each generate images in accordance with the shape of the 3D model. Normal generates straight lines correctly, but parallel steps are less likely to be represented.

If you cannot generate images as expected, please adjust parameters such as CfgScale, Weight, etc., or set Model and LoRA. Click here to see how to set each parameter.

Please see this document for how to adjust these parameters

{% content-ref url="/pages/iqpHxo6k2YSMF2I5vvmX" %}
[Text to Image With ControlNet](/english/components/text-to-image-with-controlnet.md)
{% endcontent-ref %}

The recommended use is to apply LoRA that has learned the pattern to make it consistent across multiple stages.\
Note: The use of unauthorized images of others for LoRA and other model learning is an area where regulations have not yet caught up. Wictchpot Inc will not have responsibility for such actions. Please be respectful to other creators and use images with care so as not to infringe on others' copyrights.

<figure><img src="/files/WEmSRaTP2SctJnzCM5ta" alt=""><figcaption><p>Generated  Image by Depth2Img</p></figcaption></figure>

Once a somewhat good image is generated, it can be adjusted by combining it with other tools like photoshop or Image to Image.

## Final image adjustment

Make minor adjustments with combined with Img2Img or other image editing tool like photoshop.

Generated image is in StreamingAssets, so roughly modify this image in Photoshop.

<figure><img src="/files/OfVQL0bXbIUk8G2RBNNP" alt=""><figcaption></figcaption></figure>

Adjust this image using Image2Image or Inpaint. Since stable-diffusion-for-unity runs StableDiffusionWebUI, you can use the WebUI functions from the URL listed in the command prompt.

<figure><img src="/files/KgJyqf5XobygWTslz3k0" alt=""><figcaption></figcaption></figure>

In this case, we used Image2Image and Inpaint to finalize the image.

<figure><img src="/files/4LkY2RoOURvoKHsYFZgE" alt=""><figcaption></figcaption></figure>

After finalize

Input image is this

<figure><img src="/files/pn0LwQZtMzCfurETZBF2" alt=""><figcaption></figcaption></figure>

Final image is this

<figure><img src="/files/wK8WzGP7mez7MaKCXayV" alt=""><figcaption></figcaption></figure>

This image can be used as a background image for the game stage by placing characters and other objects on the original stage base model and setting SortingLayer and Order in layer appropriately.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.witchpot.com/english/production-tips/example-of-usage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
