sharedmesh.java

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

JAVA
639
字号
/*
 * Copyright (c) 2003-2009 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.jme.scene;

import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.logging.Logger;

import com.jme.bounding.BoundingVolume;
import com.jme.math.Ray;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.state.RenderState;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;

/**
 * <code>SharedMesh</code> allows the sharing of data between multiple nodes.
 * A provided TriMesh is used as the model for this node. This allows the user
 * to place multiple copies of the same object throughout the scene without
 * having to duplicate data. It should be known that any change to the provided
 * target mesh will affect the appearance of this mesh, including animations.
 * Secondly, the SharedMesh is read only. Any attempt to write to the mesh data
 * via set* methods, will result in a warning being logged and nothing else. Any
 * changes to the mesh should happened to the target mesh being shared. <br>
 * If you plan to use collisions with a <code>SharedMesh</code> it is
 * recommended that you disable the passing of <code>updateCollisionTree</code>
 * calls to the target mesh. This is to prevent multiple calls to the target's
 * <code>updateCollisionTree</code> method, from different shared meshes.
 * Instead of this method being called from the scenegraph, you can now invoke
 * it directly on the target mesh, thus ensuring it will only be invoked once.
 * <br>
 * <b>Important:</b> It is highly recommended that the Target mesh is NOT
 * placed into the scenegraph, as its translation, rotation and scale are
 * replaced by the shared meshes using it before they are rendered. <br>
 * <b>Note:</b> Special thanks to Kevin Glass.
 * 
 * @author Mark Powell
 * @version $id$
 */
public class SharedMesh extends TriMesh {
    private static final Logger logger = Logger.getLogger(SharedMesh.class
            .getName());

    private static final long serialVersionUID = 1L;

    private TriMesh target;

    public SharedMesh() {
        super();
        defaultColor = null;
    }

	/**
	 * Constructor creates a new <code>SharedMesh</code> object. Uses the name
	 * of the target.
	 * 
	 * @param target
	 *            the TriMesh to share the data.
	 */
    public SharedMesh(TriMesh target) {
        this(target.getName(), target);
    }

    /**
     * Constructor creates a new <code>SharedMesh</code> object.
     * 
     * @param name
     *            the name of this shared mesh.
     * @param target
     *            the TriMesh to share the data.
     */
    public SharedMesh(String name, TriMesh target) {

        super(name);
        defaultColor = null;

        if (target instanceof SharedMesh) {
            setTarget(((SharedMesh) target).getTarget());
            this.setName(target.getName());
            this.setCullHint(target.cullHint);
            this.setLightCombineMode(target.lightCombineMode);
            this.getLocalRotation().set(target.getLocalRotation());
            this.getLocalScale().set(target.getLocalScale());
            this.getLocalTranslation().set(target.getLocalTranslation());
            this.setRenderQueueMode(target.renderQueueMode);
            this.setTextureCombineMode(target.textureCombineMode);
            this.setZOrder(target.getZOrder());
            for (RenderState.StateType type : RenderState.StateType.values()) {
                RenderState state = target.getRenderState( type );
                if (state != null) {
                    this.setRenderState(state );
                }
            }
        } else {
            setTarget(target);
        }

        this.localRotation.set(target.getLocalRotation());
        this.localScale.set(target.getLocalScale());
        this.localTranslation.set(target.getLocalTranslation());
    }

    /**
     * <code>setTarget</code> sets the shared data mesh.
     * 
     * @param target
     *            the TriMesh to share the data.
     */
    public void setTarget(TriMesh target) {
        this.target = target;
        UserDataManager.getInstance().bind(this, target);
        for (RenderState.StateType type : RenderState.StateType.values()) {
            RenderState state = this.target.getRenderState( type );
            if (state != null) {
                setRenderState(state);
            }
        }

        setCullHint(target.getLocalCullHint());
        setLightCombineMode(target.getLocalLightCombineMode());
        setRenderQueueMode(target.getLocalRenderQueueMode());
        setTextureCombineMode(target.getLocalTextureCombineMode());
        setZOrder(target.getZOrder());
    }

