Search in sources :

Example 31 with Node

use of com.jme3.scene.Node in project jmonkeyengine by jMonkeyEngine.

the class TestCustomMesh method simpleInitApp.

@Override
public void simpleInitApp() {
    Mesh m = new Mesh();
    // Vertex positions in space
    Vector3f[] vertices = new Vector3f[4];
    vertices[0] = new Vector3f(0, 0, 0);
    vertices[1] = new Vector3f(3, 0, 0);
    vertices[2] = new Vector3f(0, 3, 0);
    vertices[3] = new Vector3f(3, 3, 0);
    // Texture coordinates
    Vector2f[] texCoord = new Vector2f[4];
    texCoord[0] = new Vector2f(0, 0);
    texCoord[1] = new Vector2f(1, 0);
    texCoord[2] = new Vector2f(0, 1);
    texCoord[3] = new Vector2f(1, 1);
    // Indexes. We define the order in which mesh should be constructed
    short[] indexes = { 2, 0, 1, 1, 3, 2 };
    // Setting buffers
    m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
    m.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
    m.setBuffer(Type.Index, 1, BufferUtils.createShortBuffer(indexes));
    m.updateBound();
    // *************************************************************************
    // First mesh uses one solid color
    // *************************************************************************
    // Creating a geometry, and apply a single color material to it
    Geometry geom = new Geometry("OurMesh", m);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setColor("Color", ColorRGBA.Blue);
    geom.setMaterial(mat);
    // Attaching our geometry to the root node.
    rootNode.attachChild(geom);
    // *************************************************************************
    // Second mesh uses vertex colors to color each vertex
    // *************************************************************************
    Mesh cMesh = m.clone();
    Geometry coloredMesh = new Geometry("ColoredMesh", cMesh);
    Material matVC = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matVC.setBoolean("VertexColor", true);
    //We have 4 vertices and 4 color values for each of them.
    //If you have more vertices, you need 'new float[yourVertexCount * 4]' here!
    float[] colorArray = new float[4 * 4];
    int colorIndex = 0;
    //Set custom RGBA value for each Vertex. Values range from 0.0f to 1.0f
    for (int i = 0; i < 4; i++) {
        // Red value (is increased by .2 on each next vertex here)
        colorArray[colorIndex++] = 0.1f + (.2f * i);
        // Green value (is reduced by .2 on each next vertex)
        colorArray[colorIndex++] = 0.9f - (0.2f * i);
        // Blue value (remains the same in our case)
        colorArray[colorIndex++] = 0.5f;
        // Alpha value (no transparency set here)
        colorArray[colorIndex++] = 1.0f;
    }
    // Set the color buffer
    cMesh.setBuffer(Type.Color, 4, colorArray);
    coloredMesh.setMaterial(matVC);
    // move mesh a bit so that it doesn't intersect with the first one
    coloredMesh.setLocalTranslation(4, 0, 0);
    rootNode.attachChild(coloredMesh);
    //        /** Alternatively, you can show the mesh vertixes as points
    //          * instead of coloring the faces. */
    //        cMesh.setMode(Mesh.Mode.Points);
    //        cMesh.setPointSize(10f);
    //        cMesh.updateBound();
    //        cMesh.setStatic();
    //        Geometry points = new Geometry("Points", m);
    //        points.setMaterial(mat);
    //        rootNode.attachChild(points);
    // *************************************************************************
    // Third mesh will use a wireframe shader to show wireframe
    // *************************************************************************
    Mesh wfMesh = m.clone();
    Geometry wfGeom = new Geometry("wireframeGeometry", wfMesh);
    Material matWireframe = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matWireframe.setColor("Color", ColorRGBA.Green);
    matWireframe.getAdditionalRenderState().setWireframe(true);
    wfGeom.setMaterial(matWireframe);
    wfGeom.setLocalTranslation(4, 4, 0);
    rootNode.attachChild(wfGeom);
}
Also used : Geometry(com.jme3.scene.Geometry) Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f) Mesh(com.jme3.scene.Mesh) Material(com.jme3.material.Material)

Example 32 with Node

use of com.jme3.scene.Node in project jmonkeyengine by jMonkeyEngine.

the class TestAnimBlendBug method simpleInitApp.

