Beispiel #1
0
  public static void findCollisions(
      final Spatial spatial, final Spatial scene, final CollisionResults results) {
    if (spatial == scene
        || spatial.getWorldBound() == null
        || !spatial.getSceneHints().isPickingHintEnabled(PickingHint.Collidable)
        || !scene.getSceneHints().isPickingHintEnabled(PickingHint.Collidable)) {
      return;
    }

    if (spatial instanceof Node) {
      final Node node = (Node) spatial;

      if (node.getWorldBound().intersects(scene.getWorldBound())) {
        // further checking needed.
        for (int i = 0; i < node.getNumberOfChildren(); i++) {
          PickingUtil.findCollisions(node.getChild(i), scene, results);
        }
      }
    } else if (spatial instanceof Mesh) {
      final Mesh mesh = (Mesh) spatial;

      if (mesh.getWorldBound().intersects(scene.getWorldBound())) {
        if (scene instanceof Node) {
          final Node parent = (Node) scene;
          for (int i = 0; i < parent.getNumberOfChildren(); i++) {
            PickingUtil.findCollisions(mesh, parent.getChild(i), results);
          }
        } else {
          results.addCollision(mesh, (Mesh) scene);
        }
      }
    }
  }
 private void addMesh(final Spatial spatial) {
   spatial.setTranslation(
       (index % wrapCount) * 8 - wrapCount * 4, (index / wrapCount) * 8 - wrapCount * 4, -50);
   if (spatial instanceof Mesh) {
     ((Mesh) spatial).updateModelBound();
   }
   final MaterialState ms = new MaterialState();
   ms.setAmbient(ColorRGBA.DARK_GRAY);
   spatial.setRenderState(ms);
   _root.attachChild(spatial);
   index++;
 }
Beispiel #3
0
  public static boolean hasCollision(
      final Spatial spatial, final Spatial scene, final boolean checkPrimitives) {
    if (spatial == scene
        || spatial.getWorldBound() == null
        || !spatial.getSceneHints().isPickingHintEnabled(PickingHint.Collidable)
        || !scene.getSceneHints().isPickingHintEnabled(PickingHint.Collidable)) {
      return false;
    }

    if (spatial instanceof Node) {
      final Node node = (Node) spatial;

      if (node.getWorldBound().intersects(scene.getWorldBound())) {
        if (node.getNumberOfChildren() == 0 && !checkPrimitives) {
          return true;
        }
        // further checking needed.
        for (int i = 0; i < node.getNumberOfChildren(); i++) {
          if (PickingUtil.hasCollision(node.getChild(i), scene, checkPrimitives)) {
            return true;
          }
        }
      }
    } else if (spatial instanceof Mesh) {
      final Mesh mesh = (Mesh) spatial;

      if (mesh.getWorldBound().intersects(scene.getWorldBound())) {
        if (scene instanceof Node) {
          final Node parent = (Node) scene;
          for (int i = 0; i < parent.getNumberOfChildren(); i++) {
            if (PickingUtil.hasCollision(mesh, parent.getChild(i), checkPrimitives)) {
              return true;
            }
          }

          return false;
        }

        if (!checkPrimitives) {
          return true;
        }

        return PickingUtil.hasTriangleCollision(mesh, (Mesh) scene);
      }

      return false;
    }

    return false;
  }
