渲染器序列化和反序列化。

This commit is contained in:
liteng 2018-08-19 20:38:17 +08:00
parent aa4f002c39
commit 76856bb733
3 changed files with 117 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import MeshSerializer from './core/MeshSerializer';
import GroupSerializer from './core/GroupSerializer';
import SpriteSerializer from './core/SpriteSerializer';
import ServerObject from './core/ServerObject';
import WebGLRendererSerializer from './core/WebGLRendererSerializer';
// app
import ConfigSerializer from './app/ConfigSerializer';
@ -47,6 +48,10 @@ Converter.prototype.toJSON = function () {
var camera = (new CamerasSerializer(this.app)).toJSON(this.app.editor.camera);
list.push(camera);
// 渲染器
var renderer = (new WebGLRendererSerializer(this.app)).toJSON(this.app.editor.renderer);
list.push(renderer);
// 脚本
var scripts = (new ScriptSerializer(this.app)).toJSON();
scripts.forEach(n => {
@ -136,6 +141,14 @@ Converter.prototype.fromJson = function (json) {
console.warn(`Converter: 场景中不存在相机信息。`);
}
// 渲染器
var rendererJson = json.filter(n => n.metadata && n.metadata.generator.indexOf('WebGLRendererSerializer') > -1)[0];
if (rendererJson) {
(new WebGLRendererSerializer(this.app)).fromJSON(rendererJson, this.app.editor.renderer);
} else {
console.warn(`Converter: 场景中不存在渲染器信息。`);
}
// 脚本
var scriptJsons = json.filter(n => n.metadata && n.metadata.generator === 'ScriptSerializer' > -1);
if (scriptJsons) {

View File

@ -0,0 +1,62 @@
import BaseSerializer from '../BaseSerializer';
import WebGLShadowMapSerializer from './WebGLShadowMapSerializer';
/**
* WebGLRendererSerializer
* @param {*} app
*/
function WebGLRendererSerializer(app) {
BaseSerializer.call(this, app);
}
WebGLRendererSerializer.prototype = Object.create(BaseSerializer.prototype);
WebGLRendererSerializer.prototype.constructor = WebGLRendererSerializer;
WebGLRendererSerializer.prototype.toJSON = function (obj) {
var json = BaseSerializer.prototype.toJSON.call(this, obj);
json.antialias = true;
json.autoClear = obj.autoClear;
json.autoClearColor = obj.autoClearColor;
json.autoClearDepth = obj.autoClearDepth;
json.autoClearStencil = obj.autoClearStencil;
json.autoUpdateScene = obj.autoUpdateScene;
json.clippingPlanes = obj.clippingPlanes;
json.gammaFactor = obj.gammaFactor;
json.gammaInput = obj.gammaInput;
json.gammaOutput = obj.gammaOutput;
json.localClippingEnabled = obj.localClippingEnabled;
json.physicallyCorrectLights = obj.physicallyCorrectLights;
json.shadowMap = (new WebGLShadowMapSerializer(this.app)).toJSON(obj.shadowMap);
json.sortObjects = obj.sortObjects;
json.toneMapping = obj.toneMapping;
json.toneMappingExposure = obj.toneMappingExposure;
json.toneMappingWhitePoint = obj.toneMappingWhitePoint;
return json;
};
WebGLRendererSerializer.prototype.fromJSON = function (json, parent) {
var obj = parent === undefined ? new THREE.WebGLRenderer({ antialias: json.antialias }) : parent;
obj.autoClear = json.autoClear;
obj.autoClearColor = json.autoClearColor;
obj.autoClearDepth = json.autoClearDepth;
obj.autoClearStencil = json.autoClearStencil;
obj.autoUpdateScene = json.autoUpdateScene;
obj.clippingPlanes = json.clippingPlanes;
obj.gammaFactor = json.gammaFactor;
obj.gammaInput = json.gammaInput;
obj.gammaOutput = json.gammaOutput;
obj.localClippingEnabled = json.localClippingEnabled;
obj.physicallyCorrectLights = json.physicallyCorrectLights;
(new WebGLShadowMapSerializer(this.app)).fromJSON(json.shadowMap, obj.shadowMap);
obj.sortObjects = json.sortObjects;
obj.toneMapping = json.toneMapping;
obj.toneMappingExposure = json.toneMappingExposure;
obj.toneMappingWhitePoint = json.toneMappingWhitePoint;
return obj;
};
export default WebGLRendererSerializer;

View File

@ -0,0 +1,42 @@
import BaseSerializer from '../BaseSerializer';
import TexturesSerializer from '../texture/TexturesSerializer';
/**
* WebGLShadowMapSerializer
* @param {*} app
*/
function WebGLShadowMapSerializer(app) {
BaseSerializer.call(this, app);
}
WebGLShadowMapSerializer.prototype = Object.create(BaseSerializer.prototype);
WebGLShadowMapSerializer.prototype.constructor = WebGLShadowMapSerializer;
WebGLShadowMapSerializer.prototype.toJSON = function (obj) {
var json = BaseSerializer.prototype.toJSON.call(this, obj);
json.autoUpdate = obj.autoUpdate;
json.enabled = obj.enabled;
json.needsUpdate = obj.needsUpdate;
json.type = obj.type;
return json;
};
WebGLShadowMapSerializer.prototype.fromJSON = function (json, parent) {
if (parent === undefined) {
console.warn(`WebGLShadowMapSerializer: parent不允许为空`);
return null;
}
var obj = parent;
obj.autoUpdate = json.autoUpdate;
obj.enabled = json.enabled;
obj.needsUpdate = true;
obj.type = json.type;
return obj;
};
export default WebGLShadowMapSerializer;