Java Code Examples for com.jme3.math.FastMath#clamp()

The following examples show how to use com.jme3.math.FastMath#clamp() . 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: TextureGeneratorClouds.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void getPixel(TexturePixel pixel, float x, float y, float z) {
    pixel.intensity = NoiseGenerator.NoiseFunctions.turbulence(x, y, z, noisesize, noiseDepth, noiseBasis, isHard);
    pixel.intensity = FastMath.clamp(pixel.intensity, 0.0f, 1.0f);
    if (colorBand != null) {
        int colorbandIndex = (int) (pixel.intensity * 1000.0f);
        pixel.red = colorBand[colorbandIndex][0];
        pixel.green = colorBand[colorbandIndex][1];
        pixel.blue = colorBand[colorbandIndex][2];
        pixel.alpha = colorBand[colorbandIndex][3];

        this.applyBrightnessAndContrast(bacd, pixel);
    } else if (sType == TEX_COLOR) {
        pixel.red = pixel.intensity;
        pixel.green = NoiseGenerator.NoiseFunctions.turbulence(y, x, z, noisesize, noiseDepth, noiseBasis, isHard);
        pixel.blue = NoiseGenerator.NoiseFunctions.turbulence(y, z, x, noisesize, noiseDepth, noiseBasis, isHard);

        pixel.green = FastMath.clamp(pixel.green, 0.0f, 1.0f);
        pixel.blue = FastMath.clamp(pixel.blue, 0.0f, 1.0f);
        pixel.alpha = 1.0f;

        this.applyBrightnessAndContrast(bacd, pixel);
    } else {
        this.applyBrightnessAndContrast(pixel, bacd.contrast, bacd.brightness);
    }
}
 
Example 2
Source File: TextureGeneratorNoise.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void getPixel(TexturePixel pixel, float x, float y, float z) {
    int random = FastMath.rand.nextInt();
    int val = random & 3;

    int loop = noisedepth;
    while (loop-- != 0) {
        random >>= 2;
        val *= random & 3;
    }
    pixel.intensity = FastMath.clamp(val, 0.0f, 1.0f);
    if (colorBand != null) {
        int colorbandIndex = (int) (pixel.intensity * 1000.0f);
        pixel.red = colorBand[colorbandIndex][0];
        pixel.green = colorBand[colorbandIndex][1];
        pixel.blue = colorBand[colorbandIndex][2];

        this.applyBrightnessAndContrast(bacd, pixel);
        pixel.alpha = colorBand[colorbandIndex][3];
    } else {
        this.applyBrightnessAndContrast(pixel, bacd.contrast, bacd.brightness);
    }
}
 
Example 3
Source File: ColorPanel.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private Color getColor(String r, String g, String b, String a) {
    try {
        float fr = FastMath.clamp(Float.parseFloat(r), 0, 1);
        float fg = FastMath.clamp(Float.parseFloat(g), 0, 1);
        float fb = FastMath.clamp(Float.parseFloat(b), 0, 1);
        float fa = FastMath.clamp(Float.parseFloat(a), 0, 1);
        return new Color(fr, fg, fb, fa);
    } catch (NumberFormatException e) {
        return null;
    }
}
 
Example 4
Source File: InputManager.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private float computeAnalogValue(long timeDelta) {
    if (safeMode || frameDelta == 0) {
        return 1f;
    } else {
        return FastMath.clamp((float) timeDelta / (float) frameDelta, 0, 1);
    }
}
 
Example 5
Source File: TestSoftwareMouse.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void onMouseMotionEvent(MouseMotionEvent evt) {
    x += evt.getDX();
    y += evt.getDY();

    // Prevent mouse from leaving screen
    AppSettings settings = TestSoftwareMouse.this.settings;
    x = FastMath.clamp(x, 0, settings.getWidth());
    y = FastMath.clamp(y, 0, settings.getHeight());

    // adjust for hotspot
    cursor.setPosition(x, y - 64);
}
 
Example 6
Source File: CurvesHelper.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * The method computes the taper scale on the given point on the curve.
 * 
 * @param taper
 *            the taper object that defines the scale
 * @param percent
 *            the percent of the 'road' along the curve
 * @return scale on the pointed place along the curve
 */
