Skip to content

Instantly share code, notes, and snippets.

@Hoikohroh
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hoikohroh/46a3e969fe355a225e52 to your computer and use it in GitHub Desktop.
Save Hoikohroh/46a3e969fe355a225e52 to your computer and use it in GitHub Desktop.
Adobe AfterEffects Script (JSX):Z fake depth buffer
/*
Adobe AfterEffects Script (JSX)
fake_depth
v20150719
*/
////////////////////////////////variable declaration////////////////////////////////
var thisComp= app.project.activeItem;
var selectedLayers= new Array();
var thisLayer = null;
var tmpN,Zpos;
var frontNul,BackNul,thisFill,compW,compH;
var exp = "thisZ = transform.position[2];" + "\n" +
"frontZ = thisComp.layer(\"Depth_Null_Front\").transform.position[2];" + "\n" +
"backZ = thisComp.layer(\"Depth_Null_Back\").transform.position[2];" + "\n" +
"if (thisZ < frontZ){temp = 0}else if(thisZ > backZ){temp = 1}else{temp = (thisZ - frontZ) / (backZ - frontZ)};" + "\n" +
"[temp, temp, temp,1]";
////////////////////////////////body////////////////////////////////
if(thisComp instanceof CompItem){
selectedLayers = thisComp.selectedLayers;
if(selectedLayers.length > 0){
Zpos = compareZ(selectedLayers);
// 深度クリッピング用nullを作成
if (typeof(Zpos[0]) == "number" && typeof(Zpos[0]) == "number"){
app.beginUndoGroup("add_Depth_Null");
frontNul = create3dNull(thisComp,Zpos[0],"Depth_Null_Front");
backNul = create3dNull(thisComp,Zpos[1],"Depth_Null_Back");
app.endUndoGroup();
// レイヤーに塗りエフェクト、エクスプレッション追加
app.beginUndoGroup("add_explession");
for (var i = 0; i < selectedLayers.length; i++) {
thisFill = selectedLayers[i].property("ADBE Effect Parade").addProperty("ADBE Fill");
thisFill.property("ADBE Fill-0002").expression = exp;
};
app.endUndoGroup();
};
} else {
alert('unselected layer');
};
} else {
alert('unselected compositon');
};
////////////////////////////////functions////////////////////////////////
// Zを比較
function compareZ(layers){
var max = null;
var min = null;
for (var i = 0; i < layers.length; i++) {
if (! layers[i].threeDLayer){}else {
tmpN = layers[i].position.value[2];
if (typeof(max) == "number") {max = Math.max(max, tmpN)} else {max = tmpN};
if (typeof(min) == "number") {min = Math.min(min, tmpN)} else {min = tmpN};
};
};
return [min,max];
};
//3Dnullを作成
function create3dNull (comp,value,string){
var tempNull = comp.layers.addNull();
tempNull.threeDLayer = true;
tempNull.position.setValue([(comp.width/2),(comp.height/2),value]);
tempNull.name = string;
return tempNull;
};
/*エクスプレッション内容
thisZ = transform.position[2];
frontZ = thisComp.layer("Depth_Null_Front").transform.position[2];
backZ = thisComp.layer("Depth_Null_Back").transform.position[2];
if (thisZ < frontZ){
temp = 0;
}else if (thisZ > backZ){
temp = 1;
}else{
temp = (thisZ - frontZ) / (backZ - frontZ)
};
[temp, temp, temp,1]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment