示例#1
0
 def specular_reflectance(self, value):
     self._specular_reflectance = value
     if value is not None:
         self.compute_specular_lighting = True
     else:
         self._specular_reflectance = pyredner.Texture(\
             tf.zeros([3], dtype=tf.float32))
         self.compute_specular_lighting = False
示例#2
0
    def __init__(
            self,
            diffuse_reflectance: Optional[Union[tf.Tensor,
                                                pyredner.Texture]] = None,
            specular_reflectance: Optional[Union[tf.Tensor,
                                                 pyredner.Texture]] = None,
            roughness: Optional[Union[tf.Tensor, pyredner.Texture]] = None,
            generic_texture: Optional[Union[tf.Tensor,
                                            pyredner.Texture]] = None,
            normal_map: Optional[Union[tf.Tensor, pyredner.Texture]] = None,
            two_sided: bool = False,
            use_vertex_color: bool = False):
        if diffuse_reflectance is None:
            diffuse_reflectance = pyredner.Texture(
                tf.zeros([3], dtype=tf.float32))
        if specular_reflectance is None:
            specular_reflectance = pyredner.Texture(
                tf.zeros([3], dtype=tf.float32))
            compute_specular_lighting = False
        else:
            compute_specular_lighting = True
        if roughness is None:
            roughness = pyredner.Texture(tf.ones([1], dtype=tf.float32))

        # Convert to constant texture if necessary
        if tf.is_tensor(diffuse_reflectance):
            diffuse_reflectance = pyredner.Texture(diffuse_reflectance)
        if tf.is_tensor(specular_reflectance):
            specular_reflectance = pyredner.Texture(specular_reflectance)
        if tf.is_tensor(roughness):
            roughness = pyredner.Texture(roughness)
        if generic_texture is not None and tf.is_tensor(generic_texture):
            generic_texture = pyredner.Texture(generic_texture)
        if normal_map is not None and tf.is_tensor(normal_map):
            normal_map = pyredner.Texture(normal_map)

        assert((len(diffuse_reflectance.texels.shape) == 1 and diffuse_reflectance.texels.shape[0] == 3) or \
               (len(diffuse_reflectance.texels.shape) == 3 and diffuse_reflectance.texels.shape[2] == 3))
        assert((len(specular_reflectance.texels.shape) == 1 and specular_reflectance.texels.shape[0] == 3) or \
               (len(specular_reflectance.texels.shape) == 3 and specular_reflectance.texels.shape[2] == 3))
        assert((len(roughness.texels.shape) == 1 and roughness.texels.shape[0] == 1) or \
               (len(roughness.texels.shape) == 3 and roughness.texels.shape[2] == 1))
        if normal_map is not None:
            assert((len(normal_map.texels.shape) == 1 and normal_map.texels.shape[0] == 1) or \
                   (len(normal_map.texels.shape) == 3 and normal_map.texels.shape[2] == 1))

        self.diffuse_reflectance = diffuse_reflectance
        self._specular_reflectance = specular_reflectance
        self.compute_specular_lighting = compute_specular_lighting
        self.roughness = roughness
        self.generic_texture = generic_texture
        self.normal_map = normal_map
        self.two_sided = two_sided
        self.use_vertex_color = use_vertex_color
示例#3
0
    def __init__(self,
                 values: tf.Tensor,
                 env_to_world: tf.Tensor = tf.eye(4, 4)):
        # Convert to constant texture if necessary
        if tf.is_tensor(values):
            values = pyredner.Texture(values)

        assert (values.texels.dtype == tf.float32)

        self.values = values
        self.env_to_world = env_to_world
示例#4
0
    def __init__(self,
                 diffuse_reflectance,
                 specular_reflectance=None,
                 roughness=None,
                 normal_map=None,
                 two_sided=False):
        assert (tf.executing_eagerly())
        if specular_reflectance is None:
            specular_reflectance = pyredner.Texture(
                tf.zeros([3], dtype=tf.float32))
        if roughness is None:
            roughness = pyredner.Texture(tf.ones([1], dtype=tf.float32))
        # Convert to constant texture if necessary
        if tf.is_tensor(diffuse_reflectance):
            diffuse_reflectance = pyredner.Texture(diffuse_reflectance)
        if tf.is_tensor(specular_reflectance):
            specular_reflectance = pyredner.Texture(specular_reflectance)
        if tf.is_tensor(roughness):
            roughness = pyredner.Texture(roughness)
        if normal_map is not None and tf.is_tensor(normal_map):
            normal_map = pyredner.Texture(normal_map)

        self.diffuse_reflectance = diffuse_reflectance
        self.specular_reflectance = specular_reflectance
        self.roughness = roughness
        self.normal_map = normal_map
        self.two_sided = two_sided
