123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- import { Vector3 } from 'three';
- import Node from '../core/Node.js';
- import UniformNode from '../core/UniformNode.js';
- import { NodeUpdateType } from '../core/constants.js';
- class Object3DNode extends Node {
- static VIEW_MATRIX = 'viewMatrix';
- static NORMAL_MATRIX = 'normalMatrix';
- static WORLD_MATRIX = 'worldMatrix';
- static POSITION = 'position';
- static VIEW_POSITION = 'viewPosition';
- constructor( scope = Object3DNode.VIEW_MATRIX, object3d = null ) {
- super();
- this.scope = scope;
- this.object3d = object3d;
- this.updateType = NodeUpdateType.Object;
- this._uniformNode = new UniformNode( null );
- }
- getNodeType() {
- const scope = this.scope;
- if ( scope === Object3DNode.WORLD_MATRIX || scope === Object3DNode.VIEW_MATRIX ) {
- return 'mat4';
- } else if ( scope === Object3DNode.NORMAL_MATRIX ) {
- return 'mat3';
- } else if ( scope === Object3DNode.POSITION || scope === Object3DNode.VIEW_POSITION ) {
- return 'vec3';
- }
- }
- update( frame ) {
- const object = this.object3d;
- const uniformNode = this._uniformNode;
- const scope = this.scope;
- if ( scope === Object3DNode.VIEW_MATRIX ) {
- uniformNode.value = object.modelViewMatrix;
- } else if ( scope === Object3DNode.NORMAL_MATRIX ) {
- uniformNode.value = object.normalMatrix;
- } else if ( scope === Object3DNode.WORLD_MATRIX ) {
- uniformNode.value = object.matrixWorld;
- } else if ( scope === Object3DNode.POSITION ) {
- uniformNode.value.setFromMatrixPosition( object.matrixWorld );
- } else if ( scope === Object3DNode.VIEW_POSITION ) {
- const camera = frame.camera;
- uniformNode.value = uniformNode.value || new Vector3();
- uniformNode.value.setFromMatrixPosition( object.matrixWorld );
- uniformNode.value.applyMatrix4( camera.matrixWorldInverse );
- }
- }
- generate( builder ) {
- const scope = this.scope;
- if ( scope === Object3DNode.WORLD_MATRIX || scope === Object3DNode.VIEW_MATRIX ) {
- this._uniformNode.nodeType = 'mat4';
- } else if ( scope === Object3DNode.NORMAL_MATRIX ) {
- this._uniformNode.nodeType = 'mat3';
- } else if ( scope === Object3DNode.POSITION || scope === Object3DNode.VIEW_POSITION ) {
- this._uniformNode.nodeType = 'vec3';
- }
- return this._uniformNode.build( builder );
- }
- serialize( data ) {
- super.serialize( data );
- data.scope = this.scope;
- }
- deserialize( data ) {
- super.deserialize( data );
- this.scope = data.scope;
- }
- }
- export default Object3DNode;
|