浏览代码

Editor: Added basic scripting support.

Mr.doob 11 年之前
父节点
当前提交
ca7a680b33
共有 6 个文件被更改,包括 82 次插入3 次删除
  1. 1 1
      editor/js/Menubar.File.js
  2. 34 1
      editor/js/Player.js
  3. 30 0
      editor/js/Sidebar.Script.js
  4. 3 0
      src/core/Object3D.js
  5. 13 1
      src/core/Script.js
  6. 1 0
      src/loaders/ObjectLoader.js

+ 1 - 1
editor/js/Menubar.File.js

@@ -211,7 +211,7 @@ Menubar.File = function ( editor ) {
 
 		var preview = new Player( editor.scene.toJSON() );
 		preview.setSize( 800, 600 );
-		preview.update();
+		preview.play();
 
 		var popup = window.open( '', 'preview', 'width=800,height=600' );
 		popup.document.body.style.margin = 0;

+ 34 - 1
editor/js/Player.js

@@ -5,8 +5,27 @@ var Player = function ( json ) {
 	camera.lookAt( new THREE.Vector3() );
 
 	var scene = new THREE.ObjectLoader().parse( json );
+
 	var renderer = new THREE.WebGLRenderer( { antialias: true } );
 
+	//
+
+	var scriptObjects = [];
+
+	scene.traverse( function ( child ) {
+
+		if ( child.script !== undefined ) {
+
+			child.script.compiled = new Function( 'object', child.script.source );
+
+			scriptObjects.push( child );
+
+		}
+
+	} );
+
+	//
+
 	var setSize = function ( width, height ) {
 
 		camera.aspect = width / height;
@@ -16,8 +35,22 @@ var Player = function ( json ) {
 
 	};
 
+	var play = function () {
+
+		requestAnimationFrame( play );
+		update();
+
+	};
+
 	var update = function () {
 
+		for ( var i = 0; i < scriptObjects.length; i ++ ) {
+
+			var object = scriptObjects[ i ];
+			object.script.compiled( object );
+
+		}
+
 		renderer.render( scene, camera );
 
 	};
@@ -25,7 +58,7 @@ var Player = function ( json ) {
 	return {
 		dom: renderer.domElement,
 		setSize: setSize,
-		update: update 
+		play: play
 	}
 
 };

+ 30 - 0
editor/js/Sidebar.Script.js

@@ -17,12 +17,42 @@ Sidebar.Script = function ( editor ) {
 	var scriptsRow = new UI.Panel();
 	container.add( scriptsRow );
 
+	// source
+
+	var scriptSourceRow = new UI.Panel();
+	var scriptSource = new UI.TextArea().setWidth( '240px' ).setHeight( '180px' ).setColor( '#444' ).setFontSize( '12px' );
+	scriptSource.onChange( function () {
+
+		var object = editor.selected;
+
+		object.script = new THREE.Script( scriptSource.getValue() );
+
+		editor.signals.objectChanged.dispatch( object );
+
+	} );
+
+	scriptSourceRow.add( scriptSource );
+
+	container.add( scriptSourceRow );
+
+	//
+
 	signals.objectSelected.add( function ( object ) {
 
 		if ( object !== null ) {
 
 			container.setDisplay( 'block' );
 
+			if ( object.script !== undefined ) {
+
+				scriptSource.setValue( object.script.source );
+
+			} else {
+
+				scriptSource.setValue( '' );
+
+			}
+
 		} else {
 
 			container.setDisplay( 'none' );

+ 3 - 0
src/core/Object3D.js

@@ -577,6 +577,7 @@ THREE.Object3D.prototype = {
 
 			if ( object.name !== '' ) data.name = object.name;
 			if ( JSON.stringify( object.userData ) !== '{}' ) data.userData = object.userData;
+			if ( object.script !== undefined ) data.script = object.script.source;
 			if ( object.visible !== true ) data.visible = object.visible;
 
 			if ( object instanceof THREE.PerspectiveCamera ) {
@@ -690,6 +691,8 @@ THREE.Object3D.prototype = {
 
 		object.userData = JSON.parse( JSON.stringify( this.userData ) );
 
+		if ( this.script !== undefined ) object.script = this.script.clone();
+
 		if ( recursive === true ) {
 
 			for ( var i = 0; i < this.children.length; i ++ ) {

+ 13 - 1
src/core/Script.js

@@ -7,4 +7,16 @@ THREE.Script = function ( source ) {
 	this.uuid = THREE.Math.generateUUID();
 	this.source = source;
 
-};
+};
+
+THREE.Script.prototype = {
+
+	constructor: THREE.Script,
+
+	clone: function () {
+
+		return new THREE.Script( this.source );
+
+	}
+
+}

+ 1 - 0
src/loaders/ObjectLoader.js

@@ -332,6 +332,7 @@ THREE.ObjectLoader.prototype = {
 
 			if ( data.visible !== undefined ) object.visible = data.visible;
 			if ( data.userData !== undefined ) object.userData = data.userData;
+			if ( data.script !== undefined ) object.script = new THREE.Script( data.script );
 
 			if ( data.children !== undefined ) {