123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- import ClippingContext from './ClippingContext.js';
- let id = 0;
- function getKeys( obj ) {
- const keys = Object.keys( obj );
- let proto = Object.getPrototypeOf( obj );
- while ( proto ) {
- const descriptors = Object.getOwnPropertyDescriptors( proto );
- for ( const key in descriptors ) {
- if ( descriptors[ key ] !== undefined ) {
- const descriptor = descriptors[ key ];
- if ( descriptor && typeof descriptor.get === 'function' ) {
- keys.push( key );
- }
- }
- }
- proto = Object.getPrototypeOf( proto );
- }
- return keys;
- }
- export default class RenderObject {
- constructor( nodes, geometries, renderer, object, material, scene, camera, lightsNode, renderContext ) {
- this._nodes = nodes;
- this._geometries = geometries;
- this.id = id ++;
- this.renderer = renderer;
- this.object = object;
- this.material = material;
- this.scene = scene;
- this.camera = camera;
- this.lightsNode = lightsNode;
- this.context = renderContext;
- this.geometry = object.geometry;
- this.version = material.version;
- this.drawRange = null;
- this.attributes = null;
- this.pipeline = null;
- this.vertexBuffers = null;
- this.updateClipping( renderContext.clippingContext );
- this.clippingContextVersion = this.clippingContext.version;
- this.initialNodesCacheKey = this.getNodesCacheKey();
- this.initialCacheKey = this.getCacheKey();
- this._nodeBuilderState = null;
- this._bindings = null;
- this.onDispose = null;
- this.isRenderObject = true;
- this.onMaterialDispose = () => {
- this.dispose();
- };
- this.material.addEventListener( 'dispose', this.onMaterialDispose );
- }
- updateClipping( parent ) {
- const material = this.material;
- let clippingContext = this.clippingContext;
- if ( Array.isArray( material.clippingPlanes ) ) {
- if ( clippingContext === parent || ! clippingContext ) {
- clippingContext = new ClippingContext();
- this.clippingContext = clippingContext;
- }
- clippingContext.update( parent, material );
- } else if ( this.clippingContext !== parent ) {
- this.clippingContext = parent;
- }
- }
- get clippingNeedsUpdate() {
- if ( this.clippingContext.version === this.clippingContextVersion ) return false;
- this.clippingContextVersion = this.clippingContext.version;
- return true;
- }
- getNodeBuilderState() {
- return this._nodeBuilderState || ( this._nodeBuilderState = this._nodes.getForRender( this ) );
- }
- getBindings() {
- return this._bindings || ( this._bindings = this.getNodeBuilderState().createBindings() );
- }
- getIndex() {
- return this._geometries.getIndex( this );
- }
- getChainArray() {
- return [ this.object, this.material, this.context, this.lightsNode ];
- }
- getAttributes() {
- if ( this.attributes !== null ) return this.attributes;
- const nodeAttributes = this.getNodeBuilderState().nodeAttributes;
- const geometry = this.geometry;
- const attributes = [];
- const vertexBuffers = new Set();
- for ( const nodeAttribute of nodeAttributes ) {
- const attribute = nodeAttribute.node && nodeAttribute.node.attribute ? nodeAttribute.node.attribute : geometry.getAttribute( nodeAttribute.name );
- if ( attribute === undefined ) continue;
- attributes.push( attribute );
- const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
- vertexBuffers.add( bufferAttribute );
- }
- this.attributes = attributes;
- this.vertexBuffers = Array.from( vertexBuffers.values() );
- return attributes;
- }
- getVertexBuffers() {
- if ( this.vertexBuffers === null ) this.getAttributes();
- return this.vertexBuffers;
- }
- getMaterialCacheKey() {
- const { object, material } = this;
- let cacheKey = material.customProgramCacheKey();
- for ( const property of getKeys( material ) ) {
- if ( /^(is[A-Z]|_)|^(visible|version|uuid|name|opacity|userData)$/.test( property ) ) continue;
- let value = material[ property ];
- if ( value !== null ) {
- const type = typeof value;
- if ( type === 'number' ) value = value !== 0 ? '1' : '0'; // Convert to on/off, important for clearcoat, transmission, etc
- else if ( type === 'object' ) value = '{}';
- }
- cacheKey += /*property + ':' +*/ value + ',';
- }
- cacheKey += this.clippingContextVersion + ',';
- if ( object.skeleton ) {
- cacheKey += object.skeleton.bones.length + ',';
- }
- if ( object.morphTargetInfluences ) {
- cacheKey += object.morphTargetInfluences.length + ',';
- }
- if ( object.isBatchedMesh ) {
- cacheKey += object._matricesTexture.uuid + ',';
- if ( object._colorsTexture !== null ) {
- cacheKey += object._colorsTexture.uuid + ',';
- }
- }
- if ( object.count > 1 ) {
- cacheKey += object.count + ',';
- }
- return cacheKey;
- }
- get needsUpdate() {
- return this.initialNodesCacheKey !== this.getNodesCacheKey() || this.clippingNeedsUpdate;
- }
- getNodesCacheKey() {
- // Environment Nodes Cache Key
- return this._nodes.getCacheKey( this.scene, this.lightsNode );
- }
- getCacheKey() {
- return this.getMaterialCacheKey() + ',' + this.getNodesCacheKey();
- }
- dispose() {
- this.material.removeEventListener( 'dispose', this.onMaterialDispose );
- this.onDispose();
- }
- }
|