@Override
public void simpleInitApp() {
    inputManager.addMapping("One", new KeyTrigger(KeyInput.KEY_1));
    inputManager.addListener(this, "One");
    flyCam.setMoveSpeed(100f);
    cam.setLocation(new Vector3f(0f, 150f, -325f));
    cam.lookAt(new Vector3f(0f, 100f, 0f), Vector3f.UNIT_Y);
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(new Vector3f(-0.1f, -0.7f, 1).normalizeLocal());
    dl.setColor(new ColorRGBA(1f, 1f, 1f, 1.0f));
    rootNode.addLight(dl);
    Node model1 = (Node) assetManager.loadModel("Models/Ninja/Ninja.mesh.xml");
    Node model2 = (Node) assetManager.loadModel("Models/Ninja/Ninja.mesh.xml");
    //        Node model2 = model1.clone();
    model1.setLocalTranslation(-60, 0, 0);
    model2.setLocalTranslation(60, 0, 0);
    AnimControl control1 = model1.getControl(AnimControl.class);
    animNames = control1.getAnimationNames().toArray(new String[0]);
    channel1 = control1.createChannel();
    AnimControl control2 = model2.getControl(AnimControl.class);
    channel2 = control2.createChannel();
    SkeletonDebugger skeletonDebug = new SkeletonDebugger("skeleton1", control1.getSkeleton());
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.getAdditionalRenderState().setWireframe(true);
    mat.setColor("Color", ColorRGBA.Green);
    mat.getAdditionalRenderState().setDepthTest(false);
    skeletonDebug.setMaterial(mat);
    model1.attachChild(skeletonDebug);
    skeletonDebug = new SkeletonDebugger("skeleton2", control2.getSkeleton());
    skeletonDebug.setMaterial(mat);
    model2.attachChild(skeletonDebug);
    rootNode.attachChild(model1);
    rootNode.attachChild(model2);
}
Also used : SkeletonDebugger(com.jme3.scene.debug.SkeletonDebugger) ColorRGBA(com.jme3.math.ColorRGBA) KeyTrigger(com.jme3.input.controls.KeyTrigger) Vector3f(com.jme3.math.Vector3f) DirectionalLight(com.jme3.light.DirectionalLight) Node(com.jme3.scene.Node) Material(com.jme3.material.Material) AnimControl(com.jme3.animation.AnimControl)

Example 33 with Node

use of com.jme3.scene.Node in project jmonkeyengine by jMonkeyEngine.

the class TestCustomAnim method simpleInitApp.

