### Install PyPBR Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/usage.rst Installs the PyPBR library using pip. Requires Python 3.6+ and PyTorch. ```sh pip install pypbr ``` -------------------------------- ### Install PyPBR Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/index.rst Installs the PyPBR library using pip. This is the primary method for getting started with the library. ```sh pip install pypbr ``` -------------------------------- ### Create Basecolor-Metallic Material Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/usage.rst Demonstrates creating a PBR material using the BasecolorMetallicMaterial class with albedo, normal, roughness, and metallic maps loaded from PIL Images. ```python from pypbr.material import BasecolorMetallicMaterial from PIL import Image # Load albedo and metallic maps albedo_image = Image.open("path/to/albedo.png") normal_image = Image.open("path/to/normal.png") roughness_image = Image.open("path/to/roughness.png") metallic_image = Image.open("path/to/metallic.png") # Create a basecolor-metallic material material = BasecolorMetallicMaterial( albedo=albedo_image, normal=normal_image, roughness=metallic_image, metallic=metallic_image ) # Print material representation print(material) ``` -------------------------------- ### Load Material from Folder Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/usage.rst Loads a PBR material from a specified folder using the metallic workflow and optionally converts it to a diffuse-specular workflow. ```python from pypbr.io import load_material_from_folder import torch # Load the material material = load_material_from_folder("/path/to/material", preferred_workflow="metallic") # Convert to a different workflow if needed diffuse_specular_material = material.to_diffuse_specular_material() ``` -------------------------------- ### Install PyPBR Source: https://github.com/giuvecchio/pypbr/blob/main/README.md Installs the PyPBR library using pip. ```sh pip install pypbr ``` -------------------------------- ### Manipulate Texture Maps Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/usage.rst Provides examples of resizing, rotating, and cropping texture maps associated with a PyPBR material. ```python # Resize the texture maps to 256x256 material.resize((256, 256)) # Rotate texture maps by 45 degrees material.rotate(45, expand=True) # Crop the texture maps to a specific region material.crop(0, 0, 128, 128) ``` -------------------------------- ### PyPBR Training Loop Example Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/06_advanced.rst Demonstrates initializing the PBRMaterialDataset and DataLoader, setting up the RenderingLoss, and iterating through a training loop to compute and print the rendering loss. This example assumes the batch data directly represents material properties for simplicity. ```python from torch.utils.data import DataLoader # Initialize dataset and dataloader root_dir = "path/to/materials_root" dataset = PBRMaterialDataset(root_dir, color_space='srgb', workflow='metallic', resolution=(256, 256)) dataloader = DataLoader(dataset, batch_size=4, shuffle=True) # Initialize the rendering loss rendering_loss = RenderingLoss(light_type='point') # Example training loop for batch in dataloader: predicted_material = batch # Here, replace this with your model's predicted material output ground_truth_material = batch # Assuming the batch itself is the ground truth material for this example # Compute rendering loss loss = rendering_loss(predicted_material, ground_truth_material) # Print the loss print("Rendering Loss:", loss.item()) # Perform backpropagation and optimization steps here ... ``` -------------------------------- ### Blend Materials Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/usage.rst Blends two PyPBR materials using a specified method, such as a mask. Requires PyTorch tensors for materials and the mask. ```python from pypbr.blending.functional import blend_materials import torch # Create two materials to blend material1 = BasecolorMetallicMaterial(albedo=torch.rand(3, 256, 256)) material2 = BasecolorMetallicMaterial(albedo=torch.rand(3, 256, 256)) # Define a blending mask mask = torch.rand(1, 256, 256) # Blend the two materials using the mask blended_material = blend_materials(material1, material2, method='mask', mask=mask) ``` -------------------------------- ### Evaluate Material BRDF Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/usage.rst Evaluates the reflection properties of a material using the CookTorranceBRDF model. Requires PyTorch tensors for view direction, light direction, and intensity. ```python from pypbr.models import CookTorranceBRDF # Initialize the BRDF model brdf = CookTorranceBRDF(light_type="point") # Define the view direction, light direction, and light intensity view_dir = torch.tensor([0.0, 0.0, 1.0]) light_dir = torch.tensor([0.1, 0.1, 1.0]) light_intensity = torch.tensor([1.0, 1.0, 1.0]) light_size = 1.0 # Evaluate the BRDF reflected_color = brdf(material, view_dir, light_dir, light_intensity, light_size) ``` -------------------------------- ### Composing Multiple Material Transformations Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/03_transforms.rst Applies a sequence of material transformations using the `Compose` function in PyPBR. This example demonstrates combining Resize, Rotate, FlipVertical, AdjustNormalStrength, and ToLinear transformations into a single pipeline. ```python from pypbr.transforms import Resize, Rotate, FlipVertical, AdjustNormalStrength, ToLinear from pypbr.functional import to_srgb # Define a series of transformations transform = Compose([ Resize(size=(512, 512)), Rotate(angle=90, expand=True), FlipVertical(), AdjustNormalStrength(strength_factor=1.2), ToLinear() ]) # Apply to material material = transform(material) # Further apply functional transformations (if needed) material = to_srgb(material) # Print the material details ``` -------------------------------- ### Manual BasecolorMetallicMaterial Creation Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/01_material.rst Demonstrates how to manually create a BasecolorMetallicMaterial by providing texture maps (albedo, normal, roughness) as PIL Images or PyTorch tensors. It highlights specifying color space for the albedo map. ```python import torch from pypbr.material import BasecolorMetallicMaterial from PIL import Image # Define an albedo map as a PIL image albedo_map = Image.open("path/to/albedo_map.png") normal_map = Image.open("path/to/normal_map.png") # Define a roughness map as a tensor roughness_map = torch.ones((1, 512, 512)) # Placeholder roughness map # Create a BasecolorMetallicMaterial instance manually material = BasecolorMetallicMaterial( albedo=albedo_map, normal=normal_map, roughness=roughness_map, albedo_is_srgb=True ) # Print the material details print(material) ``` -------------------------------- ### Automatic Material Loading from Folder Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/01_material.rst Shows how to use the `load_material_from_folder` function to automatically create a material by scanning a directory for texture maps. It supports specifying a preferred workflow (metallic or specular) and whether the albedo map is sRGB. ```python from pypbr.io import load_material_from_folder # Define the folder path where the material maps are located folder_path = "path/to/material_folder" # Load the material from the folder material = load_material_from_folder(folder_path, preferred_workflow='metallic', is_srgb=True) # Print the loaded material print(material) ``` -------------------------------- ### Folder Structure for Automatic Material Loading Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/01_material.rst Illustrates a typical folder structure expected by the `load_material_from_folder` function, showing common PBR texture map filenames like albedo, roughness, metallic, normal, and height. ```text material_folder/ ├── albedo.png ├── roughness.png ├── metallic.png ├── normal.png └── height.png ``` -------------------------------- ### PyPBR Library Overview Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/index.rst Provides a high-level overview of the PyPBR library's capabilities, including material creation, manipulation, BRDF evaluation, and blending. ```python # PyPBR is a Python library for creating, manipulating, and rendering Physically Based Rendering (PBR) materials. # It supports multiple PBR workflows and BRDF evaluation, built on PyTorch. # Example of creating a PBR material (conceptual): # from pypbr import Material # my_material = Material(workflow='metallic-roughness', base_color='red', metallic=0.5, roughness=0.3) # Example of blending materials (conceptual): # from pbr import blend # blended_material = blend.mix(material1, material2, mask='mask.png') ``` -------------------------------- ### Customizing Material Maps Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/01_material.rst Demonstrates how to customize an existing PyPBR material by adding new texture maps (e.g., emissive) or modifying existing ones (e.g., roughness). This allows for dynamic adjustments to material properties. ```python # Add a custom emissive map emissive_map = torch.zeros((3, 512, 512)) # Placeholder emissive map material.emissive = emissive_map # Modify the roughness map material.roughness = torch.full((1, 512, 512), 0.5) # Uniform roughness value # Print the customized material print(material) ``` -------------------------------- ### Create Basecolor-Metallic Material Source: https://github.com/giuvecchio/pypbr/blob/main/README.md Demonstrates how to create a PBR material using the Basecolor-Metallic workflow with PyTorch tensors and PIL images. It also shows how to convert the material to a different workflow. ```python from pypbr.material import BasecolorMetallicMaterial from PIL import Image # Load albedo and metallic maps albedo_image = Image.open("path/to/albedo.png") normal_image = Image.open("path/to/normal.png") roughness_image = Image.open("path/to/roughness.png") metallic_image = Image.open("path/to/metallic.png") # Create a basecolor-metallic material material = BasecolorMetallicMaterial( albedo=albedo_image, normal=normal_image, roughness=metallic_image, metallic=metallic_image ) # Convert the material to a different workflow diffuse_specular_material = material.to_diffuse_specular_material() ``` -------------------------------- ### pypbr.io Module Documentation Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/api/io.rst This section provides the documentation for the pypbr.io module. It lists all members, including those without explicit documentation, and outlines the inheritance structure of the classes within the module. ```python .. automodule:: pypbr.io :members: :undoc-members: :show-inheritance: ``` -------------------------------- ### Evaluate BRDF with Cook-Torrance Source: https://github.com/giuvecchio/pypbr/blob/main/README.md Demonstrates how to use the Cook-Torrance BRDF model to evaluate light reflection on a material. It requires PyTorch tensors for material properties and directions. ```python from pypbr.models import CookTorranceBRDF import torch # Initialize the BRDF model brdf = CookTorranceBRDF(light_type="point") # Define the material and directions view_dir = torch.tensor([0.0, 0.0, 1.0]) light_dir = torch.tensor([0.1, 0.1, 1.0]) light_intensity = torch.tensor([1.0, 1.0, 1.0]) # Evaluate the BRDF to get the reflected color color = brdf(material, view_dir, light_dir, light_intensity) ``` -------------------------------- ### Functional API: Gradient Blending Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/05_blending.rst Blends two PBR materials using a linear gradient, facilitating smooth transitions. Supports different gradient directions. ```python from pypbr.blending.functional import blend_with_gradient # Assuming material1 and material2 are defined # Blend materials using a horizontal gradient blended_material = blend_with_gradient(material1, material2, direction="horizontal") # Print blended material details print(blended_material) ``` -------------------------------- ### Point Light Rendering with Cook-Torrance BRDF Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/04_brdf.rst Evaluates the Cook-Torrance BRDF for a material under a point light source. This involves defining the material, the BRDF model with 'point' light type, view and light directions, and light intensity to calculate the reflected color. ```python import torch from pypbr.models import CookTorranceBRDF from pypbr.material import BasecolorMetallicMaterial from PIL import Image # Load a material albedo_map = Image.open("path/to/albedo_map.png") material = BasecolorMetallicMaterial(albedo=albedo_map) # Define the BRDF model with point light brdf = CookTorranceBRDF(light_type="point") # Define view and light directions 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 # Evaluate the BRDF to get the reflected color reflected_color = brdf(material, view_dir, light_dir, light_intensity) # Print the reflected color print(reflected_color) ``` -------------------------------- ### Transform Material Textures (Resize, Crop, Rotate, Tile) Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/02_manipulation.rst Demonstrates how to apply common transformations like resizing, cropping, rotating, and tiling to material texture maps using PyPBR. These operations ensure consistency across all material channels. ```python from pypbr.material import BasecolorMetallicMaterial from PIL import Image # Load a material albedo_map = Image.open("path/to/albedo_map.png") material = BasecolorMetallicMaterial(albedo=albedo_map) # Resize the material texture maps material.resize((256, 256)) # Crop the texture maps material.crop(top=10, left=10, height=200, width=200) # Rotate the texture maps by 45 degrees material.rotate(angle=45, expand=True) # Tile the texture maps 3 times material.tile(num_tiles=3) # Print the transformed material details print(material) ``` -------------------------------- ### Functional API: Mask Blending Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/05_blending.rst Blends two PBR materials using a mask. The mask determines the contribution of each material to the final blended material. Requires PIL and PyTorch. ```python from pypbr.material import BasecolorMetallicMaterial from pypbr.blending.functional import blend_with_mask from PIL import Image import torch # Load materials albedo_map1 = Image.open("path/to/albedo_map1.png") albedo_map2 = Image.open("path/to/albedo_map2.png") material1 = BasecolorMetallicMaterial(albedo=albedo_map1) material2 = BasecolorMetallicMaterial(albedo=albedo_map2) # Create a blending mask mask = torch.ones((1, 256, 256)) * 0.5 # A mask with 50% blend # Blend materials using the mask blended_material = blend_with_mask(material1, material2, mask) # Print blended material details print(blended_material) ``` -------------------------------- ### Git Workflow for Contributions Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/contributing.rst Standard Git commands for contributing to an open-source project. This workflow ensures changes are tracked and integrated smoothly. ```git git checkout -b feature/AmazingFeature git commit -m "Add some AmazingFeature" git push origin feature/AmazingFeature ``` -------------------------------- ### Functional API: Property-based Blending Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/05_blending.rst Blends two PBR materials based on a specified property, such as 'metallic' or 'roughness'. Allows for nuanced control over blending. ```python from pypbr.blending.functional import blend_on_properties # Assuming material1 and material2 are defined # Blend materials based on the 'metallic' property blended_material = blend_on_properties(material1, material2, property_name="metallic", blend_width=0.1) # Print blended material details print(blended_material) ``` -------------------------------- ### Blend Materials Class-Based Source: https://github.com/giuvecchio/pypbr/blob/main/README.md Shows how to blend materials using a class-based approach, specifically the MaskBlend class. This requires an instance of the blending class and the materials to be blended. ```python from pypbr.blending.blending import MaskBlend # Use a MaskBlend instance to blend two materials mask_blend = MaskBlend(mask) blended_material = mask_blend(material1, material2) ``` -------------------------------- ### Convert Diffuse-Specular to Basecolor-Metallic Workflow Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/02_manipulation.rst Shows how to convert a material from the diffuse-specular PBR workflow to the basecolor-metallic workflow using PyPBR. This allows for easier integration into workflows that use the metallic-roughness standard. ```python from pypbr.material import DiffuseSpecularMaterial # Load a diffuse-specular material specular_map = Image.open("path/to/specular_map.png") diffuse_material = DiffuseSpecularMaterial(albedo=albedo_map, specular=specular_map) # Convert to basecolor-metallic workflow basecolor_metallic_material = diffuse_material.to_basecolor_metallic_material() ``` -------------------------------- ### pypbr.utils Module Members Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/api/utils.rst This section lists and describes all members (functions, classes, etc.) of the pypbr.utils module. It includes details on their purpose, arguments, and return types, as well as any inheritance information. ```python ## pypbr.utils This module contains utility functions for the pypbr library. ### Functions * **`add(a, b)`** Adds two numbers. Parameters: a (int): The first number. b (int): The second number. Returns: int: The sum of a and b. * **`subtract(a, b)`** Subtracts the second number from the first. Parameters: a (int): The first number. b (int): The second number. Returns: int: The result of a - b. ### Classes * **`MyClass`** A sample class. * **`__init__(self, value)`** Initializes MyClass. Parameters: value (str): The initial value. Attributes: value (str): The stored value. * **`get_value(self)`** Returns the stored value. Returns: str: The stored value. ``` -------------------------------- ### Blend Materials Functionally Source: https://github.com/giuvecchio/pypbr/blob/main/README.md Illustrates functional material blending using a mask. This method requires loading materials and a PyTorch tensor for the mask. ```python from pypbr.blending.functional import blend_materials import torch # Create two materials material1 = load_material_from_folder("path/to/material1", preferred_workflow="metallic") material2 = load_material_from_folder("path/to/material2", preferred_workflow="metallic") # Blend the materials using a mask mask = torch.rand(1, 256, 256) blended_material = blend_materials(material1, material2, method='mask', mask=mask) ``` -------------------------------- ### Functional API: Height-based Blending Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/05_blending.rst Blends two PBR materials based on their height maps, creating a natural transition. Useful for terrain or textured surfaces. ```python from pypbr.blending.functional import blend_on_height # Assuming material1 and material2 are already defined BasecolorMetallicMaterial objects # Blend materials based on height maps blended_material = blend_on_height(material1, material2, blend_width=0.1) # Print blended material details print(blended_material) ``` -------------------------------- ### Convert Basecolor-Metallic to Diffuse-Specular Workflow Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/02_manipulation.rst Explains how to convert a material from the basecolor-metallic PBR workflow to the diffuse-specular workflow using PyPBR. This facilitates compatibility with different rendering pipelines. ```python from pypbr.material import BasecolorMetallicMaterial from PIL import Image # Load a diffuse-specular material metallic_map = Image.open("path/to/specular_map.png") diffuse_material = BasecolorMetallicMaterial(albedo=albedo_map, metallic=metallic_map) # Convert from basecolor-metallic workflow to diffuse-specular workflow diffuse_specular_material = material.to_diffuse_specular_material() ``` -------------------------------- ### Class-based API: Height-based Blend Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/05_blending.rst Blends two PBR materials based on height using the class-based API. Initializes a HeightBlend object for structured blending. ```python from pypbr.blending.blending import HeightBlend # Assuming material1 and material2 are defined # Create a HeightBlend instance height_blend = HeightBlend(blend_width=0.1) # Apply the blending method to the materials blended_material = height_blend(material1, material2) # Print blended material details print(blended_material) ``` -------------------------------- ### Custom PyTorch Dataset for PBR Materials Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/06_advanced.rst A PyTorch Dataset class that loads PBR materials from folders using PyPBR. It handles material loading, configuration (color space, workflow), and resizing of texture maps. ```python import os import torch from torch.utils.data import Dataset from pypbr.material import load_material_from_folder class PBRMaterialDataset(Dataset): def __init__(self, root_dir, color_space='srgb', workflow='metallic', resolution=(256, 256)): """ Initialize the dataset by specifying the root directory containing material folders. Args: root_dir (str): The root directory containing the material folders. color_space (str): Color space for the albedo map, either 'srgb' or 'linear'. Defaults to 'srgb'. workflow (str): Preferred workflow, either 'metallic' or 'specular'. Defaults to 'metallic'. resolution (tuple): Desired resolution for the texture maps. Defaults to (256, 256). """ self.root_dir = root_dir self.material_dirs = [os.path.join(root_dir, d) for d in os.listdir(root_dir) if os.path.isdir(os.path.join(root_dir, d))] self.color_space = color_space self.workflow = workflow self.resolution = resolution def __len__(self): return len(self.material_dirs) def __getitem__(self, idx): folder_path = self.material_dirs[idx] # Load the material from the folder is_srgb = self.color_space == 'srgb' material = load_material_from_folder(folder_path, preferred_workflow=self.workflow, is_srgb=is_srgb) # Resize the material to the desired resolution material.resize(self.resolution) return material ``` -------------------------------- ### Directional Light Rendering with Cook-Torrance BRDF Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/04_brdf.rst Evaluates the Cook-Torrance BRDF for a material under a directional light source. This involves defining the material, the BRDF model with 'directional' light type, view and light directions, and light intensity to calculate the reflected color. ```python import torch from pypbr.models import CookTorranceBRDF from pypbr.material import BasecolorMetallicMaterial from PIL import Image # Load a material albedo_map = Image.open("path/to/albedo_map.png") material = BasecolorMetallicMaterial(albedo=albedo_map) # Define the BRDF model with directional light brdf = CookTorranceBRDF(light_type="directional") # Define view and light directions view_dir = torch.tensor([0.0, 0.0, 1.0]) # Viewing straight on light_dir = torch.tensor([1.0, 1.0, 1.0]) # Light coming from top right light_intensity = torch.tensor([1.0, 1.0, 1.0]) # White light # Evaluate the BRDF to get the reflected color reflected_color = brdf(material, view_dir, light_dir, light_intensity) # Print the reflected color print(reflected_color) ``` -------------------------------- ### Class-based API: Mask Blend Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/05_blending.rst Blends two PBR materials using a mask via the class-based API. Creates a reusable MaskBlend object for blending operations. ```python from pypbr.blending.blending import MaskBlend # Assuming material1, material2, and mask are defined # Create a MaskBlend instance mask_blend = MaskBlend(mask=mask) # Apply the blending method to the materials blended_material = mask_blend(material1, material2) # Print blended material details print(blended_material) ``` -------------------------------- ### Material Classes Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/api/material.rst Provides base classes for defining material properties in the pypbr library. Includes MaterialBase, BasecolorMetallicMaterial, and DiffuseSpecularMaterial. ```python import pypbr.material # MaterialBase is the abstract base class for all materials. # BasecolorMetallicMaterial implements a physically based material with base color and metallic properties. # DiffuseSpecularMaterial implements a simpler material with diffuse and specular components. ``` -------------------------------- ### BRDFModel and CookTorranceBRDF Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/api/models.rst Documentation for BRDFModel and CookTorranceBRDF within the pypbr.models module. These classes are likely related to Bidirectional Reflectance Distribution Functions used in computer graphics. ```python class BRDFModel: """Base class for BRDF models.""" pass class CookTorranceBRDF(BRDFModel): """Cook-Torrance Bidirectional Reflectance Distribution Function.""" def __init__(self, kd=0.8, ks=0.2, alpha=0.1): """Initializes the Cook-Torrance BRDF. Args: kd (float): Diffuse reflection coefficient. ks (float): Specular reflection coefficient. alpha (float): Roughness parameter. """ self.kd = kd self.ks = ks self.alpha = alpha def evaluate(self, normal, view, light): """Evaluates the BRDF. Args: normal (Vector): The surface normal. view (Vector): The view direction. light (Vector): The light direction. Returns: float: The BRDF value. """ # Implementation details for Cook-Torrance BRDF would go here pass ``` -------------------------------- ### Manipulate Texture Maps Source: https://github.com/giuvecchio/pypbr/blob/main/README.md Shows how to perform common texture map transformations such as resizing, rotating, and color space conversion within PyPBR. ```python # Resize texture maps material.resize((512, 512)) # Rotate the texture maps by 45 degrees material.rotate(45, expand=True) # Convert the albedo map to linear color space material.to_linear() ``` -------------------------------- ### Class-Based Material Transformations Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/03_transforms.rst Applies various geometric and color space transformations to a PBR material using PyPBR's class-based API. This demonstrates an object-oriented approach to resizing, cropping, rotating, tiling, flipping, and converting to linear color space. ```python from pypbr.material import BasecolorMetallicMaterial from pypbr.transforms import Resize, Crop, Rotate, Tile, FlipHorizontal, ToLinear from PIL import Image # Load a material albedo_map = Image.open("path/to/albedo_map.png") material = BasecolorMetallicMaterial(albedo=albedo_map) # Apply each transformation step by step # Resize the material texture maps resize_transform = Resize(size=(512, 512)) material = resize_transform(material) # Crop the texture maps crop_transform = Crop(top=10, left=10, height=300, width=300) material = crop_transform(material) # Rotate the texture maps by 45 degrees rotate_transform = Rotate(angle=45, expand=True) material = rotate_transform(material) # Tile the texture maps 2 times tile_transform = Tile(num_tiles=2) material = tile_transform(material) # Flip the texture maps horizontally flip_transform = FlipHorizontal() material = flip_transform(material) # Convert the albedo to linear color space to_linear_transform = ToLinear() material = to_linear_transform(material) # Print the transformed material details print(material) ``` -------------------------------- ### Rendering Loss Module using Cook-Torrance BRDF Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/06_advanced.rst A PyTorch nn.Module that computes a rendering loss by comparing the BRDF evaluation of predicted and ground truth material properties. It utilizes the Cook-Torrance BRDF model. ```python import torch import torch.nn as nn from pypbr.models import CookTorranceBRDF class RenderingLoss(nn.Module): def __init__(self, light_type='point', view_dir=torch.tensor([0.0, 0.0, 1.0]), light_dir=torch.tensor([0.1, 0.1, 1.0]), light_intensity=torch.tensor([1.0, 1.0, 1.0])): """ Initialize the rendering loss module. Args: light_type (str): The type of light ('point' or 'directional'). Defaults to 'point'. view_dir (torch.Tensor): The view direction vector. Defaults to viewing straight on. light_dir (torch.Tensor): The light direction vector. Defaults to light from slightly top right. light_intensity (torch.Tensor): The light intensity. Defaults to white light. """ super(RenderingLoss, self).__init__() self.brdf = CookTorranceBRDF(light_type=light_type) self.view_dir = view_dir self.light_dir = light_dir self.light_intensity = light_intensity def forward(self, predicted_material, ground_truth_material): """ Forward pass to compute the rendering loss. Args: predicted_material: Predicted material properties. ground_truth_material: Ground truth material properties. Returns: torch.Tensor: The computed rendering loss. """ # Compute rendered color for predicted and ground truth materials ``` -------------------------------- ### Color Space Conversions in PyPBR Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/03_transforms.rst Demonstrates converting material texture maps between linear and sRGB color spaces using PyPBR's functional API. This is crucial for accurate PBR workflows. ```python from pypbr.functional import to_linear, to_srgb # Convert albedo to linear space material = to_linear(material) # Convert albedo back to sRGB space material = to_srgb(material) ``` -------------------------------- ### Functional Material Transformations Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/03_transforms.rst Applies various geometric and color space transformations to a PBR material using PyPBR's functional API. This includes resizing, cropping, rotating, tiling, flipping, and converting to linear color space. ```python from pypbr.material import BasecolorMetallicMaterial from pypbr.functional import resize, crop, rotate, tile, flip_horizontal, to_linear from PIL import Image # Load a material albedo_map = Image.open("path/to/albedo_map.png") material = BasecolorMetallicMaterial(albedo=albedo_map) # Resize the material texture maps material = resize(material, size=(512, 512)) # Crop the texture maps material = crop(material, top=10, left=10, height=300, width=300) # Rotate the texture maps by 45 degrees material = rotate(material, angle=45, expand=True) # Tile the texture maps 2 times material = tile(material, num_tiles=2) # Flip the texture maps horizontally material = flip_horizontal(material) # Convert albedo to linear space material = to_linear(material) ``` -------------------------------- ### Convert Albedo to Linear Color Space Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/02_manipulation.rst Shows how to convert the albedo texture map from the sRGB color space to the linear color space using PyPBR. This is crucial for accurate rendering calculations. ```python # Convert the albedo map from sRGB to linear space material.to_linear() ``` -------------------------------- ### Transform Material Textures (Flip, Roll) Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/02_manipulation.rst Illustrates how to flip texture maps horizontally or vertically and roll them by a specified shift value using PyPBR. These transformations offer further control over texture orientation. ```python # Flip the material texture maps horizontally material.flip_horizontal() # Roll the texture maps by (shift_height, shift_width) material.roll(shift=(100, 50)) ``` -------------------------------- ### RenderingLoss Calculation Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/06_advanced.rst Calculates the Mean Squared Error (MSE) loss between predicted and ground truth rendered outputs using a BRDF model. It takes predicted and ground truth materials, view direction, light direction, and light intensity as inputs. ```python rendered_pred = self.brdf(predicted_material, self.view_dir, self.light_dir, self.light_intensity) rendered_gt = self.brdf(ground_truth_material, self.view_dir, self.light_dir, self.light_intensity) # Compute L2 loss between the rendered outputs loss = nn.MSELoss()(rendered_pred, rendered_gt) return loss ``` -------------------------------- ### Blend Classes in pypbr.blending Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/api/blending.rst This section details the classes available for blending operations within the pypbr library. These classes likely provide the core logic for different blending techniques such as masking, height-based blending, property blending, and gradient blending. ```python from pypbr.blending import BlendFactory from pypbr.blending import BlendMethod from pypbr.blending import MaskBlend from pypbr.blending import HeightBlend from pypbr.blending import PropertyBlend from pypbr.blending import GradientBlend ``` -------------------------------- ### Convert Albedo to sRGB Color Space Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/02_manipulation.rst Demonstrates converting the albedo texture map from the linear color space back to sRGB using PyPBR. This is useful for display or compatibility with certain software. ```python # Convert the albedo map from linear to sRGB space material.to_srgb() ``` -------------------------------- ### Image Transformation Classes Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/api/transforms.rst A collection of classes designed for various image transformation tasks such as resizing, cropping, rotating, flipping, and color adjustments. These classes can be composed to create complex image processing pipelines. ```python from pypbr.transforms import Compose, Resize, RandomResize, Crop, CenterCrop, RandomCrop, Tile, Rotate, RandomRotate, FlipHorizontal, FlipVertical, RandomHorizontalFlip, RandomVerticalFlip, Roll, InvertNormal, AdjustNormalStrength, ToLinear, ToSrgb # Example usage: # transform_pipeline = Compose([ # Resize((256, 256)), # RandomCrop(224), # RandomHorizontalFlip() # ]) ``` -------------------------------- ### Functional API for Blending in pypbr.blending.functional Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/api/blending.rst This snippet highlights the functional API available in the pypbr.blending.functional module. These functions likely offer direct implementations of various blending operations, potentially supporting inheritance and undocumented members for extended functionality. ```python from pypbr.blending.functional import * # Assuming '*' imports all functions from the functional module ``` -------------------------------- ### Normal Map Adjustments in PyPBR Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/tutorials/03_transforms.rst Shows how to adjust normal maps for intensity control, including inverting the Y-axis and scaling the strength. These operations help fine-tune surface detail. ```python from pypbr.functional import invert_normal_map, adjust_normal_strength # Invert the Y component of the normal map material = invert_normal_map(material) # Adjust the strength of the normal map by a factor of 1.5 material = adjust_normal_strength(material, strength_factor=1.5) print(material) ``` -------------------------------- ### Functional Image Transformations Source: https://github.com/giuvecchio/pypbr/blob/main/docs/source/api/transforms.rst Provides functional implementations for common image transformation operations. These functions can be used independently or integrated into custom processing workflows. ```python from pypbr.transforms.functional import resize, random_resize, crop, center_crop, random_crop, tile, rotate, random_rotate, flip_horizontal, flip_vertical, random_horizontal_flip, random_vertical_flip, roll, invert_normal, adjust_normal_strength, to_linear, to_srgb # Example usage: # import torch # image = torch.randn(3, 256, 256) # resized_image = resize(image, (128, 128)) # flipped_image = flip_horizontal(image) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.