VRML(Virtual Reality Modeling Language)是描述三維場景的標準語言。結合 PHP 動態生成 VRML 內容,可依使用者互動、資料庫資料即時產生 3D 場景,類似 Generator 動態產生 Flash 的概念。本文提供完整可執行範例,展示基本節點定義、Transform、Shape、Appearance、Material、Sensor、Script 與 ROUTE 事件連結。
須將 .wrl 副檔名對應至 PHP 解析器,或直接儲存為 .php 並送出正確 MIME:
header("Content-type: application/x-vrml");
瀏覽器需安裝 VRML 外掛(如 Cortona、Cosmo Player)方可檢視。
<?php
header("Content-type: application/x-vrml");
$txt = '#VRML V2.0 utf8
DEF leftBox Transform {
translation -5 0 0
children [
Shape {
appearance Appearance {
material Material { diffuseColor 1 0 0 }
}
geometry Box {}
}
DEF SphereChild Shape {
appearance Appearance {
material Material { diffuseColor 1 0 1 }
}
geometry Sphere { radius 1.2 }
}
]
}
DEF rightBox Transform {
translation 5 0 0
children [
Shape {
appearance Appearance {
material Material { diffuseColor 0 0 1 }
}
geometry Box {}
}
]
}
DEF onoff Transform {
translation 0 -1 0
children [
Shape {
appearance Appearance {
material Material { diffuseColor 0 1 0 }
}
geometry Box {}
}
DEF TS TouchSensor {}
]
}
DEF S Script {
eventIn SFBool isActive
eventOut MFNode child
field MFNode testNode USE SphereChild
url "javascript:
function isActive(value) {
if (value) {
child = testNode;
}
}
"
}
ROUTE TS.isActive TO S.isActive
ROUTE S.child TO leftBox.removeChildren
ROUTE S.child TO rightBox.addChildren';
echo $txt;
?>
DEF SphereChild 供後續引用)。TouchSensor(滑鼠點擊感測器)。isActive 事件,若為 true,輸出 SphereChild 節點。TS.isActive 觸發 → S.isActive 執行腳本。SphereChild → 從 leftBox 移除球體,加入 rightBox。效果:使用者點擊綠色開關,紅色區域的球體會「移動」到藍色區域。
url 欄位,注意雙引號跳脫。此範例展示 PHP 產生動態 3D 內容的基礎架構,開啟伺服器端虛擬實境應用的想像空間。