Skip to content

Commit

Permalink
cloning effectors to avoid duplicating compoents
Browse files Browse the repository at this point in the history
  • Loading branch information
ericrius1 committed Oct 14, 2014
1 parent 8a6b878 commit cd06d93
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 49 deletions.
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
<script src="lib/ShaderLoader.js"></script>
<script src="lib/jquery.min.js"></script>
<script src="lib/stats.js"></script>
<script src="lib/TextCreator.js"></script>

<script src = "js/constants.js"></script>
<script src = "js/textprimitive.js"></script>
<script src = "js/scripts/flightpath.js"></script>
<script src = "js/effectors/visibility.js"></script>
<script src = "js/effectors/scale.js"></script>
Expand Down
13 changes: 8 additions & 5 deletions js/cloners/cloner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ G.Cloner = function(params){
this.params.posRange = this.params.posRange || {x: {start: 0, end: 0}, y: {start: 0, end: 0}, z:{start: 0, end: 0 }}
this.params.scaleRange = this.params.scaleRange || {x: {start: 1, end: 1}, y: {start: 1, end: 1}, z:{start: 1, end: 1 }}
this.params.rotRange = this.params.rotRange || {start: 0, end: 0}

this.primitiveVis = false;
//we only want to wait to spawn the primitives and show them if we've specified a visibility effector, otherwise show them right away
if(!this.params.visibilityEffector){

this.spawnPrimitives();
}

Expand All @@ -23,15 +24,17 @@ G.Cloner.prototype.spawnPrimitives = function(){
primitive.transform.scale.set(G.rf(this.params.scaleRange.x.start, this.params.scaleRange.x.end), G.rf(this.params.scaleRange.y.start, this.params.scaleRange.y.end),G.rf(this.params.scaleRange.z.start, this.params.scaleRange.z.end));
primitive.transform.rotation.set(0, G.rf(this.params.rotRange.start, this.params.rotRange.end), 0)
this.addChild(primitive)

_.each(this.params.primitiveEffectors, function(effector){
primitive.addComponent(effector)
})
if(effector === G.visibilityEffector){
this.primitiveVis = true;
}
primitive.addComponent(_.clone(effector))
}, this)