示例#5
0
    def __init__(self, values, env_to_world=tf.eye(4, 4)):
        assert (tf.executing_eagerly())
        # Convert to constant texture if necessary
        if tf.is_tensor(values):
            values = pyredner.Texture(values)

        # assert(values.texels.is_contiguous())
        assert (values.texels.dtype == tf.float32)

        with tf.device(pyredner.get_device_name()):
            # Build sampling table
            luminance = 0.212671 * values.texels[:, :, 0] + \
                        0.715160 * values.texels[:, :, 1] + \
                        0.072169 * values.texels[:, :, 2]
            # For each y, compute CDF over x
            sample_cdf_xs_ = tf.cumsum(luminance, axis=1)

            y_weight = tf.sin(
                math.pi *
                (tf.cast(tf.range(luminance.shape[0].value), tf.float32) + 0.5)
                / float(luminance.shape[0].value))

            # Compute CDF for x
            sample_cdf_ys_ = tf.cumsum(sample_cdf_xs_[:, -1] * y_weight,
                                       axis=0)
            pdf_norm = (luminance.shape[0].value * luminance.shape[1].value) / \
                 (sample_cdf_ys_[-1] * (2 * math.pi * math.pi))
            # Normalize to [0, 1)
            sample_cdf_xs = (sample_cdf_xs_ - sample_cdf_xs_[:, 0:1]) / \
                tf.math.maximum(
                    sample_cdf_xs_[
                        :,
                        (luminance.shape[1].value - 1):luminance.shape[1].value],
                        1e-8 * tf.convert_to_tensor(np.ones((sample_cdf_xs_.shape[0], 1)), dtype=tf.float32)
                    )
            sample_cdf_ys = (sample_cdf_ys_ - sample_cdf_ys_[0]) / \
                tf.math.maximum(sample_cdf_ys_[-1], tf.constant([1e-8]))

            self.values = values
            self.sample_cdf_ys = sample_cdf_ys
            self.sample_cdf_xs = sample_cdf_xs
        with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())):
            self.pdf_norm = pdf_norm.cpu()
            env_to_world = tf.identity(env_to_world).cpu()
            self.env_to_world = env_to_world
            self.world_to_env = tf.linalg.inv(env_to_world)
示例#6
0
# Render our target
img = pyredner.render(0, *scene_args)
pyredner.imwrite(img, 'results/test_svbrdf/target.exr')
pyredner.imwrite(img, 'results/test_svbrdf/target.png')
target = pyredner.imread('results/test_svbrdf/target.exr')

# Our initial guess is three gray textures
with tf.device(pyredner.get_device_name()):
    diffuse_tex = tf.Variable(tf.ones((256, 256, 3), dtype=np.float32) * 0.5,
                              trainable=True)
    specular_tex = tf.Variable(tf.ones((256, 256, 3), dtype=np.float32) * 0.5,
                               trainable=True)
    roughness_tex = tf.Variable(tf.ones((256, 256, 1), dtype=np.float32) * 0.5,
                                trainable=True)
mat_perlin.diffuse_reflectance = pyredner.Texture(diffuse_tex)
mat_perlin.specular_reflectance = pyredner.Texture(specular_tex)
mat_perlin.roughness = pyredner.Texture(roughness_tex)
scene_args = pyredner.serialize_scene(scene=scene,
                                      num_samples=16,
                                      max_bounces=1)
# Render the initial guess
img = pyredner.render(1, *scene_args)
pyredner.imwrite(img, 'results/test_svbrdf/init.png')
diff = tf.abs(target - img)
pyredner.imwrite(diff, 'results/test_svbrdf/init_diff.png')

# Optimize for triangle vertices
optimizer = tf.compat.v1.train.AdamOptimizer(1e-2)
for t in range(200):
    print('iteration:', t)
示例#7
0
tf.compat.v1.enable_eager_execution()
import pyredner_tensorflow as pyredner

# Optimize for a textured plane in a specular reflection

# Use GPU if available
pyredner.set_use_gpu(
    tf.test.is_gpu_available(cuda_only=True, min_cuda_compute_capability=None))

# Load the scene from a Mitsuba scene file
scene = pyredner.load_mitsuba('scenes/teapot_specular.xml')

