![media9:切换 3D 场景的元素](https://linux22.com/image/312891/media9%EF%BC%9A%E5%88%87%E6%8D%A2%203D%20%E5%9C%BA%E6%99%AF%E7%9A%84%E5%85%83%E7%B4%A0.png)
我成功创建了一个 3D PRC 文件,并使用 LaTeX 和软件包将其嵌入到 PDF 中media9
。现在我需要创建按钮以允许用户打开/关闭场景的某些元素。根据手册,media9
我猜我可以使用 和 来做到这一点\mediabutton
。jsaction
但我不懂 Javascript 命令,不知道如何使用jsaction
。希望有人能给我举一个媒体按钮命令的例子来切换 3D 场景的一个或多个元素?
答案1
这需要一些 JavaScript。
epix.prc
下面的示例使用包中的PRC 文件media9
。初始化脚本init3D.js
定义了方法
toggleNodeByID(<id>)
<id>
以递归方式切换由其(整数)标识的节点的所有子节点的可见性。
为了确定节点<id>
,提供了辅助脚本getID.js
。单击 A-Reader 的“模型树”导航窗格中相关部件。然后将 ID 打印到控制台中。完成文档后,行
add3Djscript=getID.js,
可以被移除。
\documentclass{article}
\usepackage{media9}
\usepackage{filecontents}
%%%%%%% 3D initialisation script %%%%%%%%%%%%%%%%%%%
% Defines method toggleNodeByID(<ID>) for toggling %
% node visibility. %
% <ID> to be determined by using the auxiliary %
% script `getID.js' listed below. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{filecontents*}{init3D.js}
toggleNodeByID = function (i) {
if(scene.nodes.getByIndex(i).firstChild)
toggleSubNode(scene.nodes.getByIndex(i).firstChild);
else
scene.nodes.getByIndex(i).visible=!scene.nodes.getByIndex(i).visible;
return;
}
toggleSubNode = function (nd) {
if(nd.nextSibling) toggleSubNode(nd.nextSibling);
if(nd.firstChild) toggleSubNode(nd.firstChild);
else nd.visible=!nd.visible;
}
\end{filecontents*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% 3D-JS for getting node ID %%%%%%%%%%%%%%%%%%
% Only needed during document authoring. %
% In the Model Tree, click the part to get its ID. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{filecontents*}{getID.js}
selEventHandler=new SelectionEventHandler();
selEventHandler.onEvent=function(e){
if(e.selected){
for(var i=0; i<scene.nodes.count; i++) {
if (scene.nodes.getByIndex(i)==e.node) {
host.console.show();
host.console.println("Node ID: "+i);
}
}
}
}
runtime.addEventHandler(selEventHandler);
\end{filecontents*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\includemedia[
label=example3D,
width=0.8\linewidth,
add3Djscript=init3D.js,
add3Djscript=getID.js, %remove this after finalizing the document
add3Djscript=asylabels.js, %upright text labels
activate=pageopen,
3Dmenu,
3Dc2c=4 2 3,
3Dcoo=4.4 2.2 0,
3Droo=430,
width=0.6\linewidth, height=0.6\linewidth
]{}{epix.prc}
\mediabutton[
jsaction=example3D:{
annotRM['example3D'].context3D.toggleNodeByID(2);
}
]{\fbox{\strut surface}}
\mediabutton[
jsaction=example3D:{
annotRM['example3D'].context3D.toggleNodeByID(71);
annotRM['example3D'].context3D.toggleNodeByID(105);
}
]{\fbox{\strut x axis}}
\mediabutton[
jsaction=example3D:{
annotRM['example3D'].context3D.toggleNodeByID(3);
annotRM['example3D'].context3D.toggleNodeByID(37);
}
]{\fbox{\strut y axis}}
\mediabutton[
jsaction=example3D:{
annotRM['example3D'].context3D.toggleNodeByID(143);
annotRM['example3D'].context3D.toggleNodeByID(177);
}
]{\fbox{\strut z axis}}
\mediabutton[
jsaction=example3D:{
annotRM['example3D'].context3D.toggleNodeByID(215);
}
]{\fbox{\strut function label}}
\end{document}