Search in sources :

Example 76 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class AnimationFactory method interpolate.

/**
     * Interpolates over the key frames for the given keyFrame array and the given type of transform
     * @param keyFrames the keyFrames array
     * @param type the type of transforms
     */
private void interpolate(Object[] keyFrames, Type type) {
    int i = 0;
    while (i < totalFrames) {
        //fetching the next keyFrame index transform in the array
        int key = getNextKeyFrame(i, keyFrames);
        if (key != -1) {
            //computing the frame span to interpolate over
            int span = key - i;
            //interating over the frames
            for (int j = i; j <= key; j++) {
                // computing interpolation value
                float val = (float) (j - i) / (float) span;
                //interpolationg depending on the transform type
                switch(type) {
                    case Translation:
                        translations[j] = FastMath.interpolateLinear(val, (Vector3f) keyFrames[i], (Vector3f) keyFrames[key]);
                        break;
                    case Rotation:
                        Quaternion rot = new Quaternion();
                        rotations[j] = rot.slerp(((Rotation) keyFrames[i]).rotation, ((Rotation) keyFrames[key]).rotation, val);
                        break;
                    case Scale:
                        scales[j] = FastMath.interpolateLinear(val, (Vector3f) keyFrames[i], (Vector3f) keyFrames[key]);
                        break;
                }
            }
            //jumping to the next keyFrame
            i = key;
        } else {
            //No more key frame, filling the array witht he last transform computed.
            for (int j = i; j < totalFrames; j++) {
                switch(type) {
                    case Translation:
                        translations[j] = ((Vector3f) keyFrames[i]).clone();
                        break;
                    case Rotation:
                        rotations[j] = ((Quaternion) ((Rotation) keyFrames[i]).rotation).clone();
                        break;
                    case Scale:
                        scales[j] = ((Vector3f) keyFrames[i]).clone();
                        break;
                }
            }
            //we're done
            i = totalFrames;
        }
    }
}
Also used : Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f)

Example 77 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class SinglePassAndImageBasedLightingLogic method updateLightListUniforms.

/**
     * Uploads the lights in the light list as two uniform arrays.<br/><br/> *
     * <p>
     * <code>uniform vec4 g_LightColor[numLights];</code><br/> //
     * g_LightColor.rgb is the diffuse/specular color of the light.<br/> //
     * g_Lightcolor.a is the type of light, 0 = Directional, 1 = Point, <br/> //
     * 2 = Spot. <br/> <br/>
     * <code>uniform vec4 g_LightPosition[numLights];</code><br/> //
     * g_LightPosition.xyz is the position of the light (for point lights)<br/>
     * // or the direction of the light (for directional lights).<br/> //
     * g_LightPosition.w is the inverse radius (1/r) of the light (for
     * attenuation) <br/> </p>
     */
