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

The following examples show how to use com.jme3.math.Vector2f#distanceSquared() . 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: SelectionState.java    From Lemur with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onMouseButtonEvent( MouseButtonEvent evt ) {
    if( !isEnabled() ) {
        return;
    }
    if( evt.isPressed() ) {
        // Save the location for later
        clickStart.set(evt.getX(), evt.getY());
    } else if( evt.isReleased() ) {
        
        Vector2f click = new Vector2f(evt.getX(), evt.getY());
        if( click.distanceSquared(clickStart) < clickRadiusSq ) {
            processClickEvent(click, evt);
        }  
    }
}
 
Example 2
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 3
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();
}