protected float getTaperScale(Spline taper, float percent) {
    if (taper == null) {
        return 1;// return scale = 1 if no taper is applied
    }
    percent = FastMath.clamp(percent, 0, 1);
    List<Float> segmentLengths = taper.getSegmentsLength();
    float percentLength = taper.getTotalLength() * percent;
    float partLength = 0;
    int i;
    for (i = 0; i < segmentLengths.size(); ++i) {
        partLength += segmentLengths.get(i);
        if (partLength > percentLength) {
            partLength -= segmentLengths.get(i);
            percentLength -= partLength;
            percent = percentLength / segmentLengths.get(i);
            break;
        }
    }
    // do not cross the line :)
    if (percent >= 1) {
        percent = 1;
        --i;
    }
    if (taper.getType() == SplineType.Bezier) {
        i *= 3;
    }
    return taper.interpolate(percent, i, null).y;
}
 
Example 7
Source File: TriangulatedTexture.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * This method converts the given point into 3D UV coordinates.
 * 
 * @param boundingBox
 *            the bounding box of the mesh
 * @param point
 *            the point to be transformed
 * @param uvs
 *            the result UV coordinates
 */
private void toTextureUV(BoundingBox boundingBox, Vector3f point, float[] uvs) {
    uvs[0] = (point.x - boundingBox.getCenter().x) / (boundingBox.getXExtent() == 0 ? 1 : boundingBox.getXExtent());
    uvs[1] = (point.y - boundingBox.getCenter().y) / (boundingBox.getYExtent() == 0 ? 1 : boundingBox.getYExtent());
    uvs[2] = (point.z - boundingBox.getCenter().z) / (boundingBox.getZExtent() == 0 ? 1 : boundingBox.getZExtent());
    // UVS cannot go outside <0, 1> range, but since we are generating texture for triangle envelope it might happen that
    // some points of the envelope will exceet the bounding box of the mesh thus generating uvs outside the range
    for (int i = 0; i < 3; ++i) {
        uvs[i] = FastMath.clamp(uvs[i], 0, 1);
    }
}
 
Example 8
Source File: TextureGeneratorMagic.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void getPixel(TexturePixel pixel, float x, float y, float z) {
    float turb = turbul;
    xyz[0] = (float) Math.sin((x + y + z) * 5.0f);
    xyz[1] = (float) Math.cos((-x + y - z) * 5.0f);
    xyz[2] = -(float) Math.cos((-x - y + z) * 5.0f);

    if (colorBand != null) {
        pixel.intensity = FastMath.clamp(0.3333f * (xyz[0] + xyz[1] + xyz[2]), 0.0f, 1.0f);
        int colorbandIndex = (int) (pixel.intensity * 1000.0f);
        pixel.red = colorBand[colorbandIndex][0];
        pixel.green = colorBand[colorbandIndex][1];
        pixel.blue = colorBand[colorbandIndex][2];
        pixel.alpha = colorBand[colorbandIndex][3];
    } else {
        if (noisedepth > 0) {
            xyz[0] *= turb;
            xyz[1] *= turb;
            xyz[2] *= turb;
            for (int m = 0; m < noisedepth; ++m) {
                noiseDepthFunctions[m].compute(xyz, turb);
            }
        }

        if (turb != 0.0f) {
            turb *= 2.0f;
            xyz[0] /= turb;
            xyz[1] /= turb;
            xyz[2] /= turb;
        }
        pixel.red = 0.5f - xyz[0];
        pixel.green = 0.5f - xyz[1];
        pixel.blue = 0.5f - xyz[2];
        pixel.alpha = 1.0f;
    }
    this.applyBrightnessAndContrast(bacd, pixel);
}
 
