Skip to content

Commit

Permalink
fixed combine() resulting in incorrect normals because of not conside…
Browse files Browse the repository at this point in the history
…ring rotation and scale.
  • Loading branch information
pokepetter committed Nov 12, 2024
1 parent 3a0f8c0 commit 7eb5840
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions ursina/scripts/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ def combine(combine_parent, analyze=False, auto_destroy=True, ignore=[], ignore_
cols.extend((e.color, ) * len(e.model.vertices))

if include_normals:
rotation_scale_matrix = vertex_to_world_matrix.getUpper3() # get the rotation and scaling part of the matrix
normal_to_world_matrix = rotation_scale_matrix
normal_to_world_matrix.invertInPlace()
normal_to_world_matrix.transposeInPlace()

if e.model.normals: # if has normals
norms.extend(e.model.normals)
norms.extend([Vec3(*normal_to_world_matrix.xform(Vec3(*n)))for n in e.model.normals])
else:
norms.extend((Vec3.up, ) * len(e.model.vertices))

Expand Down Expand Up @@ -96,11 +101,12 @@ def combine(combine_parent, analyze=False, auto_destroy=True, ignore=[], ignore_
from ursina import *
app = Ursina()

from ursina.shaders import lit_with_shadows_shader
from ursina.shaders import lit_with_shadows_shader, normals_shader
Entity.default_shader = lit_with_shadows_shader
# Entity.default_shader = normals_shader

p = Entity(texture='brick')
e1 = Entity(parent=p, model='sphere', y=1.5, color=color.pink)
e1 = Entity(parent=p, model='sphere', y=1.5, color=color.pink, rotation_y=90)
e2 = Entity(parent=p, model='cube', color=color.yellow, x=1, origin_y=-.5, texture='brick')
e3 = Entity(parent=e2, model='cube', color=color.yellow, y=2, scale=.5, texture='brick', texture_scale=Vec2(3,3), texture_offset=(.1,.1))
e4 = Entity(parent=p, model='plane', color=color.lime, scale=10, texture='brick', texture_scale=Vec2(5,5))
Expand All @@ -110,10 +116,9 @@ def input(key):
from time import perf_counter
t = perf_counter()
p.combine(include_normals=True)
# p.model.generate_normals(smooth=False)
p.shader = lit_with_shadows_shader
# p.shader = normals_shader
p.texture='brick'
print(p.model.normals)
print('combined in:', perf_counter() - t)


Expand Down

0 comments on commit 7eb5840

Please sign in to comment.