protected int updateLightListUniforms(Shader shader, Geometry g, LightList lightList, int numLights, RenderManager rm, int startIndex, int lastTexUnit) {
    if (numLights == 0) {
        // this shader does not do lighting, ignore.
        return 0;
    }
    Uniform lightData = shader.getUniform("g_LightData");
    //8 lights * max 3
    lightData.setVector4Length(numLights * 3);
    Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
    Uniform lightProbeData = shader.getUniform("g_LightProbeData");
    lightProbeData.setVector4Length(1);
    Uniform lightProbeIrrMap = shader.getUniform("g_IrradianceMap");
    Uniform lightProbePemMap = shader.getUniform("g_PrefEnvMap");
    lightProbe = null;
    if (startIndex != 0) {
        // apply additive blending for 2nd and future passes
        rm.getRenderer().applyRenderState(ADDITIVE_LIGHT);
        ambientColor.setValue(VarType.Vector4, ColorRGBA.Black);
    } else {
        lightProbe = extractIndirectLights(lightList, true);
        ambientColor.setValue(VarType.Vector4, ambientLightColor);
    }
    //If there is a lightProbe in the list we force it's render on the first pass
    if (lightProbe != null) {
        BoundingSphere s = (BoundingSphere) lightProbe.getBounds();
        lightProbeData.setVector4InArray(lightProbe.getPosition().x, lightProbe.getPosition().y, lightProbe.getPosition().z, 1f / s.getRadius(), 0);
        //assigning new texture indexes
        int irrUnit = lastTexUnit++;
        int pemUnit = lastTexUnit++;
        rm.getRenderer().setTexture(irrUnit, lightProbe.getIrradianceMap());
        lightProbeIrrMap.setValue(VarType.Int, irrUnit);
        rm.getRenderer().setTexture(pemUnit, lightProbe.getPrefilteredEnvMap());
        lightProbePemMap.setValue(VarType.Int, pemUnit);
    } else {
        //Disable IBL for this pass
        lightProbeData.setVector4InArray(0, 0, 0, -1, 0);
    }
    int lightDataIndex = 0;
    TempVars vars = TempVars.get();
    Vector4f tmpVec = vars.vect4f1;
    int curIndex;
    int endIndex = numLights + startIndex;
    for (curIndex = startIndex; curIndex < endIndex && curIndex < lightList.size(); curIndex++) {
        Light l = lightList.get(curIndex);
        if (l.getType() == Light.Type.Ambient) {
            endIndex++;
            continue;
        }
        ColorRGBA color = l.getColor();
        if (l.getType() != Light.Type.Probe) {
            lightData.setVector4InArray(color.getRed(), color.getGreen(), color.getBlue(), l.getType().getId(), lightDataIndex);
            lightDataIndex++;
        }
        switch(l.getType()) {
            case Directional:
                DirectionalLight dl = (DirectionalLight) l;
                Vector3f dir = dl.getDirection();
                //Data directly sent in view space to avoid a matrix mult for each pixel
                tmpVec.set(dir.getX(), dir.getY(), dir.getZ(), 0.0f);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), -1, lightDataIndex);
                lightDataIndex++;
                //PADDING
                lightData.setVector4InArray(0, 0, 0, 0, lightDataIndex);
                lightDataIndex++;
                break;
            case Point:
                PointLight pl = (PointLight) l;
                Vector3f pos = pl.getPosition();
                float invRadius = pl.getInvRadius();
                tmpVec.set(pos.getX(), pos.getY(), pos.getZ(), 1.0f);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), invRadius, lightDataIndex);
                lightDataIndex++;
                //PADDING
                lightData.setVector4InArray(0, 0, 0, 0, lightDataIndex);
                lightDataIndex++;
                break;
            case Spot:
                SpotLight sl = (SpotLight) l;
                Vector3f pos2 = sl.getPosition();
                Vector3f dir2 = sl.getDirection();
                float invRange = sl.getInvSpotRange();
                float spotAngleCos = sl.getPackedAngleCos();
                tmpVec.set(pos2.getX(), pos2.getY(), pos2.getZ(), 1.0f);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), invRange, lightDataIndex);
                lightDataIndex++;
                tmpVec.set(dir2.getX(), dir2.getY(), dir2.getZ(), 0.0f);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), spotAngleCos, lightDataIndex);
                lightDataIndex++;
                break;
            default:
                throw new UnsupportedOperationException("Unknown type of light: " + l.getType());
        }
    }
    vars.release();
    //Padding of unsued buffer space
    while (lightDataIndex < numLights * 3) {
        lightData.setVector4InArray(0f, 0f, 0f, 0f, lightDataIndex);
        lightDataIndex++;
    }
    return curIndex;
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) TempVars(com.jme3.util.TempVars)

Example 78 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class SinglePassLightingLogic method updateLightListUniforms.

/**
     * Uploads the lights in the light list as two uniform arrays.<br/><br/> *
     * <p>
     * <code>uniform vec4 g_LightColor[numLights];</code><br/> //
     * g_LightColor.rgb is the diffuse/specular color of the light.<br/> //
     * g_Lightcolor.a is the type of light, 0 = Directional, 1 = Point, <br/> //
     * 2 = Spot. <br/> <br/>
     * <code>uniform vec4 g_LightPosition[numLights];</code><br/> //
     * g_LightPosition.xyz is the position of the light (for point lights)<br/>
     * // or the direction of the light (for directional lights).<br/> //
     * g_LightPosition.w is the inverse radius (1/r) of the light (for
     * attenuation) <br/> </p>
     */