Beispiel #4
0
  public static void findPick(final Spatial spatial, final Ray3 ray, final PickResults results) {
    if (!spatial.getSceneHints().isPickingHintEnabled(PickingHint.Pickable)) {
      return;
    }

    if (spatial instanceof Node) {
      final Node node = (Node) spatial;

      if (node.getNumberOfChildren() == 0 || node.getWorldBound() == null) {
        return;
      }
      if (node.getWorldBound().intersects(ray)) {
        // further checking needed.
        for (int i = 0; i < node.getNumberOfChildren(); i++) {
          PickingUtil.findPick(node.getChild(i), ray, results);
        }
      }
    } else if (spatial instanceof Mesh) {
      final Mesh mesh = (Mesh) spatial;

      if (mesh.getWorldBound() == null) {
        return;
      }
      if (mesh.getWorldBound().intersects(ray)) {
        // find the primitive that is being hit.
        // add this node and the primitive to the PickResults list.
        results.addPick(ray, mesh);
      }
    }
  }
 private void addMesh(final Spatial spatial) {
   spatial.setTranslation(
       (index % wrapCount) * 8 - wrapCount * 4, (index / wrapCount) * 8 - wrapCount * 4, -50);
   if (spatial instanceof Mesh) {
     ((Mesh) spatial).updateModelBound();
   }
   _root.attachChild(spatial);
   index++;
 }
 @Override
 public void add(final Spatial spatial) {
   spatial._queueDistance = Double.NEGATIVE_INFINITY;
   if (_currentListSize >= _currentList.length) {
     // grow if necessary
     final Spatial[] temp = new Spatial[_currentListSize * 2];
     System.arraycopy(_currentList, 0, temp, 0, _currentListSize);
     _currentList = temp;
     if (_tempList.length < temp.length) {
       _tempList = new Spatial[temp.length];
     }
   }
   _currentList[_currentListSize++] = spatial;
 }
Beispiel #7
0
 /**
  * Sets a spatial up for being rendered with the watereffect
  *
  * @param spatial Spatial to use as base for the watereffect
  */
 public void setWaterEffectOnSpatial(final Spatial spatial) {
   spatial.setRenderState(cullBackFace);
   if (isSupported()) {
     // spatial.setRenderBucketType(RenderBucketType.Skip);
     spatial.setRenderState(waterShader);
     spatial.setRenderState(textureState);
   } else {
     spatial.getSceneHints().setRenderBucketType(RenderBucketType.Transparent);
     spatial.getSceneHints().setLightCombineMode(LightCombineMode.Off);
     spatial.setRenderState(fallbackTextureState);
     spatial.setRenderState(as1);
   }
 }
  /**
   * Calculates the distance from a spatial to the camera. Distance is a squared distance.
   *
   * @param spat Spatial to check distance.
   * @return Distance from Spatial to current context's camera.
   */
  protected double distanceToCam(final Spatial spat) {
    if (spat._queueDistance != Double.NEGATIVE_INFINITY) {
      return spat._queueDistance;
    }

    final Camera cam = Camera.getCurrentCamera();

    if (spat.getWorldBound() != null && Vector3.isValid(spat.getWorldBound().getCenter())) {
      spat._queueDistance = spat.getWorldBound().distanceToEdge(cam.getLocation());
    } else {
      final ReadOnlyVector3 spatPosition = spat.getWorldTranslation();
      if (!Vector3.isValid(spatPosition)) {
        spat._queueDistance = Double.POSITIVE_INFINITY;
      } else {
        spat._queueDistance = cam.getLocation().distance(spatPosition);
      }
    }

    return spat._queueDistance;
  }
Beispiel #9
0
    public void valueChanged(final TreeSelectionEvent e) {
      if (e.getNewLeadSelectionPath() == null
          || e.getNewLeadSelectionPath().getLastPathComponent() == null) {
        return;
      }

      final Spatial spatial = (Spatial) e.getNewLeadSelectionPath().getLastPathComponent();

      final StringBuilder str = new StringBuilder();

      str.append(spatial.getName());
      str.append(" - ");
      str.append(spatial.getClass().getName()).append('\n');

      if (spatial instanceof Mesh) {
        final Mesh mesh = (Mesh) spatial;
        str.append("Primitives: ");
        str.append(mesh.getMeshData().getTotalPrimitiveCount()).append('\n');
        str.append("Index mode: ");
        str.append(mesh.getMeshData().getIndexMode(0)).append('\n');
      }

      str.append(spatial.getTransform()).append('\n');
      if (spatial.getWorldBound() != null) {
        str.append(spatial.getWorldBound()).append('\n');
      }

      str.append('\n');
      final SceneHints sceneHints = spatial.getSceneHints();
      str.append("Cull hint: ");
      str.append(sceneHints.getCullHint()).append('\n');
      str.append("Bucket: ");
      str.append(sceneHints.getRenderBucketType()).append('\n');

      textArea.setText(str.toString());
    }
Beispiel #10
0
 @Override
 public void update(double time, Spatial caller) {
   setWorldTransform(caller.getWorldTransform());
   if (bbvn != null) bbvn.setWorldTransform(caller.getWorldTransform());
 }
