Browse Source

Editor/Script: Option 2.

Mr.doob 10 năm trước cách đây
mục cha
commit
dd1f30ed38

+ 2 - 2
editor/examples/fountain.app.json

@@ -93,7 +93,7 @@
 		"3741222A-BD8F-401C-A5D2-5A907E891896": [
 			{
 				"name": "Particle Fountain",
-				"source": "/**\n * @author mrdoob / http://mrdoob.com/\n */\n\nvar original = scene.getObjectByName( 'Particle' );\n\nvar particles = [];\n\nreturn {\n\tupdate: function ( event ) {\n\n\t\tif ( particles.length < 200 ) {\n\n\t\t\tvar velocity = new THREE.Vector3();\n\t\t\tvelocity.x = Math.random() * 10 - 5;\n\t\t\tvelocity.y = Math.random() * 10 + 10;\n\t\t\tvelocity.z = Math.random() * 10 - 5;\n\n\t\t\tvar particle = original.clone();\n\t\t\tparticle.userData.velocity = velocity;\n\t\t\tparticles.push( particle );\n\n\t\t\tscene.add( particle );\n\n\t\t}\n\n\t\tfor ( var i = 0; i < particles.length; i ++ ) {\n\n\t\t\tvar particle = particles[ i ];\n\n\t\t\tvar velocity = particle.userData.velocity;\n\n\t\t\tvelocity.y -= 0.98;\n\n\t\t\tparticle.position.add( velocity );\n\n\t\t\tif ( particle.position.y < 0 ) {\n\n\t\t\t\tparticle.position.y = 0;\n\n\t\t\t\tvelocity.y = - velocity.y;\n\t\t\t\tvelocity.multiplyScalar( 0.6 );\n\n\t\t\t}\n\n\t\t}\n\n\t}\n};"
+				"source": "/**\n * @author mrdoob / http://mrdoob.com/\n */\n\nvar original = scene.getObjectByName( 'Particle' );\n\nvar particles = [];\n\nfunction update( event ) {\n\n\tif ( particles.length < 200 ) {\n\n\t\tvar velocity = new THREE.Vector3();\n\t\tvelocity.x = Math.random() * 10 - 5;\n\t\tvelocity.y = Math.random() * 10 + 10;\n\t\tvelocity.z = Math.random() * 10 - 5;\n\n\t\tvar particle = original.clone();\n\t\tparticle.userData.velocity = velocity;\n\t\tparticles.push( particle );\n\n\t\tscene.add( particle );\n\n\t}\n\n\tfor ( var i = 0; i < particles.length; i ++ ) {\n\n\t\tvar particle = particles[ i ];\n\n\t\tvar velocity = particle.userData.velocity;\n\n\t\tvelocity.y -= 0.98;\n\n\t\tparticle.position.add( velocity );\n\n\t\tif ( particle.position.y < 0 ) {\n\n\t\t\tparticle.position.y = 0;\n\n\t\t\tvelocity.y = - velocity.y;\n\t\t\tvelocity.multiplyScalar( 0.6 );\n\n\t\t}\n\n\t}\n\n}"
 			}]
 	}
-}
+}

+ 1 - 1
editor/examples/pong.app.json

@@ -131,7 +131,7 @@
 		"31517222-A9A7-4EAF-B5F6-60751C0BABA3": [
 			{
 				"name": "Game logic",
-				"source": "/**\n * @author mrdoob / http://mrdoob.com/\n */\n\nvar ball = scene.getObjectByName( 'Ball' );\n\nvar position = ball.position;\n\nvar direction = new THREE.Vector3();\ndirection.x = Math.random() - 0.5;\ndirection.z = Math.random() - 0.5;\ndirection.normalize();\ndirection.multiplyScalar( 8 );\n\n//\n\nvar pad1 = scene.getObjectByName( 'Pad 1' );\nvar pad2 = scene.getObjectByName( 'Pad 2' );\n\nreturn {\n\tmousemove: function ( event ) {\n\t\tpad1.position.z = ( event.clientX / window.innerWidth ) * 300 - 150;\n\t\tpad2.position.z = - pad1.position.z;\n\t},\n\tupdate: function ( event ) {\n\n\t\tif ( position.x < -300 || position.x > 300 )\n\t\t\tdirection.x = - direction.x;\n\n\t\tif ( position.z < -200 || position.z > 200 )\n\t\t\tdirection.z = - direction.z;\n\n\t\tposition.add( direction );\n\n\t}\n};"
+				"source": "/**\n * @author mrdoob / http://mrdoob.com/\n */\n\nvar ball = scene.getObjectByName( 'Ball' );\n\nvar position = ball.position;\n\nvar direction = new THREE.Vector3();\ndirection.x = Math.random() - 0.5;\ndirection.z = Math.random() - 0.5;\ndirection.normalize();\ndirection.multiplyScalar( 8 );\n\nvar pad1 = scene.getObjectByName( 'Pad 1' );\nvar pad2 = scene.getObjectByName( 'Pad 2' );\n\n//\n\nfunction mousemove( event ) {\n\n\tpad1.position.z = ( event.clientX / window.innerWidth ) * 300 - 150;\n\tpad2.position.z = - pad1.position.z;\n\n}\n\nfunction update( event ) {\n\n\tif ( position.x < -300 || position.x > 300 )\n\t\tdirection.x = - direction.x;\n\n\tif ( position.z < -200 || position.z > 200 )\n\t\tdirection.z = - direction.z;\n\n\tposition.add( direction );\n\n}"
 			}]
 	}
 }

+ 3 - 1
editor/js/libs/app.js

@@ -40,10 +40,12 @@ var APP = {
 
 					var script = sources[ i ];
 
-					var events = ( new Function( 'scene', script.source ).bind( object ) )( scene );
+					var events = ( new Function( 'scene', 'keydown', 'keyup', 'mousedown', 'mouseup', 'mousemove', 'update', script.source + '\nreturn { keydown: keydown, keyup: keyup, mousedown: mousedown, mouseup: mouseup, mousemove: mousemove, update: update };' ).bind( object ) )( scene );
 
 					for ( var name in events ) {
 
+						if ( events[ name ] === undefined ) continue;
+
 						if ( scripts[ name ] === undefined ) {
 
 							console.warn( 'APP.Player: event type not supported (', name, ')' );

+ 1 - 1
editor/js/libs/ui.editor.js

@@ -64,7 +64,7 @@ UI.ScriptEditor = function ( editor ) {
 
 			try {
 
-				( new Function( 'scene', source ).bind( object ) )( scene );
+				( new Function( 'scene', 'keydown', 'keyup', 'mousedown', 'mouseup', 'mousemove', 'update', source + '\nreturn { keydown: keydown, keyup: keyup, mousedown: mousedown, mouseup: mouseup, mousemove: mousemove, update: update };' ).bind( object ) )( scene );
 
 				textarea.dom.classList.add( 'success' );
 				textarea.dom.classList.remove( 'fail' );