Search in sources :

Example 1 with Uniform

use of com.jme3.shader.Uniform in project jmonkeyengine by jMonkeyEngine.

the class MaterialMatParamTest method outUniforms.

private void outUniforms(Uniform... uniforms) {
    if (!evaluated) {
        evaluateTechniqueDef();
    }
    HashSet<Uniform> actualUniforms = new HashSet<>();
    for (Uniform uniform : usedShader.getUniformMap().values()) {
        if (uniform.getName().startsWith("m_") && !IGNORED_UNIFORMS.contains(uniform.getName())) {
            actualUniforms.add(uniform);
        }
    }
    HashSet<Uniform> expectedUniforms = new HashSet<>(Arrays.asList(uniforms));
    if (!expectedUniforms.equals(actualUniforms)) {
        Assert.fail("Uniform lists must match: " + expectedUniforms + " != " + actualUniforms);
    }
}
Also used : Uniform(com.jme3.shader.Uniform) HashSet(java.util.HashSet)

Example 2 with Uniform

use of com.jme3.shader.Uniform in project jmonkeyengine by jMonkeyEngine.

the class MaterialMatParamTest method testRemoveTexture.

@Test
public void testRemoveTexture() {
    material("Common/MatDefs/Light/Lighting.j3md");
    Texture2D tex = new Texture2D(128, 128, Format.RGBA8);
    reset();
    inputMpo(mpoTexture2D("DiffuseMap", tex));
    outDefines(def("DIFFUSEMAP", VarType.Texture2D, tex));
    outUniforms(uniform("DiffuseMap", VarType.Int, 0));
    outTextures(tex);
    reset();
    geometry.clearMatParamOverrides();
    root.updateGeometricState();
    outDefines();
    outUniforms();
    outTextures();
}
Also used : Texture2D(com.jme3.texture.Texture2D) Test(org.junit.Test)

Example 3 with Uniform

use of com.jme3.shader.Uniform in project jmonkeyengine by jMonkeyEngine.

the class MaterialMatParamTest method testRemoveTextureOverride.

@Test
public void testRemoveTextureOverride() {
    material("Common/MatDefs/Light/Lighting.j3md");
    Texture2D tex1 = new Texture2D(128, 128, Format.RGBA8);
    Texture2D tex2 = new Texture2D(128, 128, Format.RGBA8);
    reset();
    inputMp(mpoTexture2D("DiffuseMap", tex1));
    outDefines(def("DIFFUSEMAP", VarType.Texture2D, tex1));
    outUniforms(uniform("DiffuseMap", VarType.Int, 0));
    outTextures(tex1);
    reset();
    inputMpo(mpoTexture2D("DiffuseMap", tex2));
    outDefines(def("DIFFUSEMAP", VarType.Texture2D, tex2));
    outUniforms(uniform("DiffuseMap", VarType.Int, 0));
    outTextures(tex2);
    reset();
    geometry.clearMatParamOverrides();
    root.updateGeometricState();
    outDefines(def("DIFFUSEMAP", VarType.Texture2D, tex1));
    outUniforms(uniform("DiffuseMap", VarType.Int, 0));
    outTextures(tex1);
}
Also used : Texture2D(com.jme3.texture.Texture2D) Test(org.junit.Test)

Example 4 with Uniform

use of com.jme3.shader.Uniform in project jmonkeyengine by jMonkeyEngine.

the class GLRenderer method resetUniformLocations.

protected void resetUniformLocations(Shader shader) {
    ListMap<String, Uniform> uniforms = shader.getUniformMap();
    for (int i = 0; i < uniforms.size(); i++) {
        Uniform uniform = uniforms.getValue(i);
        // e.g check location again
        uniform.reset();
    }
}
Also used : Uniform(com.jme3.shader.Uniform)

Example 5 with Uniform

use of com.jme3.shader.Uniform in project jmonkeyengine by jMonkeyEngine.

the class MultiPassLightingLogic method render.

