Search in sources :

Example 56 with Type

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

the class TextureHelper method applyColorbandAndColorFactors.

/**
     * This method applies the colorband and color factors to image type
     * textures. If there is no colorband defined for the texture or the color
     * factors are all equal to 1.0f then no changes are made.
     * 
     * @param tex
     *            the texture structure
     * @param image
     *            the image that will be altered if necessary
     * @param blenderContext
     *            the blender context
     */
private void applyColorbandAndColorFactors(Structure tex, Image image, BlenderContext blenderContext) {
    float rfac = ((Number) tex.getFieldValue("rfac")).floatValue();
    float gfac = ((Number) tex.getFieldValue("gfac")).floatValue();
    float bfac = ((Number) tex.getFieldValue("bfac")).floatValue();
    float[][] colorBand = new ColorBand(tex, blenderContext).computeValues();
    int depth = image.getDepth() == 0 ? 1 : image.getDepth();
    if (colorBand != null) {
        TexturePixel pixel = new TexturePixel();
        PixelInputOutput imageIO = PixelIOFactory.getPixelIO(image.getFormat());
        for (int layerIndex = 0; layerIndex < depth; ++layerIndex) {
            for (int x = 0; x < image.getWidth(); ++x) {
                for (int y = 0; y < image.getHeight(); ++y) {
                    imageIO.read(image, layerIndex, pixel, x, y);
                    int colorbandIndex = (int) (pixel.alpha * 1000.0f);
                    pixel.red = colorBand[colorbandIndex][0] * rfac;
                    pixel.green = colorBand[colorbandIndex][1] * gfac;
                    pixel.blue = colorBand[colorbandIndex][2] * bfac;
                    pixel.alpha = colorBand[colorbandIndex][3];
                    imageIO.write(image, layerIndex, pixel, x, y);
                }
            }
        }
    } else if (rfac != 1.0f || gfac != 1.0f || bfac != 1.0f) {
        TexturePixel pixel = new TexturePixel();
        PixelInputOutput imageIO = PixelIOFactory.getPixelIO(image.getFormat());
        for (int layerIndex = 0; layerIndex < depth; ++layerIndex) {
            for (int x = 0; x < image.getWidth(); ++x) {
                for (int y = 0; y < image.getHeight(); ++y) {
                    imageIO.read(image, layerIndex, pixel, x, y);
                    pixel.red *= rfac;
                    pixel.green *= gfac;
                    pixel.blue *= bfac;
                    imageIO.write(image, layerIndex, pixel, x, y);
                }
            }
        }
    }
}
Also used : PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput)

Example 57 with Type

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

the class UVCoordinatesGenerator method generateUVCoordinatesFor2DTexture.

/**
     * Generates a UV coordinates for 2D texture.
     * 
     * @param mesh
     *            the mesh we generate UV's for
     * @param texco
     *            UV coordinates type
     * @param projection
     *            projection type
     * @param geometries
     *            the geometris the given mesh belongs to (required to compute
     *            bounding box)
     * @return UV coordinates for the given mesh
     */
public static List<Vector2f> generateUVCoordinatesFor2DTexture(Mesh mesh, UVCoordinatesType texco, UVProjectionType projection, Geometry geometries) {
    List<Vector2f> result = new ArrayList<Vector2f>();
    BoundingBox bb = UVCoordinatesGenerator.getBoundingBox(geometries);
    // positions, normals, reflection vectors, etc.
    float[] inputData = null;
    switch(texco) {
        case TEXCO_ORCO:
            inputData = BufferUtils.getFloatArray(mesh.getFloatBuffer(VertexBuffer.Type.Position));
            break;
        case // this should be used if not defined by user explicitly
        TEXCO_UV:
            Vector2f[] data = new Vector2f[] { new Vector2f(0, 1), new Vector2f(0, 0), new Vector2f(1, 0) };
            for (int i = 0; i < mesh.getVertexCount(); ++i) {
                result.add(data[i % 3]);
            }
            break;
        case TEXCO_NORM:
            inputData = BufferUtils.getFloatArray(mesh.getFloatBuffer(VertexBuffer.Type.Normal));
            break;
        case TEXCO_REFL:
        case TEXCO_GLOB:
        case TEXCO_TANGENT:
        case TEXCO_STRESS:
        case TEXCO_LAVECTOR:
        case TEXCO_OBJECT:
        case TEXCO_OSA:
        case TEXCO_PARTICLE_OR_STRAND:
        case TEXCO_SPEED:
        case TEXCO_STICKY:
        case TEXCO_VIEW:
        case TEXCO_WINDOW:
            LOGGER.warning("Texture coordinates type not currently supported: " + texco);
            break;
        default:
            throw new IllegalStateException("Unknown texture coordinates value: " + texco);
    }
    if (inputData != null) {
        // make projection calculations
        switch(projection) {
            case PROJECTION_FLAT:
                inputData = UVProjectionGenerator.flatProjection(inputData, bb);
                break;
            case PROJECTION_CUBE:
                inputData = UVProjectionGenerator.cubeProjection(inputData, bb);
                break;
            case PROJECTION_TUBE:
                BoundingTube bt = UVCoordinatesGenerator.getBoundingTube(geometries);
                inputData = UVProjectionGenerator.tubeProjection(inputData, bt);
                break;
            case PROJECTION_SPHERE:
                BoundingSphere bs = UVCoordinatesGenerator.getBoundingSphere(geometries);
                inputData = UVProjectionGenerator.sphereProjection(inputData, bs);
                break;
            default:
                throw new IllegalStateException("Unknown projection type: " + projection);
        }
        for (int i = 0; i < inputData.length; i += 2) {
            result.add(new Vector2f(inputData[i], inputData[i + 1]));
        }
    }
    return result;
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) Vector2f(com.jme3.math.Vector2f) BoundingBox(com.jme3.bounding.BoundingBox) ArrayList(java.util.ArrayList)

