pypbr.models.CookTorranceBRDF

class pypbr.models.CookTorranceBRDF(light_type: str = 'point', override_device: device = None)[source]

Bases: BRDFModel

Implements the Cook-Torrance BRDF model. Supports both directional and point light sources.

Example

brdf = CookTorranceBRDF(light_type="point")

# Define the view direction, light direction, and light intensity
view_dir = torch.tensor([0.0, 0.0, 1.0])  # Viewing straight on
light_dir = torch.tensor([0.1, 0.1, 1.0])  # Light coming from slightly top right
light_intensity = torch.tensor([1.0, 1.0, 1.0])  # White light
light_size = 1.0

# Evaluate the BRDF to get the reflected color
color = brdf(material, view_dir, light_dir, light_intensity, light_size)
__init__(light_type: str = 'point', override_device: device = None)[source]

Initialize the Cook-Torrance BRDF.

Parameters:

light_type (str) – Type of light source (‘directional’ or ‘point’).

Methods

__init__([light_type, override_device])

Initialize the Cook-Torrance BRDF.

add_module(name, module)

Add a child module to the current module.

apply(fn)

Apply fn recursively to every submodule (as returned by .children()) as well as self.

bfloat16()

Casts all floating point parameters and buffers to bfloat16 datatype.

buffers([recurse])

Return an iterator over module buffers.

children()

Return an iterator over immediate children modules.

compile(*args, **kwargs)

Compile this Module's forward using torch.compile().

cpu()

Move all model parameters and buffers to the CPU.

cuda([device])

Move all model parameters and buffers to the GPU.

double()

Casts all floating point parameters and buffers to double datatype.

eval()

Set the module in evaluation mode.

extra_repr()

Set the extra representation of the module.

float()

Casts all floating point parameters and buffers to float datatype.

forward(material, view_dir, ...[, ...])

Evaluate the Cook-Torrance BRDF for the given directions.

fresnel_schlick(cos_theta, F0)

Compute the Fresnel term using Schlick's approximation.

geometry_schlick_ggx(NdotX, roughness)

Compute the geometry function for a single direction using Schlick-GGX.

geometry_smith(normal, view_dir_map, ...)

Compute the geometry function using Smith's method.

get_buffer(target)

Return the buffer given by target if it exists, otherwise throw an error.

get_extra_state()

Return any extra state to include in the module's state_dict.

get_parameter(target)

Return the parameter given by target if it exists, otherwise throw an error.

get_submodule(target)

Return the submodule given by target if it exists, otherwise throw an error.

half()

Casts all floating point parameters and buffers to half datatype.

ipu([device])

Move all model parameters and buffers to the IPU.

load_state_dict(state_dict[, strict, assign])

Copy parameters and buffers from state_dict into this module and its descendants.

modules()

Return an iterator over all modules in the network.

named_buffers([prefix, recurse, ...])

Return an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.

named_children()

Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.

named_modules([memo, prefix, remove_duplicate])

Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself.

named_parameters([prefix, recurse, ...])

Return an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.

normal_distribution_ggx(normal, half_vector, ...)

Compute the Normal Distribution Function using the GGX (Trowbridge-Reitz) model.

parameters([recurse])

Return an iterator over module parameters.

register_backward_hook(hook)

Register a backward hook on the module.

register_buffer(name, tensor[, persistent])

Add a buffer to the module.

register_forward_hook(hook, *[, prepend, ...])

Register a forward hook on the module.

register_forward_pre_hook(hook, *[, ...])

Register a forward pre-hook on the module.

register_full_backward_hook(hook[, prepend])

Register a backward hook on the module.

register_full_backward_pre_hook(hook[, prepend])

Register a backward pre-hook on the module.

register_load_state_dict_post_hook(hook)

Register a post hook to be run after module's load_state_dict is called.

register_module(name, module)

Alias for add_module().

register_parameter(name, param)

Add a parameter to the module.

register_state_dict_pre_hook(hook)

