提交 ca7a680b 编写于 作者: M Mr.doob

Editor: Added basic scripting support.

上级 49ab64c8
......@@ -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;
......
......@@ -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
}
};
\ No newline at end of file
......@@ -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' );
......
......@@ -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 ++ ) {
......
......@@ -7,4 +7,16 @@ THREE.Script = function ( source ) {
this.uuid = THREE.Math.generateUUID();
this.source = source;
};
\ No newline at end of file
};
THREE.Script.prototype = {
constructor: THREE.Script,
clone: function () {
return new THREE.Script( this.source );
}
}
\ No newline at end of file
......@@ -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 ) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册