Java Code Examples for com.jme3.math.Vector2f#set()

The following examples show how to use com.jme3.math.Vector2f#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PaintTerrainToolControl.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
@JmeThread
private @NotNull Vector2f getPointPercentagePosition(@NotNull final Terrain terrain,
                                                     @NotNull final Vector3f localPoint,
                                                     @NotNull final Vector3f localScale,
                                                     @NotNull final Vector2f result) {
    result.set(localPoint.x, -localPoint.z);

    float scale = localScale.getX();

    // already centered on Terrain's node origin (0,0)
    float scaledSize = terrain.getTerrainSize() * scale;
    result.addLocal(scaledSize / 2, scaledSize / 2); // shift the bottom left corner up to 0,0
    result.divideLocal(scaledSize); // get the location as a percentage

    return result;
}
 
Example 2
Source File: LODGeomap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Get the triangle that the point is on.
 * 
 * @param x coordinate in local space to the geomap
 * @param z coordinate in local space to the geomap
 * @return triangle in local space to the geomap
 */
protected Triangle getTriangleAtPoint(float x, float z) {
    Triangle[] triangles = getGridTrianglesAtPoint(x, z);
    if (triangles == null) {
        //System.out.println("x,z: " + x + "," + z);
        return null;
    }
    Vector2f point = new Vector2f(x, z);
    Vector2f t1 = new Vector2f(triangles[0].get1().x, triangles[0].get1().z);
    Vector2f t2 = new Vector2f(triangles[0].get2().x, triangles[0].get2().z);
    Vector2f t3 = new Vector2f(triangles[0].get3().x, triangles[0].get3().z);

    if (0 != FastMath.pointInsideTriangle(t1, t2, t3, point)) {
        return triangles[0];
    }

    t1.set(triangles[1].get1().x, triangles[1].get1().z);
    t1.set(triangles[1].get2().x, triangles[1].get2().z);
    t1.set(triangles[1].get3().x, triangles[1].get3().z);

    if (0 != FastMath.pointInsideTriangle(t1, t2, t3, point)) {
        return triangles[1];
    }

    return null;
}
 
Example 3
Source File: MotionPath.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * compute the index of the waypoint and the interpolation value according to a distance
 * returns a vector 2 containing the index in the x field and the interpolation value in the y field
 * @param distance the distance traveled on this path
 * @return the waypoint index and the interpolation value in a vector2
 */
public Vector2f getWayPointIndexForDistance(float distance, Vector2f store) {
    float sum = 0;
    if(spline.getTotalLength() == 0){
        store.set(0, 0);
        return store;
    }
    distance = distance % spline.getTotalLength();
    int i = 0;
    for (Float len : spline.getSegmentsLength()) {
        if (sum + len >= distance) {
            return new Vector2f(i, (distance - sum) / len);
        }
        sum += len;
        i++;
    }
    store.set((float) spline.getControlPoints().size() - 1, 1.0f);
    return store;
}
 
Example 4
Source File: LODGeomap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Get the triangle that the point is on.
 * 
 * @param x coordinate in local space to the geomap
 * @param z coordinate in local space to the geomap
 * @return triangle in local space to the geomap
 */
protected Triangle getTriangleAtPoint(float x, float z) {
    Triangle[] triangles = getGridTrianglesAtPoint(x, z);
    if (triangles == null) {
        //System.out.println("x,z: " + x + "," + z);
        return null;
    }
    Vector2f point = new Vector2f(x, z);
    Vector2f t1 = new Vector2f(triangles[0].get1().x, triangles[0].get1().z);
    Vector2f t2 = new Vector2f(triangles[0].get2().x, triangles[0].get2().z);
    Vector2f t3 = new Vector2f(triangles[0].get3().x, triangles[0].get3().z);

    if (0 != FastMath.pointInsideTriangle(t1, t2, t3, point)) {
        return triangles[0];
    }

    t1.set(triangles[1].get1().x, triangles[1].get1().z);
    t1.set(triangles[1].get2().x, triangles[1].get2().z);
    t1.set(triangles[1].get3().x, triangles[1].get3().z);

    if (0 != FastMath.pointInsideTriangle(t1, t2, t3, point)) {
        return triangles[1];
    }

    return null;
}
 
