test_textureattrib.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. from panda3d import core
  2. # Some dummy textures we can use for our texture attributes.
  3. stage1 = core.TextureStage("stage1")
  4. stage2 = core.TextureStage("stage2")
  5. stage3 = core.TextureStage("stage3")
  6. tex1 = core.Texture("tex1")
  7. tex2 = core.Texture("tex2")
  8. tex3 = core.Texture("tex3")
  9. def test_textureattrib_compose_empty():
  10. # Tests a case in which a child node does not alter the original.
  11. tattr1 = core.TextureAttrib.make()
  12. tattr1 = tattr1.add_on_stage(stage1, tex1)
  13. tattr2 = core.TextureAttrib.make()
  14. tattr3 = tattr1.compose(tattr2)
  15. assert tattr3.get_num_on_stages() == 1
  16. assert stage1 in tattr3.on_stages
  17. def test_textureattrib_compose_add():
  18. # Tests a case in which a child node adds another texture.
  19. tattr1 = core.TextureAttrib.make()
  20. tattr1 = tattr1.add_on_stage(stage1, tex1)
  21. tattr2 = core.TextureAttrib.make()
  22. tattr2 = tattr2.add_on_stage(stage2, tex2)
  23. tattr3 = tattr1.compose(tattr2)
  24. assert tattr3.get_num_on_stages() == 2
  25. assert stage1 in tattr3.on_stages
  26. assert stage2 in tattr3.on_stages
  27. def test_textureattrib_compose_override():
  28. # Tests a case in which a child node overrides a texture.
  29. tattr1 = core.TextureAttrib.make()
  30. tattr1 = tattr1.add_on_stage(stage1, tex1)
  31. tattr2 = core.TextureAttrib.make()
  32. tattr2 = tattr2.add_on_stage(stage1, tex2)
  33. tattr3 = tattr1.compose(tattr2)
  34. assert tattr3.get_num_on_stages() == 1
  35. assert stage1 in tattr3.on_stages
  36. assert tattr3.get_on_texture(stage1) == tex2
  37. def test_textureattrib_compose_subtract():
  38. # Tests a case in which a child node disables a texture.
  39. tattr1 = core.TextureAttrib.make()
  40. tattr1 = tattr1.add_on_stage(stage1, tex1)
  41. tattr1 = tattr1.add_on_stage(stage2, tex2)
  42. tattr2 = core.TextureAttrib.make()
  43. tattr2 = tattr2.add_off_stage(stage3)
  44. tattr2 = tattr2.add_off_stage(stage2)
  45. tattr3 = tattr1.compose(tattr2)
  46. assert tattr3.get_num_on_stages() == 1
  47. assert stage1 in tattr3.on_stages
  48. assert stage2 not in tattr3.on_stages
  49. assert stage3 not in tattr3.on_stages
  50. def test_textureattrib_compose_both():
  51. # Tests a case in which a child node both enables and disables a texture.
  52. tattr1 = core.TextureAttrib.make()
  53. tattr1 = tattr1.add_on_stage(stage1, tex1)
  54. tattr1 = tattr1.add_on_stage(stage2, tex2)
  55. tattr2 = core.TextureAttrib.make()
  56. tattr2 = tattr2.add_on_stage(stage3, tex3)
  57. tattr2 = tattr2.add_on_stage(stage1, tex1)
  58. tattr2 = tattr2.add_off_stage(stage2)
  59. tattr3 = tattr1.compose(tattr2)
  60. assert tattr3.get_num_on_stages() == 2
  61. assert stage1 in tattr3.on_stages
  62. assert stage2 not in tattr3.on_stages
  63. assert stage3 in tattr3.on_stages
  64. def test_textureattrib_compose_alloff():
  65. # Tests a case in which a child node disables all textures.
  66. tattr1 = core.TextureAttrib.make()
  67. tattr1 = tattr1.add_on_stage(stage1, tex1)
  68. tattr1 = tattr1.add_on_stage(stage2, tex2)
  69. assert tattr1.get_num_on_stages() == 2
  70. tattr2 = core.TextureAttrib.make_all_off()
  71. assert tattr2.has_all_off()
  72. tattr3 = tattr1.compose(tattr2)
  73. assert tattr3.get_num_on_stages() == 0
  74. assert tattr3.get_num_off_stages() == 0
  75. assert tattr3.has_all_off()
  76. def test_textureattrib_implicit_sort():
  77. # Tests that two TextureStages with same sort retain insertion order.
  78. tattr1 = core.TextureAttrib.make()
  79. tattr1 = tattr1.add_on_stage(stage1, tex1)
  80. tattr1 = tattr1.add_on_stage(stage2, tex2)
  81. assert tattr1.get_on_stage(0) == stage1
  82. assert tattr1.get_on_stage(1) == stage2
  83. tattr2 = core.TextureAttrib.make()
  84. tattr2 = tattr2.add_on_stage(stage2, tex2)
  85. tattr2 = tattr2.add_on_stage(stage1, tex1)
  86. assert tattr2.get_on_stage(0) == stage2
  87. assert tattr2.get_on_stage(1) == stage1
  88. assert tattr1.compare_to(tattr2) == -tattr2.compare_to(tattr1)
  89. def test_textureattrib_replace():
  90. # Test that replacing a texture doesn't create a unique TextureAttrib.
  91. tattr1 = core.TextureAttrib.make()
  92. tattr1 = tattr1.add_on_stage(stage1, tex1)
  93. tattr2 = tattr1.add_on_stage(stage1, tex1)
  94. assert tattr1.get_num_on_stages() == 1
  95. assert tattr2.get_num_on_stages() == 1
  96. assert tattr1.compare_to(tattr2) == 0
  97. def test_textureattrib_compare():
  98. tattr1 = core.TextureAttrib.make()
  99. tattr2 = core.TextureAttrib.make()
  100. assert tattr1.compare_to(tattr2) == 0
  101. # All-off should not compare equal to empty
  102. tattr2 = core.TextureAttrib.make_all_off()
  103. assert tattr1.compare_to(tattr2) != 0
  104. assert tattr2.compare_to(tattr1) != 0
  105. assert tattr2.compare_to(tattr1) == -tattr1.compare_to(tattr2)
  106. # Empty stage is not the same as a single off stage
  107. tattr1 = core.TextureAttrib.make()
  108. tattr2 = core.TextureAttrib.make()
  109. tattr2 = tattr2.add_off_stage(stage1)
  110. assert tattr1.compare_to(tattr2) != 0
  111. assert tattr2.compare_to(tattr1) != 0
  112. assert tattr2.compare_to(tattr1) == -tattr1.compare_to(tattr2)
  113. # All-off stage is not the same as a single off stage
  114. tattr1 = core.TextureAttrib.make_all_off()
  115. tattr2 = core.TextureAttrib.make()
  116. tattr2 = tattr2.add_off_stage(stage1)
  117. assert tattr1.compare_to(tattr2) != 0
  118. assert tattr2.compare_to(tattr1) != 0
  119. assert tattr2.compare_to(tattr1) == -tattr1.compare_to(tattr2)
  120. # Different off stages are non-equal
  121. tattr1 = core.TextureAttrib.make_all_off()
  122. tattr1 = tattr2.add_off_stage(stage1)
  123. tattr2 = core.TextureAttrib.make()
  124. tattr2 = tattr2.add_off_stage(stage2)
  125. assert tattr1.compare_to(tattr2) != 0
  126. assert tattr2.compare_to(tattr1) != 0
  127. assert tattr2.compare_to(tattr1) == -tattr1.compare_to(tattr2)
  128. # If both have a different texture, but same stage, they are not equal
  129. tattr1 = core.TextureAttrib.make()
  130. tattr1 = tattr1.add_on_stage(stage1, tex1)
  131. tattr2 = core.TextureAttrib.make()
  132. tattr2 = tattr2.add_on_stage(stage1, tex2)
  133. assert tattr1.compare_to(tattr2) != 0
  134. assert tattr2.compare_to(tattr1) != 0
  135. assert tattr2.compare_to(tattr1) == -tattr1.compare_to(tattr2)
  136. # If both have the same texture, but different stage, they are not equal
  137. tattr1 = core.TextureAttrib.make()
  138. tattr1 = tattr1.add_on_stage(stage1, tex1)
  139. tattr2 = core.TextureAttrib.make()
  140. tattr2 = tattr2.add_on_stage(stage2, tex2)
  141. assert tattr1.compare_to(tattr2) != 0
  142. assert tattr2.compare_to(tattr1) != 0
  143. assert tattr2.compare_to(tattr1) == -tattr1.compare_to(tattr2)
  144. # If both have the same texture and stage, they are equal
  145. tattr1 = core.TextureAttrib.make()
  146. tattr1 = tattr1.add_on_stage(stage1, tex1)
  147. tattr2 = core.TextureAttrib.make()
  148. tattr2 = tattr2.add_on_stage(stage1, tex1)
  149. assert tattr1.compare_to(tattr2) == 0
  150. assert tattr2.compare_to(tattr1) == 0
  151. # Adding an extra texture makes it unequal
  152. tattr2 = tattr2.add_on_stage(stage2, tex2)
  153. assert tattr1.compare_to(tattr2) != 0
  154. assert tattr2.compare_to(tattr1) != 0
  155. assert tattr2.compare_to(tattr1) == -tattr1.compare_to(tattr2)
  156. # Different textures altogether is of course unequal
  157. tattr1 = core.TextureAttrib.make()
  158. tattr1 = tattr1.add_on_stage(stage2, tex2)
  159. tattr2 = core.TextureAttrib.make()
  160. tattr2 = tattr2.add_on_stage(stage1, tex1)
  161. assert tattr1.compare_to(tattr2) != 0
  162. assert tattr2.compare_to(tattr1) != 0
  163. assert tattr2.compare_to(tattr1) == -tattr1.compare_to(tattr2)