public static int packColor(float r, float g, float b, float a) { int iR = (int) (TeraMath.clamp(r, 0.0f, 1.0f) * 255.0f); int iG = (int) (TeraMath.clamp(g, 0.0f, 1.0f) * 255.0f); int iB = (int) (TeraMath.clamp(b, 0.0f, 1.0f) * 255.0f); int iA = (int) (TeraMath.clamp(a, 0.0f, 1.0f) * 255.0f); return iA << 24 | iB << 16 | iG << 8 | iR; } }
/** * @param luminance the light level produced by this block */ public void setLuminance(byte luminance) { this.luminance = (byte) TeraMath.clamp(luminance, 0, ChunkConstants.MAX_LIGHT); }
@Override public float getDaylight() { float angle = (float) Math.toDegrees(TeraMath.clamp(Math.cos(getSunPositionAngle()))); float daylight = 1.0f; if (angle < 24.0f) { daylight = 1.0f - (24.0f - angle) / 24.0f; } return daylight; }
public static TextureData createWhiteNoiseTexture(int size, long seed, int min, int max) { int width = size; int height = size; ByteBuffer data = ByteBuffer.allocateDirect(4 * width * height); Random rng = new FastRandom(seed); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { data.put((byte) TeraMath.clamp(rng.nextInt(min, max), 0, 255)); data.put((byte) TeraMath.clamp(rng.nextInt(min, max), 0, 255)); data.put((byte) TeraMath.clamp(rng.nextInt(min, max), 0, 255)); data.put((byte) 255); } } // The buffer must be reset back to the initial position before passing it onward. data.rewind(); return new TextureData(width, height, new ByteBuffer[]{data}, WrapMode.REPEAT, FilterMode.NEAREST); } }
private void updatePosition(int pixelPos) { int newPosition = TeraMath.clamp(pixelPos, 0, sliderSize); setValue((sliderSize > 0) ? (newPosition * getRange() / sliderSize) : 0); }
/** * Computes the color of the facet at a given world coordinate. * @param facet the underlying facet * @param x the world x coordinate * @param z the world z coordinate * @return the color at the given world coords. Never <code>null</code>. */ protected Color getColor(FieldFacet2D facet, int x, int z) { double value = facet.get(x, z); if (Double.isFinite(value)) { int round = DoubleMath.roundToInt(config.offset + config.scale * value, RoundingMode.HALF_UP); int idx = TeraMath.clamp(round, 0, 255); return GRAYS.get(idx); } else { return MISSING; } }
@Override public void process(GeneratingRegion region) { SurfaceHeightFacet facet = region.getRegionFacet(SurfaceHeightFacet.class); float[] noise = riverNoise.noise(facet.getWorldRegion()); float[] surfaceHeights = facet.getInternal(); for (int i = 0; i < noise.length; ++i) { surfaceHeights[i] += configuration.maxDepth * TeraMath.clamp(7f * (TeraMath.sqrt(Math.abs(noise[i] * 2.11f)) - 0.1f) + 0.25f); } }
@Override public void process(GeneratingRegion region) { SurfaceHeightFacet facet = region.getRegionFacet(SurfaceHeightFacet.class); float[] noise = oceanNoise.noise(facet.getWorldRegion()); float[] surfaceHeights = facet.getInternal(); for (int i = 0; i < noise.length; ++i) { surfaceHeights[i] -= configuration.maxDepth * TeraMath.clamp(noise[i] * 8.0f * 2.11f + 0.25f); } }
/** * @return The current scroll value. */ public int getValue() { return TeraMath.clamp(value.get(), getMinimum(), getMinimum() + getRange()); }
@Override public void onMouseDrag(NUIMouseDragEvent event) { if (sliderWidth > 0) { Vector2i pos = event.getRelativeMousePosition(); int maxSlot = TeraMath.floorToInt(getRange() / getIncrement()); int slotWidth = sliderWidth / maxSlot; int nearestSlot = maxSlot * (pos.x - offset.x + slotWidth / 2) / sliderWidth; nearestSlot = TeraMath.clamp(nearestSlot, 0, maxSlot); float newValue = TeraMath.clamp(getIncrement() * nearestSlot, 0, getRange()) + getMinimum(); setValue(newValue); } } };
@Override public void onMouseDrag(NUIMouseDragEvent event) { if (sliderWidth > 0) { Vector2i pos = event.getRelativeMousePosition(); int maxSlot = TeraMath.floorToInt(getRange() / getIncrement()); int slotWidth = sliderWidth / maxSlot; int nearestSlot = maxSlot * (pos.x - offset.x + slotWidth / 2) / sliderWidth; nearestSlot = TeraMath.clamp(nearestSlot, 0, maxSlot); float newValue = TeraMath.clamp(getIncrement() * nearestSlot, 0, getRange()) + getMinimum(); setValueRight(newValue); } } };
@Override public void onMouseDrag(NUIMouseDragEvent event) { if (sliderWidth > 0) { Vector2i pos = event.getRelativeMousePosition(); int maxSlot = TeraMath.floorToInt(getRange() / getIncrement()); int slotWidth = sliderWidth / maxSlot; int nearestSlot = maxSlot * (pos.x - offset.x + slotWidth / 2) / sliderWidth; nearestSlot = TeraMath.clamp(nearestSlot, 0, maxSlot); float newValue = TeraMath.clamp(getIncrement() * nearestSlot, 0, getRange()) + getMinimum(); setValueLeft(newValue); } } };
private Vector2i applyStyleToSize(Vector2i size, UIStyle style) { Vector2i result = new Vector2i(size); if (style.getFixedWidth() != 0) { result.x = style.getFixedWidth(); } else { result.x = TeraMath.clamp(result.x, style.getMinWidth(), style.getMaxWidth()); } if (style.getFixedHeight() != 0) { result.y = style.getFixedHeight(); } else { result.y = TeraMath.clamp(result.y, style.getMinHeight(), style.getMaxHeight()); } return result; }
@Override public boolean onMouseClick(NUIMouseClickEvent event) { if (event.getMouseButton() == MouseInput.MOUSE_LEFT) { Vector2i pos = event.getRelativeMousePosition(); mouseOffset = (sliderSize > handleSize) ? (handleSize / 2) : 0; int pixelPosition = vertical ? pos.y - mouseOffset : pos.x - mouseOffset; updatePosition(pixelPosition); if (sliderSize > 0) { int clamped = TeraMath.clamp(pixelPosition, 0, sliderSize); setValue(clamped * getRange() / sliderSize); } else { setValue(0); } dragging = true; return true; } return false; }
@Override public void process(GeneratingRegion region) { SurfaceHeightFacet facet = region.getRegionFacet(SurfaceHeightFacet.class); float[] mountainData = mountainNoise.noise(facet.getWorldRegion()); float[] hillData = hillNoise.noise(facet.getWorldRegion()); SurfaceTemperatureFacet temperatureData = region.getRegionFacet(SurfaceTemperatureFacet.class); SurfaceHumidityFacet humidityData = region.getRegionFacet(SurfaceHumidityFacet.class); float[] heightData = facet.getInternal(); Iterator<BaseVector2i> positionIterator = facet.getRelativeRegion().contents().iterator(); for (int i = 0; i < heightData.length; ++i) { BaseVector2i pos = positionIterator.next(); float temp = temperatureData.get(pos); float tempHumid = temp * humidityData.get(pos); Vector2f distanceToMountainBiome = new Vector2f(temp - 0.25f, tempHumid - 0.35f); float mIntens = TeraMath.clamp(1.0f - distanceToMountainBiome.length() * 3.0f); float densityMountains = Math.max(mountainData[i] * 2.12f, 0) * mIntens * configuration.mountainAmplitude; float densityHills = Math.max(hillData[i] * 2.12f - 0.1f, 0) * (1.0f - mIntens) * configuration.hillAmplitude; heightData[i] = heightData[i] + 1024 * densityMountains + 128 * densityHills; } }
@Override public void process(GeneratingRegion region) { Border3D border = region.getBorderForFacet(SurfaceHumidityFacet.class); SurfaceHumidityFacet facet = new SurfaceHumidityFacet(region.getRegion(), border); float[] noise = humidityNoise.noise(facet.getWorldRegion()); for (int i = 0; i < noise.length; ++i) { noise[i] = TeraMath.clamp((noise[i] * 2.11f + 1f) * 0.5f); } facet.set(noise); region.setRegionFacet(SurfaceHumidityFacet.class, facet); }
@Override public void process(GeneratingRegion region) { SurfaceTemperatureFacet facet = new SurfaceTemperatureFacet(region.getRegion(), region.getBorderForFacet(SurfaceTemperatureFacet.class)); float[] noise = this.temperatureNoise.noise(facet.getWorldRegion()); for (int i = 0; i < noise.length; ++i) { noise[i] = TeraMath.clamp((noise[i] * 2.11f + 1f) * 0.5f); } facet.set(noise); region.setRegionFacet(SurfaceTemperatureFacet.class, facet); } }
switch (configuration.wrapMode) { case CLAMP: mapX0 = TeraMath.clamp(pos.getX(), 0, mapWidth * xzScale - 1) / xzScale; mapZ0 = TeraMath.clamp(pos.getY(), 0, mapHeight * xzScale - 1) / xzScale; mapX1 = TeraMath.clamp(mapX0 + 1, 0, mapWidth - 1); mapZ1 = TeraMath.clamp(mapZ0 + 1, 0, mapHeight - 1); break; case REPEAT:
private void processInput(EntityRef entity, CharacterMovementComponent characterMovementComponent) { lookYaw = (lookYaw - lookYawDelta) % 360; lookYawDelta = 0f; lookPitch = TeraMath.clamp(lookPitch + lookPitchDelta, -89, 89); lookPitchDelta = 0f;