Java Code Examples for com.jme3.scene.Spatial#setUserData()

The following examples show how to use com.jme3.scene.Spatial#setUserData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoadModelAction.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * The process of opening file.
 *
 * @param file the file
 */
@FxThread
protected void processOpen(@NotNull final Path file) {

    final NodeTree<?> nodeTree = getNodeTree();
    final ChangeConsumer consumer = notNull(nodeTree.getChangeConsumer());
    final SceneLayer defaultLayer = getDefaultLayer(consumer);

    final Path assetFile = notNull(getAssetFile(file), "Not found asset file for " + file);
    final String assetPath = toAssetPath(assetFile);

    final ModelKey modelKey = new ModelKey(assetPath);

    final AssetManager assetManager = EditorUtil.getAssetManager();
    final Spatial loadedModel = assetManager.loadModel(modelKey);
    loadedModel.setUserData(KEY_LOADED_MODEL, true);

    if (defaultLayer != null) {
        SceneLayer.setLayer(defaultLayer, loadedModel);
    }

    final TreeNode<?> treeNode = getNode();
    final Node parent = (Node) treeNode.getElement();
    consumer.execute(new AddChildOperation(loadedModel, parent));
}
 
Example 2
Source File: AbstractBlenderHelper.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * The method applies properties to the given spatial. The Properties
 * instance cannot be directly applied because the end-user might not have
 * the blender plugin jar file and thus receive ClassNotFoundException. The
 * values are set by name instead.
 * 
 * @param spatial
 *            the spatial that is to have properties applied
 * @param properties
 *            the properties to be applied
 */
protected void applyProperties(Spatial spatial, Properties properties) {
    List<String> propertyNames = properties.getSubPropertiesNames();
    if (propertyNames != null && propertyNames.size() > 0) {
        for (String propertyName : propertyNames) {
            Object value = properties.findValue(propertyName);
            if (value instanceof Savable || value instanceof Boolean || value instanceof String || value instanceof Float || value instanceof Integer || value instanceof Long) {
                spatial.setUserData(propertyName, value);
            } else if (value instanceof Double) {
                spatial.setUserData(propertyName, ((Double) value).floatValue());
            } else if (value instanceof int[]) {
                spatial.setUserData(propertyName, Arrays.toString((int[]) value));
            } else if (value instanceof float[]) {
                spatial.setUserData(propertyName, Arrays.toString((float[]) value));
            } else if (value instanceof double[]) {
                spatial.setUserData(propertyName, Arrays.toString((double[]) value));
            }
        }
    }
}
 
Example 3
Source File: LayerComparator.java    From Lemur with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void setLayer( Spatial s, int layer ) {
    if( layer == 0 ) {
        s.setUserData(LAYER, null);
    } else {        
        s.setUserData(LAYER, layer);
    }
}
 
Example 4
Source File: LayerComparator.java    From Lemur with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void clearEffectiveLayer( Spatial s ) {
    s.setUserData(EFFECTIVE_LAYER, null);
    if( s instanceof Node ) {
        for( Spatial child : ((Node)s).getChildren() ) {
            clearEffectiveLayer(child);
        }
    }
}
 
Example 5
Source File: SpawnToolControl.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
/**
 * Spawn models.
 *
 * @param brushRotation the brush rotation.
 * @param contactPoint  the contact point.
 */