protected int updateLightListUniforms(Shader shader, Geometry g, LightList lightList, int numLights, RenderManager rm, int startIndex) {
    if (numLights == 0) {
        // this shader does not do lighting, ignore.
        return 0;
    }
    Uniform lightData = shader.getUniform("g_LightData");
    //8 lights * max 3
    lightData.setVector4Length(numLights * 3);
    Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
    if (startIndex != 0) {
        // apply additive blending for 2nd and future passes
        rm.getRenderer().applyRenderState(ADDITIVE_LIGHT);
        ambientColor.setValue(VarType.Vector4, ColorRGBA.Black);
    } else {
        ambientColor.setValue(VarType.Vector4, getAmbientColor(lightList, true, ambientLightColor));
    }
    int lightDataIndex = 0;
    TempVars vars = TempVars.get();
    Vector4f tmpVec = vars.vect4f1;
    int curIndex;
    int endIndex = numLights + startIndex;
    for (curIndex = startIndex; curIndex < endIndex && curIndex < lightList.size(); curIndex++) {
        Light l = lightList.get(curIndex);
        if (l.getType() == Light.Type.Ambient) {
            endIndex++;
            continue;
        }
        ColorRGBA color = l.getColor();
        //Color
        lightData.setVector4InArray(color.getRed(), color.getGreen(), color.getBlue(), l.getType().getId(), lightDataIndex);
        lightDataIndex++;
        switch(l.getType()) {
            case Directional:
                DirectionalLight dl = (DirectionalLight) l;
                Vector3f dir = dl.getDirection();
                //Data directly sent in view space to avoid a matrix mult for each pixel
                tmpVec.set(dir.getX(), dir.getY(), dir.getZ(), 0.0f);
                rm.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
                //                        tmpVec.divideLocal(tmpVec.w);
                //                        tmpVec.normalizeLocal();
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), -1, lightDataIndex);
                lightDataIndex++;
                //PADDING
                lightData.setVector4InArray(0, 0, 0, 0, lightDataIndex);
                lightDataIndex++;
                break;
            case Point:
                PointLight pl = (PointLight) l;
                Vector3f pos = pl.getPosition();
                float invRadius = pl.getInvRadius();
                tmpVec.set(pos.getX(), pos.getY(), pos.getZ(), 1.0f);
                rm.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
                //tmpVec.divideLocal(tmpVec.w);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), invRadius, lightDataIndex);
                lightDataIndex++;
                //PADDING
                lightData.setVector4InArray(0, 0, 0, 0, lightDataIndex);
                lightDataIndex++;
                break;
            case Spot:
                SpotLight sl = (SpotLight) l;
                Vector3f pos2 = sl.getPosition();
                Vector3f dir2 = sl.getDirection();
                float invRange = sl.getInvSpotRange();
                float spotAngleCos = sl.getPackedAngleCos();
                tmpVec.set(pos2.getX(), pos2.getY(), pos2.getZ(), 1.0f);
                rm.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
                // tmpVec.divideLocal(tmpVec.w);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), invRange, lightDataIndex);
                lightDataIndex++;
                //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.0f);
                rm.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
                tmpVec.normalizeLocal();
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), spotAngleCos, lightDataIndex);
                lightDataIndex++;
                break;
            case Probe:
                break;
            default:
                throw new UnsupportedOperationException("Unknown type of light: " + l.getType());
        }
    }
    vars.release();
    //Padding of unsued buffer space
    while (lightDataIndex < numLights * 3) {
        lightData.setVector4InArray(0f, 0f, 0f, 0f, lightDataIndex);
        lightDataIndex++;
    }
    return curIndex;
}
Also used : Vector4f(com.jme3.math.Vector4f) ColorRGBA(com.jme3.math.ColorRGBA) DirectionalLight(com.jme3.light.DirectionalLight) SpotLight(com.jme3.light.SpotLight) Light(com.jme3.light.Light) PointLight(com.jme3.light.PointLight) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) Uniform(com.jme3.shader.Uniform) TempVars(com.jme3.util.TempVars) PointLight(com.jme3.light.PointLight) SpotLight(com.jme3.light.SpotLight)