# The last material is the teapot material, set it to a specular material
with tf.device(pyredner.get_device_name()):
    scene.materials[-1].diffuse_reflectance = \
        pyredner.Texture(tf.Variable([0.15, 0.2, 0.15], dtype=tf.float32))
    scene.materials[-1].specular_reflectance = \
        pyredner.Texture(tf.Variable([0.8, 0.8, 0.8], dtype=tf.float32))
    scene.materials[-1].roughness = \
        pyredner.Texture(tf.Variable([0.0001], dtype=tf.float32))

scene_args = pyredner.serialize_scene(scene=scene,
                                      num_samples=512,
                                      max_bounces=2)

# Render our target. The first argument is the seed for RNG in the renderer.
img = pyredner.render(0, *scene_args)
pyredner.imwrite(img, 'results/test_teapot_specular/target.exr')
pyredner.imwrite(img, 'results/test_teapot_specular/target.png')
target = pyredner.imread('results/test_teapot_specular/target.exr')
示例#8
0
def parse_material(node, two_sided=False):
    node_id = None
    if 'id' in node.attrib:
        node_id = node.attrib['id']
    if node.attrib['type'] == 'diffuse':
        diffuse_reflectance = tf.constant([0.5, 0.5, 0.5])
        diffuse_uv_scale = [1.0, 1.0]
        specular_reflectance = tf.constant([0.0, 0.0, 0.0])
        specular_uv_scale = [1.0, 1.0]
        roughness = tf.constant([1.0])

        for child in node:
            if child.attrib['name'] == 'reflectance':
                if child.tag == 'texture':
                    for grandchild in child:
                        if grandchild.attrib['name'] == 'filename':
                            diffuse_reflectance = pyredner.imread(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'uscale':
                            diffuse_uv_scale[0] = float(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'vscale':
                            diffuse_uv_scale[1] = float(
                                grandchild.attrib['value'])
                elif child.tag == 'rgb' or child.tag == 'spectrum':
                    diffuse_reflectance = parse_vector(child.attrib['value'])
            elif child.attrib['name'] == 'specular':
                if child.tag == 'texture':
                    for grandchild in child:
                        if grandchild.attrib['name'] == 'filename':
                            specular_reflectance = pyredner.imread(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'uscale':
                            specular_uv_scale[0] = float(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'vscale':
                            specular_uv_scale[1] = float(
                                grandchild.attrib['value'])
                elif child.tag == 'rgb' or child.tag == 'spectrum':
                    specular_reflectance = parse_vector(child.attrib['value'])
            elif child.attrib['name'] == 'roughness':
                roughness = tf.constant([float(child.attrib['value'])])

        diffuse_uv_scale = tf.constant(diffuse_uv_scale)
        specular_uv_scale = tf.constant(specular_uv_scale)

        return (node_id,
                pyredner.Material(diffuse_reflectance=pyredner.Texture(
                    diffuse_reflectance, diffuse_uv_scale),
                                  specular_reflectance=pyredner.Texture(
                                      specular_reflectance, specular_uv_scale),
                                  roughness=pyredner.Texture(roughness),
                                  two_sided=two_sided))

    elif node.attrib['type'] == 'roughplastic':
        diffuse_reflectance = tf.constant([0.5, 0.5, 0.5])
        diffuse_uv_scale = [1.0, 1.0]
        specular_reflectance = tf.constant([0.0, 0.0, 0.0])
        specular_uv_scale = [1.0, 1.0]
        roughness = tf.constant([1.0])
        for child in node:
            if child.attrib['name'] == 'diffuseReflectance':
                if child.tag == 'texture':
                    for grandchild in child:
                        if grandchild.attrib['name'] == 'filename':
                            diffuse_reflectance = pyredner.imread(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'uscale':
                            diffuse_uv_scale[0] = float(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'vscale':
                            diffuse_uv_scale[1] = float(
                                grandchild.attrib['value'])
                elif child.tag == 'rgb' or child.tag == 'spectrum':
                    diffuse_reflectance = parse_vector(child.attrib['value'])
            elif child.attrib['name'] == 'specularReflectance':
                if child.tag == 'texture':
                    for grandchild in child:
                        if grandchild.attrib['name'] == 'filename':
                            specular_reflectance = pyredner.imread(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'uscale':
                            specular_uv_scale[0] = float(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'vscale':
                            specular_uv_scale[1] = float(
                                grandchild.attrib['value'])
                elif child.tag == 'rgb' or child.tag == 'spectrum':
                    specular_reflectance = parse_vector(child.attrib['value'])
            elif child.attrib['name'] == 'alpha':
                alpha = float(child.attrib['value'])
                roughness = tf.constant([alpha * alpha])

        diffuse_uv_scale = tf.constant(diffuse_uv_scale)
        specular_uv_scale = tf.constant(specular_uv_scale)

        return (node_id,
                pyredner.Material(diffuse_reflectance=pyredner.Texture(
                    diffuse_reflectance, diffuse_uv_scale),
                                  specular_reflectance=pyredner.Texture(
                                      specular_reflectance, specular_uv_scale),
                                  roughness=pyredner.Texture(roughness),
                                  two_sided=two_sided))
    elif node.attrib['type'] == 'twosided':
        ret = parse_material(node[0], True)
        return (node_id, ret[1])
    else:
        print('Unsupported material type:', node.attrib['type'])
        assert (False)
import pyredner_tensorflow as pyredner
import numpy as np
import scipy

# Optimize for material parameters and camera pose

# Use GPU if available
pyredner.set_use_gpu(tf.test.is_gpu_available(cuda_only=True, min_cuda_compute_capability=None))

# Load the scene from a Mitsuba scene file
scene = pyredner.load_mitsuba('scenes/teapot.xml')

# The last material is the teapot material, set it to the target
with tf.device(pyredner.get_device_name()):
    scene.materials[-1].diffuse_reflectance = \
        pyredner.Texture(tf.Variable([0.3, 0.2, 0.2], dtype=tf.float32, use_resource=True))
    scene.materials[-1].specular_reflectance = \
        pyredner.Texture(tf.Variable([0.6, 0.6, 0.6], dtype=tf.float32, use_resource=True))
    scene.materials[-1].roughness = \
        pyredner.Texture(tf.Variable([0.05], dtype=tf.float32, use_resource=True))

scene_args = pyredner.serialize_scene(
    scene = scene,
    num_samples = 1024,
    max_bounces = 2)

# Render our target. The first argument is the seed for RNG in the renderer.
img = pyredner.render(0, *scene_args)
pyredner.imwrite(img, 'results/test_teapot_reflectance/target.exr')
pyredner.imwrite(img, 'results/test_teapot_reflectance/target.png')
target = pyredner.imread('results/test_teapot_reflectance/target.exr')
示例#10
0
def parse_material(node, two_sided=False):
    node_id = None
    if 'id' in node.attrib:
        node_id = node.attrib['id']
    if node.attrib['type'] == 'diffuse':
        diffuse_reflectance = tf.constant([0.5, 0.5, 0.5])
        diffuse_uv_scale = [1.0, 1.0]
        specular_reflectance = tf.constant([0.0, 0.0, 0.0])
        specular_uv_scale = [1.0, 1.0]
        roughness = tf.constant([1.0])

        for child in node:
            if child.attrib['name'] == 'reflectance':
                if child.tag == 'texture':
                    for grandchild in child:
                        if grandchild.attrib['name'] == 'filename':
                            diffuse_reflectance = pyredner.imread(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'uscale':
                            diffuse_uv_scale[0] = float(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'vscale':
                            diffuse_uv_scale[1] = float(
                                grandchild.attrib['value'])
                elif child.tag == 'rgb' or child.tag == 'spectrum' or child.tag == 'srgb':
                    diffuse_reflectance = parse_vector(child.attrib['value'])
                    if child.tag == 'srgb':
                        diffuse_reflectance = pyredner.srgb_to_linear(
                            diffuse_reflectance)
            elif child.attrib['name'] == 'specular':
                if child.tag == 'texture':
                    for grandchild in child:
                        if grandchild.attrib['name'] == 'filename':
                            specular_reflectance = pyredner.imread(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'uscale':
                            specular_uv_scale[0] = float(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'vscale':
                            specular_uv_scale[1] = float(
                                grandchild.attrib['value'])
                elif child.tag == 'rgb' or child.tag == 'spectrum' or child.tag == 'srgb':
                    specular_reflectance = parse_vector(child.attrib['value'])
                    if child.tag == 'srgb':
                        specular_reflectance = pyredner.srgb_to_linear(
                            specular_reflectance)
            elif child.attrib['name'] == 'roughness':
                roughness = tf.constant([float(child.attrib['value'])])

        diffuse_uv_scale = tf.constant(diffuse_uv_scale)
        specular_uv_scale = tf.constant(specular_uv_scale)

        return (node_id,
                pyredner.Material(diffuse_reflectance=pyredner.Texture(
                    diffuse_reflectance, diffuse_uv_scale),
                                  specular_reflectance=pyredner.Texture(
                                      specular_reflectance, specular_uv_scale),
                                  roughness=pyredner.Texture(roughness),
                                  two_sided=two_sided))

    elif node.attrib['type'] == 'roughplastic':
        diffuse_reflectance = tf.constant([0.5, 0.5, 0.5])
        diffuse_uv_scale = [1.0, 1.0]
        # Mitsuba defaults specular reflectance to 1.0, but we use Schilick approximation and
        # use the specular reflectance for representing both index of refraction and color tint
        # for metal materials simultaneously.
        # Schilick's appsoximation set R0 to ((n1 - n2) / (n1 + n2))^2. Mitsuba defaults
        # IOR to n1=1 and n2=1.5, so R0 ~= 0.04
        specular_reflectance = tf.constant([0.04, 0.04, 0.04])
        specular_uv_scale = [1.0, 1.0]
        roughness = tf.constant([0.01])
        for child in node:
            if child.attrib['name'] == 'diffuseReflectance' or child.attrib[
                    'name'] == 'diffuse_reflectance':
                if child.tag == 'texture':
                    for grandchild in child:
                        if grandchild.attrib['name'] == 'filename':
                            diffuse_reflectance = pyredner.imread(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'uscale':
                            diffuse_uv_scale[0] = float(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'vscale':
                            diffuse_uv_scale[1] = float(
                                grandchild.attrib['value'])
                elif child.tag == 'rgb' or child.tag == 'spectrum' or child.tag == 'srgb':
                    diffuse_reflectance = parse_vector(child.attrib['value'])
                    if child.tag == 'srgb':
                        diffuse_reflectance = pyredner.srgb_to_linear(
                            diffuse_reflectance)
            elif child.attrib['name'] == 'specularReflectance' or child.attrib[
                    'name'] == 'specular_reflectance':
                if child.tag == 'texture':
                    for grandchild in child:
                        if grandchild.attrib['name'] == 'filename':
                            specular_reflectance = pyredner.imread(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'uscale':
                            specular_uv_scale[0] = float(
                                grandchild.attrib['value'])
                        elif grandchild.attrib['name'] == 'vscale':
                            specular_uv_scale[1] = float(
                                grandchild.attrib['value'])
                elif child.tag == 'rgb' or child.tag == 'spectrum' or child.tag == 'srgb':
                    specular_reflectance = parse_vector(child.attrib['value'])
                    if child.tag == 'srgb':
                        specular_reflectance = pyredner.srgb_to_linear(
                            specular_reflectance)
            elif child.attrib['name'] == 'alpha':
                if child.tag == 'texture':
                    roughness, roughness_uv_scale = parse_texture(child)
                    roughness = roughness * roughness
                else:
                    alpha = float(child.attrib['value'])
                    roughness = tf.constant([alpha * alpha])

        diffuse_uv_scale = tf.constant(diffuse_uv_scale)
        specular_uv_scale = tf.constant(specular_uv_scale)

        return (node_id,
                pyredner.Material(diffuse_reflectance=pyredner.Texture(
                    diffuse_reflectance, diffuse_uv_scale),
                                  specular_reflectance=pyredner.Texture(
                                      specular_reflectance, specular_uv_scale),
                                  roughness=pyredner.Texture(roughness),
                                  two_sided=two_sided))
    elif node.attrib['type'] == 'twosided':
        ret = parse_material(node[0], True)
        return (node_id, ret[1])
    else:
        print('Unsupported material type:', node.attrib['type'])
        assert (False)
tf.compat.v1.enable_eager_execution()
import pyredner_tensorflow as pyredner

# Optimize for a textured plane in a specular reflection

# Use GPU if available
pyredner.set_use_gpu(
    tf.test.is_gpu_available(cuda_only=True, min_cuda_compute_capability=None))

# Load the scene from a Mitsuba scene file
scene = pyredner.load_mitsuba('scenes/teapot_specular.xml')

# The last material is the teapot material, set it to a specular material
with tf.device(pyredner.get_device_name()):
    scene.materials[-1].diffuse_reflectance = \
        pyredner.Texture(tf.Variable([0.15, 0.2, 0.15], dtype=tf.float32, use_resource=True))
    scene.materials[-1].specular_reflectance = \
        pyredner.Texture(tf.Variable([0.8, 0.8, 0.8], dtype=tf.float32, use_resource=True))
    scene.materials[-1].roughness = \
        pyredner.Texture(tf.Variable([0.0001], dtype=tf.float32, use_resource=True))

scene_args = pyredner.serialize_scene(scene=scene,
                                      num_samples=512,
                                      max_bounces=2)

# Render our target. The first argument is the seed for RNG in the renderer.
img = pyredner.render(0, *scene_args)
pyredner.imwrite(img, 'results/test_teapot_specular/target.exr')
pyredner.imwrite(img, 'results/test_teapot_specular/target.png')
target = pyredner.imread('results/test_teapot_specular/target.exr')