소스 검색

Merge pull request #19693 from mrdoob/editor

Editor: Refactored Background/Environment code
Mr.doob 5 년 전
부모
커밋
44538bc9ca
4개의 변경된 파일118개의 추가작업 그리고 132개의 파일을 삭제
  1. 4 2
      editor/js/Editor.js
  2. 55 55
      editor/js/Sidebar.Scene.js
  3. 3 0
      editor/js/Strings.js
  4. 56 75
      editor/js/Viewport.js

+ 4 - 2
editor/js/Editor.js

@@ -44,6 +44,7 @@ function Editor() {
 		rendererUpdated: new Signal(),
 		rendererUpdated: new Signal(),
 
 
 		sceneBackgroundChanged: new Signal(),
 		sceneBackgroundChanged: new Signal(),
+		sceneEnvironmentChanged: new Signal(),
 		sceneFogChanged: new Signal(),
 		sceneFogChanged: new Signal(),
 		sceneGraphChanged: new Signal(),
 		sceneGraphChanged: new Signal(),
 		sceneRendered: new Signal(),
 		sceneRendered: new Signal(),
@@ -94,7 +95,6 @@ function Editor() {
 
 
 	this.scene = new THREE.Scene();
 	this.scene = new THREE.Scene();
 	this.scene.name = 'Scene';
 	this.scene.name = 'Scene';
-	this.scene.background = new THREE.Color( 0xaaaaaa );
 
 
 	this.sceneHelpers = new THREE.Scene();
 	this.sceneHelpers = new THREE.Scene();
 
 
@@ -612,9 +612,11 @@ Editor.prototype = {
 		this.storage.clear();
 		this.storage.clear();
 
 
 		this.camera.copy( this.DEFAULT_CAMERA );
 		this.camera.copy( this.DEFAULT_CAMERA );
+
 		this.scene.name = "Scene";
 		this.scene.name = "Scene";
 		this.scene.userData = {};
 		this.scene.userData = {};
-		this.scene.background = new THREE.Color( 0xaaaaaa );
+		this.scene.background = null;
+		this.scene.environment = null;
 		this.scene.fog = null;
 		this.scene.fog = null;
 
 
 		var objects = this.scene.children;
 		var objects = this.scene.children;

+ 55 - 55
editor/js/Sidebar.Scene.js

@@ -3,7 +3,7 @@
  */
  */
 
 
 import { UIPanel, UIBreak, UIRow, UIColor, UISelect, UIText, UINumber } from './libs/ui.js';
 import { UIPanel, UIBreak, UIRow, UIColor, UISelect, UIText, UINumber } from './libs/ui.js';
-import { UIOutliner, UITexture, UICubeTexture } from './libs/ui.three.js';
+import { UIOutliner, UITexture } from './libs/ui.three.js';
 
 
 function SidebarScene( editor ) {
 function SidebarScene( editor ) {
 
 
@@ -160,29 +160,12 @@ function SidebarScene( editor ) {
 			backgroundType.getValue(),
 			backgroundType.getValue(),
 			backgroundColor.getHexValue(),
 			backgroundColor.getHexValue(),
 			backgroundTexture.getValue(),
 			backgroundTexture.getValue(),
-			backgroundCubeTexture.getValue(),
-			backgroundEquirectTexture.getValue()
+			backgroundEquirectangularTexture.getValue(),
+			environmentType.getValue()
 		);
 		);
 
 
 	}
 	}
 
 
-	function onTextureChanged( texture ) {
-
-		texture.encoding = texture.isHDRTexture ? THREE.RGBEEncoding : THREE.sRGBEncoding;
-
-		if ( texture.isCubeTexture && texture.isHDRTexture ) {
-
-			texture.format = THREE.RGBAFormat;
-			texture.minFilter = THREE.NearestFilter;
-			texture.magFilter = THREE.NearestFilter;
-			texture.generateMipmaps = false;
-
-		}
-
-		onBackgroundChanged();
-
-	}
-
 	var backgroundRow = new UIRow();
 	var backgroundRow = new UIRow();
 
 
 	var backgroundType = new UISelect().setOptions( {
 	var backgroundType = new UISelect().setOptions( {
@@ -190,8 +173,7 @@ function SidebarScene( editor ) {
 		'None': 'None',
 		'None': 'None',
 		'Color': 'Color',
 		'Color': 'Color',
 		'Texture': 'Texture',
 		'Texture': 'Texture',
-		'CubeTexture': 'CubeTexture',
-		'Equirect': 'Equirect (HDR)'
+		'Equirectangular': 'Equirectangular'
 
 
 	} ).setWidth( '150px' );
 	} ).setWidth( '150px' );
 	backgroundType.onChange( function () {
 	backgroundType.onChange( function () {
@@ -212,7 +194,7 @@ function SidebarScene( editor ) {
 	var colorRow = new UIRow();
 	var colorRow = new UIRow();
 	colorRow.setMarginLeft( '90px' );
 	colorRow.setMarginLeft( '90px' );
 
 
-	var backgroundColor = new UIColor().setValue( '#aaaaaa' ).onChange( onBackgroundChanged );
+	var backgroundColor = new UIColor().setValue( '#000000' ).onChange( onBackgroundChanged );
 	colorRow.add( backgroundColor );
 	colorRow.add( backgroundColor );
 
 
 	container.add( colorRow );
 	container.add( colorRow );
@@ -223,32 +205,21 @@ function SidebarScene( editor ) {
 	textureRow.setDisplay( 'none' );
 	textureRow.setDisplay( 'none' );
 	textureRow.setMarginLeft( '90px' );
 	textureRow.setMarginLeft( '90px' );
 
 
-	var backgroundTexture = new UITexture().onChange( onTextureChanged );
+	var backgroundTexture = new UITexture().onChange( onBackgroundChanged );
 	textureRow.add( backgroundTexture );
 	textureRow.add( backgroundTexture );
 
 
 	container.add( textureRow );
 	container.add( textureRow );
 
 
 	//
 	//
 
 
-	var cubeTextureRow = new UIRow();
-	cubeTextureRow.setDisplay( 'none' );
-	cubeTextureRow.setMarginLeft( '90px' );
+	var equirectangularRow = new UIRow();
+	equirectangularRow.setDisplay( 'none' );
+	equirectangularRow.setMarginLeft( '90px' );
 
 
-	var backgroundCubeTexture = new UICubeTexture().onChange( onTextureChanged );
-	cubeTextureRow.add( backgroundCubeTexture );
+	var backgroundEquirectangularTexture = new UITexture().onChange( onBackgroundChanged );
+	equirectangularRow.add( backgroundEquirectangularTexture );
 
 
-	container.add( cubeTextureRow );
-
-	//
-
-	var equirectRow = new UIRow();
-	equirectRow.setDisplay( 'none' );
-	equirectRow.setMarginLeft( '90px' );
-
-	var backgroundEquirectTexture = new UITexture().onChange( onTextureChanged );
-	equirectRow.add( backgroundEquirectTexture );
-
-	container.add( equirectRow );
+	container.add( equirectangularRow );
 
 
 	//
 	//
 
 
@@ -258,11 +229,32 @@ function SidebarScene( editor ) {
 
 
 		colorRow.setDisplay( type === 'Color' ? '' : 'none' );
 		colorRow.setDisplay( type === 'Color' ? '' : 'none' );
 		textureRow.setDisplay( type === 'Texture' ? '' : 'none' );
 		textureRow.setDisplay( type === 'Texture' ? '' : 'none' );
-		cubeTextureRow.setDisplay( type === 'CubeTexture' ? '' : 'none' );
-		equirectRow.setDisplay( type === 'Equirect' ? '' : 'none' );
+		equirectangularRow.setDisplay( type === 'Equirectangular' ? '' : 'none' );
 
 
 	}
 	}
 
 
+	// environment
+
+	var environmentRow = new UIRow();
+
+	var environmentType = new UISelect().setOptions( {
+
+		'None': 'None',
+		'Background': 'Background'
+
+	} ).setWidth( '150px' );
+	environmentType.setValue( 'None' );
+	environmentType.onChange( function () {
+
+		signals.sceneEnvironmentChanged.dispatch( environmentType.getValue() );
+
+	} );
+
+	environmentRow.add( new UIText( strings.getKey( 'sidebar/scene/environment' ) ).setWidth( '90px' ) );
+	environmentRow.add( environmentType );
+
+	container.add( environmentRow );
+
 	// fog
 	// fog
 
 
 	function onFogChanged() {
 	function onFogChanged() {
@@ -376,22 +368,19 @@ function SidebarScene( editor ) {
 				backgroundType.setValue( "Color" );
 				backgroundType.setValue( "Color" );
 				backgroundColor.setHexValue( scene.background.getHex() );
 				backgroundColor.setHexValue( scene.background.getHex() );
 				backgroundTexture.setValue( null );
 				backgroundTexture.setValue( null );
-				backgroundCubeTexture.setValue( null );
-				backgroundEquirectTexture.setValue( null );
+				backgroundEquirectangularTexture.setValue( null );
 
 
-			} else if ( scene.background.isTexture && ! scene.background.isPmremTexture ) {
+			} else if ( scene.background.isEquirectangularTexture ) {
 
 
-				backgroundType.setValue( "Texture" );
-				backgroundTexture.setValue( scene.background );
-				backgroundCubeTexture.setValue( null );
-				backgroundEquirectTexture.setValue( null );
+				backgroundType.setValue( "Equirectangular" );
+				backgroundTexture.setValue( null );
+				backgroundEquirectangularTexture.setValue( scene.background );
 
 
-			} else if ( scene.background.isCubeTexture ) {
+			} else if ( scene.background.isTexture ) {
 
 
-				backgroundType.setValue( "CubeTexture" );
-				backgroundCubeTexture.setValue( scene.background );
-				backgroundTexture.setValue( null );
-				backgroundEquirectTexture.setValue( null );
+				backgroundType.setValue( "Texture" );
+				backgroundTexture.setValue( scene.background );
+				backgroundEquirectangularTexture.setValue( null );
 
 
 			}
 			}
 
 
@@ -399,6 +388,17 @@ function SidebarScene( editor ) {
 
 
 			backgroundType.setValue( "None" );
 			backgroundType.setValue( "None" );
 			backgroundTexture.setValue( null );
 			backgroundTexture.setValue( null );
+			backgroundEquirectangularTexture.setValue( null );
+
+		}
+
+		if ( scene.environment ) {
+
+			// TODO
+
+		} else {
+
+			environmentType.setValue( "None" );
 
 
 		}
 		}
 
 

+ 3 - 0
editor/js/Strings.js

@@ -81,6 +81,7 @@ function Strings( config ) {
 
 
 			'sidebar/scene': 'Scene',
 			'sidebar/scene': 'Scene',
 			'sidebar/scene/background': 'Background',
 			'sidebar/scene/background': 'Background',
+			'sidebar/scene/environment': 'Environment',
 			'sidebar/scene/fog': 'Fog',
 			'sidebar/scene/fog': 'Fog',
 
 
 			'sidebar/properties/object': 'Object',
 			'sidebar/properties/object': 'Object',
@@ -398,6 +399,7 @@ function Strings( config ) {
 
 
 			'sidebar/scene': 'Scène',
 			'sidebar/scene': 'Scène',
 			'sidebar/scene/background': 'Arrière Plan',
 			'sidebar/scene/background': 'Arrière Plan',
+			'sidebar/scene/environment': 'Environment',
 			'sidebar/scene/fog': 'Brouillard',
 			'sidebar/scene/fog': 'Brouillard',
 
 
 			'sidebar/properties/object': 'Objet',
 			'sidebar/properties/object': 'Objet',
@@ -715,6 +717,7 @@ function Strings( config ) {
 
 
 			'sidebar/scene': '场景',
 			'sidebar/scene': '场景',
 			'sidebar/scene/background': '背景',
 			'sidebar/scene/background': '背景',
+			'sidebar/scene/environment': 'Environment',
 			'sidebar/scene/fog': '雾',
 			'sidebar/scene/fog': '雾',
 
 
 			'sidebar/properties/object': '属性',
 			'sidebar/properties/object': '属性',

+ 56 - 75
editor/js/Viewport.js

@@ -32,6 +32,7 @@ function Viewport( editor ) {
 
 
 	var renderer = null;
 	var renderer = null;
 	var pmremGenerator = null;
 	var pmremGenerator = null;
+	var pmremTexture = null;
 
 
 	var camera = editor.camera;
 	var camera = editor.camera;
 	var scene = editor.scene;
 	var scene = editor.scene;
@@ -298,8 +299,6 @@ function Viewport( editor ) {
 	signals.editorCleared.add( function () {
 	signals.editorCleared.add( function () {
 
 
 		controls.center.set( 0, 0, 0 );
 		controls.center.set( 0, 0, 0 );
-		currentBackgroundType = null;
-		currentFogType = null;
 		render();
 		render();
 
 
 	} );
 	} );
@@ -344,6 +343,7 @@ function Viewport( editor ) {
 
 
 			renderer.dispose();
 			renderer.dispose();
 			pmremGenerator.dispose();
 			pmremGenerator.dispose();
+			pmremTexture = null;
 
 
 			container.dom.removeChild( renderer.domElement );
 			container.dom.removeChild( renderer.domElement );
 
 
@@ -351,13 +351,13 @@ function Viewport( editor ) {
 
 
 		renderer = newRenderer;
 		renderer = newRenderer;
 
 
-		renderer.autoClear = false;
-		renderer.autoUpdateScene = false;
 		renderer.setPixelRatio( window.devicePixelRatio );
 		renderer.setPixelRatio( window.devicePixelRatio );
 		renderer.setSize( container.dom.offsetWidth, container.dom.offsetHeight );
 		renderer.setSize( container.dom.offsetWidth, container.dom.offsetHeight );
 
 
+		var isDarkMode = window.matchMedia && window.matchMedia( '(prefers-color-scheme: dark)' ).matches;
+		renderer.setClearColor( isDarkMode ? 0x333333 : 0xaaaaaa );
+
 		pmremGenerator = new THREE.PMREMGenerator( renderer );
 		pmremGenerator = new THREE.PMREMGenerator( renderer );
-		pmremGenerator.compileCubemapShader();
 		pmremGenerator.compileEquirectangularShader();
 		pmremGenerator.compileEquirectangularShader();
 
 
 		container.dom.appendChild( renderer.domElement );
 		container.dom.appendChild( renderer.domElement );
@@ -503,68 +503,55 @@ function Viewport( editor ) {
 
 
 	// background
 	// background
 
 
-	var currentBackgroundType = null;
+	signals.sceneBackgroundChanged.add( function ( backgroundType, backgroundColor, backgroundTexture, backgroundEquirectangularTexture, environmentType ) {
 
 
-	signals.sceneBackgroundChanged.add( function ( backgroundType, backgroundColor, backgroundTexture, backgroundCubeTexture, backgroundEquirectTexture ) {
+		pmremTexture = null;
 
 
-		if ( currentBackgroundType !== backgroundType ) {
+		switch ( backgroundType ) {
 
 
-			switch ( backgroundType ) {
+			case 'None':
 
 
-				case 'None':
-					scene.background = null;
-					break;
-				case 'Color':
-					scene.background = new THREE.Color();
-					break;
+				scene.background = null;
 
 
-			}
+				break;
 
 
-		}
+			case 'Color':
 
 
-		if ( backgroundType === 'Color' ) {
+				scene.background = new THREE.Color( backgroundColor );
 
 
-			scene.background.set( backgroundColor );
-			scene.environment = null;
+				break;
 
 
-		} else if ( backgroundType === 'Texture' ) {
+			case 'Texture':
 
 
-			scene.background = backgroundTexture;
-			scene.environment = null;
+				if ( backgroundTexture ) {
 
 
-		} else if ( backgroundType === 'CubeTexture' ) {
+					scene.background = backgroundTexture;
 
 
-			if ( backgroundCubeTexture && backgroundCubeTexture.isHDRTexture ) {
+				}
 
 
-				var texture = pmremGenerator.fromCubemap( backgroundCubeTexture ).texture;
-				texture.isPmremTexture = true;
+				break;
 
 
-				scene.background = texture;
-				scene.environment = texture;
+			case 'Equirectangular':
 
 
-			} else {
+				if ( backgroundEquirectangularTexture ) {
 
 
-				scene.background = backgroundCubeTexture;
-				scene.environment = null;
+					pmremTexture = pmremGenerator.fromEquirectangular( backgroundEquirectangularTexture ).texture;
 
 
-			}
-
-		} else if ( backgroundType === 'Equirect' ) {
+					var renderTarget = new THREE.WebGLCubeRenderTarget( 512 );
+					renderTarget.fromEquirectangularTexture( renderer, backgroundEquirectangularTexture );
+					renderTarget.toJSON = function () { return null };
 
 
-			if ( backgroundEquirectTexture && backgroundEquirectTexture.isHDRTexture ) {
+					scene.background = renderTarget;
 
 
-				var texture = pmremGenerator.fromEquirectangular( backgroundEquirectTexture ).texture;
-				texture.isPmremTexture = true;
+				}
 
 
-				scene.background = texture;
-				scene.environment = texture;
+				break;
 
 
-			} else {
+		}
 
 
-				scene.background = null;
-				scene.environment = null;
+		if ( environmentType === 'Background' ) {
 
 
-			}
+			scene.environment = pmremTexture;
 
 
 		}
 		}
 
 
@@ -572,46 +559,40 @@ function Viewport( editor ) {
 
 
 	} );
 	} );
 
 
-	// fog
-
-	var currentFogType = null;
-
-	signals.sceneFogChanged.add( function ( fogType, fogColor, fogNear, fogFar, fogDensity ) {
+	// environment
 
 
-		if ( currentFogType !== fogType ) {
-
-			switch ( fogType ) {
-
-				case 'None':
-					scene.fog = null;
-					break;
-				case 'Fog':
-					scene.fog = new THREE.Fog();
-					break;
-				case 'FogExp2':
-					scene.fog = new THREE.FogExp2();
-					break;
+	signals.sceneEnvironmentChanged.add( function ( environmentType ) {
 
 
-			}
+		switch ( environmentType ) {
 
 
-			currentFogType = fogType;
+			case 'None':
+				scene.environment = null;
+				break;
+			case 'Background':
+				scene.environment = pmremTexture;
+				break;
 
 
 		}
 		}
 
 
-		if ( scene.fog ) {
+		render();
 
 
-			if ( scene.fog.isFog ) {
+	} );
 
 
-				scene.fog.color.setHex( fogColor );
-				scene.fog.near = fogNear;
-				scene.fog.far = fogFar;
+	// fog
 
 
-			} else if ( scene.fog.isFogExp2 ) {
+	signals.sceneFogChanged.add( function ( fogType, fogColor, fogNear, fogFar, fogDensity ) {
 
 
-				scene.fog.color.setHex( fogColor );
-				scene.fog.density = fogDensity;
+		switch ( fogType ) {
 
 
-			}
+			case 'None':
+				scene.fog = null;
+				break;
+			case 'Fog':
+				scene.fog = new THREE.Fog( fogColor, fogNear, fogFar );
+				break;
+			case 'FogExp2':
+				scene.fog = new THREE.FogExp2( fogColor, fogDensity );
+				break;
 
 
 		}
 		}
 
 
@@ -696,13 +677,13 @@ function Viewport( editor ) {
 
 
 		startTime = performance.now();
 		startTime = performance.now();
 
 
-		scene.updateMatrixWorld();
 		renderer.render( scene, camera );
 		renderer.render( scene, camera );
 
 
 		if ( camera === editor.camera ) {
 		if ( camera === editor.camera ) {
 
 
-			sceneHelpers.updateMatrixWorld();
+			renderer.autoClear = false;
 			renderer.render( sceneHelpers, camera );
 			renderer.render( sceneHelpers, camera );
+			renderer.autoClear = true;
 
 
 		}
 		}