|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2026 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 | +package org.jmonkeyengine.screenshottests.light.pbr; |
| 33 | + |
| 34 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 35 | + |
| 36 | +import com.jme3.asset.AssetManager; |
| 37 | +import com.jme3.material.Material; |
| 38 | +import com.jme3.material.RenderState; |
| 39 | +import com.jme3.math.ColorRGBA; |
| 40 | +import com.jme3.math.Vector3f; |
| 41 | +import com.jme3.renderer.Camera; |
| 42 | +import com.jme3.renderer.RenderManager; |
| 43 | +import com.jme3.renderer.Renderer; |
| 44 | +import com.jme3.scene.Geometry; |
| 45 | +import com.jme3.scene.VertexBuffer; |
| 46 | +import com.jme3.scene.shape.Quad; |
| 47 | +import com.jme3.system.AppSettings; |
| 48 | +import com.jme3.texture.FrameBuffer; |
| 49 | +import com.jme3.texture.Image; |
| 50 | +import com.jme3.texture.Texture; |
| 51 | +import com.jme3.texture.Texture2D; |
| 52 | +import com.jme3.texture.image.ColorSpace; |
| 53 | +import com.jme3.util.BufferUtils; |
| 54 | +import java.nio.ByteBuffer; |
| 55 | +import java.util.concurrent.CompletableFuture; |
| 56 | +import java.util.concurrent.CountDownLatch; |
| 57 | +import java.util.concurrent.TimeUnit; |
| 58 | +import org.jmonkeyengine.screenshottests.testframework.TestContainingApp; |
| 59 | +import org.jmonkeyengine.screenshottests.testframework.desktop.DesktopRunner; |
| 60 | +import org.junit.jupiter.api.Tag; |
| 61 | +import org.junit.jupiter.api.Test; |
| 62 | + |
| 63 | +/** Compiles and renders exposure variants on both desktop and ANGLE CI jobs. */ |
| 64 | +@Tag("integration") |
| 65 | +public class TestPbrSunExposure { |
| 66 | + |
| 67 | + @Test |
| 68 | + public void sunlightInputsRenderExpectedExposure() throws Exception { |
| 69 | + CompletableFuture<Void> result = new CompletableFuture<>(); |
| 70 | + CountDownLatch finished = new CountDownLatch(1); |
| 71 | + final TestContainingApp app = new TestContainingApp() { |
| 72 | + @Override |
| 73 | + public void simpleInitApp() { |
| 74 | + try { |
| 75 | + verifyExposure(renderManager, assetManager); |
| 76 | + result.complete(null); |
| 77 | + } catch (Throwable failure) { |
| 78 | + result.completeExceptionally(failure); |
| 79 | + } finally { |
| 80 | + finished.countDown(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void handleError(String message, Throwable failure) { |
| 86 | + result.completeExceptionally(failure == null ? new AssertionError(message) : failure); |
| 87 | + stop(); |
| 88 | + finished.countDown(); |
| 89 | + } |
| 90 | + }; |
| 91 | + AppSettings settings = new AppSettings(true); |
| 92 | + settings.setResolution(16, 16); |
| 93 | + settings.setAudioRenderer(null); |
| 94 | + settings.setUseInput(false); |
| 95 | + settings.setRenderer(System.getProperty("jme.screenshot.renderer", AppSettings.LWJGL_OPENGL45)); |
| 96 | + app.setSettings(settings); |
| 97 | + app.setShowSettings(false); |
| 98 | + new DesktopRunner().runApplicationUntilScenarioCompletes(app, finished); |
| 99 | + result.get(1, TimeUnit.SECONDS); |
| 100 | + } |
| 101 | + |
| 102 | + static void verifyExposure(RenderManager manager, AssetManager assets) { |
| 103 | + Renderer renderer = manager.getRenderer(); |
| 104 | + renderer.setMainFrameBufferSrgb(false); |
| 105 | + renderer.setLinearizeSrgbImages(false); |
| 106 | + Camera camera = new Camera(16, 16); |
| 107 | + camera.setParallelProjection(true); |
| 108 | + camera.setFrustum(0.1f, 10, -1, 1, 1, -1); |
| 109 | + camera.setLocation(new Vector3f(0, 0, 2)); |
| 110 | + camera.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y); |
| 111 | + manager.setCamera(camera, false); |
| 112 | + FrameBuffer target = new FrameBuffer(16, 16, 1); |
| 113 | + target.setColorBuffer(Image.Format.RGBA8); |
| 114 | + target.setDepthBuffer(Image.Format.Depth24); |
| 115 | + try { |
| 116 | + renderer.setFrameBuffer(target); |
| 117 | + for (String variant : new String[]{"static", "vertex", "map", "parallax", "no-tangents", |
| 118 | + "module-map", "module-control"}) { |
| 119 | + boolean module = variant.startsWith("module"); |
| 120 | + Material material = new Material(assets, module |
| 121 | + ? "TestMatDefs/PbrSunExposureModule.j3md" : "Common/MatDefs/Light/PBRLighting.j3md"); |
| 122 | + if (!module) { |
| 123 | + material.setInt("DebugValuesMode", 6); |
| 124 | + material.setBoolean("UseSpecularAA", false); |
| 125 | + } |
| 126 | + material.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off); |
| 127 | + Geometry geometry = new Geometry("exposure", new Quad(2, 2)); |
| 128 | + geometry.move(-1, -1, 0); |
| 129 | + geometry.setMaterial(material); |
| 130 | + if (variant.equals("static")) { |
| 131 | + material.setFloat("StaticSunIntensity", 0.25f); |
| 132 | + } else if (variant.equals("vertex")) { |
| 133 | + material.setBoolean("UseVertexColorsAsSunIntensity", true); |
| 134 | + geometry.getMesh().setBuffer(VertexBuffer.Type.Color, 4, new float[]{ |
| 135 | + 0.25f, 0, 0, 1, 0.25f, 0, 0, 1, 0.25f, 0, 0, 1, 0.25f, 0, 0, 1}); |
| 136 | + } else if (!variant.equals("module-control")) { |
| 137 | + material.setTexture("SunLightExposureMap", exposureMap()); |
| 138 | + } |
| 139 | + if (variant.equals("parallax") || variant.equals("no-tangents")) { |
| 140 | + material.setTexture("ParallaxMap", exposureMap()); |
| 141 | + material.setFloat("ParallaxHeight", 0); |
| 142 | + if (variant.equals("parallax")) { |
| 143 | + geometry.getMesh().setBuffer(VertexBuffer.Type.Tangent, 4, new float[]{ |
| 144 | + 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1}); |
| 145 | + } |
| 146 | + } |
| 147 | + renderer.setBackgroundColor(ColorRGBA.Black); |
| 148 | + renderer.clearBuffers(true, true, true); |
| 149 | + geometry.updateGeometricState(); |
| 150 | + manager.renderGeometry(geometry); |
| 151 | + ByteBuffer pixels = BufferUtils.createByteBuffer(16 * 16 * 4); |
| 152 | + renderer.readFrameBufferWithFormat(target, pixels, Image.Format.RGBA8); |
| 153 | + int left = variant.equals("module-control") ? 255 : 64; |
| 154 | + int right = variant.equals("static") || variant.equals("vertex") ? 64 |
| 155 | + : variant.equals("module-control") ? 255 : 192; |
| 156 | + assertPixel(pixels, 4, left, variant); |
| 157 | + assertPixel(pixels, 12, right, variant); |
| 158 | + } |
| 159 | + } finally { |
| 160 | + renderer.setFrameBuffer(null); |
| 161 | + target.dispose(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private static Texture2D exposureMap() { |
| 166 | + ByteBuffer data = BufferUtils.createByteBuffer(8); |
| 167 | + data.put(new byte[]{64, 64, 64, (byte) 255, (byte) 192, (byte) 192, (byte) 192, (byte) 255}).flip(); |
| 168 | + Texture2D texture = new Texture2D(new Image(Image.Format.RGBA8, 2, 1, data, ColorSpace.Linear)); |
| 169 | + texture.setMinFilter(Texture.MinFilter.NearestNoMipMaps); |
| 170 | + texture.setMagFilter(Texture.MagFilter.Nearest); |
| 171 | + return texture; |
| 172 | + } |
| 173 | + |
| 174 | + private static void assertPixel(ByteBuffer pixels, int x, int expected, String variant) { |
| 175 | + int offset = (8 * 16 + x) * 4; |
| 176 | + for (int channel = 0; channel < 3; channel++) { |
| 177 | + assertEquals(expected, pixels.get(offset + channel) & 255, 1, variant + " channel " + channel); |
| 178 | + } |
| 179 | + assertEquals(255, pixels.get(offset + 3) & 255, variant + " alpha"); |
| 180 | + } |
| 181 | +} |
0 commit comments