-
Notifications
You must be signed in to change notification settings - Fork 0
/
constantShading.py
69 lines (51 loc) · 1.8 KB
/
constantShading.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
import openmesh as om
from math import ceil
def faceGeometricCenter(mesh, faceHandler):
maxCoord = np.copy(mesh.point(next(mesh.fv(faceHandler))))
minCoord = np.copy(maxCoord)
for vh in mesh.fv(faceHandler):
point = mesh.point(vh)
for pos in range(len(point)):
if point[pos] > maxCoord[pos]: maxCoord[pos] = point[pos]
if point[pos] < minCoord[pos]: minCoord[pos] = point[pos]
GC = (maxCoord + minCoord)/2
return GC
def constantShading(mesh, faceHandler, VRP, lAmbiente, lPontual, lPontualCord, kA, kD, kS, n):
GC = faceGeometricCenter(mesh, faceHandler)
#iluminação ambiente
iA = lAmbiente*kA
#iluminação difusa
N = mesh.calc_face_normal(faceHandler)
L = (lPontualCord - GC)/np.linalg.norm(lPontualCord - GC)
N_dot_L = np.dot(N,L)
if N_dot_L <= 0:
#print(f'amb: {np.append(iA,1)}')
return np.append(iA,1)
iD = lPontual*kD*N_dot_L
#iluminação especular
R =np.dot(2*np.dot(L,N),N)-L
S = (VRP-GC)/np.linalg.norm(VRP-GC)
R_dot_S = np.dot(R,S)
if R_dot_S <= 0:
#print(f'a+d: {np.append(iA+iD,1)}')
return np.append(iA+iD,1)
iE = lPontual*kS*(R_dot_S**n)
#print(f'all: {np.append(iA+iD+iE,1)}')
return np.append(iA+iD+iE,1)
def applyConstantShading(mesh, VRP, lAmbiente, lPontual, lPontualCord, kA, kD, kS, n):
for fh in mesh.faces():
#print(constantShading(mesh, fh, VRP, lAmbiente, lPontual, lPontualCord, kA, kD, kS, n))
color = constantShading(mesh, fh, VRP, lAmbiente, lPontual, lPontualCord, kA, kD, kS, n)
color = np.clip(color,0,255)
mesh.set_color(fh, color)
if __name__ == '__main__':
face = [[1,1,0],[2,2,0],[1,2,0]]
lAmbiente = [120, 120, 120]
lPontual = [120, 120, 120]
lPontualCord = [10,10,10]
kA = [120, 120, 120]
kD = [120, 120, 120]
kS = [120, 120, 120]
n = 2
constantShading(face, lAmbiente, lPontual, lPontualCord, kA, kD, kS, n)