Example 58 with Type

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

the class UVCoordinatesGenerator method generateUVCoordinatesFor3DTexture.

/**
     * Generates a UV coordinates for 3D texture.
     * 
     * @param mesh
     *            the mesh we generate UV's for
     * @param texco
     *            UV coordinates type
     * @param coordinatesSwappingIndexes
     *            coordinates swapping indexes
     * @param geometries
     *            the geometris the given mesh belongs to (required to compute
     *            bounding box)
     * @return UV coordinates for the given mesh
     */
public static List<Vector3f> generateUVCoordinatesFor3DTexture(Mesh mesh, UVCoordinatesType texco, int[] coordinatesSwappingIndexes, Geometry... geometries) {
    List<Vector3f> result = new ArrayList<Vector3f>();
    BoundingBox bb = UVCoordinatesGenerator.getBoundingBox(geometries);
    // positions, normals, reflection vectors, etc.
    float[] inputData = null;
    switch(texco) {
        case TEXCO_ORCO:
            inputData = BufferUtils.getFloatArray(mesh.getFloatBuffer(VertexBuffer.Type.Position));
            break;
        case TEXCO_UV:
            Vector2f[] data = new Vector2f[] { new Vector2f(0, 1), new Vector2f(0, 0), new Vector2f(1, 0) };
            for (int i = 0; i < mesh.getVertexCount(); ++i) {
                Vector2f uv = data[i % 3];
                result.add(new Vector3f(uv.x, uv.y, 0));
            }
            break;
        case TEXCO_NORM:
            inputData = BufferUtils.getFloatArray(mesh.getFloatBuffer(VertexBuffer.Type.Normal));
            break;
        case TEXCO_REFL:
        case TEXCO_GLOB:
        case TEXCO_TANGENT:
        case TEXCO_STRESS:
        case TEXCO_LAVECTOR:
        case TEXCO_OBJECT:
        case TEXCO_OSA:
        case TEXCO_PARTICLE_OR_STRAND:
        case TEXCO_SPEED:
        case TEXCO_STICKY:
        case TEXCO_VIEW:
        case TEXCO_WINDOW:
            LOGGER.warning("Texture coordinates type not currently supported: " + texco);
            break;
        default:
            throw new IllegalStateException("Unknown texture coordinates value: " + texco);
    }
    if (inputData != null) {
        // make calculations
        Vector3f min = bb.getMin(null);
        // used for coordinates swapping
        float[] uvCoordsResults = new float[4];
        float[] ext = new float[] { bb.getXExtent() * 2, bb.getYExtent() * 2, bb.getZExtent() * 2 };
        for (int i = 0; i < ext.length; ++i) {
            if (ext[i] == 0) {
                ext[i] = 1;
            }
        }
        // <0; 1>
        for (int i = 0; i < inputData.length; i += 3) {
            uvCoordsResults[1] = (inputData[i] - min.x) / ext[0];
            uvCoordsResults[2] = (inputData[i + 1] - min.y) / ext[1];
            uvCoordsResults[3] = (inputData[i + 2] - min.z) / ext[2];
            result.add(new Vector3f(uvCoordsResults[coordinatesSwappingIndexes[0]], uvCoordsResults[coordinatesSwappingIndexes[1]], uvCoordsResults[coordinatesSwappingIndexes[2]]));
        }
    }
    return result;
}
Also used : Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f) BoundingBox(com.jme3.bounding.BoundingBox) ArrayList(java.util.ArrayList)

Example 59 with Type

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

the class ShaderGenerator method generateShader.

/**
     * Generate vertex and fragment shaders for the given technique
     *
     * @return a Shader program
     */
public Shader generateShader(String definesSourceCode) {
    if (techniqueDef == null) {
        throw new UnsupportedOperationException("The shaderGenerator was not " + "properly initialized, call " + "initialize(TechniqueDef) before any generation");
    }
    String techniqueName = techniqueDef.getName();
    ShaderGenerationInfo info = techniqueDef.getShaderGenerationInfo();
    Shader shader = new Shader();
    for (ShaderType type : ShaderType.values()) {
        String extension = type.getExtension();
        String language = getLanguageAndVersion(type);
        String shaderSourceCode = buildShader(techniqueDef.getShaderNodes(), info, type);
        if (shaderSourceCode != null) {
            String shaderSourceAssetName = techniqueName + "." + extension;
            shader.addSource(type, shaderSourceAssetName, shaderSourceCode, definesSourceCode, language);
        }
    }
    techniqueDef = null;
    return shader;
}
Also used : ShaderGenerationInfo(com.jme3.material.ShaderGenerationInfo) ShaderType(com.jme3.shader.Shader.ShaderType)

Example 60 with Type

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

the class ShaderNodeDefinition method write.

/**
     * jme serialization (not used)
     *
     * @param ex the exporter
     * @throws IOException
     */
@Override
public void write(JmeExporter ex) throws IOException {
    OutputCapsule oc = (OutputCapsule) ex.getCapsule(this);
    oc.write(name, "name", "");
    String[] str = new String[shadersLanguage.size()];
    oc.write(shadersLanguage.toArray(str), "shadersLanguage", null);
    oc.write(shadersPath.toArray(str), "shadersPath", null);
    oc.write(type, "type", null);
    oc.writeSavableArrayList((ArrayList) inputs, "inputs", new ArrayList<ShaderNodeVariable>());
    oc.writeSavableArrayList((ArrayList) outputs, "inputs", new ArrayList<ShaderNodeVariable>());
}
Also used : OutputCapsule(com.jme3.export.OutputCapsule)

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