1+ /*
2+ * Copyright (c) 2023 jMonkeyEngine
3+ * All rights reserved.
4+ *
5+ * Redistribution and use in source and binary forms, with or without
6+ * modification, are permitted provided that the following conditions are
7+ * met:
8+ *
9+ * * Redistributions of source code must retain the above copyright
10+ * notice, this list of conditions and the following disclaimer.
11+ *
12+ * * Redistributions in binary form must reproduce the above copyright
13+ * notice, this list of conditions and the following disclaimer in the
14+ * documentation and/or other materials provided with the distribution.
15+ *
16+ * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+ * may be used to endorse or promote products derived from this software
18+ * without specific prior written permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ */
32+
33+ package com .jme3 .tools ;
34+
35+ import static org .junit .Assert .assertArrayEquals ;
36+
37+ import org .junit .Test ;
38+
39+ import com .jme3 .asset .AssetManager ;
40+ import com .jme3 .scene .Geometry ;
41+ import com .jme3 .scene .Mesh ;
42+ import com .jme3 .scene .Node ;
43+ import com .jme3 .scene .Spatial ;
44+ import com .jme3 .scene .VertexBuffer ;
45+ import com .jme3 .scene .shape .Sphere ;
46+ import com .jme3 .system .TestUtil ;
47+
48+ import jme3tools .optimize .LodGenerator ;
49+
50+ /**
51+ * Tests the result of the LodGenerator.
52+ *
53+ * @author Melvyn Linke
54+ */
55+ public class LodGeneratorTest {
56+ AssetManager assetManager = TestUtil .createAssetManager ();
57+
58+ float [] REDUCTION_VALUES = { 0.5f , 0.55f , 0.6f , 0.65f , 0.7f , 0.75f , 0.80f };
59+
60+ /**
61+ * Tests the construction of the LodGenerator.
62+ */
63+ @ Test
64+ public void testInit () {
65+ LodGenerator lod = new LodGenerator (sphere ());
66+ assert true ;
67+ }
68+
69+ /**
70+ * Returns a List of the sizes of the VertexBuff.
71+ */
72+ private int [] getBufferSizes (VertexBuffer [] buffers ) {
73+ int [] result = new int [buffers .length ];
74+
75+ for (int i = 0 ; i < buffers .length ; i ++) {
76+ result [i ] = buffers [i ].getNumElements ();
77+ }
78+
79+ return result ;
80+ }
81+
82+ /**
83+ * Tests the LodGenerator with proportional reduction on a sphere(see sphere()).
84+ */
85+ @ Test
86+ public void testSphereReductionProportional () {
87+ LodGenerator lod = new LodGenerator (sphere ());
88+ VertexBuffer [] buffer = lod .computeLods (LodGenerator .TriangleReductionMethod .PROPORTIONAL ,
89+ REDUCTION_VALUES );
90+
91+ int [] expected = { 240 , 120 , 108 , 96 , 84 , 72 , 60 , 48 };
92+ int [] actual = getBufferSizes (buffer );
93+
94+ assertArrayEquals (expected , actual );
95+ }
96+
97+ /**
98+ * Tests the LodGenerator with collapse cost reduction on a sphere(see sphere()).
99+ */
100+ @ Test
101+ public void testSphereReductionCollapsCost () {
102+ LodGenerator lod = new LodGenerator (sphere ());
103+ VertexBuffer [] buffer = lod .computeLods (LodGenerator .TriangleReductionMethod .COLLAPSE_COST ,
104+ REDUCTION_VALUES );
105+
106+ int [] expected = { 240 , 6 , 2 , 1 };
107+ int [] actual = getBufferSizes (buffer );
108+ assert buffer != null ;
109+ assertArrayEquals (expected , actual );
110+
111+ }
112+
113+ /**
114+ * Returns the mesh of a node.
115+ */
116+ private Mesh getMesh (Node node ) {
117+ Mesh m = null ;
118+ for (Spatial spatial : node .getChildren ()) {
119+ if (spatial instanceof Geometry ) {
120+ m = ((Geometry ) spatial ).getMesh ();
121+ if (m .getVertexCount () == 5108 ) {
122+
123+ }
124+
125+ }
126+ }
127+ return m ;
128+ }
129+
130+ /**
131+ * Returns the Monkey mesh used in the TestLodGeneration stresstest. Note: Doesn't work durring gradle
132+ * build.
133+ */
134+ private Mesh monkey () {
135+ Node model = (Node ) assetManager .loadModel ("Models/Jaime/Jaime.j3o" );
136+ return getMesh (model );
137+ }
138+
139+ /**
140+ * Returns a 12x12 Sphere mesh.
141+ */
142+ private Mesh sphere () {
143+ return new Sphere (12 , 12 , 1 , false , false );
144+ }
145+
146+ /**
147+ * Tests the LodGenerator with constnat reduction on a monkey(see monkey()).
148+ */
149+ // @Test
150+ public void testMonkeyReductionConstant () {
151+
152+ LodGenerator lod = new LodGenerator (monkey ());
153+ VertexBuffer [] buffer = lod .computeLods (LodGenerator .TriangleReductionMethod .CONSTANT ,
154+ REDUCTION_VALUES );
155+
156+ int [] expected = { 5108 };
157+ int [] actual = getBufferSizes (buffer );
158+
159+ assertArrayEquals (expected , actual );
160+ }
161+
162+ /**
163+ * Tests the LodGenerator with proportional reduction on a sphere(see sphere()).
164+ */
165+ // @Test
166+ public void testMonkeyReductionProportional () {
167+
168+ LodGenerator lod = new LodGenerator (monkey ());
169+ VertexBuffer [] buffer = lod .computeLods (LodGenerator .TriangleReductionMethod .PROPORTIONAL ,
170+ REDUCTION_VALUES );
171+
172+ int [] expected = { 5108 , 2553 , 2298 , 2043 , 1787 , 1531 , 1276 , 1021 };
173+ int [] actual = getBufferSizes (buffer );
174+
175+ assertArrayEquals (expected , actual );
176+ }
177+
178+ /**
179+ * Tests the LodGenerator with collapse cost reduction on a monkey(see monkey()).
180+ */
181+ // @Test
182+ public void testMonkeyReductionCollapsCost () {
183+ LodGenerator lod = new LodGenerator (monkey ());
184+ VertexBuffer [] buffer = lod .computeLods (LodGenerator .TriangleReductionMethod .COLLAPSE_COST ,
185+ REDUCTION_VALUES );
186+
187+ int [] expected = { 5108 , 16 };
188+ int [] actual = getBufferSizes (buffer );
189+
190+ assertArrayEquals (expected , actual );
191+ }
192+ }
0 commit comments