> For the complete documentation index, see [llms.txt](https://morningheartgames.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://morningheartgames.gitbook.io/docs/asyncautoexposure/runtime-usage/api-reference.md).

# API Reference

Namespace:

```csharp
using AsyncAutoExposure;
```

## AsyncAutoExposure

The core component processes textures and owns the output `RenderTexture`.

### ProcessTexture

Processes a texture and returns the exposure-corrected output texture.

```csharp
RenderTexture ProcessTexture(Texture inputTexture);
```

Call this from `Update()` for live sources.

```csharp
display.texture = autoExposure.ProcessTexture(webcamTexture);
```

Returns `null` when `inputTexture` is `null` or the exposure material could not be created.

### ProcessTextureWithMask

Processes a texture while using a mask to decide which pixels affect brightness analysis.

```csharp
RenderTexture ProcessTextureWithMask(Texture inputTexture, Texture maskTexture);
```

White mask pixels are included. Black mask pixels are ignored. The full source image is still corrected and returned.

```csharp
display.texture = autoExposure.ProcessTextureWithMask(webcamTexture, personMask);
```

Returns `null` when the source texture, mask texture, or exposure material is missing.

### CalculateBrightness

Performs a one-off brightness calculation.

```csharp
float CalculateBrightness(Texture inputTexture);
```

This is useful for editor tools or setup logic. Avoid calling it every frame in gameplay.

### Static Brightness Helpers

```csharp
static float CalculateBrightness(Texture inputTexture, Shader shader, int downsample = 4);

static float CalculateBrightnessWithMask(
    Texture inputTexture,
    Texture maskTexture,
    Shader shader,
    int downsample = 4
);
```

These helper methods create a temporary material from the supplied shader, read pixels synchronously, then return average brightness in the `0` to `1` range.

### ResetExposure

Resets current and target exposure to `1.0`.

```csharp
void ResetExposure();
```

Use this when switching source textures, scenes, or camera feeds.

### Runtime Properties

```csharp
RenderTexture OutputTexture { get; }
float CurrentExposure { get; }
float TargetExposure { get; }
float CurrentBrightness { get; }
```

| Property            | Meaning                                                 |
| ------------------- | ------------------------------------------------------- |
| `OutputTexture`     | Last processed output render texture                    |
| `CurrentExposure`   | Exposure multiplier currently applied                   |
| `TargetExposure`    | Multiplier calculated from the latest brightness result |
| `CurrentBrightness` | Latest measured average luminance from `0` to `1`       |

### Exposure Event

`OnExposureChanged` fires when a new target exposure is calculated.

```csharp
autoExposure.OnExposureChanged += exposure =>
{
    Debug.Log($"New exposure: {exposure}");
};
```

The event passes the new target exposure multiplier.

## AutoExposureRawImage

Drag-and-drop component for Unity UI `RawImage`.

### Fields

```csharp
AsyncAutoExposure autoExposure;
bool enableAutoExposure;
bool considerAlphaChannel;
bool continuousUpdate;
float alphaThreshold;
```

### Methods

```csharp
void ForceUpdate();
void ResetToOriginal();
```

`ForceUpdate()` marks the current texture for reprocessing. `ResetToOriginal()` restores the cached source texture.

## AutoExposureImage

Drag-and-drop component for Unity UI `Image` sprites. The processed result is shown through `outputRawImage`.

### Fields

```csharp
AsyncAutoExposure autoExposure;
bool enableAutoExposure;
bool considerAlphaChannel;
bool continuousUpdate;
float alphaThreshold;
RawImage outputRawImage;
```

### Methods

```csharp
void ForceUpdate();
float GetCurrentBrightness();
```

`GetCurrentBrightness()` calculates the current sprite brightness synchronously and returns `0` when no valid sprite or shader is available.

## Common Examples

### Webcam

```csharp
WebCamTexture webcam = new WebCamTexture();
webcam.Play();

AsyncAutoExposure exposure = gameObject.AddComponent<AsyncAutoExposure>();
exposure.targetBrightness = 0.4f;

void Update()
{
    if (webcam.didUpdateThisFrame)
        display.texture = exposure.ProcessTexture(webcam);
}
```

### VideoPlayer Target Texture

```csharp
RenderTexture source = videoPlayer.targetTexture;
RenderTexture corrected = exposure.ProcessTexture(source);

videoRawImage.texture = corrected;
previewMaterial.mainTexture = corrected;
```

### Masked Foreground

```csharp
RenderTexture corrected = exposure.ProcessTextureWithMask(cameraTexture, foregroundMask);
display.texture = corrected;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://morningheartgames.gitbook.io/docs/asyncautoexposure/runtime-usage/api-reference.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.
