Skip to content

Commit

Permalink
[#209] : Added texturing tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
ange-yaghi committed Apr 24, 2020
1 parent 8e3452f commit af92d6a
Show file tree
Hide file tree
Showing 13 changed files with 677 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ If the specular is set to 0 then the coating layer will only be reflective at ve
We've now explored some of the more sophisticated surface models provided by MantaRay. In the next tutorial we'll look at applying textures.

* [Previous Tutorial (Basics of Materials)](../2_basics_of_materials/2_basics_of_materials.md)
* [Next Tutorial (Textures)](../4_textures/4_textures.md)

## Useful Links

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "mantaray.mr"

// 41.48
// 34.28
material_library library()

bsdf_material cube_material(
Expand All @@ -17,7 +18,7 @@ bsdf_material floor_material(
library: library,
name: "Floor",
reflectance: srgb_i(0x30, 0x33, 0x6b),
bsdf: microfacet_brdf(
bsdf: microfacet_reflection_bsdf(
ggx_distribution(0.05)
)
)
Expand All @@ -34,7 +35,7 @@ bsdf_material cone_material(
bsdf_material sphere_material(
library: library,
name: "Sphere",
bsdf: bilayer_brdf(
bsdf: bilayer_bsdf(
coating: ggx_distribution(0.01),
diffuse: srgb_i(0xeb, 0x4d, 0x4b),
specular: 1.0
Expand Down
133 changes: 133 additions & 0 deletions docs/public/tutorials/4_textures/4_textures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# 4 Textures

In the [previous tutorial](../3_realistic_materials/2_realistic_masterials.md) we used more advanced microfacet models to add more realism our objects.

![Alt text](../3_realistic_materials/assets/render10.jpg)

While these models form a good basis for a realistic material, they often aren't enough to capture the complexity of a real surface. Thankfully, it's easy to apply textures to surfaces using *MantaRay*.

## Applying a Texture

Recall the template for a basic material:

```
bsdf_material cube_material(
library: library,
name: "Cube",
reflectance: srgb_i(0xba, 0xdc, 0x58),
bsdf: microfacet_brdf(
ggx_distribution(0.5)
)
)
```

This material would look something like this when rendered:

![Alt text](assets/render1.jpg)

To map a texture onto the cube we first need to ensure that the input file contains UV texture coordinates. This would require unwrapping the mesh in a 3D editor and enabling UV coordinates when exporting the scene.

To load a texture we would add the following to our SDL file:

```
image_file crate("crate.jpg")
```

The ```image_file``` node loads a texture from a file. It takes two input parameters:

| Parameter | Description |
|---------------|--------------------|
|`filename`|The name of the image file to load.|
|`correct_gamma`|Whether or not to correct gamma of input data. Color data should be corrected (ie. diffuse colors, etc.). Defaults to ```true```. Can be ```true``` or ```false```.|

The newly created ```crate``` node can then be used like the ```srgb_i``` node introduced earlier. To use this texture as the color of the cube material, we could use it like this:

```
bsdf_material cube_material(
library: library,
name: "Cube",
reflectance: crate,
bsdf: microfacet_brdf(
ggx_distribution(0.5)
)
)
```

This will produce the following image:

![Alt text](assets/render2.jpg)

As expected the crate image has become the diffuse color of the cube.

## RGBA Channels

MantaRay offers functionality for accessing individual color channels in an image. For instance, suppose that we wanted to render the image using only the red channel of the crate image, with the green and blue channels set to 0. This can easily be achieved with this syntax:

```
bsdf_material cube_material(
library: library,
name: "Cube",
reflectance: vector(crate.r, 0.0, 0.0),
bsdf: microfacet_brdf(
ggx_distribution(0.5)
)
)
```

As expected, only the red channel is present in the color of the cube in the rendered image:

![Alt text](assets/render3.jpg)

Further manipulations are also possible. Inverting an image is also possible:

```
bsdf_material cube_material(
library: library,
name: "Cube",
reflectance: scalar(1.0) - crate,
bsdf: microfacet_brdf(
ggx_distribution(0.5)
)
)
```

This produces the following output:

![Alt text](assets/render4.jpg)

The ```scalar(1.0)``` node in this instance would be equivalent to ```vector(1.0, 1.0, 1.0, 1.0)```.

## Exporting Images

MantaRay can also be used as a basic image editor. This can be achieved using the ```image_output``` node.

| Parameter | Description |
|---------------|--------------------|
|`map`|The image to export.|
|`filename`|The filename of the saved image.|

A simple script to load an image file, invert it and then save the output would be as follows:

```
import "mantaray.mr"
image_output(
1.0 - image_file("crate.jpg"),
"inverted_crate.jpg"
)
```

As can be seen, with minimal syntax and only intuitive mathematical operations, complex image manipulations could be done all in MantaRay's SDL. In later tutorials it will be shown how this can be applied to convenient post-processing of rendered images.


## Conclusion

We've now seen how textures can be manipulated and used to add detail to a 3D scene. In the next tutorial we'll look at basic image post-processing and compositing.

* [Previous Tutorial (Realistic Materials)](../3_realistic_materials/3_realistic_materials.md)

## Useful Links

* [MantaRay Tutorials](../all_tutorials.md)
* [Piranha Handbook](https://github.com/ange-yaghi/piranha/blob/master/docs/handbook/handbook.md)
* [VS Code Piranha Extension](https://github.com/ange-yaghi/piranha-vscode-extension)
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/public/tutorials/4_textures/crate.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions docs/public/tutorials/4_textures/image_editing.mr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import "mantaray.mr"

image_output(
1.0 - image_file("crate.jpg"),
"inverted_crate.jpg"
)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit af92d6a

Please sign in to comment.