Skip to content

Commit

Permalink
glossy emitters from blender
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrit committed Feb 28, 2024
1 parent 088091d commit 8e7d64a
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 19 deletions.
8 changes: 7 additions & 1 deletion SeeSharp/IO/IMeshLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ public interface IMeshLoader {
/// <param name="emissiveMaterials">All emissive materials in the scene</param>
/// <exception cref="MeshLoadException">Thrown if the file is corrupted</exception>
(IEnumerable<Mesh>, IEnumerable<Emitter>) LoadMesh(Dictionary<string, Material> namedMaterials,
Dictionary<string, RgbColor> emissiveMaterials, JsonElement jsonElement, string dirname);
Dictionary<string, EmissionParameters> emissiveMaterials, JsonElement jsonElement, string dirname);

struct EmissionParameters {
public RgbColor Radiance;
public bool IsGlossy;
public float Exponent;
}
}
20 changes: 15 additions & 5 deletions SeeSharp/IO/JsonScene.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using static SeeSharp.IO.IMeshLoader;

namespace SeeSharp.IO;

/// <summary>
Expand All @@ -14,7 +16,7 @@ static IMeshLoader[] KnownLoaders {

private static void ReadMeshes(string path, Scene resultScene, JsonElement root,
Dictionary<string, Material> namedMaterials,
Dictionary<string, RgbColor> emissiveMaterials) {
Dictionary<string, EmissionParameters> emissiveMaterials) {
var meshes = root.GetProperty("objects");

ProgressBar progressBar = new(prefix: "Loading meshes...");
Expand Down Expand Up @@ -49,10 +51,10 @@ private static void ReadMeshes(string path, Scene resultScene, JsonElement root,
}

private static void ReadMaterials(string path, JsonElement root, out Dictionary<string, Material> namedMaterials,
out Dictionary<string, RgbColor> emissiveMaterials) {
out Dictionary<string, EmissionParameters> emissiveMaterials) {

var namedMats = new Dictionary<string, Material>();
var emissiveMats = new Dictionary<string, RgbColor>();
var emissiveMats = new Dictionary<string, EmissionParameters>();
if (root.TryGetProperty("materials", out var materials)) {
ProgressBar progressBar = new(prefix: "Loading materials...");
progressBar.Start(materials.GetArrayLength());
Expand Down Expand Up @@ -103,8 +105,16 @@ bool ReadOptionalBool(string name, bool defaultValue) {
// Check if the material is emissive
if (m.TryGetProperty("emission", out elem)) {
RgbColor emission = JsonUtils.ReadRgbColor(elem);
if (emission != RgbColor.Black)
lock (emissiveMats) emissiveMats.Add(name, emission);
if (emission != RgbColor.Black) {
bool isGlossy = ReadOptionalBool("emissionIsGlossy", false);
float exponent = ReadOptionalFloat("emissionExponent", 50);
lock (emissiveMats)
emissiveMats.Add(name, new() {
Radiance = emission,
IsGlossy = isGlossy,
Exponent = exponent
});
}
}

lock (progressBar) progressBar.ReportDone(1);
Expand Down
22 changes: 14 additions & 8 deletions SeeSharp/IO/ObjConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace SeeSharp.IO;
using static SeeSharp.IO.IMeshLoader;

namespace SeeSharp.IO;

/// <summary>
/// Converts our parsed wavefront .obj mesh representation into an actual mesh.
Expand Down Expand Up @@ -30,7 +32,7 @@ public TriIdx(ObjMesh.Index v0, ObjMesh.Index v1, ObjMesh.Index v2) {
/// </param>
public static (IEnumerable<Mesh>, IEnumerable<Emitter>) CreateMeshes(ObjMesh mesh,
Dictionary<string, Material> materialOverride,
Dictionary<string, RgbColor> emissionOverride = null) {
Dictionary<string, EmissionParameters> emissionOverride = null) {
// Create a dummy constant texture color for incorrect texture references
var dummyColor = new TextureRgb(RgbColor.White);
var dummyMaterial = new GenericMaterial(new GenericMaterial.Parameters {
Expand Down Expand Up @@ -194,13 +196,17 @@ int RemapIndex(ObjMesh.Index oldIndex) {
// Create an emitter if the obj material is emissive
RgbColor emission = RgbColor.Black;
emitters.TryGetValue(materialName, out emission);
emissionOverride?.TryGetValue(materialName, out emission);

if (emission != RgbColor.Black) {
var emitter = DiffuseEmitter.MakeFromMesh(m, emission);
loadedEmitters.AddRange(emitter);
}
IEnumerable<Emitter> emitter = null;
if (emissionOverride.TryGetValue(materialName, out EmissionParameters e))
emitter = e.IsGlossy
? GlossyEmitter.MakeFromMesh(m, e.Radiance, e.Exponent)
: DiffuseEmitter.MakeFromMesh(m, e.Radiance);
else if (emission != RgbColor.Black)
emitter = DiffuseEmitter.MakeFromMesh(m, emission);

if (emitter != null)
loadedEmitters.AddRange(emitter);
}
}
}
Expand All @@ -209,7 +215,7 @@ int RemapIndex(ObjMesh.Index oldIndex) {
}

public (IEnumerable<Mesh>, IEnumerable<Emitter>) LoadMesh(Dictionary<string, Material> namedMaterials,
Dictionary<string, RgbColor> emissiveMaterials,
Dictionary<string, EmissionParameters> emissiveMaterials,
JsonElement jsonElement, string dirname) {
// The path is relative to this .json, we need to make it absolute / relative to the CWD
string relpath = jsonElement.GetProperty("relativePath").GetString();
Expand Down
8 changes: 6 additions & 2 deletions SeeSharp/IO/PlyLoader.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using static SeeSharp.IO.IMeshLoader;

namespace SeeSharp.IO;

/// <summary>
Expand All @@ -7,7 +9,7 @@ public class PlyLoader : IMeshLoader {
public string Type => "ply";

public (IEnumerable<Mesh>, IEnumerable<Emitter>) LoadMesh(Dictionary<string, Material> namedMaterials,
Dictionary<string, RgbColor> emissiveMaterials,
Dictionary<string, EmissionParameters> emissiveMaterials,
JsonElement jsonElement, string dirname) {
// The path is relative to this .json, we need to make it absolute / relative to the CWD
string relpath = jsonElement.GetProperty("relativePath").GetString();
Expand All @@ -27,7 +29,9 @@ public class PlyLoader : IMeshLoader {

IEnumerable<Emitter> emitters = null;
if (emissiveMaterials != null && emissiveMaterials.TryGetValue(materialName, out var emission)) {
emitters = DiffuseEmitter.MakeFromMesh(mesh, emission);
emitters = emission.IsGlossy
? GlossyEmitter.MakeFromMesh(mesh, emission.Radiance, emission.Exponent)
: DiffuseEmitter.MakeFromMesh(mesh, emission.Radiance);
} else if (jsonElement.TryGetProperty("emission", out var emissionJson)) {
emitters = DiffuseEmitter.MakeFromMesh(mesh, JsonUtils.ReadRgbColor(emissionJson));
}
Expand Down
10 changes: 8 additions & 2 deletions SeeSharp/IO/TriMeshLoader.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using static SeeSharp.IO.IMeshLoader;

namespace SeeSharp.IO;

internal class TriMeshLoader : IMeshLoader {
public string Type => "trimesh";

public (IEnumerable<Mesh>, IEnumerable<Emitter>) LoadMesh(Dictionary<string, Material> namedMaterials,
Dictionary<string, RgbColor> emissiveMaterials,
Dictionary<string, EmissionParameters> emissiveMaterials,
JsonElement jsonElement, string dirname) {
string materialName = jsonElement.GetProperty("material").GetString();
var material = namedMaterials[materialName];
Expand Down Expand Up @@ -50,7 +52,11 @@ int[] ReadIntArray(JsonElement json) {
var mesh = new Mesh(vertices, indices, normals, uvs) { Material = material };

IEnumerable<Emitter> emitters = null;
if (jsonElement.TryGetProperty("emission", out var emissionJson)) {
if (emissiveMaterials != null && emissiveMaterials.TryGetValue(materialName, out var emission)) {
emitters = emission.IsGlossy
? GlossyEmitter.MakeFromMesh(mesh, emission.Radiance, emission.Exponent)
: DiffuseEmitter.MakeFromMesh(mesh, emission.Radiance);
} else if (jsonElement.TryGetProperty("emission", out var emissionJson)) {
emitters = DiffuseEmitter.MakeFromMesh(mesh, JsonUtils.ReadRgbColor(emissionJson));
}

Expand Down
4 changes: 3 additions & 1 deletion see_blender/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def material_to_json(material, out_dir):
material.emission_color[0] * material.emission_strength,
material.emission_color[1] * material.emission_strength,
material.emission_color[2] * material.emission_strength
))
)),
"emissionIsGlossy": material.emission_is_glossy,
"emissionExponent": material.emission_glossy_exponent
}

def export_materials(result, out_dir):
Expand Down
12 changes: 12 additions & 0 deletions see_blender/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ class SeeSharpMaterial(bpy.types.PropertyGroup):
default=0, min=0
)

emission_is_glossy: BoolProperty(
name="Glossy Emission",
description="Emission profile is sharpened like a glossy material's reflectance",
default=False
)

emission_glossy_exponent: FloatProperty(
name="Glossy Emission Exponent",
description="Glossy Emission Exponent",
default=20
)

@classmethod
def register(cls):
bpy.types.Material.seesharp = PointerProperty(
Expand Down
3 changes: 3 additions & 0 deletions see_blender/material_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ def draw(self, context):

layout.prop(mat, "emission_color")
layout.prop(mat, "emission_strength")
layout.prop(mat, "emission_is_glossy")
if mat.emission_is_glossy == True:
layout.prop(mat, "emission_glossy_exponent")

def register():
bpy.utils.register_class(SEESHARP_PT_context_material)
Expand Down

0 comments on commit 8e7d64a

Please sign in to comment.