Example 9
Source File: TextureGeneratorDistnoise.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void getPixel(TexturePixel pixel, float x, float y, float z) {
    pixel.intensity = this.musgraveVariableLunacrityNoise(x * 4, y * 4, z * 4, distAmount, noisebasis, noisebasis2);
    pixel.intensity = FastMath.clamp(pixel.intensity, 0.0f, 1.0f);
    if (colorBand != null) {
        int colorbandIndex = (int) (pixel.intensity * 1000.0f);
        pixel.red = colorBand[colorbandIndex][0];
        pixel.green = colorBand[colorbandIndex][1];
        pixel.blue = colorBand[colorbandIndex][2];

        this.applyBrightnessAndContrast(bacd, pixel);
    } else {
        this.applyBrightnessAndContrast(pixel, bacd.contrast, bacd.brightness);
    }
}
 
Example 10
Source File: InputManager.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private float computeAnalogValue(long timeDelta) {
    if (safeMode || frameDelta == 0) {
        return 1f;
    } else {
        return FastMath.clamp(timeDelta / (float) frameDelta, 0, 1);
    }
}
 
Example 11
Source File: ChaseCameraAppState.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * rotate the camera around the target
 */
protected void rotateCamera() {
    verticalRotation = FastMath.clamp(verticalRotation, minVerticalRotation, maxVerticalRotation);
    TempVars vars = TempVars.get();
    Quaternion rot = vars.quat1;
    Quaternion rot2 = vars.quat2;
    rot.fromAngleNormalAxis(verticalRotation, leftVector);
    rot2.fromAngleNormalAxis(horizontalRotation, upVector);
    rot2.multLocal(rot);
    target.setLocalRotation(rot2);
    vars.release();
}
 
Example 12
Source File: LightScatteringFilter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void postQueue(RenderQueue queue) {
    getClipCoordinates(lightPosition, screenLightPos, viewPort.getCamera());
    viewPort.getCamera().getViewMatrix().mult(lightPosition, viewLightPos);        
    if (adaptative) {
        float densityX = 1f - FastMath.clamp(FastMath.abs(screenLightPos.x - 0.5f), 0, 1);
        float densityY = 1f - FastMath.clamp(FastMath.abs(screenLightPos.y - 0.5f), 0, 1);
        innerLightDensity = lightDensity * densityX * densityY;
    } else {
        innerLightDensity = lightDensity;
    }
    display = innerLightDensity != 0.0 && viewLightPos.z < 0;
}
 
Example 13
Source File: TestSoftwareMouse.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onMouseMotionEvent(MouseMotionEvent evt) {
    x += evt.getDX();
    y += evt.getDY();

    // Prevent mouse from leaving screen
    AppSettings settings = TestSoftwareMouse.this.settings;
    x = FastMath.clamp(x, 0, settings.getWidth());
    y = FastMath.clamp(y, 0, settings.getHeight());

    // adjust for hotspot
    cursor.setPosition(x, y - 64);
}
 
Example 14
Source File: ChaseCameraAppState.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * move the camera toward or away the target
 */
protected void zoomCamera(float value) {
    distance = FastMath.clamp(distance + value, minDistance, maxDistance);
    camNode.setLocalTranslation(new Vector3f(0, 0, distance));
}
 
Example 15
Source File: TriangulatedTexture.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Constructor that creates an image element from the 3D texture
 * (generated texture). It computes a flat smallest rectangle that can
 * hold a (3D) triangle defined by the given UV coordinates. Then it
 * defines the image pixels for points in 3D space that define the
 * calculated rectangle.
 * 
 * @param faceIndex
 *            the face index this image refers to
 * @param boundingBox
 *            the bounding box of the mesh
 * @param texture
 *            the texture that allows to compute a pixel value in 3D
 *            space
 * @param uv
 *            the UV coordinates of the mesh
 * @param blenderContext
 *            the blender context
 */
