-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Former-commit-id: ca50f04
- Loading branch information
Showing
94 changed files
with
1,333 additions
and
369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var BABYLON = BABYLON || {}; | ||
|
||
(function () { | ||
BABYLON.PickingInfo = function () { | ||
}; | ||
|
||
// Properties | ||
BABYLON.PickingInfo.prototype.hit = false; | ||
BABYLON.PickingInfo.prototype.distance = 0; | ||
BABYLON.PickingInfo.prototype.pickedPoint = null; | ||
BABYLON.PickingInfo.prototype.pickedMesh = null; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var BABYLON = BABYLON || {}; | ||
|
||
(function () { | ||
BABYLON.LensFlare = function (size, position, color, imgUrl, system) { | ||
this.color = color || new BABYLON.Color3(1, 1, 1); | ||
this.position = position; | ||
this.size = size; | ||
this.texture = imgUrl ? new BABYLON.Texture(imgUrl, system.getScene(), true) : null; | ||
this._system = system; | ||
|
||
system.lensFlares.push(this); | ||
}; | ||
|
||
// Properties | ||
BABYLON.LensFlare.prototype.position = 0; | ||
BABYLON.LensFlare.prototype.size = 1.0; | ||
BABYLON.LensFlare.prototype.texture = null; | ||
|
||
// Methods | ||
BABYLON.LensFlare.prototype.dispose = function() { | ||
if (this.texture) { | ||
this.texture.dispose(); | ||
} | ||
|
||
// Remove from scene | ||
var index = this._system.lensFlares.indexOf(this); | ||
this._system.lensFlares.splice(index, 1); | ||
}; | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
var BABYLON = BABYLON || {}; | ||
|
||
(function () { | ||
BABYLON.LensFlareSystem = function (name, emitter, scene) { | ||
this.lensFlares = []; | ||
this._scene = scene; | ||
this._emitter = emitter; | ||
this.name = name; | ||
|
||
scene.lensFlareSystems.push(this); | ||
|
||
this.meshesSelectionPredicate = function(m) { | ||
return m.material && m.isVisible && m.isEnabled() && m.checkCollisions; | ||
}; | ||
|
||
// VBO | ||
var vertices = []; | ||
vertices.push(1, 1); | ||
vertices.push(-1, 1); | ||
vertices.push(-1, -1); | ||
vertices.push(1, -1); | ||
this._vertexDeclaration = [2]; | ||
this._vertexStrideSize = 2 * 4; | ||
this._vertexBuffer = scene.getEngine().createVertexBuffer(vertices); | ||
|
||
// Indices | ||
var indices = []; | ||
indices.push(0); | ||
indices.push(1); | ||
indices.push(2); | ||
|
||
indices.push(0); | ||
indices.push(2); | ||
indices.push(3); | ||
|
||
this._indexBuffer = scene.getEngine().createIndexBuffer(indices); | ||
|
||
// Effects | ||
this._effect = this._scene.getEngine().createEffect("lensFlare", | ||
["position"], | ||
["color", "viewportMatrix"], | ||
["textureSampler"], ""); | ||
}; | ||
|
||
// Members | ||
BABYLON.LensFlareSystem.prototype.borderLimit = 300; | ||
|
||
// Properties | ||
BABYLON.LensFlareSystem.prototype.getScene = function() { | ||
return this._scene; | ||
}; | ||
|
||
BABYLON.LensFlareSystem.prototype.getEmitterPosition = function() { | ||
return this._emitter.getAbsolutePosition ? this._emitter.getAbsolutePosition() : this._emitter.position; | ||
}; | ||
|
||
// Methods | ||
BABYLON.LensFlareSystem.prototype.computeEffectivePosition = function (globalViewport) { | ||
var position = this.getEmitterPosition(); | ||
|
||
position = BABYLON.Vector3.Project(position, BABYLON.Matrix.Identity(), this._scene.getTransformMatrix(), globalViewport); | ||
|
||
this._positionX = position.x; | ||
this._positionY = position.y; | ||
|
||
position = BABYLON.Vector3.TransformCoordinates(this.getEmitterPosition(), this._scene.getViewMatrix()); | ||
|
||
if (position.z > 0) { | ||
if ((this._positionX > globalViewport.x) && (this._positionX < globalViewport.x + globalViewport.width)) { | ||
if ((this._positionY > globalViewport.y) && (this._positionY < globalViewport.y + globalViewport.height)) | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
BABYLON.LensFlareSystem.prototype._isVisible = function () { | ||
var emitterPosition = this.getEmitterPosition(); | ||
var direction = emitterPosition.subtract(this._scene.activeCamera.position); | ||
var distance = direction.length(); | ||
direction.normalize(); | ||
|
||
var ray = new BABYLON.Ray(this._scene.activeCamera.position, direction); | ||
var pickInfo = this._scene.pickWithRay(ray, this.meshesSelectionPredicate, true); | ||
|
||
return !pickInfo.hit || pickInfo.distance > distance; | ||
}; | ||
|
||
BABYLON.LensFlareSystem.prototype.render = function () { | ||
if (!this._effect.isReady()) | ||
return false; | ||
|
||
var engine = this._scene.getEngine(); | ||
var viewport = this._scene.activeCamera.viewport; | ||
var globalViewport = viewport.toGlobal(engine); | ||
|
||
// Position | ||
if (!this.computeEffectivePosition(globalViewport)) { | ||
return false; | ||
} | ||
|
||
// Visibility | ||
if (!this._isVisible()) { | ||
return false; | ||
} | ||
|
||
// Intensity | ||
var awayX; | ||
var awayY; | ||
|
||
if (this._positionX < this.borderLimit + globalViewport.x) { | ||
awayX = this.borderLimit + globalViewport.x - this._positionX; | ||
} else if (this._positionX > globalViewport.x + globalViewport.width - this.borderLimit) { | ||
awayX = this._positionX - globalViewport.x - globalViewport.width + this.borderLimit; | ||
} else { | ||
awayX = 0; | ||
} | ||
|
||
if (this._positionY < this.borderLimit + globalViewport.y) { | ||
awayY = this.borderLimit + globalViewport.y - this._positionY; | ||
} else if (this._positionY > globalViewport.y + globalViewport.height - this.borderLimit) { | ||
awayY = this._positionY - globalViewport.y - globalViewport.height + this.borderLimit; | ||
} else { | ||
awayY = 0; | ||
} | ||
|
||
var away = (awayX > awayY) ? awayX : awayY; | ||
if (away > this.borderLimit) { | ||
away = this.borderLimit; | ||
} | ||
|
||
var intensity = 1.0 - (away / this.borderLimit); | ||
if (intensity < 0) { | ||
return false; | ||
} | ||
|
||
if (intensity > 1.0) { | ||
intensity = 1.0; | ||
} | ||
|
||
// Position | ||
var centerX = globalViewport.x + globalViewport.width / 2; | ||
var centerY = globalViewport.y + globalViewport.height / 2; | ||
var distX = centerX - this._positionX; | ||
var distY = centerY - this._positionY; | ||
|
||
// Effects | ||
engine.enableEffect(this._effect); | ||
engine.setState(false); | ||
engine.setDepthBuffer(false); | ||
engine.setAlphaMode(BABYLON.Engine.ALPHA_ADD); | ||
|
||
// VBOs | ||
engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect); | ||
|
||
// Flares | ||
for (var index = 0; index < this.lensFlares.length; index++) { | ||
var flare = this.lensFlares[index]; | ||
|
||
var x = centerX - (distX * flare.position); | ||
var y = centerY - (distY * flare.position); | ||
|
||
var cw = flare.size; | ||
var ch = flare.size * engine.getAspectRatio(); | ||
var cx = 2 * (x / globalViewport.width) - 1.0; | ||
var cy = 1.0 - 2 * (y / globalViewport.height); | ||
|
||
var viewportMatrix = BABYLON.Matrix.FromValues( | ||
cw / 2, 0, 0, 0, | ||
0, ch / 2, 0, 0, | ||
0, 0, 1, 0, | ||
cx, cy, 0, 1); | ||
|
||
this._effect.setMatrix("viewportMatrix", viewportMatrix); | ||
|
||
// Texture | ||
this._effect.setTexture("textureSampler", flare.texture); | ||
|
||
// Color | ||
this._effect.setFloat4("color", flare.color.r * intensity, flare.color.g * intensity, flare.color.b * intensity, 1.0); | ||
|
||
// Draw order | ||
engine.draw(true, 0, 6); | ||
} | ||
|
||
engine.setDepthBuffer(true); | ||
engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE); | ||
return true; | ||
}; | ||
|
||
BABYLON.LensFlareSystem.prototype.dispose = function () { | ||
if (this._vertexBuffer) { | ||
this._scene.getEngine()._releaseBuffer(this._vertexBuffer); | ||
this._vertexBuffer = null; | ||
} | ||
|
||
if (this._indexBuffer) { | ||
this._scene.getEngine()._releaseBuffer(this._indexBuffer); | ||
this._indexBuffer = null; | ||
} | ||
|
||
while (this.lensFlares.length) { | ||
this.lensFlares[0].dispose(); | ||
} | ||
|
||
// Remove from scene | ||
var index = this._scene.lensFlareSystems.indexOf(this); | ||
this._scene.lensFlareSystems.splice(index, 1); | ||
}; | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.