spatial.java

来自「java 3d game jme 工程开发源代码」· Java 代码 · 共 1,773 行 · 第 1/4 页

JAVA
1,773
字号
     * the spatial.
     * 
     * @return the world's tranlsation vector.
     */
    public Vector3f getWorldTranslation() {
        return worldTranslation;
    }

    /**
     * <code>getWorldScale</code> retrieves the absolute scale factor of the
     * spatial.
     * 
     * @return the world's scale factor.
     */
    public Vector3f getWorldScale() {
        return worldScale;
    }

    /**
     * <code>rotateUpTo</code> is a util function that alters the
     * localrotation to point the Y axis in the direction given by newUp.
     * 
     * @param newUp
     *            the up vector to use - assumed to be a unit vector.
     */
    public void rotateUpTo(Vector3f newUp) {
        // First figure out the current up vector.
        Vector3f upY = compVecA.set(Vector3f.UNIT_Y);
        localRotation.multLocal(upY);

        // get angle between vectors
        float angle = upY.angleBetween(newUp);

        // figure out rotation axis by taking cross product
        Vector3f rotAxis = upY.crossLocal(newUp).normalizeLocal();

        // Build a rotation quat and apply current local rotation.
        Quaternion q = compQuat;
        q.fromAngleNormalAxis(angle, rotAxis);
        q.mult(localRotation, localRotation);
    }

    /**
     * <code>lookAt</code> is a convienence method for auto-setting the local
     * rotation based on a position and an up vector. It computes the rotation
     * to transform the z-axis to point onto 'position' and the y-axis to 'up'.
     * Unlike {@link Quaternion#lookAt} this method takes a world position to
     * look at not a relative direction.
     * 
     * @param position
     *            where to look at in terms of world coordinates
     * @param upVector
     *            a vector indicating the (local) up direction. (typically {0,
     *            1, 0} in jME.)
     */
    public void lookAt(Vector3f position, Vector3f upVector) {
        compVecA.set(position).subtractLocal(getWorldTranslation());
        getLocalRotation().lookAt(compVecA, upVector);
    }

    /**
     * <code>updateGeometricState</code> updates all the geometry information
     * for the node.
     * 
     * @param time
     *            the frame time.
     * @param initiator
     *            true if this node started the update process.
     */
    public void updateGeometricState(float time, boolean initiator) {
        if ((lockedMode & Spatial.LOCKED_BRANCH) != 0)
            return;
        updateWorldData(time);
        if ((lockedMode & Spatial.LOCKED_BOUNDS) == 0) {
            updateWorldBound();
            if (initiator) {
                propagateBoundToRoot();
            }
        }
    }

    /**
     * <code>updateWorldData</code> updates the world transforms from the
     * parent down to the leaf.
     * 
     * @param time
     *            the frame time.
     */
    public void updateWorldData(float time) {
        // update spatial state via controllers
        if (geometricalControllers != null) {
            for (int i = 0, gSize = geometricalControllers.size(); i < gSize; i++) {
                try {
                    Controller controller = geometricalControllers.get(i);
                    if (controller != null) {
                        if (controller.isActive()) {
                            controller.update(time);
                        }
                    }
                } catch (IndexOutOfBoundsException e) {
                    // a controller was removed in Controller.update (note: this
                    // may skip one controller)
                    break;
                }
            }
        }

        updateWorldVectors();
    }

    /**
     * If not locked, updates worldscale, worldrotation and worldtranslation
     */
    public void updateWorldVectors() {
        updateWorldVectors(false);
    }

    /**
     * If not locked, updates worldscale, worldrotation and worldtranslation
     * 
     * @param recurse
     *            usually false when updating the tree. Set to true when you
     *            just want to update the world transforms for a branch without
     *            updating geometric state.
     */
    public void updateWorldVectors(boolean recurse) {
        if (((lockedMode & Spatial.LOCKED_TRANSFORMS) == 0)) {
            updateWorldScale();
            updateWorldRotation();
            updateWorldTranslation();
        }
    }

    protected void updateWorldTranslation() {
        if (parent != null) {
            worldTranslation = parent.localToWorld(localTranslation,
                    worldTranslation);
        } else {
            worldTranslation.set(localTranslation);
        }
    }

    protected void updateWorldRotation() {
        if (parent != null) {
            parent.getWorldRotation().mult(localRotation, worldRotation);
        } else {
            worldRotation.set(localRotation);
        }
    }

    protected void updateWorldScale() {
        if (parent != null) {
            worldScale.set(parent.getWorldScale()).multLocal(localScale);
        } else {
            worldScale.set(localScale);
        }
    }

    /**
     * Convert a vector (in) from this spatials local coordinate space to world
     * coordinate space.
     * 
     * @param in
     *            vector to read from
     * @param store
     *            where to write the result (null to create a new vector, may be
     *            same as in)
     * @return the result (store)
     */
    public Vector3f localToWorld(final Vector3f in, Vector3f store) {
        if (store == null)
            store = new Vector3f();
        // multiply with scale first, then rotate, finally translate (cf.
        // Eberly)
        return getWorldRotation().mult(
                store.set(in).multLocal(getWorldScale()), store).addLocal(
                getWorldTranslation());
    }

    /**
     * Convert a vector (in) from world coordinate space to this spatials local
     * coordinate space.
     * 
     * @param in
     *            vector to read from
     * @param store
     *            where to write the result
     * @return the result (store)
     */
    public Vector3f worldToLocal(final Vector3f in, final Vector3f store) {
        in.subtract(getWorldTranslation(), store).divideLocal(getWorldScale());
        getWorldRotation().inverse().mult(store, store);
        return store;
    }

    /**
     * <code>getParent</code> retrieve's this node's parent. If the parent is
     * null this is the root node.
     * 
     * @return the parent of this node.
     */
    public Node getParent() {
        return parent;
    }

    /**
     * Called by {@link Node#attachChild(Spatial)} and
     * {@link Node#detachChild(Spatial)} - don't call directly.
     * <code>setParent</code> sets the parent of this node.
     * 
     * @param parent
     *            the parent of this node.
     */
    protected void setParent(Node parent) {
        this.parent = parent;
    }

    /**
     * <code>removeFromParent</code> removes this Spatial from it's parent.
     * 
     * @return true if it has a parent and performed the remove.
     */
    public boolean removeFromParent() {
        if (parent != null) {
            parent.detachChild(this);
            return true;
        }
        return false;
    }

    /**
     * determines if the provided Node is the parent, or parent's parent, etc. of this Spatial.
     * 
     * @param ancestor
     *            the ancestor object to look for.
     * @return true if the ancestor is found, false otherwise.
     */
    public boolean hasAncestor(Node ancestor) {
        if (parent == null) {
            return false;
        } else if (parent.equals(ancestor)) {
            return true;
        } else {
            return parent.hasAncestor(ancestor);
        }
    }
    
    /**
     * <code>getLocalRotation</code> retrieves the local rotation of this
     * node.
     * 
     * @return the local rotation of this node.
     */
    public Quaternion getLocalRotation() {
        return localRotation;
    }

    /**
     * <code>setLocalRotation</code> sets the local rotation of this node.
     * 
     * @param rotation
     *            the new local rotation.
     */
    public void setLocalRotation(Matrix3f rotation) {
        if (localRotation == null)
            localRotation = new Quaternion();
        localRotation.fromRotationMatrix(rotation);
        this.worldRotation.set(this.localRotation);
    }

    /**
     * <code>setLocalRotation</code> sets the local rotation of this node,
     * using a quaterion to build the matrix.
     * 
     * @param quaternion
     *            the quaternion that defines the matrix.
     */
    public void setLocalRotation(Quaternion quaternion) {
        localRotation = quaternion;
        this.worldRotation.set(this.localRotation);
    }

    /**
     * <code>getLocalScale</code> retrieves the local scale of this node.
     * 
     * @return the local scale of this node.
     */
    public Vector3f getLocalScale() {
        return localScale;
    }

    /**
     * <code>setLocalScale</code> sets the local scale of this node.
     * 
     * @param localScale
     *            the new local scale, applied to x, y and z
     */
    public void setLocalScale(float localScale) {
        this.localScale.x = localScale;
        this.localScale.y = localScale;
        this.localScale.z = localScale;
        this.worldScale.set(this.localScale);
    }

    /**
     * <code>setLocalScale</code> sets the local scale of this node.
     * 
     * @param localScale
     *            the new local scale.
     */
    public void setLocalScale(Vector3f localScale) {
        this.localScale = localScale;
        this.worldScale.set(this.localScale);
    }

    /**
     * <code>getLocalTranslation</code> retrieves the local translation of
     * this node.
     * 
     * @return the local translation of this node.
     */
    public Vector3f getLocalTranslation() {
        return localTranslation;
    }

    /**
     * <code>setLocalTranslation</code> sets the local translation of this
     * node.
     * 
     * @param localTranslation
     *            the local translation of this node.
     */
    public void setLocalTranslation(Vector3f localTranslation) {
        this.localTranslation = localTranslation;
        this.worldTranslation.set(this.localTranslation);
    }

    public void setLocalTranslation(float x, float y, float z) {
        localTranslation.set(x, y, z);
        worldTranslation.set(localTranslation);
    }

    /**
     * Sets the zOrder of this Spatial and, if setOnChildren is true, all
     * children as well. This value is used in conjunction with the RenderQueue
     * and QUEUE_ORTHO for determining draw order.
     * 
     * @param zOrder
     *            the new zOrder.
     * @param setOnChildren
     *            if true, children will also have their zOrder set to the given
     *            value.
     */
    public void setZOrder(int zOrder, boolean setOnChildren) {
        setZOrder(zOrder);
        if (setOnChildren) {
            if (this instanceof Node) {
                Node n = (Node) this;
                if (n.getChildren() != null) {
                    for (Spatial child : n.getChildren()) {
                        child.setZOrder(zOrder, true);
                    }
                }
            }
        }
    }

    /**
     * @see #setCullHint(CullHint)
     * @return the cull mode of this spatial, or if set to INHERIT, the cullmode
     *         of it's parent.
     */
    public CullHint getCullHint() {
        if (cullHint != CullHint.Inherit)
            return cullHint;
        else if (parent != null)
            return parent.getCullHint();
        else
            return CullHint.Dynamic;
    }

    /**
     * Returns this spatial's texture combine mode. If the mode is set to
     * inherit, then the spatial gets its combine mode from its parent.
     * 
     * @return The spatial's texture current combine mode.
     */
    public TextureCombineMode getTextureCombineMode() {
        if (textureCombineMode != TextureCombineMode.Inherit)
            return textureCombineMode;
        else if (parent != null)
            return parent.getTextureCombineMode();
        else
            return TextureCombineMode.CombineClosest;
    }

    /**
     * Returns this spatial's light combine mode. If the mode is set to inherit,
     * then the spatial gets its combine mode from its parent.
     * 
     * @return The spatial's light current combine mode.
     */
    public LightCombineMode getLightCombineMode() {
        if (lightCombineMode != LightCombineMode.Inherit)
            return lightCombineMode;
        else if (parent != null)
            return parent.getLightCombineMode();
        else
            return LightCombineMode.CombineFirst;
    }

    /**
     * Returns this spatial's renderqueue mode. If the mode is set to inherit,
     * then the spatial gets its renderqueue mode from its parent.
     * 
     * @return The spatial's current renderqueue mode.
     */
    public int getRenderQueueMode() {
        if (renderQueueMode != Renderer.QUEUE_INHERIT)
            return renderQueueMode;
        else if (parent != null)
            return parent.getRenderQueueMode();
        else
            return Renderer.QUEUE_SKIP;
    }

    /**
     * Returns this spatial's normals mode. If the mode is set to inherit, then
     * the spatial gets its normals mode from its parent.
     * 
     * @return The spatial's current normals mode.
     */
    public NormalsMode getNormalsMode() {
        if (normalsMode != NormalsMode.Inherit)
            return normalsMode;
        else if (parent != null)
            return parent.getNormalsMode();
        else
            return NormalsMode.NormalizeIfScaled;
    }

    /**
     * Called during updateRenderState(Stack[]), this function goes up the scene
     * graph tree until the parent is null and pushes RenderStates onto the

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?