r3-legacy/src/r3-d3-scene.js

386 lines
8.5 KiB
JavaScript
Raw Normal View History

2018-04-09 09:35:04 +02:00
/**
2019-07-24 08:08:02 +02:00
* R3.D3.Scene
2019-10-15 22:21:34 +02:00
* @param apiComponent
2018-04-09 09:35:04 +02:00
* @constructor
*/
2019-07-24 08:08:02 +02:00
R3.D3.Scene = function(
2019-10-15 22:21:34 +02:00
apiComponent
2018-04-09 09:35:04 +02:00
) {
2019-10-15 22:21:34 +02:00
__RUNTIME_COMPONENT__;
2018-04-09 09:35:04 +02:00
2019-10-15 22:21:34 +02:00
this.linkedComponents.meshes = [R3.D3.Mesh];
this.linkedComponents.lights = [R3.D3.Light];
this.linkedComponents.textures = [R3.D3.Texture];
this.linkedComponents.materials = [R3.D3.Material];
this.linkedComponents.fog = R3.D3.Fog;
2019-10-18 16:39:56 +02:00
this.linkedComponents.viewports = [R3.D3.Viewport];
this.linkedComponents.camera = R3.D3.Camera;
2018-04-09 09:35:04 +02:00
2019-10-15 22:21:34 +02:00
this.helpers = [];
2018-04-09 09:35:04 +02:00
2019-10-15 22:21:34 +02:00
this.clones = [];
2018-04-09 09:35:04 +02:00
this.grid = [];
this.axis = [];
2019-10-15 22:21:34 +02:00
this.storeClones = false;
2018-04-09 09:35:04 +02:00
2019-10-15 22:21:34 +02:00
__UPGRADE_TO_RUNTIME__;
2018-05-11 12:51:30 +02:00
2018-04-09 09:35:04 +02:00
};
R3.D3.Scene.prototype = Object.create(R3.Component.prototype);
R3.D3.Scene.prototype.constructor = R3.D3.Scene;
/**
* Creates an instance scene
* @returns {THREE.Scene}
*/
R3.D3.Scene.prototype.createInstance = function() {
2019-10-15 22:21:34 +02:00
this.instance = this.graphics.Scene(this);
2018-04-09 09:35:04 +02:00
if (this.showGrid) {
this.drawGrid();
}
if (this.showAxis) {
this.drawAxis();
}
2019-10-06 21:11:18 +02:00
__CREATE_INSTANCE__;
2018-04-09 09:35:04 +02:00
};
R3.D3.Scene.prototype.updateInstance = function(property) {
if (property === 'name') {
this.instance.name = this.name;
return;
}
if (property === 'fog') {
2019-10-15 22:21:34 +02:00
if (R3.Utils.Instance(this.fog)) {
2018-04-09 09:35:04 +02:00
this.instance.fog = this.fog.instance;
2019-10-15 22:21:34 +02:00
} else {
this.instance.fog = null;
2018-04-09 09:35:04 +02:00
}
}
if (property === 'meshes') {
/**
* Add missing meshes
*/
this.meshes.map(
2019-07-24 08:08:02 +02:00
function(mesh) {
2018-04-09 09:35:04 +02:00
if (this.instance.children.indexOf(mesh.instance === -1)) {
this.instance.add(mesh.instance);
}
}.bind(this)
);
}
if (property === 'lights') {
/**
* Add missing lights
*/
this.lights.map(
2019-07-24 08:08:02 +02:00
function(light) {
2018-04-09 09:35:04 +02:00
if (this.instance.children.indexOf(light.instance) === -1) {
this.instance.add(light.instance);
}
}.bind(this)
);
}
if (
property === 'meshes' ||
property === 'lights'
) {
/**
* Remove extra meshes and lights
*/
this.instance.children.map(
2019-07-24 08:08:02 +02:00
function(instanceObject) {
2018-04-09 09:35:04 +02:00
var instanceMeshes = this.meshes.map(
2019-07-24 08:08:02 +02:00
function(mesh) {
2018-04-09 09:35:04 +02:00
return mesh.instance;
}
);
var instanceLights = this.lights.map(
2019-07-24 08:08:02 +02:00
function(light) {
2018-04-09 09:35:04 +02:00
return light.instance;
}
);
if (
(
instanceObject instanceof THREE.Mesh ||
instanceObject instanceof THREE.Light
) &&
(
instanceLights.indexOf(instanceObject) === -1 &&
instanceMeshes.indexOf(instanceObject) === -1
)
) {
this.instance.remove(instanceObject);
}
}.bind(this)
);
return;
}
if (
property === 'showGrid' ||
property === 'gridSize' ||
property === 'gridColor'
) {
if (this.showGrid) {
this.drawGrid();
} else {
this.removeGrid();
}
}
if (property === 'showAxis') {
if (this.showAxis) {
this.drawAxis();
} else {
this.removeAxis();
}
}
2019-10-06 21:11:18 +02:00
__UPDATE_INSTANCE__;
2018-04-09 09:35:04 +02:00
};
/**
* Adds a mesh to the scene
* @param object R3.D3.Mesh
*/
R3.D3.Scene.prototype.addObject = function(object) {
if (object instanceof R3.D3.Mesh) {
if (this.meshes.indexOf(object.id) === -1) {
R3.Utils.PushUnique(this.meshes, object);
}
}
if (object instanceof R3.D3.Light) {
if (this.lights.indexOf(object.id) === -1) {
R3.Utils.PushUnique(this.lights, object);
}
}
object.parentScene = this;
if (
this.instance &&
object.instance
) {
if (this.instance.children.indexOf(object.instance) === -1) {
this.instance.add(object.instance);
}
} else {
// console.warn('either scene or mesh instance not ready');
}
2019-07-24 08:08:02 +02:00
// if (this.parent) {
// this.parent.addComponent(object);
2018-04-09 09:35:04 +02:00
// }
};
R3.D3.Scene.prototype.addClone = function(component) {
if (component instanceof R3.D3.Mesh ||
component instanceof R3.D3.Light
) {
if (this.instance && component.instance) {
if (this.instance.children.indexOf(component.instance) === -1) {
this.instance.add(component.instance);
}
}
component.isClone = true;
R3.Utils.PushUnique(this.clones, component);
var index = this.meshes.indexOf(component);
if (index !== -1) {
this.meshes.splice(index, 1);
}
component.parentScene = this;
}
};
/**
*
* @param object
*/
R3.D3.Scene.prototype.removeObject = function(object) {
var index = -1;
if (object instanceof R3.D3.Mesh) {
index = this.meshes.indexOf(object);
if (index !== -1) {
this.meshes.splice(index, 1);
}
index = this.clones.indexOf(object);
if (index !== -1) {
this.clones.splice(index, 1);
}
} else if (object instanceof R3.D3.Light) {
index = this.lights.indexOf(object);
if (index !== -1) {
this.lights.splice(index, 1);
}
index = this.clones.indexOf(object);
if (index !== -1) {
this.clones.splice(index, 1);
}
} else {
console.warn('Cannot remove this object - what is this ?' + object.toString());
return;
}
if (this.instance.children.indexOf(object.instance) !== -1) {
this.instance.remove(object.instance);
} else {
console.warn('no scene instance');
}
if (object.parentScene === this) {
object.parentScene = null;
}
//
2019-07-24 08:08:02 +02:00
// if (this.parent) {
// this.parent.removeComponent(object);
2018-04-09 09:35:04 +02:00
// }
// this.buildIdToObject();
};
R3.D3.Scene.prototype.drawGrid = function() {
this.removeGrid();
var lineMaterial = new THREE.LineBasicMaterial({
color: this.gridColor.toHex(),
linewidth: 1
});
for (var y = -this.gridSize; y <= this.gridSize; y += 1) {
var Xgeometry = new THREE.Geometry();
Xgeometry.vertices.push(
new THREE.Vector3( y, 0, this.gridSize * -1 ),
new THREE.Vector3( y, 0, this.gridSize )
);
var lineX = new THREE.Line(Xgeometry, lineMaterial);
this.instance.add(lineX);
this.grid.push(lineX);
var Ygeometry = new THREE.Geometry();
Ygeometry.vertices.push(
new THREE.Vector3( this.gridSize * -1 , 0, y ),
new THREE.Vector3( this.gridSize, 0, y )
);
var lineY = new THREE.Line(Ygeometry, lineMaterial);
this.instance.add(lineY);
this.grid.push(lineY);
}
};
R3.D3.Scene.prototype.removeGrid = function() {
this.grid.map(
function(object) {
this.instance.remove(object);
}.bind(this)
);
};
R3.D3.Scene.prototype.drawAxis = function() {
this.removeAxis();
var Xmaterial = new THREE.LineBasicMaterial({
color: 0xff0000,
linewidth: 2
});
var Xgeometry = new THREE.Geometry();
Xgeometry.vertices.push(
new THREE.Vector3( 0, 0, 0 ),
new THREE.Vector3( 100, 0, 0 )
);
var lineX = new THREE.Line(Xgeometry, Xmaterial);
this.instance.add(lineX);
this.axis.push(lineX);
var Ymaterial = new THREE.LineBasicMaterial({
color: 0x00ff00,
linewidth: 2
});
var Ygeometry = new THREE.Geometry();
Ygeometry.vertices.push(
new THREE.Vector3( 0, 0, 0 ),
new THREE.Vector3( 0, 100, 0 )
);
var lineY = new THREE.Line(Ygeometry, Ymaterial);
this.instance.add(lineY);
this.axis.push(lineY);
var Zmaterial = new THREE.LineBasicMaterial({
color: 0x0000ff,
linewidth: 2
});
var Zgeometry = new THREE.Geometry();
Zgeometry.vertices.push(
new THREE.Vector3( 0, 0, 0 ),
new THREE.Vector3( 0, 0, 100 )
);
var lineZ = new THREE.Line(Zgeometry, Zmaterial);
this.instance.add(lineZ);
this.axis.push(lineZ);
};
R3.D3.Scene.prototype.removeAxis = function() {
this.axis.map(
function(object) {
this.instance.remove(object);
}.bind(this)
);
};