use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.
the class Curve method createNurbMesh.
/**
* This method creates the Nurb path for this curve.
*
* @param nbSubSegments amount of subsegments between position control
* points
*/
private void createNurbMesh(int nbSubSegments) {
if (spline.getControlPoints() != null && spline.getControlPoints().size() > 0) {
if (nbSubSegments == 0) {
nbSubSegments = spline.getControlPoints().size() + 1;
} else {
nbSubSegments = spline.getControlPoints().size() * nbSubSegments + 1;
}
float minKnot = spline.getMinNurbKnot();
float maxKnot = spline.getMaxNurbKnot();
float deltaU = (maxKnot - minKnot) / nbSubSegments;
float[] array = new float[(nbSubSegments + 1) * 3];
float u = minKnot;
Vector3f interpolationResult = new Vector3f();
for (int i = 0; i < array.length; i += 3) {
spline.interpolate(u, 0, interpolationResult);
array[i] = interpolationResult.x;
array[i + 1] = interpolationResult.y;
array[i + 2] = interpolationResult.z;
u += deltaU;
}
//calculating indexes
int i = 0;
short[] indices = new short[nbSubSegments << 1];
for (int j = 0; j < nbSubSegments; ++j) {
indices[i++] = (short) j;
indices[i++] = (short) (j + 1);
}
this.setMode(Mesh.Mode.Lines);
this.setBuffer(VertexBuffer.Type.Position, 3, array);
this.setBuffer(VertexBuffer.Type.Index, 2, indices);
this.updateBound();
this.updateCounts();
}
}
use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.
the class HelloTerrain method simpleInitApp.
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(50);
/** 1. Create terrain material and load four textures into it. */
mat_terrain = new Material(assetManager, "Common/MatDefs/Terrain/Terrain.j3md");
/** 1.1) Add ALPHA map (for red-blue-green coded splat textures) */
mat_terrain.setTexture("Alpha", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));
/** 1.2) Add GRASS texture into the red layer (Tex1). */
Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
grass.setWrap(WrapMode.Repeat);
mat_terrain.setTexture("Tex1", grass);
mat_terrain.setFloat("Tex1Scale", 64f);
/** 1.3) Add DIRT texture into the green layer (Tex2) */
Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
dirt.setWrap(WrapMode.Repeat);
mat_terrain.setTexture("Tex2", dirt);
mat_terrain.setFloat("Tex2Scale", 32f);
/** 1.4) Add ROAD texture into the blue layer (Tex3) */
Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
rock.setWrap(WrapMode.Repeat);
mat_terrain.setTexture("Tex3", rock);
mat_terrain.setFloat("Tex3Scale", 128f);
/** 2.a Create a custom height map from an image */
AbstractHeightMap heightmap = null;
Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
heightmap = new ImageBasedHeightMap(heightMapImage.getImage());
/** 2.b Create a random height map */
// HillHeightMap heightmap = null;
// HillHeightMap.NORMALIZE_RANGE = 100;
// try {
// heightmap = new HillHeightMap(513, 1000, 50, 100, (byte) 3);
// } catch (Exception ex) {
// ex.printStackTrace();
// }
heightmap.load();
/** 3. We have prepared material and heightmap.
* Now we create the actual terrain:
* 3.1) Create a TerrainQuad and name it "my terrain".
* 3.2) A good value for terrain tiles is 64x64 -- so we supply 64+1=65.
* 3.3) We prepared a heightmap of size 512x512 -- so we supply 512+1=513.
* 3.4) As LOD step scale we supply Vector3f(1,1,1).
* 3.5) We supply the prepared heightmap itself.
*/
int patchSize = 65;
terrain = new TerrainQuad("my terrain", patchSize, 513, heightmap.getHeightMap());
/** 4. We give the terrain its material, position & scale it, and attach it. */
terrain.setMaterial(mat_terrain);
terrain.setLocalTranslation(0, -100, 0);
terrain.setLocalScale(2f, 1f, 2f);
rootNode.attachChild(terrain);
/** 5. The LOD (level of detail) depends on were the camera is: */
TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
// patch size, and a multiplier
control.setLodCalculator(new DistanceLodCalculator(patchSize, 2.7f));
terrain.addControl(control);
}
use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.
the class VRAppState method update.
@Override
public void update(float tpf) {
// update VR pose & cameras
if (environment.getVRViewManager() != null) {
environment.getVRViewManager().update(tpf);
} else if (environment.getObserver() != null) {
environment.getCamera().setFrame(((Spatial) environment.getObserver()).getWorldTranslation(), ((Spatial) environment.getObserver()).getWorldRotation());
}
//FIXME: check if this code is necessary.
// Updates scene and gui states.
Iterator<Spatial> spatialIter = application.getViewPort().getScenes().iterator();
Spatial spatial = null;
while (spatialIter.hasNext()) {
spatial = spatialIter.next();
spatial.updateLogicalState(tpf);
spatial.updateGeometricState();
}
if (environment.isInVR() == false || environment.getVRGUIManager().getPositioningMode() == VRGUIPositioningMode.MANUAL) {
// only update geometric state here if GUI is in manual mode, or not in VR
// it will get updated automatically in the viewmanager update otherwise
spatialIter = application.getGuiViewPort().getScenes().iterator();
spatial = null;
while (spatialIter.hasNext()) {
spatial = spatialIter.next();
spatial.updateGeometricState();
}
}
// use the analog control on the first tracked controller to push around the mouse
environment.getVRMouseManager().updateAnalogAsMouse(0, null, null, null, tpf);
}
use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.
the class Spatial method oldClone.
/**
* The old clone() method that did not use the new Cloner utility.
*/
public Spatial oldClone(boolean cloneMaterial) {
try {
Spatial clone = (Spatial) super.clone();
if (worldBound != null) {
clone.worldBound = worldBound.clone();
}
clone.worldLights = worldLights.clone();
clone.localLights = localLights.clone();
// Set the new owner of the light lists
clone.localLights.setOwner(clone);
clone.worldLights.setOwner(clone);
clone.worldOverrides = new SafeArrayList<>(MatParamOverride.class);
clone.localOverrides = new SafeArrayList<>(MatParamOverride.class);
for (MatParamOverride override : localOverrides) {
clone.localOverrides.add((MatParamOverride) override.clone());
}
// No need to force cloned to update.
// This node already has the refresh flags
// set below so it will have to update anyway.
clone.worldTransform = worldTransform.clone();
clone.localTransform = localTransform.clone();
if (clone instanceof Node) {
Node node = (Node) this;
Node nodeClone = (Node) clone;
nodeClone.children = new SafeArrayList<Spatial>(Spatial.class);
for (Spatial child : node.children) {
Spatial childClone = child.clone(cloneMaterial);
childClone.parent = nodeClone;
nodeClone.children.add(childClone);
}
}
clone.parent = null;
clone.setBoundRefresh();
clone.setTransformRefresh();
clone.setLightListRefresh();
clone.setMatParamOverrideRefresh();
clone.controls = new SafeArrayList<Control>(Control.class);
for (int i = 0; i < controls.size(); i++) {
Control newControl = controls.get(i).cloneForSpatial(clone);
newControl.setSpatial(clone);
clone.controls.add(newControl);
}
if (userData != null) {
clone.userData = (HashMap<String, Savable>) userData.clone();
}
return clone;
} catch (CloneNotSupportedException ex) {
throw new AssertionError();
}
}
use of com.jme3.scene.control.Control in project jmonkeyengine by jMonkeyEngine.
the class RollingTheMonkey method simpleInitApp.
@Override
public void simpleInitApp() {
flyCam.setEnabled(false);
cam.setLocation(new Vector3f(0.0f, 12.0f, 21.0f));
viewPort.setBackgroundColor(new ColorRGBA(0.2118f, 0.0824f, 0.6549f, 1.0f));
// init physics
BulletAppState bulletState = new BulletAppState();
stateManager.attach(bulletState);
space = bulletState.getPhysicsSpace();
space.addCollisionListener(this);
// create light
DirectionalLight sun = new DirectionalLight();
sun.setDirection((new Vector3f(-0.7f, -0.3f, -0.5f)).normalizeLocal());
System.out.println("Here We Go: " + sun.getDirection());
sun.setColor(ColorRGBA.White);
rootNode.addLight(sun);
// create materials
Material materialRed = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
materialRed.setBoolean("UseMaterialColors", true);
materialRed.setBoolean("HardwareShadows", true);
materialRed.setColor("Diffuse", new ColorRGBA(0.9451f, 0.0078f, 0.0314f, 1.0f));
materialRed.setColor("Specular", ColorRGBA.White);
materialRed.setFloat("Shininess", 64.0f);
Material materialGreen = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
materialGreen.setBoolean("UseMaterialColors", true);
materialGreen.setBoolean("HardwareShadows", true);
materialGreen.setColor("Diffuse", new ColorRGBA(0.0431f, 0.7725f, 0.0078f, 1.0f));
materialGreen.setColor("Specular", ColorRGBA.White);
materialGreen.setFloat("Shininess", 64.0f);
Material logoMaterial = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
logoMaterial.setBoolean("UseMaterialColors", true);
logoMaterial.setBoolean("HardwareShadows", true);
logoMaterial.setTexture("DiffuseMap", assetManager.loadTexture("com/jme3/app/Monkey.png"));
logoMaterial.setColor("Diffuse", ColorRGBA.White);
logoMaterial.setColor("Specular", ColorRGBA.White);
logoMaterial.setFloat("Shininess", 32.0f);
Material materialYellow = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
materialYellow.setBoolean("UseMaterialColors", true);
materialYellow.setBoolean("HardwareShadows", true);
materialYellow.setColor("Diffuse", new ColorRGBA(0.9529f, 0.7843f, 0.0078f, 1.0f));
materialYellow.setColor("Specular", ColorRGBA.White);
materialYellow.setFloat("Shininess", 64.0f);
// create level spatial
// TODO: create your own level mesh
Node level = new Node("level");
level.setShadowMode(ShadowMode.CastAndReceive);
Geometry floor = new Geometry("floor", new Box(22.0f, 0.5f, 22.0f));
floor.setShadowMode(ShadowMode.Receive);
floor.setLocalTranslation(0.0f, -0.5f, 0.0f);
floor.setMaterial(materialGreen);
Geometry wallNorth = new Geometry("wallNorth", new Box(22.0f, 2.0f, 0.5f));
wallNorth.setLocalTranslation(0.0f, 2.0f, 21.5f);
wallNorth.setMaterial(materialRed);
Geometry wallSouth = new Geometry("wallSouth", new Box(22.0f, 2.0f, 0.5f));
wallSouth.setLocalTranslation(0.0f, 2.0f, -21.5f);
wallSouth.setMaterial(materialRed);
Geometry wallEast = new Geometry("wallEast", new Box(0.5f, 2.0f, 21.0f));
wallEast.setLocalTranslation(-21.5f, 2.0f, 0.0f);
wallEast.setMaterial(materialRed);
Geometry wallWest = new Geometry("wallWest", new Box(0.5f, 2.0f, 21.0f));
wallWest.setLocalTranslation(21.5f, 2.0f, 0.0f);
wallWest.setMaterial(materialRed);
level.attachChild(floor);
level.attachChild(wallNorth);
level.attachChild(wallSouth);
level.attachChild(wallEast);
level.attachChild(wallWest);
// The easy way: level.addControl(new RigidBodyControl(0));
// create level Shape
CompoundCollisionShape levelShape = new CompoundCollisionShape();
BoxCollisionShape floorShape = new BoxCollisionShape(new Vector3f(22.0f, 0.5f, 22.0f));
BoxCollisionShape wallNorthShape = new BoxCollisionShape(new Vector3f(22.0f, 2.0f, 0.5f));
BoxCollisionShape wallSouthShape = new BoxCollisionShape(new Vector3f(22.0f, 2.0f, 0.5f));
BoxCollisionShape wallEastShape = new BoxCollisionShape(new Vector3f(0.5f, 2.0f, 21.0f));
BoxCollisionShape wallWestShape = new BoxCollisionShape(new Vector3f(0.5f, 2.0f, 21.0f));
levelShape.addChildShape(floorShape, new Vector3f(0.0f, -0.5f, 0.0f));
levelShape.addChildShape(wallNorthShape, new Vector3f(0.0f, 2.0f, -21.5f));
levelShape.addChildShape(wallSouthShape, new Vector3f(0.0f, 2.0f, 21.5f));
levelShape.addChildShape(wallEastShape, new Vector3f(-21.5f, 2.0f, 0.0f));
levelShape.addChildShape(wallEastShape, new Vector3f(21.5f, 2.0f, 0.0f));
level.addControl(new RigidBodyControl(levelShape, 0));
rootNode.attachChild(level);
space.addAll(level);
// create Pickups
// TODO: create your own pickUp mesh
// create single mesh for all pickups
// HINT: think particles.
pickUps = new Node("pickups");
Quaternion rotation = new Quaternion();
Vector3f translation = new Vector3f(0.0f, PICKUP_SIZE * 1.5f, -PICKUP_RADIUS);
int index = 0;
float ammount = FastMath.TWO_PI / PICKUP_COUNT;
for (float angle = 0; angle < FastMath.TWO_PI; angle += ammount) {
Geometry pickUp = new Geometry("pickUp" + (index++), new Box(PICKUP_SIZE, PICKUP_SIZE, PICKUP_SIZE));
pickUp.setShadowMode(ShadowMode.CastAndReceive);
pickUp.setMaterial(materialYellow);
pickUp.setLocalRotation(rotation.fromAngles(FastMath.rand.nextFloat() * FastMath.TWO_PI, FastMath.rand.nextFloat() * FastMath.TWO_PI, FastMath.rand.nextFloat() * FastMath.TWO_PI));
rotation.fromAngles(0.0f, angle, 0.0f);
rotation.mult(translation, pickUp.getLocalTranslation());
pickUps.attachChild(pickUp);
pickUp.addControl(new GhostControl(new SphereCollisionShape(PICKUP_SIZE)));
space.addAll(pickUp);
//space.addCollisionListener(pickUpControl);
}
rootNode.attachChild(pickUps);
// Create player
// TODO: create your own player mesh
Geometry playerGeometry = new Geometry("player", new Sphere(16, 32, PLAYER_RADIUS));
playerGeometry.setShadowMode(ShadowMode.CastAndReceive);
playerGeometry.setLocalTranslation(PLAYER_START.clone());
playerGeometry.setMaterial(logoMaterial);
// Store control for applying forces
// TODO: create your own player control
player = new RigidBodyControl(new SphereCollisionShape(PLAYER_RADIUS), PLAYER_MASS);
player.setRestitution(PLAYER_REST);
playerGeometry.addControl(player);
rootNode.attachChild(playerGeometry);
space.addAll(playerGeometry);
inputManager.addMapping(INPUT_MAPPING_FORWARD, new KeyTrigger(KeyInput.KEY_UP), new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping(INPUT_MAPPING_BACKWARD, new KeyTrigger(KeyInput.KEY_DOWN), new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping(INPUT_MAPPING_LEFT, new KeyTrigger(KeyInput.KEY_LEFT), new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping(INPUT_MAPPING_RIGHT, new KeyTrigger(KeyInput.KEY_RIGHT), new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping(INPUT_MAPPING_RESET, new KeyTrigger(KeyInput.KEY_R));
inputManager.addListener(this, INPUT_MAPPING_FORWARD, INPUT_MAPPING_BACKWARD, INPUT_MAPPING_LEFT, INPUT_MAPPING_RIGHT, INPUT_MAPPING_RESET);
// init UI
infoText = new BitmapText(guiFont, false);
infoText.setText(INFO_MESSAGE);
guiNode.attachChild(infoText);
scoreText = new BitmapText(guiFont, false);
scoreText.setText("Score: 0");
guiNode.attachChild(scoreText);
messageText = new BitmapText(guiFont, false);
messageText.setText(MESSAGE);
messageText.setLocalScale(0.0f);
guiNode.attachChild(messageText);
infoText.setLocalTranslation(0.0f, cam.getHeight(), 0.0f);
scoreText.setLocalTranslation((cam.getWidth() - scoreText.getLineWidth()) / 2.0f, scoreText.getLineHeight(), 0.0f);
messageText.setLocalTranslation((cam.getWidth() - messageText.getLineWidth()) / 2.0f, (cam.getHeight() - messageText.getLineHeight()) / 2, 0.0f);
// init shadows
FilterPostProcessor processor = new FilterPostProcessor(assetManager);
DirectionalLightShadowFilter filter = new DirectionalLightShadowFilter(assetManager, 2048, 1);
filter.setLight(sun);
processor.addFilter(filter);
viewPort.addProcessor(processor);
}
Aggregations