Example 5
Source File: Vector2fPropertyEditor.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void setAsText(String text) throws IllegalArgumentException {
    text = text.replace('[', ' ');
    text = text.replace(']', ' ');
    String[] values = text.split(",");
    if (values.length != 2) {
        throw (new IllegalArgumentException("String not correct"));
    }
    float[] floats = new float[2];
    for (int i = 0; i < values.length; i++) {
        String string = values[i];
        floats[i] = Float.parseFloat(string);
    }
    Vector2f old = new Vector2f();
    old.set(vector);
    vector.set(floats[0], floats[1]);
    notifyListeners(old, vector);
}
 
Example 6
Source File: LODGeomap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Vector2f getUV(int x, int y, Vector2f store, Vector2f offset, float offsetAmount, int totalSize) {
    float offsetX = offset.x + (offsetAmount * 1.0f);
    float offsetY = -offset.y + (offsetAmount * 1.0f);//note the -, we flip the tex coords

    store.set((x + offsetX) / (totalSize - 1), // calculates percentage of texture here
            (y + offsetY) / (totalSize - 1));
    return store;
}
 
Example 7
Source File: HDRRenderer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Material createLumShader(int srcW, int srcH, int bufW, int bufH, int mode,
                            int iters, Texture tex){
    Material mat = new Material(manager, "Common/MatDefs/Hdr/LogLum.j3md");
    
    Vector2f blockSize = new Vector2f(1f / bufW, 1f / bufH);
    Vector2f pixelSize = new Vector2f(1f / srcW, 1f / srcH);
    Vector2f blocks = new Vector2f();
    float numPixels = Float.POSITIVE_INFINITY;
    if (iters != -1){
        do {
            pixelSize.multLocal(2);
            blocks.set(blockSize.x / pixelSize.x,
                       blockSize.y / pixelSize.y);
            numPixels = blocks.x * blocks.y;
        } while (numPixels > iters);
    }else{
        blocks.set(blockSize.x / pixelSize.x,
                   blockSize.y / pixelSize.y);
        numPixels = blocks.x * blocks.y;
    }

    mat.setBoolean("Blocks", true);
    if (mode == LUMMODE_ENCODE_LUM)
        mat.setBoolean("EncodeLum", true);
    else if (mode == LUMMODE_DECODE_LUM)
        mat.setBoolean("DecodeLum", true);

    mat.setTexture("Texture", tex);
    mat.setVector2("BlockSize", blockSize);
    mat.setVector2("PixelSize", pixelSize);
    mat.setFloat("NumPixels", numPixels);

    return mat;
}
 
Example 8
Source File: HDRRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private Material createLumShader(int srcW, int srcH, int bufW, int bufH, int mode,
                            int iters, Texture tex){
    Material mat = new Material(manager, "Common/MatDefs/Hdr/LogLum.j3md");
    
    Vector2f blockSize = new Vector2f(1f / bufW, 1f / bufH);
    Vector2f pixelSize = new Vector2f(1f / srcW, 1f / srcH);
    Vector2f blocks = new Vector2f();
    float numPixels = Float.POSITIVE_INFINITY;
    if (iters != -1){
        do {
            pixelSize.multLocal(2);
            blocks.set(blockSize.x / pixelSize.x,
                       blockSize.y / pixelSize.y);
            numPixels = blocks.x * blocks.y;
        } while (numPixels > iters);
    }else{
        blocks.set(blockSize.x / pixelSize.x,
                   blockSize.y / pixelSize.y);
        numPixels = blocks.x * blocks.y;
    }
    System.out.println(numPixels);

    mat.setBoolean("Blocks", true);
    if (mode == LUMMODE_ENCODE_LUM)
        mat.setBoolean("EncodeLum", true);
    else if (mode == LUMMODE_DECODE_LUM)
        mat.setBoolean("DecodeLum", true);

    mat.setTexture("Texture", tex);
    mat.setVector2("BlockSize", blockSize);
    mat.setVector2("PixelSize", pixelSize);
    mat.setFloat("NumPixels", numPixels);

    return mat;
}
 
Example 9
Source File: LODGeomap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Vector2f getUV(int x, int y, Vector2f store, Vector2f offset, float offsetAmount, int totalSize) {
    float offsetX = offset.x + (offsetAmount * 1.0f);
    float offsetY = -offset.y + (offsetAmount * 1.0f);//note the -, we flip the tex coords

    store.set((((float) x) + offsetX) / (float) (totalSize - 1), // calculates percentage of texture here
            (((float) y) + offsetY) / (float) (totalSize - 1));
    return store;
}
 
