Skip to content

Commit 48b9954

Browse files
committed
first attempt at pbr shading
1 parent 7ee0fde commit 48b9954

7 files changed

Lines changed: 92 additions & 11 deletions

File tree

src/blender-addon/leaf/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def view_update(self, context):
3838
def view_draw(self, context):
3939
vm = context.region_data.view_matrix.copy()
4040
vm.transpose()
41-
print(vm)
4241
view_matrix = (ctypes.c_float * 16)(
4342
vm[0][0], vm[0][1], vm[0][2], vm[0][3],
4443
vm[1][0], vm[1][1], vm[1][2], vm[1][3],

src/blender-addon/leaf/export.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ def export_light(obj):
5252
return {}
5353

5454
def export_material(mtl):
55+
lmtl = mtl.leaf
5556
return {
56-
"diffuse": [mtl.diffuse_color.r, mtl.diffuse_color.g, mtl.diffuse_color.b]
57+
"albedo": [mtl.diffuse_color.r, mtl.diffuse_color.g, mtl.diffuse_color.b],
58+
"metalness": lmtl.metalness,
59+
"roughness": lmtl.roughness
5760
}
5861

5962
def export_mesh(sourceMesh):

src/blender-addon/leaf/material.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
import bpy
22

33
from bpy.types import Panel, Menu, Operator
4+
from bpy.props import (BoolProperty,
5+
EnumProperty,
6+
FloatProperty,
7+
IntProperty,
8+
PointerProperty)
9+
10+
class LeafMaterialSettings(bpy.types.PropertyGroup):
11+
@classmethod
12+
def register(cls):
13+
bpy.types.Material.leaf = PointerProperty(
14+
name="Leaf Material Settings",
15+
description="Leaf material settings",
16+
type=cls,
17+
)
18+
cls.metalness = FloatProperty(
19+
name="Metalness",
20+
description="0: dielectric, 1: metal",
21+
min=0.0, max=1.0,
22+
default=0.0,
23+
)
24+
cls.roughness = FloatProperty(
25+
name="Roughness",
26+
description="0: smooth, 1: rough",
27+
min=0.0, max=1.0,
28+
default=0.5,
29+
)
30+
31+
@classmethod
32+
def unregister(cls):
33+
del bpy.types.Material.leaf
434

535
class LeafMaterialButtonsPanel():
636
bl_space_type = "PROPERTIES"
@@ -23,5 +53,8 @@ def poll(cls, context):
2353
def draw(self, context):
2454
layout = self.layout
2555
mat = context.material
56+
lmat = context.material.leaf
2657

27-
layout.prop(mat, "diffuse_color")
58+
layout.prop(mat, "diffuse_color", text="Albedo")
59+
layout.prop(lmat, "metalness")
60+
layout.prop(lmat, "roughness")

src/engine/Material.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
#include <engine/ResourceManager.h>
44

55
const std::string Material::resourceClassName = "Material";
6-
const std::string Material::defaultResourceData = "{\"diffuse\": [1.0, 0.0, 1.0]}";
6+
const std::string Material::defaultResourceData = "{\"albedo\": [1.0, 0.0, 1.0], \"metalness\": 0.5, \"roughness\": 0.5}";
77

88
void Material::load(const cJSON *json)
99
{
10-
cJSON *diffuse = cJSON_GetObjectItem(json, "diffuse");
11-
this->data.diffuse = glm::vec3(cJSON_GetArrayItem(diffuse, 0)->valuedouble, cJSON_GetArrayItem(diffuse, 1)->valuedouble, cJSON_GetArrayItem(diffuse, 2)->valuedouble);
10+
cJSON *diffuse = cJSON_GetObjectItem(json, "albedo");
11+
this->data.albedo = glm::vec3(cJSON_GetArrayItem(diffuse, 0)->valuedouble, cJSON_GetArrayItem(diffuse, 1)->valuedouble, cJSON_GetArrayItem(diffuse, 2)->valuedouble);
12+
this->data.metalness = (float)cJSON_GetObjectItem(json, "metalness")->valuedouble;
13+
this->data.roughness = (float)cJSON_GetObjectItem(json, "roughness")->valuedouble;
14+
15+
// normalize diffuse BRDF
16+
this->data.albedo /= 3.1415926535f;
1217
}
1318

1419
void Material::unload()

src/engine/Material.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ class Material: public Resource
1313
#pragma pack(16)
1414
struct MaterialData
1515
{
16-
glm::vec3 diffuse;
17-
float _padding[1];
16+
glm::vec3 albedo;
17+
float metalness;
18+
float roughness;
19+
float _padding[3];
1820
};
1921
#pragma pack(pop)
2022

src/engine/shaders/basic.ps.hlsl

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ struct PS_OUTPUT
55
float4 color: SV_TARGET;
66
};
77

8+
float g1v(float dotNV, float k)
9+
{
10+
return 1.0 / (dotNV * (1.0 - k) + 1.0);
11+
}
12+
813
PS_OUTPUT main(BASIC_PS_INPUT input)
914
{
1015
PS_OUTPUT output;
@@ -14,10 +19,42 @@ PS_OUTPUT main(BASIC_PS_INPUT input)
1419
const float3 normal = normalize(input.normal);
1520
const float3 h = normalize(eye + light);
1621

17-
float d = saturate(dot(normal, light));
22+
/*float d = saturate(dot(normal, light));
1823
float s = saturate(pow(dot(normal, h), 80.0));
24+
float3 blinn_phong = diffuse * d + s;*/
25+
26+
// blend between dielectric and metal
27+
const float f0 = 0.03;
28+
float3 specularColor = lerp(float3(1.0, 1.0, 1.0), albedo, metalness);
29+
float3 finalAlbedo = albedo * (1.0 - metalness);
30+
31+
// precompute all cosines
32+
float dotLH = saturate(dot(light, h));
33+
float dotNH = saturate(dot(normal, h));
34+
float dotNL = saturate(dot(normal, light));
35+
float dotNV = saturate(dot(normal, eye));
36+
37+
// simple lambert for diffuse
38+
float3 diffuse = dotNL * finalAlbedo;
39+
40+
// schlick fresnel approximation
41+
float fresnel = f0 + (1.0 - f0) * pow(1.0 - dotNV, 5.0);
42+
43+
float alpha = roughness * roughness;
44+
float alphaSquared = alpha * alpha;
45+
46+
// GGX normal distribution
47+
float denominator = dotNH * dotNH * (alphaSquared - 1.0) + 1.0;
48+
float normalDistribution = alphaSquared / (3.141592 * denominator * denominator);
49+
50+
// schlick approximation for geometry factor
51+
float k = alpha * 0.5;
52+
float geometryFactor = g1v(dotNL, k) * g1v(dotNV, k);
53+
54+
// cook-torrance microfacet model
55+
float3 specular = specularColor * dotNL * fresnel * normalDistribution * geometryFactor;
1956

20-
output.color = float4(diffuse * d + s, 1.0);
57+
output.color = float4((diffuse + specular) * 10.0, 1.0);
2158

2259
return output;
2360
}

src/engine/shaders/shared.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ cbuffer SceneData: register(b0)
2323

2424
cbuffer MaterialData: register(b1)
2525
{
26-
float3 diffuse;
26+
float3 albedo; // already divided by Pi
27+
float metalness;
28+
float roughness;
2729
};
2830

2931
cbuffer InstanceData: register(b2)

0 commit comments

Comments
 (0)