@Override
public void render(RenderManager renderManager, Shader shader, Geometry geometry, LightList lights, int lastTexUnit) {
    Renderer r = renderManager.getRenderer();
    Uniform lightDir = shader.getUniform("g_LightDirection");
    Uniform lightColor = shader.getUniform("g_LightColor");
    Uniform lightPos = shader.getUniform("g_LightPosition");
    Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
    boolean isFirstLight = true;
    boolean isSecondLight = false;
    getAmbientColor(lights, false, ambientLightColor);
    for (int i = 0; i < lights.size(); i++) {
        Light l = lights.get(i);
        if (l instanceof AmbientLight) {
            continue;
        }
        if (isFirstLight) {
            // set ambient color for first light only
            ambientColor.setValue(VarType.Vector4, ambientLightColor);
            isFirstLight = false;
            isSecondLight = true;
        } else if (isSecondLight) {
            ambientColor.setValue(VarType.Vector4, ColorRGBA.Black);
            // apply additive blending for 2nd and future lights
            r.applyRenderState(ADDITIVE_LIGHT);
            isSecondLight = false;
        }
        TempVars vars = TempVars.get();
        Quaternion tmpLightDirection = vars.quat1;
        Quaternion tmpLightPosition = vars.quat2;
        ColorRGBA tmpLightColor = vars.color;
        Vector4f tmpVec = vars.vect4f1;
        ColorRGBA color = l.getColor();
        tmpLightColor.set(color);
        tmpLightColor.a = l.getType().getId();
        lightColor.setValue(VarType.Vector4, tmpLightColor);
        switch(l.getType()) {
            case Directional:
                DirectionalLight dl = (DirectionalLight) l;
                Vector3f dir = dl.getDirection();
                //FIXME : there is an inconstency here due to backward
                //compatibility of the lighting shader.
                //The directional light direction is passed in the
                //LightPosition uniform. The lighting shader needs to be
                //reworked though in order to fix this.
                tmpLightPosition.set(dir.getX(), dir.getY(), dir.getZ(), -1);
                lightPos.setValue(VarType.Vector4, tmpLightPosition);
                tmpLightDirection.set(0, 0, 0, 0);
                lightDir.setValue(VarType.Vector4, tmpLightDirection);
                break;
            case Point:
                PointLight pl = (PointLight) l;
                Vector3f pos = pl.getPosition();
                float invRadius = pl.getInvRadius();
                tmpLightPosition.set(pos.getX(), pos.getY(), pos.getZ(), invRadius);
                lightPos.setValue(VarType.Vector4, tmpLightPosition);
                tmpLightDirection.set(0, 0, 0, 0);
                lightDir.setValue(VarType.Vector4, tmpLightDirection);
                break;
            case Spot:
                SpotLight sl = (SpotLight) l;
                Vector3f pos2 = sl.getPosition();
                Vector3f dir2 = sl.getDirection();
                float invRange = sl.getInvSpotRange();
                float spotAngleCos = sl.getPackedAngleCos();
                tmpLightPosition.set(pos2.getX(), pos2.getY(), pos2.getZ(), invRange);
                lightPos.setValue(VarType.Vector4, tmpLightPosition);
                //We transform the spot direction in view space here to save 5 varying later in the lighting shader
                //one vec4 less and a vec4 that becomes a vec3
                //the downside is that spotAngleCos decoding happens now in the frag shader.
                tmpVec.set(dir2.getX(), dir2.getY(), dir2.getZ(), 0);
                renderManager.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
                tmpLightDirection.set(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), spotAngleCos);
                lightDir.setValue(VarType.Vector4, tmpLightDirection);
                break;
            case Probe:
                break;
            default:
                throw new UnsupportedOperationException("Unknown type of light: " + l.getType());
        }
        vars.release();
        r.setShader(shader);
        renderMeshFromGeometry(r, geometry);
    }
    if (isFirstLight) {
        // Either there are no lights at all, or only ambient lights.
        // Render a dummy "normal light" so we can see the ambient color.
        ambientColor.setValue(VarType.Vector4, getAmbientColor(lights, false, ambientLightColor));
        lightColor.setValue(VarType.Vector4, ColorRGBA.BlackNoAlpha);
        lightPos.setValue(VarType.Vector4, NULL_DIR_LIGHT);
        r.setShader(shader);
        renderMeshFromGeometry(r, geometry);
    }
}
Also used : Quaternion(com.jme3.math.Quaternion) Uniform(com.jme3.shader.Uniform) TempVars(com.jme3.util.TempVars) SpotLight(com.jme3.light.SpotLight) ColorRGBA(com.jme3.math.ColorRGBA) Vector4f(com.jme3.math.Vector4f) DirectionalLight(com.jme3.light.DirectionalLight) SpotLight(com.jme3.light.SpotLight) Light(com.jme3.light.Light) PointLight(com.jme3.light.PointLight) AmbientLight(com.jme3.light.AmbientLight) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) Renderer(com.jme3.renderer.Renderer) PointLight(com.jme3.light.PointLight) AmbientLight(com.jme3.light.AmbientLight)

Aggregations

Uniform (com.jme3.shader.Uniform)10 Vector3f (com.jme3.math.Vector3f)4 Texture2D (com.jme3.texture.Texture2D)4 Test (org.junit.Test)4 DirectionalLight (com.jme3.light.DirectionalLight)3 PointLight (com.jme3.light.PointLight)3 SpotLight (com.jme3.light.SpotLight)3 ColorRGBA (com.jme3.math.ColorRGBA)3 ShaderNodeVariable (com.jme3.shader.ShaderNodeVariable)3 TempVars (com.jme3.util.TempVars)3 Light (com.jme3.light.Light)2 Vector4f (com.jme3.math.Vector4f)2 Renderer (com.jme3.renderer.Renderer)2 VarType (com.jme3.shader.VarType)2 BoundingSphere (com.jme3.bounding.BoundingSphere)1 AmbientLight (com.jme3.light.AmbientLight)1 MatParam (com.jme3.material.MatParam)1 Quaternion (com.jme3.math.Quaternion)1 Caps (com.jme3.renderer.Caps)1 Mesh (com.jme3.scene.Mesh)1