Search in sources :

Example 6 with Uniform

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

the class StaticPassLightingLogic method updateLightListUniforms.

private void updateLightListUniforms(Matrix4f viewMatrix, Shader shader, LightList lights) {
    Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
    ambientColor.setValue(VarType.Vector4, getAmbientColor(lights, true, ambientLightColor));
    Uniform lightData = shader.getUniform("g_LightData");
    int totalSize = tempDirLights.size() * 2 + tempPointLights.size() * 2 + tempSpotLights.size() * 3;
    lightData.setVector4Length(totalSize);
    int index = 0;
    for (DirectionalLight light : tempDirLights) {
        ColorRGBA color = light.getColor();
        tempDirection.set(light.getDirection());
        transformDirection(viewMatrix, tempDirection);
        lightData.setVector4InArray(color.r, color.g, color.b, 1f, index++);
        lightData.setVector4InArray(tempDirection.x, tempDirection.y, tempDirection.z, 1f, index++);
    }
    for (PointLight light : tempPointLights) {
        ColorRGBA color = light.getColor();
        tempPosition.set(light.getPosition());
        float invRadius = light.getInvRadius();
        transformPosition(viewMatrix, tempPosition);
        lightData.setVector4InArray(color.r, color.g, color.b, 1f, index++);
        lightData.setVector4InArray(tempPosition.x, tempPosition.y, tempPosition.z, invRadius, index++);
    }
    for (SpotLight light : tempSpotLights) {
        ColorRGBA color = light.getColor();
        Vector3f pos = light.getPosition();
        Vector3f dir = light.getDirection();
        tempPosition.set(light.getPosition());
        tempDirection.set(light.getDirection());
        transformPosition(viewMatrix, tempPosition);
        transformDirection(viewMatrix, tempDirection);
        float invRange = light.getInvSpotRange();
        float spotAngleCos = light.getPackedAngleCos();
        lightData.setVector4InArray(color.r, color.g, color.b, 1f, index++);
        lightData.setVector4InArray(tempPosition.x, tempPosition.y, tempPosition.z, invRange, index++);
        lightData.setVector4InArray(tempDirection.x, tempDirection.y, tempDirection.z, spotAngleCos, index++);
    }
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) Uniform(com.jme3.shader.Uniform) PointLight(com.jme3.light.PointLight) SpotLight(com.jme3.light.SpotLight)

Example 7 with Uniform

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

the class Material method applyOverrides.

private int applyOverrides(Renderer renderer, Shader shader, SafeArrayList<MatParamOverride> overrides, int unit) {
    for (MatParamOverride override : overrides.getArray()) {
        VarType type = override.getVarType();
        MatParam paramDef = def.getMaterialParam(override.getName());
        if (paramDef == null || paramDef.getVarType() != type || !override.isEnabled()) {
            continue;
        }
        Uniform uniform = shader.getUniform(override.getPrefixedName());
        if (override.getValue() != null) {
            if (type.isTextureType()) {
                renderer.setTexture(unit, (Texture) override.getValue());
                uniform.setValue(VarType.Int, unit);
                unit++;
            } else {
                uniform.setValue(type, override.getValue());
            }
        } else {
            uniform.clearValue();
        }
    }
    return unit;
}
Also used : Uniform(com.jme3.shader.Uniform) VarType(com.jme3.shader.VarType)

Example 8 with Uniform

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

the class Material method updateShaderMaterialParameters.

private int updateShaderMaterialParameters(Renderer renderer, Shader shader, SafeArrayList<MatParamOverride> worldOverrides, SafeArrayList<MatParamOverride> forcedOverrides) {
    int unit = 0;
    if (worldOverrides != null) {
        unit = applyOverrides(renderer, shader, worldOverrides, unit);
    }
    if (forcedOverrides != null) {
        unit = applyOverrides(renderer, shader, forcedOverrides, unit);
    }
    for (int i = 0; i < paramValues.size(); i++) {
        MatParam param = paramValues.getValue(i);
        VarType type = param.getVarType();
        Uniform uniform = shader.getUniform(param.getPrefixedName());
        if (uniform.isSetByCurrentMaterial()) {
            continue;
        }
        if (type.isTextureType()) {
            renderer.setTexture(unit, (Texture) param.getValue());
            uniform.setValue(VarType.Int, unit);
            unit++;
        } else {
            uniform.setValue(type, param.getValue());
        }
    }
    //TODO HACKY HACK remove this when texture unit is handled by the uniform.
    return unit;
}
Also used : Uniform(com.jme3.shader.Uniform) VarType(com.jme3.shader.VarType)

Example 9 with Uniform

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

the class Material method resetUniformsNotSetByCurrent.

private void resetUniformsNotSetByCurrent(Shader shader) {
    ListMap<String, Uniform> uniforms = shader.getUniformMap();
    int size = uniforms.size();
    for (int i = 0; i < size; i++) {
        Uniform u = uniforms.getValue(i);
        if (!u.isSetByCurrentMaterial()) {
            if (u.getName().charAt(0) != 'g') {
                // Don't reset world globals!
                // The benefits gained from this are very minimal
                // and cause lots of matrix -> FloatBuffer conversions.
                u.clearValue();
            }
        }
    }
}
Also used : Uniform(com.jme3.shader.Uniform)

Example 10 with Uniform

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

the class Material method clearUniformsSetByCurrent.

private void clearUniformsSetByCurrent(Shader shader) {
    ListMap<String, Uniform> uniforms = shader.getUniformMap();
    int size = uniforms.size();
    for (int i = 0; i < size; i++) {
        Uniform u = uniforms.getValue(i);
        u.clearSetByCurrentMaterial();
    }
}
Also used : Uniform(com.jme3.shader.Uniform)

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