Skip to content

Commit 9c3d363

Browse files
authored
Fix #1882 (J3MLoader always generates mips ignoring MinFilter) (#1884)
* Get texture mips generation flag from MinFilter specified in j3m file. * Update copyright date. * Fix J3MLoaderTest failing. * Fix TestMaterialWrite failing. * Update copyright date. * Use Trilinear if no min filter is specified in j3m file. * Add copyright note in J3MOutputCapsule. * Fix J3MLoaderTest failing.
1 parent f92a73f commit 9c3d363

File tree

4 files changed

+58
-22
lines changed

4 files changed

+58
-22
lines changed

jme3-core/src/plugins/java/com/jme3/material/plugins/J3MLoader.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2022 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -222,6 +222,7 @@ private Texture parseTextureType(final VarType type, final String value) {
222222
// If there is only one token on the value, it must be the path to the texture.
223223
if (textureValues.size() == 1) {
224224
textureKey = new TextureKey(textureValues.get(0), false);
225+
textureKey.setGenerateMips(true);
225226
} else {
226227
String texturePath = value.trim();
227228

@@ -256,6 +257,8 @@ private Texture parseTextureType(final VarType type, final String value) {
256257
textureKey = new TextureKey(textureValues.get(textureValues.size() - 1), false);
257258
}
258259

260+
textureKey.setGenerateMips(true);
261+
259262
// Apply texture options to the texture key
260263
if (!textureOptionValues.isEmpty()) {
261264
for (final TextureOptionValue textureOptionValue : textureOptionValues) {
@@ -276,8 +279,6 @@ private Texture parseTextureType(final VarType type, final String value) {
276279
break;
277280
}
278281

279-
textureKey.setGenerateMips(true);
280-
281282
Texture texture;
282283

283284
try {
@@ -861,6 +862,13 @@ private enum TextureOption {
861862
* Applies a {@link com.jme3.texture.Texture.MinFilter} to the texture.
862863
*/
863864
Min {
865+
866+
@Override
867+
public void applyToTextureKey(final String option, final TextureKey textureKey) {
868+
Texture.MinFilter minFilter = Texture.MinFilter.valueOf(option);
869+
textureKey.setGenerateMips(minFilter.usesMipMapLevels());
870+
}
871+
864872
@Override
865873
public void applyToTexture(final String option, final Texture texture) {
866874
texture.setMinFilter(Texture.MinFilter.valueOf(option));

jme3-core/src/test/java/com/jme3/material/plugins/J3MLoaderTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ public void oldStyleTextureParameters_shouldBeSupported() throws Exception {
8787
final Texture textureOldStyle = Mockito.mock(Texture.class);
8888
final Texture textureOldStyleUsingQuotes = Mockito.mock(Texture.class);
8989

90-
final TextureKey textureKeyUsingQuotes = setupMockForTexture("OldStyleUsingQuotes", "old style using quotes/texture.png", true, textureOldStyleUsingQuotes);
91-
final TextureKey textureKeyOldStyle = setupMockForTexture("OldStyle", "old style/texture.png", true, textureOldStyle);
90+
final TextureKey textureKeyUsingQuotes = setupMockForTexture("OldStyleUsingQuotes", "old style using quotes/texture.png", true, true, textureOldStyleUsingQuotes);
91+
final TextureKey textureKeyOldStyle = setupMockForTexture("OldStyle", "old style/texture.png", true, true, textureOldStyle);
9292

9393
j3MLoader.load(assetInfo);
9494

@@ -111,14 +111,14 @@ public void newStyleTextureParameters_shouldBeSupported() throws Exception {
111111
final Texture textureCombined = Mockito.mock(Texture.class);
112112
final Texture textureLooksLikeOldStyle = Mockito.mock(Texture.class);
113113

114-
final TextureKey textureKeyNoParameters = setupMockForTexture("Empty", "empty.png", false, textureNoParameters);
115-
final TextureKey textureKeyFlip = setupMockForTexture("Flip", "flip.png", true, textureFlip);
116-
setupMockForTexture("Repeat", "repeat.png", false, textureRepeat);
117-
setupMockForTexture("RepeatAxis", "repeat-axis.png", false, textureRepeatAxis);
118-
setupMockForTexture("Min", "min.png", false, textureMin);
119-
setupMockForTexture("Mag", "mag.png", false, textureMag);
120-
setupMockForTexture("Combined", "combined.png", true, textureCombined);
121-
setupMockForTexture("LooksLikeOldStyle", "oldstyle.png", true, textureLooksLikeOldStyle);
114+
final TextureKey textureKeyNoParameters = setupMockForTexture("Empty", "empty.png", false, true, textureNoParameters);
115+
final TextureKey textureKeyFlip = setupMockForTexture("Flip", "flip.png", true, true, textureFlip);
116+
setupMockForTexture("Repeat", "repeat.png", false, true, textureRepeat);
117+
setupMockForTexture("RepeatAxis", "repeat-axis.png", false, true, textureRepeatAxis);
118+
setupMockForTexture("Min", "min.png", false, true, textureMin);
119+
setupMockForTexture("Mag", "mag.png", false, true, textureMag);
120+
setupMockForTexture("Combined", "combined.png", true, false, textureCombined);
121+
setupMockForTexture("LooksLikeOldStyle", "oldstyle.png", true, true, textureLooksLikeOldStyle);
122122

123123
j3MLoader.load(assetInfo);
124124

@@ -135,11 +135,11 @@ public void newStyleTextureParameters_shouldBeSupported() throws Exception {
135135
verify(textureCombined).setWrap(Texture.WrapMode.Repeat);
136136
}
137137

138-
private TextureKey setupMockForTexture(final String paramName, final String path, final boolean flipY, final Texture texture) {
138+
private TextureKey setupMockForTexture(final String paramName, final String path, final boolean flipY, boolean generateMips, final Texture texture) {
139139
when(materialDef.getMaterialParam(paramName)).thenReturn(new MatParamTexture(VarType.Texture2D, paramName, texture, null));
140140

141141
final TextureKey textureKey = new TextureKey(path, flipY);
142-
textureKey.setGenerateMips(true);
142+
textureKey.setGenerateMips(generateMips);
143143

144144
when(assetManager.loadTexture(textureKey)).thenReturn(texture);
145145

jme3-plugins/src/main/java/com/jme3/material/plugin/export/material/J3MOutputCapsule.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
/*
2+
* Copyright (c) 2009-2022 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+
*/
132
package com.jme3.material.plugin.export.material;
233

334
import com.jme3.asset.TextureKey;
@@ -140,11 +171,7 @@ protected static String formatMatParamTexture(MatParamTexture param) {
140171
ret.append(formatWrapMode(tex, Texture.WrapAxis.R));
141172

142173
//Min and Mag filter
143-
Texture.MinFilter def = Texture.MinFilter.BilinearNoMipMaps;
144-
if (tex.getImage().hasMipmaps() || (key != null && key.isGenerateMips())) {
145-
def = Texture.MinFilter.Trilinear;
146-
}
147-
if (tex.getMinFilter() != def) {
174+
if (tex.getMinFilter() != Texture.MinFilter.Trilinear) {
148175
ret.append("Min").append(tex.getMinFilter().name()).append(" ");
149176
}
150177

jme3-plugins/src/test/java/com/jme3/material/plugin/TestMaterialWrite.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2016 jMonkeyEngine
2+
* Copyright (c) 2009-2022 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -34,6 +34,7 @@
3434
import com.jme3.asset.AssetInfo;
3535
import com.jme3.asset.AssetKey;
3636
import com.jme3.asset.AssetManager;
37+
import com.jme3.asset.TextureKey;
3738
import com.jme3.material.Material;
3839
import com.jme3.material.RenderState;
3940
import com.jme3.material.plugin.export.material.J3MExporter;
@@ -76,7 +77,7 @@ public void testWriteMat() throws Exception {
7677

7778
mat.setFloat("Shininess", 2.5f);
7879

79-
Texture tex = assetManager.loadTexture("Common/Textures/MissingTexture.png");
80+
Texture tex = assetManager.loadTexture(new TextureKey("Common/Textures/MissingTexture.png", true));
8081
tex.setMagFilter(Texture.MagFilter.Nearest);
8182
tex.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
8283
tex.setWrap(Texture.WrapAxis.S, Texture.WrapMode.Repeat);

0 commit comments

Comments
 (0)