Beispiel #11
0
  @Override
  public RenderState extract(final Stack<? extends RenderState> stack, final Spatial spat) {
    if (spat == null) {
      return stack.peek();
    }

    final LightCombineMode mode = spat.getLightCombineMode();

    final Mesh mesh = (Mesh) spat;
    LightState lightState = mesh.getLightState();
    if (lightState == null) {
      lightState = new LightState();
      mesh.setLightState(lightState);
    }

    lightState.detachAll();

    if (mode == LightCombineMode.Replace || (mode != LightCombineMode.Off && stack.size() == 1)) {
      // todo: use dummy state if off?

      final LightState copyLightState = (LightState) stack.peek();
      copyLightState(copyLightState, lightState);
    } else {
      // accumulate the lights in the stack into a single LightState object
      final Object states[] = stack.toArray();
      boolean foundEnabled = false;
      switch (mode) {
        case CombineClosest:
        case CombineClosestEnabled:
          for (int iIndex = states.length - 1; iIndex >= 0; iIndex--) {
            final LightState pkLState = (LightState) states[iIndex];
            if (!pkLState.isEnabled()) {
              if (mode == LightCombineMode.CombineClosestEnabled) {
                break;
              }

              continue;
            }

            foundEnabled = true;
            copyLightState(pkLState, lightState);
          }
          break;
        case CombineFirst:
          for (int iIndex = 0, max = states.length; iIndex < max; iIndex++) {
            final LightState pkLState = (LightState) states[iIndex];
            if (!pkLState.isEnabled()) {
              continue;
            }

            foundEnabled = true;
            copyLightState(pkLState, lightState);
          }
          break;
        case Off:
          break;
      }
      lightState.setEnabled(foundEnabled);
    }

    return lightState;
  }
  @Override
  public void render(final Renderer renderer) {
    // Grab our render context - used to enforce renderstates
    final RenderContext context = ContextManager.getCurrentContext();

    // go through our bucket contents
    Spatial spatial;
    for (int i = 0; i < _currentListSize; i++) {
      spatial = _currentList[i];

      // make sure we have a real spatial
      if (spatial == null) {
        continue;
      }

      // we only care about altering the Mesh... perhaps could be altered later to some interface
      // shared by Mesh
      // and other leaf nodes.
      if (spatial instanceof Mesh) {

        // get our transparency rendering type.
        final TransparencyType renderType = spatial.getSceneHints().getTransparencyType();

        // check for one of the two pass types...
        if (renderType != TransparencyType.OnePass) {

          // get handle to Mesh
          final Mesh mesh = (Mesh) spatial;

          // check if we have a Cull state set or enforced. If one is explicitly set and is not
          // Face.None,
          // we'll not do two-pass transparency.
          RenderState setState =
              context.hasEnforcedStates() ? context.getEnforcedState(StateType.Cull) : null;
          if (setState == null) {
            setState = mesh.getWorldRenderState(RenderState.StateType.Cull);
          }

          // Do the described check.
          if (setState == null || ((CullState) setState).getCullFace() == Face.None) {

            // pull any currently enforced cull or zstate. We'll put them back afterwards
            final RenderState oldCullState = context.getEnforcedState(StateType.Cull);
            final RenderState oldZState = context.getEnforcedState(StateType.ZBuffer);

            // enforce our cull and zstate. The zstate is setup to respect depth, but not write to
            // it.
            context.enforceState(_tranparentCull);
            context.enforceState(_transparentZBuff);

            // first render back-facing tris only
            _tranparentCull.setCullFace(CullState.Face.Front);
            mesh.draw(renderer);

            // revert z state
            context.clearEnforcedState(StateType.ZBuffer);
            if (oldZState != null) {
              context.enforceState(oldZState);
            }

            // render front-facing tris
            _tranparentCull.setCullFace(CullState.Face.Back);
            mesh.draw(renderer);

            // revert cull state
            if (oldCullState != null) {
              context.enforceState(oldCullState);
            } else {
              context.clearEnforcedState(StateType.Cull);
            }
            continue;
          }
        }
      }

      // go ahead and draw as usual
      spatial.draw(renderer);
    }
  }