if(!this.params.visibilityEffector){
if(!this.primitiveVis){
primitive.appear();
}
else{
Expand Down
87 changes: 44 additions & 43 deletions js/cloners/clonermanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,65 @@

//ranges are relative to the parent object
G.ClonerManager = function(){
this.cloners = [];

var visibilityEffector = new G.VisibilityEffector(G.dolly, {distance: 600});
var visibilityEffector = new G.VisibilityEffector({distance: 600});
var primitiveVisibilityEffector = new G.VisibilityEffector({distance: 200});
var cloner = new G.Cloner({
primitive: G.ArcPrimitive,
num: 10,
position: new THREE.Vector3(-10, 0, 300),
posRange: {x: {start: -200, end: -20}, y: {start: 0, end: 0}, z:{start: -300, end: 0 }},
scaleRange: { x: {start: 1, end: 2}, y: {start: 1, end: 10}, z:{start: 1, end: 2 }},
rotRange: {start: 0, end: Math.PI * 2},
primitiveEffectors: [primitiveVisibilityEffector]
})
cloner.addComponent(visibilityEffector)
G.app.addObject(cloner)
cloner.addEventListener('distancethreshold', function(){
this.spawnPrimitives();
})

cloner = new G.Cloner({
primitive: G.TracerPrimitive,
num: 20,
position: new THREE.Vector3(10, 0, 200),
posRange: {x: {start: -200, end: -20}, y: {start: 20, end: 40}, z:{start: -300, end: 0 }},
scaleRange: {x: {start: 1, end: 10}, y: {start: 1, end: 2}, z:{start: 1, end: 1 }},
});
var visibilityEffector = new G.VisibilityEffector({distance: 600});
cloner.addComponent(visibilityEffector)
G.app.addObject(cloner)
cloner.addEventListener('distancethreshold', function(){
this.spawnPrimitives();
})



//FRESNAL PRIMITIVE******************
var scaleEffector = new G.ScaleEffector( {nearDistance: 50, farDistance: 400, nearScale: 10, farScale: 1})
var cloner = new G.Cloner({
primitive: G.FresnalPrimitive,
num: 1,
position: new THREE.Vector3(0, 0, -100),
// posRange: {x: {start: -200, end: -20}, y: {start: 0, end: 0}, z:{start: -800, end: 0 }},
rotRange: {start: 0, end: Math.PI * 2},
primitiveEffectors: [scaleEffector]
})

G.app.addObject(cloner)

scaleEffector = new G.ScaleEffector( {nearDistance: 50, farDistance: 400, nearScale: 2, farScale: 1})
var cloner = new G.Cloner({
primitive: G.CurveDotPrimitive,
num: 1,
position: new THREE.Vector3(0, 0, 200),
// posRange: {x: {start: -200, end: -20}, y: {start: 0, end: 0}, z:{start: -800, end: 0 }},
rotRange: {start: 0, end: Math.PI * 2},
primitiveEffectors: [scaleEffector]

})

G.app.addObject(cloner)
// cloner = new G.Cloner({
// primitive: G.TracerPrimitive,
// num: 20,
// position: new THREE.Vector3(10, 0, 200),
// posRange: {x: {start: -200, end: -20}, y: {start: 20, end: 40}, z:{start: -300, end: 0 }},
// scaleRange: {x: {start: 1, end: 10}, y: {start: 1, end: 2}, z:{start: 1, end: 1 }},
// });
// var visibilityEffector = new G.VisibilityEffector({distance: 600});
// cloner.addComponent(visibilityEffector)
// G.app.addObject(cloner)
// cloner.addEventListener('distancethreshold', function(){
// this.spawnPrimitives();
// })



// //FRESNAL PRIMITIVE******************
// var scaleEffector = new G.ScaleEffector( {nearDistance: 50, farDistance: 400, nearScale: 10, farScale: 1})
// var cloner = new G.Cloner({
// primitive: G.FresnalPrimitive,
// num: 1,
// position: new THREE.Vector3(0, 0, -100),
// // posRange: {x: {start: -200, end: -20}, y: {start: 0, end: 0}, z:{start: -800, end: 0 }},
// rotRange: {start: 0, end: Math.PI * 2},
// primitiveEffectors: [scaleEffector]
// })

// G.app.addObject(cloner)

// scaleEffector = new G.ScaleEffector( {nearDistance: 50, farDistance: 400, nearScale: 2, farScale: 1})
// var cloner = new G.Cloner({
// primitive: G.CurveDotPrimitive,
// num: 1,
// position: new THREE.Vector3(0, 0, 200),
// // posRange: {x: {start: -200, end: -20}, y: {start: 0, end: 0}, z:{start: -800, end: 0 }},
// rotRange: {start: 0, end: Math.PI * 2},
// primitiveEffectors: [scaleEffector]

// })

// G.app.addObject(cloner)



Expand Down
2 changes: 1 addition & 1 deletion js/infoworld.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ InfoWorld.prototype.init = function(param) {
vSceneObj.addComponent(visual);
this.addObject(vSceneObj);

this._services[4].scene.fog = new THREE.Fog(0x00000, -4000, 80000)
G.scene.fog = new THREE.Fog(0x00000, -4000, 80000)

var cloners = new G.ClonerManager();
//Should be able to just update matrix world oncee before we start rendering, because then threejs renderer automatically calls this every frame... something with vizi?
Expand Down
Empty file added js/textprimitive.js
Empty file.
162 changes: 162 additions & 0 deletions lib/TextCreator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//Author: @Cabbibo

function TextCreator( totalSize ){


this.params = {

size: totalSize ,
type: "10pt GeoSans",
color: "rgba( 255 , 255 , 255 , 0.95 )",
crispness: 10.,
margin: totalSize / 10,
square: false


}

this.font = "GeoSans";

}

TextCreator.prototype.createTexture = function( string , params ){


var canvas = document.createElement('canvas');

/*canvas.style.position = 'fixed';
canvas.style.zIndex = '999';
document.body.appendChild( canvas );*/

var ctx = canvas.getContext( '2d' );
var params = {

color: this.params.color,
size: this.params.size,
crispness: this.params.crispness,
square: this.params.square,
margin: this.params.size / 2

}


var size = params.size;
var color = params.color;

// This is the factor the canvas will be scaled
// up by, which basically equates to 'crispness'
var scaleFactor = params.crispness;

// To make sure that the text is crisp,
// need to draw it large and scale down
var fullSize = scaleFactor * size;


// If you want a margin, you can define it in the params
if( !params.margin )
margin = size*.5;
else
margin = params.margin;

// Gets how wide the text is
ctx.font = fullSize + "pt " + this.font;
var textWidth = ctx.measureText(string).width;

// Can choose to make a square texture if we want to
if( !params.square ){

canvas.width = textWidth + margin;
canvas.height = fullSize + margin;

}else{

canvas.width = textWidth + margin;
canvas.height = textWidth + margin;

}

ctx.font = fullSize + "pt " + this.font;

// Gives us a background instead of transparent background
if( params.backgroundColor ) {
ctx.fillStyle = params.backgroundColor;
ctx.fillRect(
canvas.width / 2 - textWidth / 2 - margin / 2,
canvas.height / 2 - fullSize / 2 - + margin / 2,
textWidth + margin,
fullSize + margin
);
}

// Makes sure our text is centered
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
ctx.fillText(string, canvas.width / 2, canvas.height / 2);

// Creates a texture
var texture = new THREE.Texture(canvas);

texture.scaledWidth = canvas.width;
texture.scaledHeight = canvas.height;

if( texture.scaledWidth > texture.scaleHeight ){
texture.scaledWidth /= texture.scaledWidth;
texture.scaledHeight /= texture.scaledWidth;
}else{
texture.scaledWidth /= texture.scaledHeight;
texture.scaledHeight /= texture.scaledHeight;
}


texture.needsUpdate = true;

return texture;

}

TextCreator.prototype.createMesh = function( string , par ){
var params = {

color: par.color || this.params.color,
size: par.size || this.params.size ,
crispness: par.crispness || this.params.crispness,
margin: par.margin || this.params.margin

}


var texture = this.createTexture( string , params );

var material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending,
depthwrite: false,
opacity: 1.,
color: 0xffffff
});

var geo = new THREE.PlaneGeometry(
texture.scaledWidth / params.crispness,
texture.scaledHeight / params.crispness
);

var mesh = new THREE.Mesh(geo, material);
mesh.scale.multiplyScalar( params.size );
mesh.scaledWidth = texture.scaledWidth;
mesh.scaledHeight = texture.scaledHeight;

mesh.totalWidth = mesh.scaledWidth * params.size;
mesh.totalHeight = mesh.scaledHeight * params.size;

// Assigning the texture
mesh.string = string;

return mesh;

}



0 comments on commit cd06d93

Please sign in to comment.