Browse Source

Script.js adjusted for Materials

Daniel 10 years ago
parent
commit
4fa8fba2dd
4 changed files with 48 additions and 22 deletions
  1. 11 4
      editor/js/CmdSetMaterialValue.js
  2. 2 0
      editor/js/History.js
  3. 32 15
      editor/js/Script.js
  4. 3 3
      editor/js/Sidebar.Material.js

+ 11 - 4
editor/js/CmdSetMaterialValue.js

@@ -7,6 +7,7 @@ CmdSetMaterialValue = function ( object, attributeName, newValue ) {
 	Cmd.call( this );
 	Cmd.call( this );
 
 
 	this.type = 'CmdSetMaterialValue';
 	this.type = 'CmdSetMaterialValue';
+	this.updatable = true;
 
 
 	this.object = object;
 	this.object = object;
 	this.attributeName = attributeName;
 	this.attributeName = attributeName;
@@ -21,16 +22,22 @@ CmdSetMaterialValue.prototype = {
 	execute: function () {
 	execute: function () {
 
 
 		this.object.material[ this.attributeName ] = this.newValue;
 		this.object.material[ this.attributeName ] = this.newValue;
-		this.editor.signals.objectChanged.dispatch( this.object );
-		this.editor.signals.sceneGraphChanged.dispatch();
+		this.object.material.needsUpdate = true;
+		this.editor.signals.materialChanged.dispatch( this.object.material );
 
 
 	},
 	},
 
 
 	undo: function () {
 	undo: function () {
 
 
 		this.object.material[ this.attributeName ] = this.oldValue;
 		this.object.material[ this.attributeName ] = this.oldValue;
-		this.editor.signals.objectChanged.dispatch( this.object );
-		this.editor.signals.sceneGraphChanged.dispatch();
+		this.object.material.needsUpdate = true;
+		this.editor.signals.materialChanged.dispatch( this.object.material );
+
+	},
+
+	update: function ( cmd ) {
+
+		this.newValue = cmd.newValue;
 
 
 	},
 	},
 
 

+ 2 - 0
editor/js/History.js

@@ -22,6 +22,7 @@ History.prototype = {
 
 
 		var isScriptCmd = lastCmd &&
 		var isScriptCmd = lastCmd &&
 			lastCmd.updatable &&
 			lastCmd.updatable &&
+			cmd.updatable &&
 			lastCmd.script !== undefined &&
 			lastCmd.script !== undefined &&
 			lastCmd.object === cmd.object &&
 			lastCmd.object === cmd.object &&
 			lastCmd.type === cmd.type &&
 			lastCmd.type === cmd.type &&
@@ -30,6 +31,7 @@ History.prototype = {
 
 
 		var isUpdatableCmd = lastCmd &&
 		var isUpdatableCmd = lastCmd &&
 			lastCmd.updatable &&
 			lastCmd.updatable &&
+			cmd.updatable &&
 			lastCmd.object === cmd.object &&
 			lastCmd.object === cmd.object &&
 			lastCmd.type === cmd.type &&
 			lastCmd.type === cmd.type &&
 			timeDifference < 500;
 			timeDifference < 500;

+ 32 - 15
editor/js/Script.js

@@ -91,12 +91,28 @@ var Script = function ( editor ) {
 			if ( currentScript !== 'programInfo' ) return;
 			if ( currentScript !== 'programInfo' ) return;
 
 
 			var json = JSON.parse( value );
 			var json = JSON.parse( value );
-			currentObject.defines = json.defines;
-			currentObject.uniforms = json.uniforms;
-			currentObject.attributes = json.attributes;
 
 
-			currentObject.needsUpdate = true;
-			signals.materialChanged.dispatch( currentObject );
+			if ( JSON.stringify( currentObject.material.defines ) !== JSON.stringify( json.defines ) ) {
+
+				var cmd = new CmdSetMaterialValue( currentObject, 'defines', json.defines );
+				cmd.updatable = false;
+				editor.execute( cmd );
+
+			}
+			if ( JSON.stringify( currentObject.material.uniforms ) !== JSON.stringify( json.uniforms ) ) {
+
+				var cmd = new CmdSetMaterialValue( currentObject, 'uniforms', json.uniforms );
+				cmd.updatable = false;
+				editor.execute( cmd );
+
+			}
+			if ( JSON.stringify( currentObject.material.attributes ) !== JSON.stringify( json.attributes ) ) {
+
+				var cmd = new CmdSetMaterialValue( currentObject, 'attributes', json.attributes );
+				cmd.updatable = false;
+				editor.execute( cmd );
+
+			}
 
 
 		}, 300 );
 		}, 300 );
 
 
@@ -225,9 +241,9 @@ var Script = function ( editor ) {
 					if ( errors.length !== 0 ) break;
 					if ( errors.length !== 0 ) break;
 					if ( renderer instanceof THREE.WebGLRenderer === false ) break;
 					if ( renderer instanceof THREE.WebGLRenderer === false ) break;
 
 
-					currentObject[ currentScript ] = string;
-					currentObject.needsUpdate = true;
-					signals.materialChanged.dispatch( currentObject );
+					currentObject.material[ currentScript ] = string;
+					currentObject.material.needsUpdate = true;
+					signals.materialChanged.dispatch( currentObject.material );
 
 
 					var programs = renderer.info.programs;
 					var programs = renderer.info.programs;
 
 
@@ -239,7 +255,7 @@ var Script = function ( editor ) {
 						var diagnostics = programs[i].diagnostics;
 						var diagnostics = programs[i].diagnostics;
 
 
 						if ( diagnostics === undefined ||
 						if ( diagnostics === undefined ||
-								diagnostics.material !== currentObject ) continue;
+								diagnostics.material !== currentObject.material ) continue;
 
 
 						if ( ! diagnostics.runnable ) valid = false;
 						if ( ! diagnostics.runnable ) valid = false;
 
 
@@ -345,6 +361,7 @@ var Script = function ( editor ) {
 			mode = 'javascript';
 			mode = 'javascript';
 			name = script.name;
 			name = script.name;
 			source = script.source;
 			source = script.source;
+			title.setValue( object.name + ' / ' + name );
 
 
 		} else {
 		} else {
 
 
@@ -354,7 +371,7 @@ var Script = function ( editor ) {
 
 
 					mode = 'glsl';
 					mode = 'glsl';
 					name = 'Vertex Shader';
 					name = 'Vertex Shader';
-					source = object.vertexShader || "";
+					source = object.material.vertexShader || "";
 
 
 					break;
 					break;
 
 
@@ -362,7 +379,7 @@ var Script = function ( editor ) {
 
 
 					mode = 'glsl';
 					mode = 'glsl';
 					name = 'Fragment Shader';
 					name = 'Fragment Shader';
-					source = object.fragmentShader || "";
+					source = object.material.fragmentShader || "";
 
 
 					break;
 					break;
 
 
@@ -371,13 +388,14 @@ var Script = function ( editor ) {
 					mode = 'json';
 					mode = 'json';
 					name = 'Program Properties';
 					name = 'Program Properties';
 					var json = {
 					var json = {
-						defines: object.defines,
-						uniforms: object.uniforms,
-						attributes: object.attributes
+						defines: object.material.defines,
+						uniforms: object.material.uniforms,
+						attributes: object.material.attributes
 					};
 					};
 					source = JSON.stringify( json, null, '\t' );
 					source = JSON.stringify( json, null, '\t' );
 
 
 			}
 			}
+			title.setValue( object.material.name + ' / ' + name );
 
 
 		}
 		}
 
 
@@ -385,7 +403,6 @@ var Script = function ( editor ) {
 		currentScript = script;
 		currentScript = script;
 		currentObject = object;
 		currentObject = object;
 
 
-		title.setValue( object.name + ' / ' + name );
 		container.setDisplay( '' );
 		container.setDisplay( '' );
 		codemirror.setValue( source );
 		codemirror.setValue( source );
 		if (mode === 'json' ) mode = { name: 'javascript', json: true };
 		if (mode === 'json' ) mode = { name: 'javascript', json: true };

+ 3 - 3
editor/js/Sidebar.Material.js

@@ -82,7 +82,7 @@ Sidebar.Material = function ( editor ) {
 	materialProgramInfo.setMarginLeft( '4px' );
 	materialProgramInfo.setMarginLeft( '4px' );
 	materialProgramInfo.onClick( function () {
 	materialProgramInfo.onClick( function () {
 
 
-		signals.editScript.dispatch( currentObject.material, 'programInfo' );
+		signals.editScript.dispatch( currentObject, 'programInfo' );
 
 
 	} );
 	} );
 	materialProgramRow.add( materialProgramInfo );
 	materialProgramRow.add( materialProgramInfo );
@@ -91,7 +91,7 @@ Sidebar.Material = function ( editor ) {
 	materialProgramVertex.setMarginLeft( '4px' );
 	materialProgramVertex.setMarginLeft( '4px' );
 	materialProgramVertex.onClick( function () {
 	materialProgramVertex.onClick( function () {
 
 
-		signals.editScript.dispatch( currentObject.material, 'vertexShader' );
+		signals.editScript.dispatch( currentObject, 'vertexShader' );
 
 
 	} );
 	} );
 	materialProgramRow.add( materialProgramVertex );
 	materialProgramRow.add( materialProgramVertex );
@@ -100,7 +100,7 @@ Sidebar.Material = function ( editor ) {
 	materialProgramFragment.setMarginLeft( '4px' );
 	materialProgramFragment.setMarginLeft( '4px' );
 	materialProgramFragment.onClick( function () {
 	materialProgramFragment.onClick( function () {
 
 
-		signals.editScript.dispatch( currentObject.material, 'fragmentShader' );
+		signals.editScript.dispatch( currentObject, 'fragmentShader' );
 
 
 	} );
 	} );
 	materialProgramRow.add( materialProgramFragment );
 	materialProgramRow.add( materialProgramFragment );