Example 10
Source File: RoughTerrainToolControl.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
/**
 * Modify height of terrain points.
 *
 * @param contactPoint the contact point.
 */
@JmeThread
private void modifyHeight(@NotNull final Vector3f contactPoint) {

    final LocalObjects local = getLocalObjects();
    final Spatial paintedModel = notNull(getPaintedModel());
    final Geometry brush = getBrush();

    final float brushSize = getBrushSize();
    final int twoBrushSize = (int) (brushSize * 2);

    final Basis fractalFilter = createFractalGenerator();
    final List<Vector2f> locs = new ArrayList<>();
    final List<Float> heights = new ArrayList<>();

    for (final Terrain terrain : getTerrains()) {

        final Node terrainNode = (Node) terrain;

        locs.clear();
        heights.clear();

        final Vector3f worldTranslation = terrainNode.getWorldTranslation();
        final Vector3f localScale = terrainNode.getLocalScale();
        final Vector3f localPoint = contactPoint.subtract(worldTranslation, local.nextVector());
        final Vector2f terrainLoc = local.nextVector2f();
        final Vector2f effectPoint = local.nextVector2f();

        final FloatBuffer buffer = fractalFilter.getBuffer(terrainLoc.getX(), terrainLoc.getY(), 0, twoBrushSize);

        final int radiusStepsX = (int) (brushSize / localScale.getX());
        final int radiusStepsZ = (int) (brushSize / localScale.getY());

        final float xStepAmount = localScale.getX();
        final float zStepAmount = localScale.getZ();

        for (int z = -radiusStepsZ, yfb = 0; z < radiusStepsZ; z++, yfb++) {
            for (int x = -radiusStepsX, xfb = 0; x < radiusStepsX; x++, xfb++) {

                final float locX = localPoint.getX() + (x * xStepAmount);
                final float locZ = localPoint.getZ() + (z * zStepAmount);

                effectPoint.set(locX - localPoint.getX(), locZ - localPoint.getZ());

                if (!isContains(brush, effectPoint.getX(), effectPoint.getX())) {
                    continue;
                }

                final float height = buffer.get(yfb * twoBrushSize + xfb);

                terrainLoc.set(locX, locZ);

                final float heightmapHeight = terrain.getHeightmapHeight(terrainLoc);
                if (Float.isNaN(heightmapHeight)) {
                    continue;
                }

                final float currentHeight = heightmapHeight * localScale.getY();
                // see if it is in the radius of the tool
                final float newHeight = calculateHeight(brushSize, height, effectPoint);

                locs.add(terrainLoc.clone());
                heights.add(currentHeight + newHeight);
            }
        }

        locs.forEach(location -> change(terrain, location));

        // do the actual height adjustment
        terrain.setHeight(locs, heights);
    }

    // or else we won't collide with it where we just edited
    paintedModel.updateModelBound();
}
 
Example 11
Source File: RaiseLowerTerrainToolControl.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
/**
 * Modify height of terrain points.
 *
 * @param input the type of input.
 * @param contactPoint the contact point.
 */
