This commit is contained in:
tengge1 2019-06-27 20:57:05 +08:00
parent 002722937e
commit cea9ef8014

View File

@ -10,7 +10,9 @@ import TimelinePanel from './timeline/TimelinePanel.jsx';
import EditorSideBar from './sidebar/EditorSideBar.jsx';
import AssetsPanel from './assets/AssetsPanel.jsx';
import Editor from '../editor/Editor';
import History from '../command/History';
import Helpers from '../helper/Helpers';
import Visualization from '../visual/Visualization';
/**
* 编辑器UI
@ -32,7 +34,394 @@ class EditorUI extends React.Component {
}
componentDidMount() {
app.editor = new Editor(this);
this.app = app;
this.app.editor = this;
//
this.history = new History(this);
//
this.scene = new THREE.Scene();
this.scene.name = L_SCENE;
this.scene.background = new THREE.Color(0xaaaaaa);
this.sceneHelpers = new THREE.Scene();
this.sceneID = null; // ID
this.sceneName = null; //
var width = this.app.viewport.clientWidth;
var height = this.app.viewport.clientHeight;
//
this.DEFAULT_CAMERA = new THREE.PerspectiveCamera(50, width / height, 0.1, 10000);
this.DEFAULT_CAMERA.name = L_DEFAULT_CAMERA;
this.DEFAULT_CAMERA.userData.isDefault = true;
this.DEFAULT_CAMERA.userData.control = 'OrbitControls'; //
this.DEFAULT_CAMERA.position.set(20, 10, 20);
this.DEFAULT_CAMERA.lookAt(new THREE.Vector3());
this.camera = this.DEFAULT_CAMERA.clone();
//
this.renderer = new THREE.WebGLRenderer({
antialias: true
});
this.renderer.gammaInput = false;
this.renderer.gammaOutput = false;
this.renderer.shadowMap.enabled = true;
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
this.renderer.autoClear = false;
this.renderer.autoUpdateScene = false;
this.renderer.setPixelRatio(window.devicePixelRatio);
this.app.viewport.appendChild(this.renderer.domElement);
this.renderer.setSize(width, height);
//
this.audioListener = new THREE.AudioListener();
this.audioListener.name = L_AUDIO_LISTENER;
//
this.objects = [];
// { uuid: { id: 'MongoDB _id', name: 'Script Name', type: 'Script Type', source: 'Source Code', uuid: 'uuid' }}
// uuid使idmongoIDname便typejavascriptvertexShader, fragmentShader, jsonsource
this.scripts = {};
// [{ id: 'MongoDB _id', uuid: 'uuid', layer: '', layerName: '', animations: '' }, ...]
// [{ id: 'MongoDB _id', uuid: 'uuid', name: '', target: 'uuid', type: '', beginTime: '(s)', endTime: '(s)', data: '' }, ...]
// uuid使
// 0
// Tween-Skeletal-Audio-Shader-Filter-Particle-
//
this.animations = [];
//
this.selected = null;
//
this.transformControls = new THREE.TransformControls(this.camera, this.app.viewport);
this.sceneHelpers.add(this.transformControls);
//
this.controls = new THREE.EditorControls(this.camera, this.app.viewport);
//
this.raycaster = new THREE.Raycaster();
this.mouse = new THREE.Vector2();
//
var light = new THREE.DirectionalLight(0xffffff, 1.0);
light.position.z = 10;
this.sceneHelpers.add(light);
this.showViewHelper = true;
//
// this.svg = UI.get('SvgContainer').dom;
// this.visual = new Visualization();
//
this.app.on(`appStarted.${this.id}`, this.onAppStarted.bind(this));
this.app.on(`mousedown.${this.id}`, this.onMouseDown.bind(this));
this.app.on(`mousemove.${this.id}`, this.onMouseMove.bind(this));
//
this.helpers = new Helpers(this.app);
}
onAppStarted() {
this.helpers.start();
this.clear();
}
// -------------------- --------------------------
setScene(scene) { //
//
var objects = this.scene.children;
while (objects.length > 0) {
this.removeObject(objects[0]);
}
//
var children = scene.children.slice();
scene.children.length = 0;
this.scene = scene;
children.forEach(n => {
this.addObject(n);
});
this.app.call('sceneGraphChanged', this);
}
clear(addObject = true) { //
this.history.clear();
this.camera.copy(this.DEFAULT_CAMERA);
if (this.camera.children.findIndex(o => o instanceof THREE.AudioListener) === -1) {
this.camera.add(this.audioListener);
}
if (this.scene.background instanceof THREE.Texture) {
this.scene.background = new THREE.Color(0xaaaaaa);
} else if (this.scene.background instanceof THREE.Color) {
this.scene.background.setHex(0xaaaaaa);
}
this.scene.fog = null;
this.deselect();
//
var objects = this.scene.children;
while (objects.length > 0) {
this.removeObject(objects[0]);
}
this.scripts = {};
this.animations = [{
id: null,
uuid: THREE.Math.generateUUID(),
layer: 0,
layerName: L_ANIMATION_LAYER_1,
animations: []
}, {
id: null,
uuid: THREE.Math.generateUUID(),
layer: 1,
layerName: L_ANIMATION_LAYER_2,
animations: []
}, {
id: null,
uuid: THREE.Math.generateUUID(),
layer: 2,
layerName: L_ANIMATION_LAYER_3,
animations: []
}];
//
if (addObject) {
var light1 = new THREE.AmbientLight(0xffffff, 0.24);
light1.name = L_AMBIENT;
this.addObject(light1);
var light2 = new THREE.DirectionalLight(0xffffff, 0.56);
light2.name = L_DIRECTIONAL;
light2.castShadow = true;
light2.position.set(5, 10, 7.5);
light2.shadow.radius = 0;
light2.shadow.mapSize.x = 2048;
light2.shadow.mapSize.y = 2048;
light2.shadow.camera.left = -20;
light2.shadow.camera.right = 20;
light2.shadow.camera.top = 20;
light2.shadow.camera.bottom = -20;
light2.shadow.camera.near = 0.1;
light2.shadow.camera.far = 500;
this.addObject(light2);
}
this.app.call('editorCleared', this);
this.app.call('scriptChanged', this);
this.app.call('animationChanged', this);
}
// ---------------------- ---------------------------
objectByUuid(uuid) { // uuid
return this.scene.getObjectByProperty('uuid', uuid, true);
}
addObject(object) { //
this.scene.add(object);
this.app.call('objectAdded', this, object);
this.app.call('sceneGraphChanged', this);
}
moveObject(object, parent, before) { //
if (parent === undefined) {
parent = this.scene;
}
parent.add(object);
// sort children array
if (before !== undefined) {
var index = parent.children.indexOf(before);
parent.children.splice(index, 0, object);
parent.children.pop();
}
this.app.call('sceneGraphChanged', this);
}
removeObject(object) { //
if (object.parent === null) { //
return;
}
object.parent.remove(object);
this.app.call('objectRemoved', this, object);
this.app.call('sceneGraphChanged', this);
}
// ------------------------- ------------------------------
addPhysicsHelper(helper) {
var geometry = new THREE.SphereBufferGeometry(2, 4, 2);
var material = new THREE.MeshBasicMaterial({
color: 0xff0000,
visible: false
});
var picker = new THREE.Mesh(geometry, material);
picker.name = 'picker';
picker.userData.object = helper.object;
helper.add(picker);
this.sceneHelpers.add(helper);
this.helpers[helper.object.id] = helper;
this.objects.push(picker);
}
removePhysicsHelper(helper) {
if (this.helpers[helper.object.id] !== undefined) {
var helper = this.helpers[helper.object.id];
helper.parent.remove(helper);
delete this.helpers[helper.object.id];
var objects = this.objects;
objects.splice(objects.indexOf(helper.getObjectByName('picker')), 1);
}
}
// ------------------------ ----------------------------
addScript(object, script) { //
if (this.scripts[object.uuid] === undefined) {
this.scripts[object.uuid] = [];
}
this.scripts[object.uuid].push(script);
this.app.call('scriptAdded', this, script);
}
removeScript(object, script) { //
if (this.scripts[object.uuid] === undefined) {
return;
}
var index = this.scripts[object.uuid].indexOf(script);
if (index !== -1) {
this.scripts[object.uuid].splice(index, 1);
}
this.app.call('scriptRemoved', this);
}
// ------------------------ --------------------------------
select(object) { //
if (this.selected === object) {
return;
}
this.selected = object;
this.app.call('objectSelected', this, object);
}
selectById(id) { // id
if (id === this.camera.id) {
this.select(this.camera);
return;
}
this.select(this.scene.getObjectById(id, true));
}
selectByUuid(uuid) { // uuid
if (uuid === this.camera.uuid) {
this.select(this.camera);
return;
}
this.scene.traverse(child => {
if (child.uuid === uuid) {
this.select(child);
}
});
}
deselect() { //
this.select(null);
}
// ---------------------- --------------------------
focus(object) { //
this.app.call('objectFocused', this, object);
}
focusById(id) { // id
var obj = this.scene.getObjectById(id, true);
if (obj) {
this.focus(obj);
}
}
focusByUUID(uuid) { // uuid
if (uuid === this.camera.uuid) {
this.focus(this.camera);
return;
}
this.scene.traverse(child => {
if (child.uuid === uuid) {
this.focus(child);
}
});
}
// ----------------------- --------------------------
execute(cmd, optionalName) { //
this.history.execute(cmd, optionalName);
}
undo() { //
this.history.undo();
}
redo() { //
this.history.redo();
}
// ---------------------- -----------------------------
onMouseDown(event) {
this.raycaster.setFromCamera(this.mouse, this.camera);
var intersect = this.raycaster.intersectObjects(this.scene.children, true)[0];
if (intersect) {
this.app.call(`intersect`, this, intersect, event);
}
}
onMouseMove(event) {
this.mouse.x = (event.offsetX / this.renderer.domElement.clientWidth) * 2 - 1;
this.mouse.y = -(event.offsetY / this.renderer.domElement.clientHeight) * 2 + 1;
}
}