public TriangleTextureElement(int faceIndex, BoundingBox boundingBox, GeneratedTexture texture, Vector3f[] uv, int[] uvIndices, BlenderContext blenderContext) {
    this.faceIndex = faceIndex;

    // compute the face vertices from the UV coordinates
    float width = boundingBox.getXExtent() * 2;
    float height = boundingBox.getYExtent() * 2;
    float depth = boundingBox.getZExtent() * 2;

    Vector3f min = boundingBox.getMin(null);
    Vector3f v1 = min.add(uv[uvIndices[0]].x * width, uv[uvIndices[0]].y * height, uv[uvIndices[0]].z * depth);
    Vector3f v2 = min.add(uv[uvIndices[1]].x * width, uv[uvIndices[1]].y * height, uv[uvIndices[1]].z * depth);
    Vector3f v3 = min.add(uv[uvIndices[2]].x * width, uv[uvIndices[2]].y * height, uv[uvIndices[2]].z * depth);

    // get the rectangle envelope for the triangle
    RectangleEnvelope envelope = this.getTriangleEnvelope(v1, v2, v3);

    // create the result image
    Format imageFormat = texture.getImage().getFormat();
    int imageWidth = (int) (envelope.width * blenderContext.getBlenderKey().getGeneratedTexturePPU());
    if (imageWidth == 0) {
        imageWidth = 1;
    }
    int imageHeight = (int) (envelope.height * blenderContext.getBlenderKey().getGeneratedTexturePPU());
    if (imageHeight == 0) {
        imageHeight = 1;
    }
    ByteBuffer data = BufferUtils.createByteBuffer(imageWidth * imageHeight * (imageFormat.getBitsPerPixel() >> 3));
    image = new Image(texture.getImage().getFormat(), imageWidth, imageHeight, data);

    // computing the pixels
    PixelInputOutput pixelWriter = PixelIOFactory.getPixelIO(imageFormat);
    TexturePixel pixel = new TexturePixel();
    float[] uvs = new float[3];
    Vector3f point = new Vector3f(envelope.min);
    Vector3f vecY = new Vector3f();
    Vector3f wDelta = new Vector3f(envelope.w).multLocal(1.0f / imageWidth);
    Vector3f hDelta = new Vector3f(envelope.h).multLocal(1.0f / imageHeight);
    for (int x = 0; x < imageWidth; ++x) {
        for (int y = 0; y < imageHeight; ++y) {
            this.toTextureUV(boundingBox, point, uvs);
            texture.getPixel(pixel, uvs[0], uvs[1], uvs[2]);
            pixelWriter.write(image, 0, pixel, x, y);
            point.addLocal(hDelta);
        }

        vecY.addLocal(wDelta);
        point.set(envelope.min).addLocal(vecY);
    }

    // preparing UV coordinates for the flatted texture
    this.uv = new Vector2f[3];
    this.uv[0] = new Vector2f(FastMath.clamp(v1.subtract(envelope.min).length(), 0, Float.MAX_VALUE) / envelope.height, 0);
    Vector3f heightDropPoint = v2.subtract(envelope.w);// w is directed from the base to v2
    this.uv[1] = new Vector2f(1, heightDropPoint.subtractLocal(envelope.min).length() / envelope.height);
    this.uv[2] = new Vector2f(0, 1);
}
 
Example 16
Source File: TestInstanceNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private float smoothstep(float edge0, float edge1, float x) {
    // Scale, bias and saturate x to 0..1 range
    x = FastMath.clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
    // Evaluate polynomial
    return x * x * (3 - 2 * x);
}
 
Example 17
Source File: TexturePixel.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * This method clamps the pixel values to the given borders.
 * 
 * @param min
 *            the minimum value
 * @param max
 *            the maximum value
 */
public void clamp(float min, float max) {
    this.red = FastMath.clamp(this.red, min, max);
    this.green = FastMath.clamp(this.green, min, max);
    this.blue = FastMath.clamp(this.blue, min, max);
    this.alpha = FastMath.clamp(this.alpha, min, max);
    this.intensity = FastMath.clamp(this.intensity, min, max);
}
 
Example 18
Source File: AnimChannel.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * @param time Set the time of the currently playing animation, the time
 * is clamped from 0 to {@link #getAnimMaxTime()}. 
 */
public void setTime(float time) {
    this.time = FastMath.clamp(time, 0, getAnimMaxTime());
}
 
Example 19
Source File: AnimChannel.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * @param time Set the time of the currently playing animation, the time
 * is clamped from 0 to {@link #getAnimMaxTime()}. 
 */
public void setTime(float time) {
    this.time = FastMath.clamp(time, 0, getAnimMaxTime());
}