private Vector3f getQuantizedMainLightDirection(float stepSize) { float mainLightAngle = (float) Math.floor((double) backdropProvider.getSunPositionAngle() * stepSize) / stepSize + 0.0001f; Vector3f mainLightDirection = new Vector3f(0.0f, (float) Math.cos(mainLightAngle), (float) Math.sin(mainLightAngle)); // When the sun goes under the horizon we flip the vector, to provide the moon direction, and viceversa. if (mainLightDirection.y < 0.0f) { mainLightDirection.scale(-1.0f); } return mainLightDirection; } }
@Override public Vector3f getSunDirection(boolean moonlightFlip) { float sunAngle = getSunPositionAngle() + 0.0001f; Vector3f sunDirection = new Vector3f(0.0f, (float) Math.cos(sunAngle), (float) Math.sin(sunAngle)); // Moonlight flip if (moonlightFlip && sunDirection.y < 0.0f) { sunDirection.scale(-1.0f); } return sunDirection; } }
/** * @return The distance from the center to the max node */ public Vector3f getExtents() { Vector3f dimensions = new Vector3f(max); dimensions.sub(min); dimensions.scale(HALVING_FACTOR); return dimensions; }
public Vector3f getCenter() { Vector3f dimensions = new Vector3f(max); dimensions.add(min); dimensions.scale(HALVING_FACTOR); return dimensions; }
/** * @return the portion of direction that is perpendicular to normal */ public static Vector3f getPerpendicularComponent(Vector3f direction, Vector3f normal, Vector3f out) { Vector3f perpendicular = getParallelComponent(direction, normal, out); perpendicular.scale(-1); perpendicular.add(direction); return perpendicular; }
/** * @return the portion of direction that is parallel to normal */ public static Vector3f getParallelComponent(Vector3f direction, Vector3f normal, Vector3f out) { out.set(normal); out.scale(direction.dot(normal)); return out; }
/** * @return The reflection of direction against normal */ public static Vector3f reflect(Vector3f direction, Vector3f normal, Vector3f out) { out.set(normal); out.scale(-2.0f * direction.dot(normal)); out.add(direction); return out; }
/** * @return The position at the center of the region */ public Vector3f center() { Vector3f result = min.toVector3f(); Vector3f halfSize = size.toVector3f(); halfSize.scale(0.5f); result.add(halfSize); return result; }
/** * Returns a Vector3f with a given size whose components can range from -size (inclusive) to +size (inclusive) * * @param size * @return The vector */ public Vector3f nextVector3f(float size) { // Create a vector whose length is not zero Vector3f vector = new Vector3f(); do { nextVector3f(vector); } while (vector.x == 0.0f && vector.y == 0.0f && vector.z == 0.0f); float length = vector.length(); vector.scale(size / length); return vector; }
private void createSamplesBuffer() { ssaoSamples = BufferUtils.createFloatBuffer(SSAO_KERNEL_ELEMENTS * 3); for (int i = 0; i < SSAO_KERNEL_ELEMENTS; ++i) { Vector3f vec = new Vector3f(); vec.x = randomGenerator.nextFloat(-1.0f, 1.0f); vec.y = randomGenerator.nextFloat(-1.0f, 1.0f); vec.z = randomGenerator.nextFloat(); vec.normalize(); vec.scale(randomGenerator.nextFloat(0.0f, 1.0f)); float scale = i / (float) SSAO_KERNEL_ELEMENTS; scale = TeraMath.lerp(0.25f, 1.0f, scale * scale); vec.scale(scale); ssaoSamples.put(vec.x); ssaoSamples.put(vec.y); ssaoSamples.put(vec.z); } ssaoSamples.flip(); }
public List<Vector3f> getVertexNormals(List<Vector3f> bonePositions, List<Quat4f> boneRotations) { List<Vector3f> results = Lists.newArrayListWithCapacity(getVertexCount()); for (int i = 0; i < vertexStartWeights.size(); ++i) { Vector3f vertexNorm = new Vector3f(); for (int weightIndexOffset = 0; weightIndexOffset < vertexWeightCounts.get(i); ++weightIndexOffset) { int weightIndex = vertexStartWeights.get(i) + weightIndexOffset; BoneWeight weight = weights.get(weightIndex); Vector3f current = boneRotations.get(weight.getBoneIndex()).rotate(weight.getNormal(), new Vector3f()); current.scale(weight.getBias()); vertexNorm.add(current); } results.add(vertexNorm); } return results; }
public List<Vector3f> getVertexPositions(List<Vector3f> bonePositions, List<Quat4f> boneRotations) { List<Vector3f> results = Lists.newArrayListWithCapacity(getVertexCount()); for (int i = 0; i < vertexStartWeights.size(); ++i) { Vector3f vertexPos = new Vector3f(); for (int weightIndexOffset = 0; weightIndexOffset < vertexWeightCounts.get(i); ++weightIndexOffset) { int weightIndex = vertexStartWeights.get(i) + weightIndexOffset; BoneWeight weight = weights.get(weightIndex); Vector3f current = boneRotations.get(weight.getBoneIndex()).rotate(weight.getPosition(), new Vector3f()); current.add(bonePositions.get(weight.getBoneIndex())); current.scale(weight.getBias()); vertexPos.add(current); } results.add(vertexPos); } return results; }
@ReceiveEvent public void updateExtentsOnBlockItemBoxShape(OnAddedComponent event, EntityRef itemEntity, BlockItemComponent blockItemComponent, BoxShapeComponent boxShapeComponent) { BlockFamily blockFamily = blockItemComponent.blockFamily; if (blockFamily == null) { LOGGER.warn("Prefab " + itemEntity.getParentPrefab().getName() + " does not have a block family"); return; } if (blockFamily.getArchetypeBlock().getCollisionShape() instanceof BoxShape) { BoxShape collisionShape = (BoxShape) blockFamily.getArchetypeBlock().getCollisionShape(); Vector3f extents = collisionShape.getHalfExtentsWithoutMargin(); extents.scale(2.0f); extents.x = Math.max(extents.x, 0.5f); extents.y = Math.max(extents.y, 0.5f); extents.z = Math.max(extents.z, 0.5f); boxShapeComponent.extents.set(extents); itemEntity.saveComponent(boxShapeComponent); } }
public Vector3f getWorldPosition(Vector3f output) { output.set(position); LocationComponent parentLoc = parent.getComponent(LocationComponent.class); while (parentLoc != null) { output.scale(parentLoc.scale); parentLoc.getLocalRotation().rotate(output, output); output.add(parentLoc.position); parentLoc = parentLoc.parent.getComponent(LocationComponent.class); } return output; }
public void setToExtrapolateState(EntityRef entity, CharacterStateEvent state, long time) { float t = (time - state.getTime()) * 0.0001f; Vector3f newPos = new Vector3f(state.getVelocity()); newPos.scale(t); newPos.add(state.getPosition()); extrapolateLocationComponent(entity, state, newPos); extrapolateCharacterMovementComponent(entity, state); setPhysicsLocation(entity, newPos); }
private void checkCollision(final ParticlePool pool, final int offset) { final Vector3f vel = new Vector3f(); final Vector3f halfVelDir = new Vector3f(); final Vector3f curr = new Vector3f(); for (int i = offset; i < pool.livingParticles(); i += PHYSICS_SKIP_NR) { int i3 = i * 3; curr.set(pool.position[i3 + 0], pool.position[i3 + 1], pool.position[i3 + 2]); vel.set(pool.velocity[i3 + 0], pool.velocity[i3 + 1], pool.velocity[i3 + 2]); halfVelDir.scale(0).add(vel).normalize().scale(0.5f); curr.sub(halfVelDir); float dist = (vel.length() + 0.5f) * movingAvgDelta * PHYSICS_SKIP_NR * 1.5f; vel.normalize(); HitResult hitResult = physics.rayTrace(curr, vel, dist, StandardCollisionGroup.WORLD); if (hitResult.isHit()) { pool.energy[i] = 0; } } }
public void setWorldPosition(Vector3f value) { this.position.set(value); LocationComponent parentLoc = parent.getComponent(LocationComponent.class); if (parentLoc != null) { this.position.sub(parentLoc.getWorldPosition()); this.position.scale(1f / parentLoc.getWorldScale()); Quat4f rot = new Quat4f(0, 0, 0, 1); rot.inverse(parentLoc.getWorldRotation()); rot.rotate(this.position, this.position); } }
private Vector3f extractResidualMovement(Vector3f hitNormal, Vector3f direction, float normalMag) { float movementLength = direction.length(); if (movementLength > physics.getEpsilon()) { direction.normalize(); Vector3f reflectDir = Vector3fUtil.reflect(direction, hitNormal, new Vector3f()); reflectDir.normalize(); Vector3f perpendicularDir = Vector3fUtil.getPerpendicularComponent(reflectDir, hitNormal, new Vector3f()); if (normalMag != 0.0f) { Vector3f perpComponent = new Vector3f(perpendicularDir); perpComponent.scale(normalMag * movementLength); direction.set(perpComponent); } } return direction; }
private void positionShadowMapCamera() { // We begin by setting our light coordinates at the player coordinates, ignoring the player's altitude Vector3f mainLightPosition = new Vector3f(activeCamera.getPosition().x, 0.0f, activeCamera.getPosition().z); // world-space coordinates // The shadow projected onto the ground must move in in light-space texel-steps, to avoid causing flickering. // That's why we first convert it to the previous frame's light-space coordinates and then back to world-space. shadowMapCamera.getViewProjectionMatrix().transformPoint(mainLightPosition); // to light-space mainLightPosition.set(TeraMath.fastFloor(mainLightPosition.x / texelSize) * texelSize, 0.0f, TeraMath.fastFloor(mainLightPosition.z / texelSize) * texelSize); shadowMapCamera.getInverseViewProjectionMatrix().transformPoint(mainLightPosition); // back to world-space // This is what causes the shadow map to change infrequently, to prevent flickering. // Notice that this is different from what is done above, which is about spatial steps // and is related to the player's position and texels. Vector3f quantizedMainLightDirection = getQuantizedMainLightDirection(STEP_SIZE); // The shadow map camera is placed away from the player, in the direction of the main light. Vector3f offsetFromPlayer = new Vector3f(quantizedMainLightDirection); offsetFromPlayer.scale(256.0f + 64.0f); // these hardcoded numbers are another mystery. mainLightPosition.add(offsetFromPlayer); shadowMapCamera.getPosition().set(mainLightPosition); // Finally, we adjust the shadow map camera to look toward the player Vector3f fromLightToPlayerDirection = new Vector3f(quantizedMainLightDirection); fromLightToPlayerDirection.scale(-1.0f); shadowMapCamera.getViewingDirection().set(fromLightToPlayerDirection); shadowMapCamera.update(worldRenderer.getSecondsSinceLastFrame()); }
@Command(shortDescription = "Mass-drops the desired block however many times the player indicates", helpText = "First parameter indicates which block to drop, second parameter how many", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION) public String bulkDrop(@Sender EntityRef sender, @CommandParam("blockName") String blockName, @CommandParam("value") int value) { //This is a loop which gives the particular amount of block the player wants to spawn ClientComponent clientComponent = sender.getComponent(ClientComponent.class); LocationComponent characterLocation = clientComponent.character.getComponent(LocationComponent.class); Vector3f spawnPos = characterLocation.getWorldPosition(); Vector3f offset = characterLocation.getWorldDirection(); offset.scale(3); spawnPos.add(5, 10, 0); BlockFamily block = blockManager.getBlockFamily(blockName); if (block == null) { return "Sorry, your block is not found"; } BlockItemFactory blockItemFactory = new BlockItemFactory(entityManager); if (value > 5000) { return "Value exceeds the maximum limit of 5000 blocks. your value: " + value + " blocks"; } for (int i = 0; i < value; i++) { EntityRef blockItem = blockItemFactory.newInstance(block); blockItem.send(new DropItemEvent(spawnPos)); } // this returns the block you have spawned and the amount return "Dropped " + value + " " + blockName + " Blocks :)"; }