Example 79 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class TechniqueDef method loadShader.

private Shader loadShader(AssetManager assetManager, EnumSet<Caps> rendererCaps, DefineList defines) {
    StringBuilder sb = new StringBuilder();
    sb.append(shaderPrologue);
    defines.generateSource(sb, defineNames, defineTypes);
    String definesSourceCode = sb.toString();
    Shader shader;
    if (isUsingShaderNodes()) {
        ShaderGenerator shaderGenerator = assetManager.getShaderGenerator(rendererCaps);
        if (shaderGenerator == null) {
            throw new UnsupportedOperationException("ShaderGenerator was not initialized, " + "make sure assetManager.getGenerator(caps) has been called");
        }
        shaderGenerator.initialize(this);
        shader = shaderGenerator.generateShader(definesSourceCode);
    } else {
        shader = new Shader();
        for (ShaderType type : ShaderType.values()) {
            String language = shaderLanguages.get(type);
            String shaderSourceAssetName = shaderNames.get(type);
            if (language == null || shaderSourceAssetName == null) {
                continue;
            }
            String shaderSourceCode = (String) assetManager.loadAsset(shaderSourceAssetName);
            shader.addSource(type, shaderSourceAssetName, shaderSourceCode, definesSourceCode, language);
        }
    }
    if (getWorldBindings() != null) {
        for (UniformBinding binding : getWorldBindings()) {
            shader.addUniformBinding(binding);
        }
    }
    return shader;
}
Also used : ShaderType(com.jme3.shader.Shader.ShaderType)

Example 80 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class Spatial method clone.

/**
     * @return A clone of this Spatial, the scene graph in its entirety
     * is cloned and can be altered independently of the original scene graph.
     *
     * Note that meshes of geometries are not cloned explicitly, they
     * are shared if static, or specially cloned if animated.
     *
     * @see Mesh#cloneForAnim()
     */
public Spatial clone(boolean cloneMaterial) {
    // Setup the cloner for the type of cloning we want to do.
    Cloner cloner = new Cloner();
    // First, we definitely do not want to clone our own parent
    cloner.setClonedValue(parent, null);
    // aren't cloned also
    if (!cloneMaterial) {
        cloner.setCloneFunction(Material.class, new IdentityCloneFunction<Material>());
    }
    // By default the meshes are not cloned.  The geometry
    // may choose to selectively force them to be cloned but
    // normally they will be shared
    cloner.setCloneFunction(Mesh.class, new IdentityCloneFunction<Mesh>());
    // Clone it!
    Spatial clone = cloner.clone(this);
    // Because we've nulled the parent out we need to make sure
    // the transforms and stuff get refreshed.
    clone.setTransformRefresh();
    clone.setLightListRefresh();
    clone.setMatParamOverrideRefresh();
    return clone;
}
Also used : Material(com.jme3.material.Material) Cloner(com.jme3.util.clone.Cloner)

Aggregations

ArrayList (java.util.ArrayList)18 Structure (com.jme3.scene.plugins.blender.file.Structure)12 Vector3f (com.jme3.math.Vector3f)11 Pointer (com.jme3.scene.plugins.blender.file.Pointer)10 IOException (java.io.IOException)10 List (java.util.List)9 ColorRGBA (com.jme3.math.ColorRGBA)8 Texture (com.jme3.texture.Texture)8 Geometry (com.jme3.scene.Geometry)6 Image (com.jme3.texture.Image)6 BoundingBox (com.jme3.bounding.BoundingBox)5 BoundingSphere (com.jme3.bounding.BoundingSphere)5 Light (com.jme3.light.Light)5 TemporalMesh (com.jme3.scene.plugins.blender.meshes.TemporalMesh)5 Uniform (com.jme3.shader.Uniform)5 Material (com.jme3.material.Material)4 Quaternion (com.jme3.math.Quaternion)4 Transform (com.jme3.math.Transform)4 Mesh (com.jme3.scene.Mesh)4 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)4