123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- import { TempNode } from '../core/TempNode.js';
- import { FunctionNode } from '../core/FunctionNode.js';
- import { FloatNode } from '../inputs/FloatNode.js';
- import { PositionNode } from '../accessors/PositionNode.js';
- function CameraNode( scope, camera ) {
- TempNode.call( this, 'v3' );
- this.setScope( scope || CameraNode.POSITION );
- this.setCamera( camera );
- }
- CameraNode.Nodes = ( function () {
- var depthColor = new FunctionNode( [
- 'float depthColor( float mNear, float mFar ) {',
- ' #ifdef USE_LOGDEPTHBUF_EXT',
- ' float depth = gl_FragDepthEXT / gl_FragCoord.w;',
- ' #else',
- ' float depth = gl_FragCoord.z / gl_FragCoord.w;',
- ' #endif',
- ' return 1.0 - smoothstep( mNear, mFar, depth );',
- '}'
- ].join( '\n' ) );
- return {
- depthColor: depthColor
- };
- } )();
- CameraNode.POSITION = 'position';
- CameraNode.DEPTH = 'depth';
- CameraNode.TO_VERTEX = 'toVertex';
- CameraNode.prototype = Object.create( TempNode.prototype );
- CameraNode.prototype.constructor = CameraNode;
- CameraNode.prototype.nodeType = 'Camera';
- CameraNode.prototype.setCamera = function ( camera ) {
- this.camera = camera;
- this.updateFrame = camera !== undefined ? this.onUpdateFrame : undefined;
- };
- CameraNode.prototype.setScope = function ( scope ) {
- switch ( this.scope ) {
- case CameraNode.DEPTH:
- delete this.near;
- delete this.far;
- break;
- }
- this.scope = scope;
- switch ( scope ) {
- case CameraNode.DEPTH:
- var camera = this.camera;
- this.near = new FloatNode( camera ? camera.near : 1 );
- this.far = new FloatNode( camera ? camera.far : 1200 );
- break;
- }
- };
- CameraNode.prototype.getType = function ( /* builder */ ) {
- switch ( this.scope ) {
- case CameraNode.DEPTH:
- return 'f';
- }
- return this.type;
- };
- CameraNode.prototype.getUnique = function ( /* builder */ ) {
- switch ( this.scope ) {
- case CameraNode.DEPTH:
- case CameraNode.TO_VERTEX:
- return true;
- }
- return false;
- };
- CameraNode.prototype.getShared = function ( /* builder */ ) {
- switch ( this.scope ) {
- case CameraNode.POSITION:
- return false;
- }
- return true;
- };
- CameraNode.prototype.generate = function ( builder, output ) {
- var result;
- switch ( this.scope ) {
- case CameraNode.POSITION:
- result = 'cameraPosition';
- break;
- case CameraNode.DEPTH:
- var depthColor = builder.include( CameraNode.Nodes.depthColor );
- result = depthColor + '( ' + this.near.build( builder, 'f' ) + ', ' + this.far.build( builder, 'f' ) + ' )';
- break;
- case CameraNode.TO_VERTEX:
- result = 'normalize( ' + new PositionNode( PositionNode.WORLD ).build( builder, 'v3' ) + ' - cameraPosition )';
- break;
- }
- return builder.format( result, this.getType( builder ), output );
- };
- CameraNode.prototype.onUpdateFrame = function ( /* frame */ ) {
- switch ( this.scope ) {
- case CameraNode.DEPTH:
- var camera = this.camera;
- this.near.value = camera.near;
- this.far.value = camera.far;
- break;
- }
- };
- CameraNode.prototype.copy = function ( source ) {
- TempNode.prototype.copy.call( this, source );
- this.setScope( source.scope );
- if ( source.camera ) {
- this.setCamera( source.camera );
- }
- switch ( source.scope ) {
- case CameraNode.DEPTH:
- this.near.number = source.near;
- this.far.number = source.far;
- break;
- }
- return this;
- };
- CameraNode.prototype.toJSON = function ( meta ) {
- var data = this.getJSONNode( meta );
- if ( ! data ) {
- data = this.createJSONNode( meta );
- data.scope = this.scope;
- if ( this.camera ) data.camera = this.camera.uuid;
- switch ( this.scope ) {
- case CameraNode.DEPTH:
- data.near = this.near.value;
- data.far = this.far.value;
- break;
- }
- }
- return data;
- };
- export { CameraNode };
|