    /**
     * <code>getTarget</code> returns the mesh that is being shared by this
     * object.
     * 
     * @return the mesh being shared.
     */
    public TriMesh getTarget() {
        return target;
    }

    /**
     * <code>reconstruct</code> is not supported in SharedMesh.
     * 
     * @param vertices
     *            the new vertices to use.
     * @param normals
     *            the new normals to use.
     * @param colors
     *            the new colors to use.
     * @param textureCoords
     *            the new texture coordinates to use (position 0).
     */
    @Override
    public void reconstruct(FloatBuffer vertices, FloatBuffer normals,
            FloatBuffer colors, TexCoords textureCoords) {
        logger.info("SharedMesh will ignore reconstruct.");
    }

    /**
     * <code>setVBOInfo</code> is not supported in SharedMesh.
     */
    @Override
    public void setVBOInfo(VBOInfo info) {
        logger.warning("SharedMesh does not allow the manipulation"
                + "of the the mesh data.");
    }

    /**
     * <code>getVBOInfo</code> returns the target mesh's vbo info.
     */
    @Override
    public VBOInfo getVBOInfo() {
        return target.getVBOInfo();
    }

    /**
     * <code>setSolidColor</code> is not supported by SharedMesh.
     * 
     * @param color
     *            the color to set.
     */
    @Override
    public void setSolidColor(ColorRGBA color) {
        logger.warning("SharedMesh does not allow the manipulation"
                + "of the the mesh data.");
    }

    /**
     * <code>setRandomColors</code> is not supported by SharedMesh.
     */
    @Override
    public void setRandomColors() {
        logger.warning("SharedMesh does not allow the manipulation"
                + "of the the mesh data.");
    }

    /**
     * <code>getVertexBuffer</code> returns the float buffer that contains the
     * target geometry's vertex information.
     * 
     * @return the float buffer that contains the target geometry's vertex
     *         information.
     */
    @Override
    public FloatBuffer getVertexBuffer() {
        return target.getVertexBuffer();
    }

    /**
     * <code>setVertexBuffer</code> is not supported by SharedMesh.
     * 
     * @param buff
     *            the new vertex buffer.
     */
    @Override
    public void setVertexBuffer(FloatBuffer buff) {
        logger.warning("SharedMesh does not allow the manipulation"
                + "of the the mesh data.");
    }

    /**
     * Returns the number of vertexes defined in the target's Geometry object.
     * 
     * @return The number of vertexes in the target Geometry object.
     */
    @Override
    public int getVertexCount() {
        return target.getVertexCount();
    }

    /**
     * <code>getNormalBuffer</code> retrieves the target geometry's normal
     * information as a float buffer.
     * 
     * @return the float buffer containing the target geometry information.
     */
    @Override
    public FloatBuffer getNormalBuffer() {
        return target.getNormalBuffer();
    }

    /**
     * <code>setNormalBuffer</code> is not supported by SharedMesh.
     * 
     * @param buff
     *            the new normal buffer.
     */
    @Override
    public void setNormalBuffer(FloatBuffer buff) {
        logger.warning("SharedMesh does not allow the manipulation"
                + "of the the mesh data.");
    }

    /**
     * <code>getColorBuffer</code> retrieves the float buffer that contains
     * the target geometry's color information.
     * 
     * @return the buffer that contains the target geometry's color information.
     */
    @Override
    public FloatBuffer getColorBuffer() {
        return target.getColorBuffer();
    }

    /**
     * <code>setColorBuffer</code> is not supported by SharedMesh.
     * 
     * @param buff
     *            the new color buffer.
     */
    @Override
    public void setColorBuffer(FloatBuffer buff) {
        logger.warning("SharedMesh does not allow the manipulation"
                + "of the the mesh data.");
    }

    /**
     * <code>getIndexAsBuffer</code> retrieves the target's indices array as
     * an <code>IntBuffer</code>.
     * 
     * @return the indices array as an <code>IntBuffer</code>.
     */
    @Override
    public IntBuffer getIndexBuffer() {
        return target.getIndexBuffer();
    }

    /**
     * <code>setIndexBuffer</code> is not supported by SharedMesh.

⌨️ 快捷键说明

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