@JmeThread
protected void spawn(@NotNull Quaternion brushRotation, @NotNull Vector3f contactPoint) {

    var brushSize = getBrushSize();

    var random = ThreadLocalRandom.current();
    var local = getLocalObjects();
    var spawnPosition = local.nextVector();

    var paintedModel = getPaintedModel();
    var direction = GeomUtils.getDirection(brushRotation, local.nextVector())
            .negateLocal()
            .multLocal(10);

    var sourcePoint = contactPoint.subtract(direction, local.nextVector());
    var ray = local.nextRay();
    ray.setOrigin(sourcePoint);

    var minScale = getMinScale();
    var maxScale = getMaxScale();
    var padding = getPadding();

    var resultPosition = local.nextVector();
    var collisions = local.nextCollisionResults();
    var spawnedCollisions = local.nextCollisionResults();
    var resultScale = local.nextVector();
    var needCalculateScale = !minScale.equals(maxScale);

    var maxCount = (int) Math.max(getBrushPower() / 2F, 1F);
    var spawnedModels = getSpawnedModels();

    for(var count = 0; count < maxCount; count++) {
        for (var attempts = 0; attempts < 10; attempts++, attempts++) {

            collisions.clear();
            spawnedCollisions.clear();

            var x = nextOffset(brushSize, random);
            var y = nextOffset(brushSize, random);
            var z = nextOffset(brushSize, random);

            spawnPosition.set(x, y, z)
                    .addLocal(contactPoint)
                    .subtractLocal(sourcePoint)
                    .normalizeLocal();

            ray.setDirection(spawnPosition);

            paintedModel.collideWith(ray, collisions);

            var closest = collisions.getClosestCollision();
            if (closest == null || contactPoint.distance(closest.getContactPoint()) > brushSize / 2) {
                continue;
            }

            resultPosition.set(closest.getContactPoint())
                    .subtractLocal(paintedModel.getWorldTranslation());

            Spatial clone = examples.get(random.nextInt(0, examples.size())).clone();
            clone.setUserData(KEY_IGNORE_RAY_CAST, Boolean.TRUE);
            clone.setLocalTranslation(resultPosition);

            if (needCalculateScale) {
                clone.setLocalScale(nextScale(minScale, maxScale, resultScale, random));
            } else {
                clone.setLocalScale(minScale);
            }

            clone.updateModelBound();

            var worldBound = clone.getWorldBound();

            if (!Vector3f.ZERO.equals(padding)) {
                worldBound = addPadding(worldBound, padding);
            }

            if (paintedModel.collideWith(worldBound, spawnedCollisions) > 2) {
                continue;
            }

            spawnedModels.add(clone);
            paintedModel.attachChild(clone);
            break;
        }
    }
}
 
Example 6
Source File: SelectionState.java    From Lemur with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void addIgnore( Spatial... ignore ) {
    //this.ignore.addAll( Arrays.asList(ignore) );
    for( Spatial s : ignore ) {
        s.setUserData(UD_IGNORE, true);
    } 
}
 
Example 7
Source File: TestInstanceNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void simpleUpdate(float tpf) {
    time += tpf;

    if (time > 1f) {
        time = 0f;
        
        for (Spatial instance : instancedNode.getChildren()) {
            if (!(instance instanceof InstancedGeometry)) {
                Geometry geom = (Geometry) instance;
                geom.setMaterial(materials[FastMath.nextRandomInt(0, materials.length - 1)]);

                Mesh mesh; 
                if (FastMath.nextRandomInt(0, 1) == 1) mesh = mesh2;
                else mesh = mesh1;
                geom.setMesh(mesh);
            }
        }
    }
    
    for (Spatial child : instancedNode.getChildren()) {
        if (!(child instanceof InstancedGeometry)) {
            float val = ((Float)child.getUserData("height")).floatValue();
            float dir = ((Float)child.getUserData("dir")).floatValue();

            val += (dir + ((FastMath.nextRandomFloat() * 0.5f) - 0.25f)) * tpf;

            if (val > 1f) {
                val = 1f;
                dir = -dir;
            } else if (val < 0f) {
                val = 0f;
                dir = -dir;
            }

            Vector3f translation = child.getLocalTranslation();
            translation.y = (smoothstep(0, 1, val) * 2.5f) - 1.25f;

            child.setUserData("height", val);
            child.setUserData("dir", dir);

            child.setLocalTranslation(translation);
        }
    }
}