firtoz / react-three-renderer

Render into a three.js canvas using React.
https://toxicfork.github.com/react-three-renderer-example/
MIT License
1.49k stars 155 forks source link

bounding sphere do not recomputed when geometry's vertices updated #193

Open iamnotstone opened 6 years ago

iamnotstone commented 6 years ago

As the title. I met this problem when I update a element's vertices. The bounding sphere do not updated as I updating the vertices. This cause a geometry may 'disappear' as Three.js's frustum check boundingSphere for rendering. I found this problem and fixed it by change your code in './src/lib/descriptors/Geometry/GeometryDescriptor.js' below:

import * as THREE from 'three';
import PropTypes from 'react/lib/ReactPropTypes';

import GeometryDescriptorBase from './GeometryDescriptorBase';
import propTypeInstanceOf from '../../utils/propTypeInstanceOf';

class GeometryDescriptor extends GeometryDescriptorBase {
  constructor(react3RendererInstance) {
    super(react3RendererInstance);

    this.hasProp('vertices', {
      override: true,
      type: PropTypes.arrayOf(propTypeInstanceOf(THREE.Vector3)).isRequired,
      update(threeObject, vertices) {
        if (threeObject.vertices !== vertices) {
          threeObject.vertices = vertices;

          threeObject.verticesNeedUpdate = true;

      ** +    //iamnotstone modified
      +    threeObject.computeBoundingSphere();
      +   console.log('GeometryDescriptor update called') **

        }
      },
      updateInitial: true,
      default: [],
    });
  }

  construct() {
    return new THREE.Geometry();
  }
}

module.exports = GeometryDescriptor;

This solution works for me. And I think there are also the same problems in other descriptors

toxicFork commented 6 years ago

Ah yes, good find :)

toxicFork commented 6 years ago

I think a fix should also consider perhaps giving the option (or just doing) to update vertices immediately instead of setting verticesNeedUpdate , for example there may be cases where computeBoundingSphere may be called before a vertex update so it'd be one frame out of date