Skip to content

Commit

Permalink
Merge branch 'master' of github.com:playcanvas/engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksims committed Aug 5, 2016
2 parents c52a0e7 + ac8f2c6 commit c048df5
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 7 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.191.0-dev
0.192.0-dev
4 changes: 2 additions & 2 deletions build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ var getVersion = function (callback) {
var loadDependencies = function (fullpath, callback) {
fs.readFile(fullpath, function (err, data) {
if (err) callback(err);
callback(data.toString().trim().split(os.EOL))
callback(data.toString().trim().split(new RegExp("[\\r\\n]+", 'g')));
});
};

Expand All @@ -144,7 +144,7 @@ var concatentateShaders = function (callback) {
if (ext) {
var fullpath = dir + file;

var content = replaceAll(fs.readFileSync(fullpath).toString(), os.EOL, "\\n");
var content = replaceAll(fs.readFileSync(fullpath).toString(), "[\\r\\n]+", "\\n");
var name = file.split(".")[0] + ext;
var data = util.format('pc.shaderChunks.%s = "%s";\n', name, content);
fs.writeSync(fd, data);
Expand Down
203 changes: 203 additions & 0 deletions examples/portal/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<!doctype html>
<html>
<head>
<script src="../../build/output/playcanvas-latest.js"></script>
<link href="../style.css" rel="stylesheet" />
</head>

<body>
<!-- The canvas element -->
<canvas id="application-canvas" tabindex="0"></canvas>

<!-- The script -->
<script>
var canvas = document.getElementById("application-canvas");
canvas.focus();

// Create the application and start the update loop
var app = new pc.Application(canvas, {
keyboard: new pc.Keyboard(canvas)
});

// Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
app.start();

///////////////////////////////
// Scipt to rotate the scene //
///////////////////////////////
var Rotator = pc.createScript('rotator');

Rotator.prototype.update = function(dt) {
this.entity.setEulerAngles(0, Math.sin(new Date().getTime() / 500) * 20, 0);
};

//////////////////////////////////////////////////////////////////
// Script to set stencil options for entities inside the portal //
//////////////////////////////////////////////////////////////////
var InsidePortal = pc.createScript('insidePortal');

InsidePortal.prototype.initialize = function() {
var meshInstances = this.entity.model.meshInstances;
var mat, i;
var stencil = new pc.StencilParameters({
func: pc.FUNC_NOTEQUAL,
ref: 0
});
for (i = 0; i < meshInstances.length; i++) {
meshInstances[i].layer -= 2;
mat = meshInstances[i].material;
mat.stencilBack = mat.stencilFront = stencil;
}

var prt = this.entity.particlesystem;
if (prt) {
prt.emitter.meshInstance.layer -= 2;
mat = prt.emitter.material;
mat.stencilBack = mat.stencilFront = stencil;
}
};

///////////////////////////////////////////////////////////////////
// Script to set stencil options for entities outside the portal //
///////////////////////////////////////////////////////////////////
var OutsidePortal = pc.createScript('outsidePortal');

OutsidePortal.prototype.initialize = function() {
var meshInstances = this.entity.model.meshInstances;
var mat, i;
var stencil = new pc.StencilParameters({
func: pc.FUNC_EQUAL,
ref: 0
});
for (i = 0; i < meshInstances.length; i++) {
meshInstances[i].layer--;
mat = meshInstances[i].material;
mat.stencilBack = mat.stencilFront = stencil;
}

var prt = this.entity.particlesystem;
if (prt) {
prt.emitter.meshInstance.meshes[i].layer--;
mat = prt.emitter.material;
mat.stencilBack = mat.stencilFront = stencil;
}
};

///////////////////////////////////////////////
// Set stencil options for the portal itself //
///////////////////////////////////////////////
var Portal = pc.createScript('portal');

// initialize code called once per entity
Portal.prototype.initialize = function() {
// We only want to write to the stencil buffer
var mat = this.entity.model.meshInstances[0].material;
mat.depthWrite = false;
mat.redWrite = mat.greenWrite = mat.blueWrite = mat.alphaWrite = false;
mat.stencilBack = mat.stencilFront = new pc.StencilParameters({
zpass: pc.STENCILOP_INCREMENT
});
mat.update();
};

app.scene.ambientLight = new pc.Color(0.2, 0.2, 0.2);

var insideMat = new pc.StandardMaterial();
var outsideMat = new pc.StandardMaterial();
var portalMat = new pc.StandardMaterial();
var borderMat = new pc.StandardMaterial();
borderMat.emissive.set(1, 0.4, 0);
borderMat.update();

// Create an Entity with a camera component
var camera = new pc.Entity();
camera.addComponent('camera', {
clearColor: new pc.Color(0.12, 0.12, 0.12)
});
camera.setLocalPosition(7.5, 5.5, 6.1);
camera.setLocalEulerAngles(-30, 45, 0);

// Create an Entity with a directional light component
var light = new pc.Entity();
light.addComponent('light', {
type: 'directional',
color: new pc.Color(1, 1, 1)
});
light.setLocalPosition(2, 2, -2);
light.setEulerAngles(45, 135, 0);

// Create a root for the graphical scene
var group = new pc.Entity();
group.addComponent('script');
group.script.create('rotator');

// Create a Entity with a Box model component
var box = new pc.Entity();
box.addComponent('model', {
type: 'box',
});
box.model.material = insideMat;
box.addComponent('particlesystem', {
numParticles: 128,
lifetime: 5,
rate: 0.1,
rate2: 0.1,
emitterShape: pc.EMITTERSHAPE_BOX,
emitterExtents: new pc.Vec3(0, 0, 0),
scaleGraph: new pc.Curve([0, 0.1]),
velocityGraph: new pc.CurveSet([[0, 3], [0, 3], [0, 3]]),
velocityGraph2: new pc.CurveSet([[0, -3], [0, -3], [0, -3]])
});
box.addComponent('script');
box.script.create('insidePortal');
box.setLocalPosition(0, 0.5, -1.936);

// Create the portal entity
var portal = new pc.Entity();
portal.addComponent('model', {
type: 'plane',
});
portal.model.material = portalMat;
portal.addComponent('script');
portal.script.create('portal');
portal.setLocalPosition(0, 0, 0);
portal.setLocalEulerAngles(90, 0, 0);
portal.setLocalScale(4, 1, 6);

// Create the portal border entity
var border = new pc.Entity();
border.addComponent('model', {
type: 'plane',
});
border.model.material = borderMat;
border.addComponent('script');
border.script.create('outsidePortal');
border.setLocalPosition(0, 0, 0);
border.setLocalEulerAngles(90, 0, 0);
border.setLocalScale(4.68, 1.17, 7.019);

// Create an entity with a sphere model component
var sphere = new pc.Entity();
sphere.addComponent('model', {
type: 'sphere',
});
sphere.model.material = outsideMat;
sphere.addComponent('script');
sphere.script.create('outsidePortal');
sphere.setLocalPosition(0, 0, -2.414);
sphere.setLocalEulerAngles(0, 0, 0);
sphere.setLocalScale(1, 1, 1);

// Add the new entities to the hierarchy
app.root.addChild(camera);
app.root.addChild(light);
app.root.addChild(group);
group.addChild(box);
group.addChild(portal);
group.addChild(border);
group.addChild(sphere);
</script>
</body>
</html>
18 changes: 17 additions & 1 deletion src/asset/asset-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ pc.extend(pc, function () {
var url = asset.file.url;

// apply prefix if present
if (self.prefix) {
if (self.prefix && !self._isAbsoluteUrl(url)) {
if (url.startsWith('/')) {
url = url.slice(1);
}
Expand Down Expand Up @@ -689,6 +689,22 @@ pc.extend(pc, function () {
getAssetById: function (id) {
console.warn("DEPRECATED: getAssetById() use get() instead");
return this.get(id);
},

// check if url is absolute (e.g. begins with 'http://', 'https://', 'ftp://', '//')
_isAbsoluteUrl: function (url) {
var pattern = new RegExp(
'^' + // beginning of the url
'\\s*' + // ignore leading spaces (some browsers trim the url automatically, but we can't assume that)
'(?:' + // beginning of protocol scheme (non-captured regex group)
'[a-z]+[a-z0-9\\-\\+\\.]*' + // protocol scheme must (RFC 3986) consist of "a letter and followed by any combination of letters, digits, plus ("+"), period ("."), or hyphen ("-")."
':' + // protocol scheme must end with colon character
')?' + // end of optional scheme group, the group is optional since the string may be a protocol-relative absolute URL
'//', // a absolute url must always begin with two forward slash characters (ignoring any leading spaces and protocol scheme)
'i' // non case-sensitive flag
);

return pattern.test(url);
}

};
Expand Down
6 changes: 3 additions & 3 deletions src/graphics/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pc.extend(pc, function () {
var _createContext = function (canvas, options) {
var names = ["webgl", "experimental-webgl"];
var context = null;
options = options || {};
options.stencil = true;
for (var i = 0; i < names.length; i++) {
try {
context = canvas.getContext(names[i], options);
Expand Down Expand Up @@ -219,7 +221,6 @@ pc.extend(pc, function () {

// Retrieve the WebGL context
if (canvas) {
options.stencil = true;
this.gl = _createContext(canvas, options);
}

Expand Down Expand Up @@ -1744,8 +1745,7 @@ pc.extend(pc, function () {
/**
* @function
* @name pc.GraphicsDevice#setStencilOperation
* @description Configures how stencil buffer values should be modified based on the result of depth/stencil tests.
* @description Works for both front and back faces.
* @description Configures how stencil buffer values should be modified based on the result of depth/stencil tests. Works for both front and back faces.
* @param {Number} fail Action to take if stencil test is failed
* @param {Number} zfail Action to take if depth test is failed
* @param {Number} zpass Action to take if both depth and stencil test are passed
Expand Down

0 comments on commit c048df5

Please sign in to comment.