@JmeThread
private void modifyHeight(@NotNull final PaintingInput input, @NotNull final Vector3f contactPoint) {

    final LocalObjects local = getLocalObjects();
    final Spatial paintedModel = notNull(getPaintedModel());
    final Geometry brush = getBrush();

    final float brushSize = getBrushSize();
    final float brushPower = input == PaintingInput.MOUSE_PRIMARY ? getBrushPower() : getBrushPower() * -1F;

    final List<Vector2f> locs = new ArrayList<>();
    final List<Float> heights = new ArrayList<>();

    for (final Terrain terrain : getTerrains()) {

        final Node terrainNode = (Node) terrain;

        locs.clear();
        heights.clear();

        final Vector3f worldTranslation = terrainNode.getWorldTranslation();
        final Vector3f localScale = terrainNode.getLocalScale();
        final Vector3f localPoint = contactPoint.subtract(worldTranslation, local.nextVector());
        final Vector2f terrainLoc = local.nextVector2f();
        final Vector2f effectPoint = local.nextVector2f();

        final int radiusStepsX = (int) (brushSize / localScale.getX());
        final int radiusStepsZ = (int) (brushSize / localScale.getY());

        final float xStepAmount = localScale.getX();
        final float zStepAmount = localScale.getZ();

        for (int z = -radiusStepsZ; z < radiusStepsZ; z++) {
            for (int x = -radiusStepsX; x < radiusStepsX; x++) {

                float locX = localPoint.getX() + (x * xStepAmount);
                float locZ = localPoint.getZ() + (z * zStepAmount);

                effectPoint.set(locX - localPoint.getX(), locZ - localPoint.getZ());

                if (!isContains(brush, effectPoint.getX(), effectPoint.getY())) {
                    continue;
                }

                terrainLoc.set(locX, locZ);

                final float heightmapHeight = terrain.getHeightmapHeight(terrainLoc);
                if (Float.isNaN(heightmapHeight)) {
                    continue;
                }

                final float currentHeight = heightmapHeight * localScale.getY();
                // adjust height based on radius of the tool
                final float newHeight = calculateHeight(brushSize, brushPower, effectPoint.getX(), effectPoint.getY());

                // increase the height
                locs.add(terrainLoc.clone());
                heights.add(currentHeight + newHeight);
            }
        }

        locs.forEach(location -> change(terrain, location));
        // do the actual height adjustment
        terrain.setHeight(locs, heights);
    }

    // or else we won't collide with it where we just edited
    paintedModel.updateModelBound();
}
 
Example 12
Source File: PaintTerrainToolControl.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
/**
 * Goes through each pixel in the image. At each pixel it looks to see if the UV mouse coordinate is within the
 * of the brush. If it is in the brush radius, it gets the existing color from that pixel so it can add/subtract to/from it.
 * Essentially it does a radius check and adds in a fade value. It does this to the color value returned by the
 * first pixel color query.
 * Next it sets the color of that pixel. If it was within the radius, the color will change. If it was outside
 * the radius, then nothing will change, the color will be the same; but it will set it nonetheless. Not efficient.
 * <p>
 * If the mouse is being dragged with the button down, then the dragged value should be set to true. This will reduce
 * the intensity of the brush to 10% of what it should be per spray. Otherwise it goes to 100% opacity within a few pixels.
 * This makes it work a little more realistically.
 *
 * @param colorFunction the color function.
 * @param image         to manipulate
 * @param uv            the world x,z coordinate
 * @param radius        in percentage so it can be translated to the image dimensions
 * @param erase         true if the tool should remove the paint instead of add it
 * @param fadeFalloff   the percentage of the radius when the paint begins to start fading
 */
@JmeThread
private void doPaintAction(@NotNull final ObjectFloatObjectConsumer<ColorRGBA, Boolean> colorFunction,
                           @NotNull final Image image, @NotNull final Vector2f uv, @NotNull final Vector2f temp,
                           @NotNull final ColorRGBA color, final float radius, final boolean erase,
                           final float fadeFalloff) {

    final ByteBuffer buffer = image.getData(0);

    final int width = image.getWidth();
    final float height = image.getHeight();

    // convert percents to pixels to limit how much we iterate
    final int minX = (int) Math.max(0, (uv.getX() * width - radius * width));
    final int maxX = (int) Math.min(width, (uv.getX() * width + radius * width));
    final int minY = (int) Math.max(0, (uv.getY() * height - radius * height));
    final int maxY = (int) Math.min(height, (uv.getY() * height + radius * height));

    final float radiusSquared = radius * radius;

    // go through each pixel, in the radius of the tool, in the image
    for (int y = minY; y < maxY; y++) {
        for (int x = minX; x < maxX; x++) {

            // gets the position in percentage so it can compare with the mouse UV coordinate
            temp.set((float) x / width, (float) y / height);

            float dist = temp.distanceSquared(uv);

            // if the pixel is within the distance of the radius, set a color (distance times intensity)
            if (dist < radiusSquared) {

                final int position = (y * width + x) * 4;
                if (position > buffer.capacity() - 1 || position < 0) {
                    continue;
                }

                // gets the color at that location (false means don't write to the buffer)
                manipulatePixel(image, buffer, color, position, false);

                // calculate the fade falloff intensity
                final float intensity = (1.0f - (dist / radiusSquared)) * fadeFalloff;

                colorFunction.accept(color, intensity, erase);
                color.clamp();

                change(position, color);

                // set the new color
                manipulatePixel(image, buffer, color, position, true);
            }
        }
    }

    image.getData(0).rewind();
}
 
