Skip to content

Commit ccccc2c

Browse files
committed
First draft for Physical Based Rendering example
1 parent ecce963 commit ccccc2c

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

examples/core_display_pbr.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
##Copyright 2021 Thomas Paviot ([email protected])
2+
##
3+
##This file is part of pythonOCC.
4+
##
5+
##pythonOCC is free software: you can redistribute it and/or modify
6+
##it under the terms of the GNU Lesser General Public License as published by
7+
##the Free Software Foundation, either version 3 of the License, or
8+
##(at your option) any later version.
9+
##
10+
##pythonOCC is distributed in the hope that it will be useful,
11+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
##GNU Lesser General Public License for more details.
14+
##
15+
##You should have received a copy of the GNU Lesser General Public License
16+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
17+
18+
import sys
19+
20+
from OCC.Display.SimpleGui import init_display
21+
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeCone
22+
from OCC.Core.Graphic3d import (Graphic3d_NOM_PLASTIC, Graphic3d_NOM_ALUMINIUM,
23+
Graphic3d_TOSM_PBR, Graphic3d_TOSM_PBR_FACET,
24+
Graphic3d_TOSM_FRAGMENT, Graphic3d_TOSM_VERTEX,
25+
Graphic3d_PBRMaterial, Graphic3d_MaterialAspect)
26+
from OCC.Core.V3d import V3d_SpotLight, V3d_XnegYnegZpos, V3d_AmbientLight, V3d_DirectionalLight
27+
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_WHITE, Quantity_NOC_CORAL2, Quantity_NOC_BROWN, Quantity_NOC_GRAY
28+
from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Cut
29+
from OCC.Core.gp import gp_Vec, gp_Pnt, gp_Dir
30+
31+
from OCC.Extend.ShapeFactory import translate_shp
32+
33+
# first create geometry
34+
from core_classic_occ_bottle import bottle
35+
table = translate_shp(BRepPrimAPI_MakeBox(100, 100, 10).Shape(), gp_Vec(-50, -50, -10))
36+
glass_out = BRepPrimAPI_MakeCone(7, 9, 25).Shape()
37+
glass_in = translate_shp(BRepPrimAPI_MakeCone(7, 9, 25).Shape(), gp_Vec(0., 0., 0.2))
38+
glass = BRepAlgoAPI_Cut(glass_out, glass_in).Shape()
39+
translated_glass = translate_shp(glass, gp_Vec(-30, -30, 0))
40+
41+
# then inits display
42+
display, start_display, add_menu, add_function_to_menu = init_display()
43+
44+
# ambient light
45+
ambient_light = V3d_AmbientLight(Quantity_Color(Quantity_NOC_WHITE))
46+
display.Viewer.AddLight(ambient_light)
47+
48+
# directional light
49+
dir_light = V3d_DirectionalLight(gp_Dir(0,0,1), Quantity_Color(Quantity_NOC_WHITE))
50+
display.Viewer.AddLight(dir_light)
51+
52+
# create one spotlight
53+
spot_light = V3d_SpotLight(gp_Pnt(-100, -100, 100),
54+
V3d_XnegYnegZpos, Quantity_Color(Quantity_NOC_WHITE))
55+
## display the spotlight in rasterized mode
56+
display.Viewer.AddLight(spot_light)
57+
58+
display.View.SetLightOn()
59+
60+
# first create the PBR material
61+
pbr_mat = Graphic3d_PBRMaterial()
62+
print(dir(pbr_mat))
63+
pbr_mat.SetMetallic(0.8)
64+
pbr_mat.SetRoughness(0.5)
65+
# modifies albedo color
66+
#pbr_mat.SetColor(Quantity_Color(Quantity_NOC_GRAY))
67+
68+
# then the material aspect
69+
alu_pbr_aspect = Graphic3d_MaterialAspect(Graphic3d_NOM_ALUMINIUM)
70+
alu_pbr_aspect.SetPBRMaterial(pbr_mat)
71+
72+
#print(dir(pbr_mat))
73+
display.EnableAntiAliasing()
74+
display.DisplayShape(bottle, material=alu_pbr_aspect)
75+
display.DisplayShape(table, material=Graphic3d_NOM_PLASTIC, color=Quantity_NOC_CORAL2)
76+
display.DisplayShape(translated_glass,
77+
material=Graphic3d_NOM_PLASTIC,
78+
color=Quantity_NOC_BROWN,
79+
transparency=0.6,
80+
update=True)
81+
82+
def pbr(event=None):
83+
display.View.SetShadingModel(Graphic3d_TOSM_PBR)
84+
display.View.Redraw()
85+
86+
def pbr_facet(event=None):
87+
display.View.SetShadingModel(Graphic3d_TOSM_PBR_FACET)
88+
display.View.Redraw()
89+
90+
def phong(event=None):
91+
display.View.SetShadingModel(Graphic3d_TOSM_VERTEX)
92+
display.View.Redraw()
93+
94+
def gouraud(event=None):
95+
display.View.SetShadingModel(Graphic3d_TOSM_FRAGMENT)
96+
display.View.Redraw()
97+
98+
def rasterization(event=None):
99+
display.SetRasterizationMode()
100+
display.View.Redraw()
101+
102+
def exit(event=None):
103+
sys.exit(0)
104+
105+
106+
#display.View.GeneratePBREnvironment(True)
107+
108+
if __name__ == '__main__':
109+
add_menu('PBR')
110+
add_function_to_menu('PBR', rasterization)
111+
add_function_to_menu('PBR', gouraud)
112+
add_function_to_menu('PBR', phong)
113+
add_function_to_menu('PBR', pbr)
114+
add_function_to_menu('PBR', pbr_facet)
115+
start_display()

0 commit comments

Comments
 (0)