package jme3test.light.pbr;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.UrlLocator;
import com.jme3.environment.EnvironmentCamera;
import com.jme3.environment.LightProbeFactory;
import com.jme3.environment.generation.JobProgressAdapter;
import com.jme3.light.DirectionalLight;
import com.jme3.light.LightProbe;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import com.jme3.system.AppSettings;

/**
 * @author: rvandoosselaer
 */
public class TestIssue1340 extends SimpleApplication {

    int frame;

    public static void main(String[] args) {
        TestIssue1340 testIssue1340 = new TestIssue1340();
        testIssue1340.setSettings(createSettings());
        testIssue1340.start();
    }

    private static AppSettings createSettings() {
        AppSettings settings = new AppSettings(true);
        settings.setRenderer(AppSettings.LWJGL_OPENGL32);
        return settings;
    }

    @Override
    public void simpleInitApp() {
        stateManager.attach(new EnvironmentCamera(32));

        DirectionalLight light = new DirectionalLight(Vector3f.UNIT_Y.negate(), ColorRGBA.White);
        rootNode.addLight(light);

        assetManager.registerLocator("https://github.com/KhronosGroup/glTF-Sample-Models/raw/master/2.0/RiggedFigure/", UrlLocator.class);

        Spatial model = assetManager.loadModel("/glTF-Embedded/RiggedFigure.gltf");
        rootNode.attachChild(model);

        viewPort.setBackgroundColor(ColorRGBA.LightGray);
    }

    @Override
    public void simpleUpdate(float tpf) {
        frame++;
        if (frame == 2) {
            LightProbeFactory.makeProbe(stateManager.getState(EnvironmentCamera.class), rootNode, new JobProgressAdapter<LightProbe>() {
                @Override
                public void done(LightProbe result) {
                    enqueue(() -> rootNode.addLight(result));
                }
            });
        }
    }
}

@@ -22,16 +22,54 @@
 t.pbr;%0A%0A
+import com.jme3.anim.SkinningControl;%0A
 import c
@@ -431,16 +431,44 @@
 ctor3f;%0A
+import com.jme3.scene.Node;%0A
 import c
@@ -538,17 +538,344 @@
 %0A * 
-@author: 
+This test case validates a shader compilation fix with a model using a PBR material in combination with a%0A * SkinningControl. When you run this application and you don't see a RenderException, the test is successful.%0A * For a detailed explanation consult the github issue: https://github.com/jMonkeyEngine/jmonkeyengine/issues/1340%0A * -
 rvan
@@ -949,16 +949,51 @@
  %7B%0A%0A    
+private Spatial model;%0A    private 
 int fram
@@ -1771,24 +1771,16 @@
         
-Spatial 
 model = 
@@ -1851,36 +1851,311 @@
     
-rootNode.attachChild(model);
+SkinningControl skinningControl = getSkinningControl(model);%0A        if (skinningControl == null %7C%7C !skinningControl.isHardwareSkinningPreferred()) %7B%0A            throw new IllegalArgumentException(%22This test case requires a model with a SkinningControl and with Hardware skinning preferred!%22);%0A        %7D
 %0A%0A  
@@ -2566,16 +2566,95 @@
 ue(() -%3E
+ %7B%0A                        rootNode.attachChild(model);%0A                       
  rootNod
@@ -2671,16 +2671,39 @@
 (result)
+;%0A                    %7D
 );%0A     
@@ -2747,10 +2747,561 @@
 %7D%0A    %7D%0A
+%0A    private SkinningControl getSkinningControl(Spatial model) %7B%0A        SkinningControl control = model.getControl(SkinningControl.class);%0A        if (control != null) %7B%0A            return control;%0A        %7D%0A%0A        if (model instanceof Node) %7B%0A            for (Spatial child : ((Node) model).getChildren()) %7B%0A                SkinningControl skinningControl = getSkinningControl(child);%0A                if (skinningControl != null) %7B%0A                    return skinningControl;%0A                %7D%0A            %7D%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A
 %7D%0A

Clean up the test case and add some more context. #1340