Register a pre-hook for the state_dict() method.

requires_grad_([requires_grad])

Change if autograd should record operations on parameters in this module.

set_extra_state(state)

Set extra state contained in the loaded state_dict.

share_memory()

See torch.Tensor.share_memory_().

state_dict(*args[, destination, prefix, ...])

Return a dictionary containing references to the whole state of the module.

to(*args, **kwargs)

Move and/or cast the parameters and buffers.

to_empty(*, device[, recurse])

Move the parameters and buffers to the specified device without copying storage.

train([mode])

Set the module in training mode.

type(dst_type)

Casts all parameters and buffers to dst_type.

xpu([device])

Move all model parameters and buffers to the XPU.

zero_grad([set_to_none])

Reset gradients of all model parameters.

Attributes

T_destination

call_super_init

dump_patches

training

forward(material: MaterialBase, view_dir: Tensor, light_dir_or_position: Tensor, light_intensity: Tensor, light_size: float | None = None, return_srgb: bool = True) Tensor[source]

Evaluate the Cook-Torrance BRDF for the given directions.

Parameters:
  • material (MaterialBase) – Material properties (can be BasecolorMetallicMaterial or DiffuseSpecularMaterial).

  • view_dir (Tensor) – View direction vector, shape (3,).

  • light_dir_or_position (Tensor) – Light direction vector (for directional light, shape (3,)) or light position vector (for point light, shape (3,)).

  • light_intensity (Tensor) – Light intensity, shape (3,).

  • light_size (float) – Size of the light source (for point light only).

  • return_srgb (bool) – Whether to return the color in sRGB space.

Returns:

The reflected color at each point, shape (3, H, W).

Return type:

Tensor

fresnel_schlick(cos_theta: Tensor, F0: Tensor) Tensor[source]

Compute the Fresnel term using Schlick’s approximation.

Parameters:
  • cos_theta (Tensor) – Cosine of the angle between view and half-vector, shape (1, H, W).

  • F0 (Tensor) – Base reflectivity at normal incidence, shape (3, H, W).

Returns:

Fresnel term, shape (3, H, W).

Return type:

Tensor

geometry_schlick_ggx(NdotX: Tensor, roughness: Tensor) Tensor[source]

Compute the geometry function for a single direction using Schlick-GGX.

Parameters:
  • NdotX (Tensor) – Cosine of angle between normal and direction, shape (1, H, W).

  • roughness (Tensor) – Surface roughness, shape (1, H, W).

Returns:

Geometry term for one direction, shape (1, H, W).

Return type:

Tensor

geometry_smith(normal: Tensor, view_dir_map: Tensor, light_dir_map: Tensor, roughness: Tensor) Tensor[source]

Compute the geometry function using Smith’s method.

Parameters:
  • normal (Tensor) – Surface normals (N), shape (3, H, W).

  • view_dir_map (Tensor) – View directions (V), shape (3, H, W).

  • light_dir_map (Tensor) – Light directions (L), shape (3, H, W).

  • roughness (Tensor) – Surface roughness, shape (1, H, W).

Returns:

Geometry term, shape (1, H, W).

Return type:

Tensor

normal_distribution_ggx(normal: Tensor, half_vector: Tensor, roughness: Tensor) Tensor[source]

Compute the Normal Distribution Function using the GGX (Trowbridge-Reitz) model.

The GGX NDF is used to model the distribution of microfacets on a surface.

Parameters:
  • normal (Tensor) – Surface normals (N), shape (3, H, W).

  • half_vector (Tensor) – Half vectors (H), shape (3, H, W).

  • roughness (Tensor) – Surface roughness, shape (1, H, W).

Returns:

NDF term, shape (1, H, W).

Return type:

Tensor

References

Walter, B., Marschner, S.R., Li, H., and Kautz, J. (2007). Microfacet Models for Refraction through Rough Surfaces. Journal of Computer Graphics Techniques (JCGT).