@Override
public void simpleInitApp() {
    AmbientLight al = new AmbientLight();
    rootNode.addLight(al);
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(Vector3f.UNIT_XYZ.negate());
    rootNode.addLight(dl);
    Box box = new Box(1, 1, 1);
    VertexBuffer weightsHW = new VertexBuffer(Type.HWBoneWeight);
    VertexBuffer indicesHW = new VertexBuffer(Type.HWBoneIndex);
    indicesHW.setUsage(Usage.CpuOnly);
    weightsHW.setUsage(Usage.CpuOnly);
    box.setBuffer(weightsHW);
    box.setBuffer(indicesHW);
    // Setup bone weight buffer
    FloatBuffer weights = FloatBuffer.allocate(box.getVertexCount() * 4);
    VertexBuffer weightsBuf = new VertexBuffer(Type.BoneWeight);
    weightsBuf.setupData(Usage.CpuOnly, 4, Format.Float, weights);
    box.setBuffer(weightsBuf);
    // Setup bone index buffer
    ByteBuffer indices = ByteBuffer.allocate(box.getVertexCount() * 4);
    VertexBuffer indicesBuf = new VertexBuffer(Type.BoneIndex);
    indicesBuf.setupData(Usage.CpuOnly, 4, Format.UnsignedByte, indices);
    box.setBuffer(indicesBuf);
    // Create bind pose buffers
    box.generateBindPose(true);
    // Create skeleton
    bone = new Bone("root");
    bone.setBindTransforms(Vector3f.ZERO, Quaternion.IDENTITY, Vector3f.UNIT_XYZ);
    bone.setUserControl(true);
    skeleton = new Skeleton(new Bone[] { bone });
    // Assign all verticies to bone 0 with weight 1
    for (int i = 0; i < box.getVertexCount() * 4; i += 4) {
        // assign vertex to bone index 0
        indices.array()[i + 0] = 0;
        indices.array()[i + 1] = 0;
        indices.array()[i + 2] = 0;
        indices.array()[i + 3] = 0;
        // set weight to 1 only for first entry
        weights.array()[i + 0] = 1;
        weights.array()[i + 1] = 0;
        weights.array()[i + 2] = 0;
        weights.array()[i + 3] = 0;
    }
    // Maximum number of weights per bone is 1
    box.setMaxNumWeights(1);
    // Create model
    Geometry geom = new Geometry("box", box);
    geom.setMaterial(assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m"));
    Node model = new Node("model");
    model.attachChild(geom);
    // Create skeleton control
    SkeletonControl skeletonControl = new SkeletonControl(skeleton);
    model.addControl(skeletonControl);
    rootNode.attachChild(model);
}
Also used : Geometry(com.jme3.scene.Geometry) VertexBuffer(com.jme3.scene.VertexBuffer) DirectionalLight(com.jme3.light.DirectionalLight) Node(com.jme3.scene.Node) SkeletonControl(com.jme3.animation.SkeletonControl) Box(com.jme3.scene.shape.Box) FloatBuffer(java.nio.FloatBuffer) Skeleton(com.jme3.animation.Skeleton) Bone(com.jme3.animation.Bone) ByteBuffer(java.nio.ByteBuffer) AmbientLight(com.jme3.light.AmbientLight)

Example 34 with Node

use of com.jme3.scene.Node in project jmonkeyengine by jMonkeyEngine.

the class TestHoverTank method simpleInitApp.

@Override
public void simpleInitApp() {
    Node tank = (Node) assetManager.loadModel("Models/HoverTank/Tank2.mesh.xml");
    flyCam.setEnabled(false);
    ChaseCamera chaseCam = new ChaseCamera(cam, tank, inputManager);
    chaseCam.setSmoothMotion(true);
    chaseCam.setMaxDistance(100000);
    chaseCam.setMinVerticalRotation(-FastMath.PI / 2);
    viewPort.setBackgroundColor(ColorRGBA.DarkGray);
    Geometry tankGeom = (Geometry) tank.getChild(0);
    LodControl control = new LodControl();
    tankGeom.addControl(control);
    rootNode.attachChild(tank);
    Vector3f lightDir = new Vector3f(-0.8719428f, -0.46824604f, 0.14304268f);
    DirectionalLight dl = new DirectionalLight();
    dl.setColor(new ColorRGBA(1.0f, 0.92f, 0.75f, 1f));
    dl.setDirection(lightDir);
    Vector3f lightDir2 = new Vector3f(0.70518064f, 0.5902297f, -0.39287305f);
    DirectionalLight dl2 = new DirectionalLight();
    dl2.setColor(new ColorRGBA(0.7f, 0.85f, 1.0f, 1f));
    dl2.setDirection(lightDir2);
    rootNode.addLight(dl);
    rootNode.addLight(dl2);
    rootNode.attachChild(tank);
    FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
    BloomFilter bf = new BloomFilter(BloomFilter.GlowMode.Objects);
    bf.setBloomIntensity(2.0f);
    bf.setExposurePower(1.3f);
    fpp.addFilter(bf);
    BloomUI bui = new BloomUI(inputManager, bf);
    viewPort.addProcessor(fpp);
}
Also used : Geometry(com.jme3.scene.Geometry) BloomUI(jme3test.post.BloomUI) LodControl(com.jme3.scene.control.LodControl) ColorRGBA(com.jme3.math.ColorRGBA) Node(com.jme3.scene.Node) Vector3f(com.jme3.math.Vector3f) DirectionalLight(com.jme3.light.DirectionalLight) ChaseCamera(com.jme3.input.ChaseCamera) FilterPostProcessor(com.jme3.post.FilterPostProcessor) BloomFilter(com.jme3.post.filters.BloomFilter)

Example 35 with Node

use of com.jme3.scene.Node in project jmonkeyengine by jMonkeyEngine.

the class PssmShadowRenderer method postQueue.

@SuppressWarnings("fallthrough")
public void postQueue(RenderQueue rq) {
    for (Spatial scene : viewPort.getScenes()) {
        ShadowUtil.getGeometriesInCamFrustum(scene, viewPort.getCamera(), ShadowMode.Receive, lightReceivers);
    }
    Camera viewCam = viewPort.getCamera();
    float zFar = zFarOverride;
    if (zFar == 0) {
        zFar = viewCam.getFrustumFar();
    }
    //We prevent computing the frustum points and splits with zeroed or negative near clip value
    float frustumNear = Math.max(viewCam.getFrustumNear(), 0.001f);
    ShadowUtil.updateFrustumPoints(viewCam, frustumNear, zFar, 1.0f, points);
    //shadowCam.setDirection(direction);
    shadowCam.getRotation().lookAt(direction, shadowCam.getUp());
    shadowCam.update();
    shadowCam.updateViewProjection();
    PssmShadowUtil.updateFrustumSplits(splitsArray, frustumNear, zFar, lambda);
    switch(splitsArray.length) {
        case 5:
            splits.a = splitsArray[4];
        case 4:
            splits.b = splitsArray[3];
        case 3:
            splits.g = splitsArray[2];
        case 2:
        case 1:
            splits.r = splitsArray[1];
            break;
    }
    Renderer r = renderManager.getRenderer();
    renderManager.setForcedMaterial(preshadowMat);
    renderManager.setForcedTechnique("PreShadow");
    for (int i = 0; i < nbSplits; i++) {
        // update frustum points based on current camera and split
        ShadowUtil.updateFrustumPoints(viewCam, splitsArray[i], splitsArray[i + 1], 1.0f, points);
        //Updating shadow cam with curent split frustra
        ShadowUtil.updateShadowCamera(viewPort, lightReceivers, shadowCam, points, splitOccluders, shadowMapSize);
        //saving light view projection matrix for this split            
        lightViewProjectionsMatrices[i].set(shadowCam.getViewProjectionMatrix());
        renderManager.setCamera(shadowCam, false);
        if (debugfrustums) {
            //                    frustrumFromBound(b.casterBB,ColorRGBA.Blue );
            //                    frustrumFromBound(b.receiverBB,ColorRGBA.Green );
            //                    frustrumFromBound(b.splitBB,ColorRGBA.Yellow );
            ((Node) viewPort.getScenes().get(0)).attachChild(createFrustum(points, i));
            ShadowUtil.updateFrustumPoints2(shadowCam, points);
            ((Node) viewPort.getScenes().get(0)).attachChild(createFrustum(points, i));
        }
        r.setFrameBuffer(shadowFB[i]);
        r.clearBuffers(true, true, true);
        // render shadow casters to shadow map
        viewPort.getQueue().renderShadowQueue(splitOccluders, renderManager, shadowCam, true);
    }
    debugfrustums = false;
    //restore setting for future rendering
    r.setFrameBuffer(viewPort.getOutputFrameBuffer());
    renderManager.setForcedMaterial(null);
    renderManager.setForcedTechnique(null);
    renderManager.setCamera(viewCam, false);
}
Also used : Spatial(com.jme3.scene.Spatial) Node(com.jme3.scene.Node) Renderer(com.jme3.renderer.Renderer) Camera(com.jme3.renderer.Camera)

Aggregations

Node (com.jme3.scene.Node)135 Vector3f (com.jme3.math.Vector3f)81 Geometry (com.jme3.scene.Geometry)64 Spatial (com.jme3.scene.Spatial)53 Material (com.jme3.material.Material)51 DirectionalLight (com.jme3.light.DirectionalLight)35 Quaternion (com.jme3.math.Quaternion)32 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)26 Box (com.jme3.scene.shape.Box)24 Sphere (com.jme3.scene.shape.Sphere)19 AmbientLight (com.jme3.light.AmbientLight)17 BulletAppState (com.jme3.bullet.BulletAppState)16 ColorRGBA (com.jme3.math.ColorRGBA)15 AnimControl (com.jme3.animation.AnimControl)14 KeyTrigger (com.jme3.input.controls.KeyTrigger)14 FilterPostProcessor (com.jme3.post.FilterPostProcessor)14 HashMap (java.util.HashMap)14 CameraNode (com.jme3.scene.CameraNode)13 ArrayList (java.util.ArrayList)13 ActionListener (com.jme3.input.controls.ActionListener)12