Example 13
Source File: GeoMap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Vector2f getUV(int x, int y, Vector2f store){
    store.set( x / (float)getWidth(),
               y / (float)getHeight() );
    return store;
}
 
Example 14
Source File: GeoMap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Vector2f getUV(int x, int y, Vector2f store){
    store.set( (float)x / (float)getWidth(),
               (float)y / (float)getHeight() );
    return store;
}
 
Example 15
Source File: PaintTerrainToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Goes through each pixel in the image. At each pixel it looks to see if the UV mouse coordinate is within the
 * of the brush. If it is in the brush radius, it gets the existing color from that pixel so it can add/subtract to/from it.
 * Essentially it does a radius check and adds in a fade value. It does this to the color value returned by the
 * first pixel color query.
 * Next it sets the color of that pixel. If it was within the radius, the color will change. If it was outside
 * the radius, then nothing will change, the color will be the same; but it will set it nonetheless. Not efficient.
 *
 * If the mouse is being dragged with the button down, then the dragged value should be set to true. This will reduce
 * the intensity of the brush to 10% of what it should be per spray. Otherwise it goes to 100% opacity within a few pixels.
 * This makes it work a little more realistically.
 *
 * @param image to manipulate
 * @param uv the world x,z coordinate
 * @param dragged true if the mouse button is down and it is being dragged, use to reduce brush intensity
 * @param radius in percentage so it can be translated to the image dimensions
 * @param erase true if the tool should remove the paint instead of add it
 * @param fadeFalloff the percentage of the radius when the paint begins to start fading
 */
protected void doPaintAction(int texIndex, Image image, Vector2f uv, boolean dragged, float radius, boolean erase, float fadeFalloff){
    Vector2f texuv = new Vector2f();
    ColorRGBA color = ColorRGBA.Black;
    
    float width = image.getWidth();
    float height = image.getHeight();

    int minx = (int) Math.max(0, (uv.x*width - radius*width)); // convert percents to pixels to limit how much we iterate
    int maxx = (int) Math.min(width,(uv.x*width + radius*width));
    int miny = (int) Math.max(0,(uv.y*height - radius*height));
    int maxy = (int) Math.min(height,(uv.y*height + radius*height));

    float radiusSquared = radius*radius;
    float radiusFalloff = radius*fadeFalloff;
    // go through each pixel, in the radius of the tool, in the image
    for (int y = miny; y < maxy; y++){
        for (int x = minx; x < maxx; x++){
            
            texuv.set((float)x / width, (float)y / height);// gets the position in percentage so it can compare with the mouse UV coordinate

            float dist = texuv.distanceSquared(uv);
            if (dist < radiusSquared ) { // if the pixel is within the distance of the radius, set a color (distance times intensity)
                manipulatePixel(image, x, y, color, false); // gets the color at that location (false means don't write to the buffer)

                // calculate the fade falloff intensity
                float intensity = 0.1f;
                if (dist > radiusFalloff) {
                    float dr = radius - radiusFalloff; // falloff to radius length
                    float d2 = dist - radiusFalloff; // dist minus falloff
                    d2 = d2/dr; // dist percentage of falloff length
                    intensity = 1-d2; // fade out more the farther away it is
                }

                //if (dragged)
                //	intensity = intensity*0.1f; // magical divide it by 10 to reduce its intensity when mouse is dragged

                if (erase) {
                    switch (texIndex) {
                        case 0:
                            color.r -= intensity; break;
                        case 1:
                            color.g -= intensity; break;
                        case 2:
                            color.b -= intensity; break;
                        case 3:
                            color.a -= intensity; break;
                    }
                } else {
                    switch (texIndex) {
                        case 0:
                            color.r += intensity; break;
                        case 1:
                            color.g += intensity; break;
                        case 2:
                            color.b += intensity; break;
                        case 3:
                            color.a += intensity; break;
                    }
                }
                color.clamp();

                manipulatePixel(image, x, y, color, true); // set the new color
            }

        }
    }

    image.getData(0).rewind();
}