//EditPlus命令: ctrl+shift+D,ctrl+M //version 【 2012年3月14日 10:55】 //------------js3d.js封装--------------------------------------------------------------------------- <script src="js3d.js"></script> <body onLoad="initAjax3d()"> <div class="content"> <embed WIDTH=100% HEIGHT=100% NAME="PowerSpotLight" SRC="3dsence.wrl" TYPE="model/vrml"> </div> </body> parent.main3d.setv('sbqview','gotoView','bq1') var PowerS = document.PowerSpotLight.getBrowser(); var browser = PowerS.getExecutionContext(); var node = browser.getNode(n); var scene = browser.createHRMSceneFromString(str); var node = browser.getNode(n, "DEFNode"); // 取节点 browser.deleteNode(node); function setv(n,f,v) { var node = browser.getNode(n); var ff = node.getField(f); ff.value =ft.value; } //获取节点参数 function getv(n,f) { var node = browser.getNode(n); // 取节点 var ff = node.getField(f); return ff.value|| ff.valueString||ff.get1Value(0); } //添加新的VRML对象 var vrml ="Shape { geometry Cylinder {height 10}}" RTaddscene("abc","Viewpoint {position 10 10 10 description \"10\"}"+vrml) //------------Browser对象方法--------------------------------------------------------------------------- Browser.addRoute(/*fromNode, fromEventOut, toNode, toEventIn*/); Browser.createVrmlFromString(/*vrmlSyntax*/); Browser.createVrmlFromURL(/*url, node, event*/); Browser.deleteRoute(/*fromNode, fromEventOut, toNode, toEventIn*/); Browser.getCurrentFrameRate(); Browser.getCurrentSpeed(); Browser.getName(); Browser.getVersion(); Browser.getWorldURL(); Browser.loadURL(/*url, parameter*/); Browser.replaceWorld(/*nodes*/); Browser.setDescription(/*description*/); //------------------------------基本语法 ------------------------------------------------------------- Transform { scale 20 20 20 #//缩放比例 rotation 1 1 1 -1 #//旋转 translation 100 0 300 #//空间位置 children [ Shape { appearance Appearance {material Material { diffuseColor 1 0 0}} #// 要点 geometry Box {} #// 要点 } ] } //--拖拽 = 转圈 效果 #VRML V2.0 utf8 DEF ss SphereSensor { #// 要点 enabled TRUE offset 0.0 1.0 0.0 0.0 autoOffset TRUE } Transform { children [ DEF tr Transform { children Shape {geometry Box {}} } ] } ROUTE ss.rotation_changed TO tr.set_rotation #// 要点 指向某一个Transform //拖拽 = 移动 Group { children [ DEF ps PlaneSensor { #// 要点 enabled TRUE offset 0.0 0.0 0.0 autoOffset TRUE minPosition 0 0 maxPosition -1 -1 } DEF ss SphereSensor { enabled TRUE offset 0.0 1.0 0.0 0.0 autoOffset TRUE } DEF tr Transform { children Shape {geometry Sphere {}} } ] } ROUTE ps.translation_changed TO tr.set_translation #// 要点 //不能移动目标 Transform { children [ DEF ts TouchSensor {enabled TRUE} #// 要点 Shape {geometry Box {}} ] } //点击图形 让图形动起来 #//造型 DEF tr Transform { children[Shape {geometry Box {} } ] } #//位置插补器 PositionInterpolator DEF path PositionInterpolator { #// 位置插补器 key [0 .25 .5 .75 1] keyValue [ 0 0 0, 3 0 0, 3 3 0, 0 3 0, 0 0 0] } #//传感器 DEF time TimeSensor {cycleInterval 5 } #//时间传感器 DEF touch TouchSensor {} #//接触传感器 //-------------------END #//路由 ROUTE touch.touchTime TO time.startTime #// 要点 ROUTE time.fraction_changed TO path.set_fraction #// 要点 ROUTE path.value_changed TO tr.translation #// 要点 //位置插入器 PositionInterpolator DEF tr Transform { children [ Shape {geometry Box {}} DEF ci CoordinateInterpolator { # // AAA key [ 0 0.5 1 ] keyValue [ 0 0 0, 0 3 0, 0 0 0 ] } DEF ts TimeSensor { cycleInterval 2 loop TRUE #//default is false } ] } ROUTE ts.fraction_changed TO ci.set_fraction #//AAA ROUTE ci.value_changed TO tr.set_translation #//AAA //-------------------END //use的使用 NavigationInfo { avatarSize [0.25, 1.6, 0.75] headlight TRUE speed 1.0 type "WALK" visibilityLimit 0.0 } Viewpoint { position 0 1.6 10} Shape { appearance DEF ap Appearance { material Material {}} geometry Box { size 20 0.01 20} } Shape { appearance USE ap #// use 已经存在的颜色 geometry Box { size 5 0.5 1} } Transform { translation 0 0.5 -0.5 children Shape { appearance USE ap geometry Box { size 5 0.5 1} } } #//公示牌 始终面对用户 Billboard { #//重点 children [ Shape { geometry Text {string "ABC"} } ] } #//侧面面对用户 rotation 0 1 0 1.75 #//背景 Background { # groundColor0 .6 0 skyAngle[ .785, 1.57 ] skyColor .3 .4 .5 backUrl"tex/ocean_3_back.jpg" frontUrl"tex/ocean_3_front.jpg" leftUrl"tex/ocean_3_left.jpg" rightUrl"tex/ocean_3_right.jpg" topUrl"tex/ocean_3_top.jpg" bottomUrl"tex/ocean_3_bottom.jpg" } #//咋类 NavigationInfo { type "EXAMINE"} Viewpoint {description "z 10" position 0 0 10 orientation 1 0 0 0} DirectionalLight { intensity 1.0 #//强度 color 1 0 0 } PointLight { radius 10 color 1 0 0 location 0 5 -2 } WorldInfo { title "my wordinfo title" info ["a","b"] } Fog { color 0.2 0.8 0.2 fogType "LINEAR" #//类型:LINEAR EXPONENTAL } #//用盒子限制用户浏览范围 (3000 * 3000 * 8000)(井偏左右各1000米,再预留1000空间 预留上下左右各自1000米的空间) #//原理:使用模型边界 和 透明效果 实现。 #//把transparency 0 改成transparency 1 就可以 Transform { translation 0 -500 0 # (-5000/2 + 2000) children [ #//上下包裹 Transform { translation 0 4000 0 children [Shape {geometry Box {size 3000 .1 3000} appearance Appearance {material Material {diffuseColor 1 0 0 transparency 0} }}] } Transform { translation 0 -4000 0 children [Shape {geometry Box {size 3000 .1 3000} appearance Appearance {material Material {diffuseColor 1 1 0 transparency 0}}}] } #//左右包裹 Transform { translation 1500 0 0 children [Shape {geometry Box {size .1 8000 3000} appearance Appearance {material Material {diffuseColor 1 0 1 transparency 0}}}] } Transform { translation -1500 0 0 children [Shape {geometry Box {size .1 8000 3000} appearance Appearance {material Material {diffuseColor 1 0 0 transparency 0} }}] } #//前后包裹 Transform { translation 0 0 1500 children [Shape {geometry Box {size 3000 8000 .1} appearance Appearance {material Material {diffuseColor 0 0 0 transparency 0} }}] } Transform { translation 0 0 -1500 children [Shape {geometry Box {size 3000 8000 .1} appearance Appearance {material Material {diffuseColor 1 1 0 transparency 0} }}] } ] } //-------------------END #//控制用户操作的速度。 #//移动速度变为十分之一,然后提供跟多视点供用户选择 NavigationInfo { speed 0.1 #//重点 } #//控制台打印信息 Browser.print('hello word:'); //-------------------END #//定义传感器 DEF lightSwitch Group { children [ Shape { geometry Box {} } DEF touchSensor TouchSensor {} #//为当前Group定义点击传感器(可以捕捉用户的点击操作) ] } DEF lightSwitch Group { children [ Shape { geometry Box {size 4 4 4 } } Shape { geometry Cylinder {height 40} } DEF ts TouchSensor {} #//为当前Group定义点击传感器(可以捕捉用户的点击操作) ] } //-------------------END #//传感器和路由的使用(按住后自动切换视角) DEF view_zero Viewpoint { position 5 0 20 description "0" } DEF view_qf Viewpoint { position 5 50 200 #orientation 0 1 0 -0.75 description "正前方" } #//用户按住传感器 切换视点 #ROUTE ts.isActive TO view_qf.set_bind //-------------------END #//Script节点语法 DEF MyScript Script{ directOutput TRUE mustEvaluate TRUE field MFNode tr USE tr #//定义了方法可以操控的节点 #eventIn SFBool changePos #//定义了方法的参数类型:Boolean类型 (只能传递一个参数??) eventIn SFFloat changePos #//定义了方法的参数类型:Float类型 (只能传递一个参数??) url "javascript: //这个Javascript必须有 -------------------------------------- function changePos(x,t){ Browser.print('changePos x = '+x+',,t = '+t); #//控制台打印 } " } #//锚点的使用 Anchor{ url"javascript:test('xudajian');" #//直接调用JS方法 #//指定方法所在的Iframe (默认为_self) 或者 Browser.loadURL('javascript:externalFunction()', param); #//parameter["target=_self"] children[ Shape {geometry Box{}}] } //-------------------END #//定义Script节点 DEF MyScript Script { #//定义Script节点的域(field) field SFNode cyj1 USE cyj1 #//引用已经存在对象作为属性的写法 #directOutput TRUE #//似乎True也没有什么区别 #mustEvaluate TRUE #eventIn SFBool start #//定义当前SCript节点的入口(eventIn) #//事件入口和事件的处理方法必须同名 eventIn SFBool pause #//暂停方法 eventIn SFBool resume #//重新启动 eventIn SFTime speed #//更改速度 url "javascript: function initialize() { //start(true,t); #//可应用于初始化 } function pause (v,t) { if (v){ cyj1.pauseTime=t; } } function resume (v,t) { if (v){ cyj1.resumeTime=t; ; } } function speed (v,t) { Browser.print('速度speed:'+v); if (v>=1 && v<=20) { cyj1.enabled=false; cyj1.cycleInterval =v; cyj1.enabled=true; Browser.print('cyj1.cycleInterval:'+cyj1.cycleInterval); } } "#//URL END } //-------------------END---------------------------------- //-----------------------------------自定义VRML节点----------------- #VRML V2.0 utf8 #//TODO 自定义节点 (MyBox 是节点名称) PROTO MyNode[ #//TODO 自定义用户设置参数 (必须设置默认值) #//TODO 注意,尽量使用Float,少用int field SFFloat p_node_size 0 field SFRotation p_node_rota 0 0 0 0 field SFVec3f p_node_trans 0 0 0 ]{ #//TODO 节点内容 or body DEF rootForm Transform {} Script { #//TODO 脚本变量 (用来引用用户传递参数) #//TODO 使用USE 和 IS进行参数引用 #//TODO USE 表示对象引用(引用节点对象),IS表示赋值操作(获取用户设置的参数) field SFNode root USE rootForm field SFFloat node_size IS p_node_size field SFRotation node_rote IS p_node_rota field SFVec3f node_trans IS p_node_trans url "javascript: /** * 通过字符串创建Node节点并添加到Root 的children中 * @param str 构成节点的字符串(不允许有双引号\"出现) * @return 一个TransForm的字符串 */ function add(str){ root.addChildren = Browser.createVrmlFromString(str); } //TODO 初始化方法(自动执行) function initialize(){ //Browser.print('box_size = '+box_size+',str ='+str); add('Shape {geometry Cone {height 3}}'); add(makeATransform(node_trans,node_rote,'Shape {geometry Box {size '+node_size+' 2 2 }}')); } /** * TODO 创建一个TransForm * @param pos 空间位置字符串形如 '1 1 1' * @param rot 旋转值字符串形如 '1 1 1 0.75' * @param child 子节点字符串 'Shape {geometry Box {size '+box_size+' 2 2 }}' * @return 一个TransForm的字符串 */ function makeATransform (pos,rot,child) { var str = 'Transform{'; str += 'translation '+pos+' '; str += 'rotation '+rot+' '; str += 'children ['+child+']}'; Browser.print('makeATransform = '+str); return (str); } " #//url END } } MyNode{ p_node_size 2 p_node_rota 1 0 0 1 #//TODO x,y,z不能全部为0,否则不显示 p_node_trans -1 -1 1 } //-----------------------------------END---------------------------- #//Script节点语法 DEF MyScript Script{ directOutput TRUE mustEvaluate TRUE #//TODO (注意必须是【SFNode】节点) field SFNode aMyBox USE MyBox #//定义了方法可以操控的节点(注意必须是【SFNode】节点) eventIn SFBool changePos #//定义了方法的参数类型:Float类型 (只能传递一个参数??) url "javascript: //这个Javascript必须有 -------------------------------------- function changePos(x,t){ Browser.print('changePos x = '+x+',t = '+t); //控制台打印 Browser.print(aMyBox.getField('size'));// = new MFVec3f(10,2000,10); aMyBox.size = new SFVec3f(10,2000,10);//TODO 类型一定要和属性的类型对上(使用new的方法实现。) //var context = Browser.currentScene; //【vrmlscript下获取上下文】 //var msnode = context.getNode('test_material'); //msnode.diffuseColor =new SFColor(0,1,0); //Browser.print(msnode.diffuseColor); } " } //js调用: setv('MyScript','changePos',true); //---------------待整理(参考使用)---------------------------------------------------- DEF seditbqmov Script { directOutput TRUE mustEvaluate TRUE eventIn SFInt32 bsweditmovgasID eventIn SFTime tick url "javascript: function bsweditmovgasID (v,t) { var context = Browser.currentScene; var bqgas1 = context.getNode('bqgas1'); var bqgas2 = context.getNode('bqgas2'); bqgas1.state=7; bqgas2.state=7; var timegas = context.getNode('timegas'); timegas.enabled=true; } function n2color (v){ return color=new SFColor(0, 1-v*2, 0); } function tick (t){ var context = Browser.currentScene; var bqgas1 = context.getNode('bqgas1'); var bqgas2 = context.getNode('bqgas2'); bqgas1.text[1]='温度'+(Math.random()*50+20); bqgas2.text[1]='温度'+(Math.random()*1+0.7);; bqgas1.fgColor=n2color(Math.random()*0.5+0.1); bqgas2.fgColor=n2color(Math.random()*0.2+0.7); } " } var msnode = Browser.pickedNode; Browser.print('name:' + msnode.name + ' Type:'+msnode.getType()); var mspt =Browser.mouseHitPoint; Browser.print('点击的坐标:'); Browser.print(mspt[0]+','+mspt[1]+','+mspt[2]); if (msnode.parent.getType()=='Transform') var nameh=msnode.parent.name.substring(0,2); if (msnode.appearance.texture.getType()=='MultiTexture') var vppos=Browser.viewerPosition; bqjs.position=mspt.add((vppos.subtract(zx)).multiply(0.001)); var nowtime = new SFTime(); var nowyea=nowtime.getYear(); var nowmon=nowtime.getMonth(); var nowday=nowtime.getDay(); var nowh=nowtime.getHour(); var nowm=nowtime.getMinute(); var nows=nowtime.getSecond(); DEF gd Inline {url "gd.wrl"}