ãã®æè¡ã¯æ¯èŒçè¥ãããã䜿çšããããã®ããã¥ã¢ã«ã¯ããã»ã©å€ããããŸããã 圌女ãšã®ä»äºã«ã€ããŠã¯ãã¡ããã芧ãã ãã ïŒ ãã¡ã -ãã·ã¢èªãžã®ç¿»èš³ïŒã ããã§ã¯ãWebGLã®åºæ¬ã«ã€ããŠåŠã¶ããšãã§ããŸãã
WebGLã§ã®äœæ¥ã容æã«ããããã«ãå€ãã®ã©ã€ãã©ãªãéçºãããŠããŸãïŒãã ãããããã®ã»ãšãã©ã¯ãŸã ããªãç²éã§ãïŒã ãã®ãã¡ã®1ã€ã§ããwebGLUãé©çšããŠãåäžã®æäžé»ç¯ã§ç §ããããã·ã³ãã«ãªã·ãŒã³ã圢æããŸãã ããã§äŸãèŠãããšãã§ããããããå®å šãªã¢ãŒã«ã€ããããŠã³ããŒãããŠããã·ã³ã§å®è¡ã§ããŸãã
ãŸããOpenGLãšWebGLã®éããå°ãæãåºããŠãã ããã WebGLã¯GLESã«åºã¥ããŠããããããã®ãã¯ãããžãŒã¯OpenGLãããå€§å¹ ã«å£ã£ãŠããŸããå€ãã®äŸ¿å©ãªæ¡åŒµæ©èœïŒARBãªã©ïŒããªããçµã¿èŸŒã¿ã®ã©ã€ãã£ã³ã°ãµããŒãããããŸãããGL_QUADSã§ãããµããŒããããŠããŸããããµãŒãããŒãã£ã®ãã©ã°ã€ã³ãªãã§Webã«3Dãå®è£ ã§ãã人ã¯ããŸããã
ã·ã§ãŒããŒã¯ãé ç¹ã®äœçœ®ãšæçµç»åã®è²ãèšç®ããããã«äœ¿çšãããŸãã 3次å ã·ãŒã³ã®æãåçŽãªæ§ç¯ã§ã¯ãã·ã§ãŒããŒã¯é¢é£ä»ããããŠããåé ç¹ã«å¯ŸããŠåŒã³åºãããŸãã ããããããè€éãªã·ãŒã³ããã®æ¹æ³ã§èª¬æããããšã¯ã§ããŸããã ãã®å Žåã圌ãã¯æçµã·ãŒã³ã圢æãããã¯ã¹ãã£ã圢æããã·ã§ãŒããŒãæžãããšã«é ŒããŸãïŒäŸã¯ããã§èŠãããšãã§ããŸã ãå€å次ã®èšäºã§3次å ã·ãŒã³ãèšè¿°ãããã®æ¹æ³ã«è§ŠããŸãã
WebGLUã©ã€ãã©ãªã«ã¯ãããããªããã¥ã¡ã³ããšããã€ãã®äŸãå«ãŸããŠããŸãã ããã¯ãã©ã€ãã©ãªãå®å šã«æäœããã«ã¯äžååã§ããããããœãŒã¹ã³ãŒããèªãå¿ èŠããããŸãã ããã«ãã©ã€ãã©ãªã¯éåžžã«ç²éãªã®ã§ãã³ãŒããæãäžããå¿ èŠãããå ŽåããããŸãã
ãããã£ãŠãWebGLUã§ã®äœæ¥ãéå§ããã«ã¯ãwebglu.jsã¹ã¯ãªãããæ¥ç¶ããŠåæåããå¿ èŠããããŸãã
<script type="text/javascript" src="./src/webglu.js"></script> ⊠$W.initialize();
$ Wã¯ãWebGLUåå空éã®ã¡ã€ã³ãªããžã§ã¯ãã§ãã ã»ãšãã©ã®ä»äºã¯åœŒãšãããŸãã ãã®ãªããžã§ã¯ãã«å ããŠããªããžã§ã¯ã$ GïŒGameGLUåå空éïŒããããããŠã¹ãšããŒããŒãã䜿çšããŠã·ãŒã³ãå¶åŸ¡ããäœæ¥ãç°¡çŽ åããŸãã WebGLUã«ã¯ãããã€ãã®å®éšçãªæ©èœããããŸãïŒããšãã°ãCrazyGLU-éžæããç䌌ãããã¡ãŒãšç©çåŠãæäœããããïŒã ãã¹ãŠã®ãœãŒã¹ãã¡ã€ã«ã¯ã察å¿ããé¢æ°ã®èµ·åæã«WebGLUã«ãã£ãŠããŒãããããã
useControlProfiles();
é¢æ°ãªã©
useControlProfiles();
ãã¡ã€ã«ãããŒãããŸãïŒãwebglu.js以å€ã®ã¹ã¯ãªãããæåã§ããŒãããå¿ èŠã¯ãããŸããã
WebGLUãåæåããåŸãã·ãŒã³ãªããžã§ã¯ãã®äœæãéå§ã§ããŸãã ãªããžã§ã¯ããäœæããããã«ãWebGLUã¯ã€ã³ã¿ãŒãã§ã€ã¹
$W.Object(type, flags)
æäŸããŸããããã§ãtypeã¯ãªããžã§ã¯ãã®ã¿ã€ãã§ãïŒOpenGLãšåæ§ïŒã
- $ W.GL.POINTS-åé ç¹ã¯ç¹ã§è¡šç€ºããã
- $ W.GL.LINES-é ç¹2n-1ãš2nã®åãã¢ã¯ç·ã§æ¥ç¶ããã
- $ W.GL.LINE_LOOP-ãã¹ãŠã®é ç¹ã¯ãæåã®é ç¹ãéããŠããã»ã°ã¡ã³ãã«ãã£ãŠé çªã«æ¥ç¶ãããŸãã
- $ W.GL.LINE_STRIP-ãã¹ãŠã®é ç¹ã¯ãéããã«ã»ã°ã¡ã³ãã«ãã£ãŠæ¥ç¶ãããŸãã
- $ W.GL.TRIANGLES-é ç¹ã®åããªãã«ïŒé çªã«ïŒäžè§åœ¢ã圢æãã
- $ W.GL.TRIANGLE_STRIP-é ç¹ã¯äžè§åœ¢ã§é çªã«æ¥ç¶ããã
- $ W.GL.TRIANGLE_FAN-é ç¹ã¯ãå ±é-æå-é ç¹ã®åšãã®äžè§åœ¢ã§æ¥ç¶ãããŠããŸãã
$W.RENDERABLE | $W.PICKABLE
ãªãã·ã§ã³ã®ãã©ã°ïŒããã©ã«ãã§ã¯ã
$W.RENDERABLE | $W.PICKABLE
ãã ãã
$W.RENDERABLE | $W.PICKABLE
ã¯ãåã§ãããªããžã§ã¯ããæç»ããå Žåã
$W.PICKABLE
ãæ瀺çã«
$W.PICKABLE
å¿ èŠããã
$W.PICKABLE
ã æãåçŽãªã±ãŒã¹ã§ã¯ããªããžã§ã¯ãã®åé ç¹ã®è²ãæå®ããå¿ èŠããããŸãããç¬èªã®ã·ã§ãŒããŒãèªç±ã«äœæããŠïŒæ¬¡ã«è¡ããŸãïŒããªããžã§ã¯ãå šäœã«å ±éã®è²ãæå®ã§ããŸãã
ãããã£ãŠãããšãã°ãè²åº§æšè»žãäœæããã«ã¯ã次ã®ããã«ããŸãã
var originLines = new $W.Object($W.GL.LINES); originLines.vertexCount = 6; originLines.fillArray("vertex", [[0,0,0], [3,0,0], [0,0,0], [0,3,0], [0,0,0], [0,0,3]]); with ($W.constants.colors){ originLines.fillArray("color", [ RED, RED, GREEN, GREEN, BLUE, BLUE]); }
ã·ã§ãŒããŒã¯ããã©ã«ãã§æ¬¡ã®é åã䜿çšããŠããªããžã§ã¯ãã®åé ç¹ãç¹åŸŽä»ããŸãã
- ãé ç¹ã-é ç¹ã®åº§æšã
- ãè²ã-é ç¹ã®è²ã
- ãæšæºã-é ç¹ã®æ³ç·ã
- ãTexCoordã-ãã®é ç¹ã®ãã¯ã¹ãã£åº§æšã
- "Wglu_elements"-é ç¹åº§æšã€ã³ããã¯ã¹ïŒè€éãªãªããžã§ã¯ãã®å ŽåïŒã
åæ§ã«ïŒã€ã³ããã¯ã¹ãé€ãïŒãã·ã§ãŒããŒã®å¯Ÿå¿ããå€æ°ã«ååãä»ããããŸãïŒã·ã§ãŒããŒã®åã ã®é ç¹ãç¹åŸŽä»ããå€æ°ã«ã¯å±æ§
attribute
ãããããšãæãåºã
attribute
ïŒã
ã·ã§ãŒããŒãšWebGLã®æ¥ç¶ã¯ã
Material
ã¡ãœããã䜿çšããŠå®è£ ãããŸãã ãã®ã¡ãœããã®å¯äžã®åŒæ°ã¯ãJSONã·ã§ãŒããŒèšè¿°ãã¡ã€ã«ãžã®ãã¹ã§ãã ããšãã°ãç §æã·ã§ãŒããŒã¯æ¬¡ã®ããã«æ¥ç¶ãããŠããŸãã
var lights = new $W.Material({path:$W.paths.materials + "light.json"});
light.jsonãã¡ã€ã«èªäœã¯æ¬¡ã®ããã«ãªããŸãã
{ name: "light", program: { name: "light", shaders: [ {name:"light_vs", path:$W.paths.shaders+"light.vert"}, {name:"light_fs", path:$W.paths.shaders+"light.frag"} ] } }
ããã§ãååã¯ãææãã®äžè¬åã§ãã ããã°ã©ã âåå-æããã«ããã°ã©ã ã®ååãç¹åŸŽä»ããïŒãããããWebGLã®äœæè ã¯ã1ã€ã®ãããªã¢ã«ã«è€æ°ã®ããã°ã©ã ã䜿çšã§ãããšç€ºåãããããã®ãã©ã¡ãŒã¿ãŒã¯ç¹å¥ãªåœ¹å²ãæãããªãïŒã ã·ã§ãŒããŒ-ã·ã§ãŒããŒãžã®ãã¹ã瀺ãã·ã§ãŒããŒã䜿çšã
åãªããžã§ã¯ããŸãã¯ã·ã¹ãã å šäœïŒå ±éå±æ§ãæã€ïŒã«å ±éã®å€æ°ã¯ã
setUniformAction(n, f)
ãªããžã§ã¯ãã®
setUniformAction(n, f)
ã¡ãœããã䜿çšããŠã察å¿ããJavaScriptå€æ°ã«é¢é£ä»ããããŸãã ãã®ã¡ãœããã®åŒæ°ã®æå³ã¯æ¬¡ã®ãšããã§ããn-ã·ã§ãŒããŒå ã®å€æ°ã®ååïŒã¡ãœããã§ã¯æååãšããŠç€ºãããŸãïŒã fã¯ãé¢æ°
function(u, o, m)
é¢æ°ã§ããããã§ãuã¯uniformïŒïŒãªããžã§ã¯ããoã¯ãªããžã§ã¯ãèªäœãmã¯ãããªã¢ã«ã§ãã ããšãã°ããcolorããã©ã¡ãŒã¿ãŒã®ãªããžã§ã¯ãã®è²ãžã®ãã€ã³ãã¯ã次ã®ããã«å®è¡ãããŸãã
lights.setUniformAction('color', function(uniform, object, material){ $W.GL.uniform4fv(uniform.location, object.color); });
éæšæºã®ãããªã¢ã«ããªããžã§ã¯ãã«èšå®ããã«ã¯ã
setMaterial(mat)
ãªããžã§ã¯ãã®ããããã£ã
setMaterial(mat)
ãããã§ãmatã¯å¿ èŠãªãããªã¢ã«ã§ãã JavaScriptã䜿çšãããšãå®çŸ©æžã¿ã®ãªããžã§ã¯ãã«ãã®å Žã§ããããã£ãè¿œå ã§ããããããªããžã§ã¯ããç°¡åã«å€æŽããŠã·ã§ãŒããŒãšèª¿æŽã§ããŸãã
WebGLUã䜿çšããŠããã®ã·ãŒã³ãäœæããŸãããã
ããã§ã¯ããªããžã§ã¯ãã®ç¶æ¿ã䜿çšãããŸããã¡ã€ã³ã®åçŽã·ãªã³ããŒã¯ã2çªç®ã®ã·ãªã³ããŒãšäžã®åãç¶æ¿ããŸãã ããã¯ãäžã®åãšã2ã€ã®åã®éã®å¹³é¢ã«ã©ã³ãã ã«é 眮ããããµã€ãºã®ç°ãªãè€æ°è²ã®çã®ã»ãããç¶æ¿ããŸãã ã·ãŒã³å šäœã1ã€ã®æåæ§å æºïŒãªã¬ã³ãžè²ã®ãæäžé»ç¯ãïŒã§ç §ããããå°ããªãªã¬ã³ãžè²ã®çäœã§ç€ºãããŸãã
ã©ã€ãã£ã³ã°ã䜿çšããã«ã¯ããã€ã«ãããŒã¿ãã·ã§ãŒãã«ãã£ãŠåŠçããããã¹ãŠã®é ç¹ã®æ³ç·ãæ£ããèšç®ããå¿ èŠããããŸãã
genSphere(n1,n2,rad)
ã©ã€ãã©ãªã®
genSphere(n1,n2,rad)
é¢æ°ã䜿çšããŠçãæç»ã§ããŸãããã·ãªã³ããŒãèªåã§æç»ããå¿ èŠããããŸãã ãããè¡ãæãç°¡åãªæ¹æ³ã¯ãæ¥ç¶ãããäžè§åœ¢ã§åæ±ã®åŽé¢ãåããããšã§ãã
function drawCylinder(R, H, n, flags){ var v = [], norm = []; var C = new $W.Object($W.GL.TRIANGLE_STRIP, flags); C.vertexCount = n * 2+2; for(var i = -1; i < n; i++){ var a = _2PI/n*i; var cc = Math.cos(a), ss = Math.sin(a); v = v.concat([R*cc, R*ss,0.]); v = v.concat([R*cc, R*ss,H]); norm = norm.concat([-cc, -ss, 0.]); norm = norm.concat([-cc, -ss, 0.]); } C.fillArray("vertex", v); C.fillArray("normal", norm); return C; }
åŸã§èŠãããã«ããã®æ¹æ³ã¯ããªãåå§çã§ãïŒäž¡ç«¯ã®éã«åæ±ã®è¡šé¢ã«é ç¹ãé 眮ããªããšããäºå®ã«ããããã®ç §æã¯èª€ã£ãŠèšç®ãããŸãïŒãã©ã³ãããåæ±ã®è¡šé¢ã®äžå€®ã®ã¿ãç §ããããã®ç«¯ããã£ããã£ããªãå Žåãåæ±ç¹ç¯ããŠããŸããã åæ±ãæ£ããæç»ããã«ã¯ãè¿œå ã®äžéé ç¹ãè¿œå ããäžè§åœ¢ã®æ£ãã衚瀺ã®ããã«ã€ã³ããã¯ã¹ã®é åãåããå¿ èŠããããŸãã å¥ã®ãªãã·ã§ã³ã¯ããããããäžè§åœ¢ã®ã»ããã§æ§æãããããã€ãã®é·æ¹åœ¢ãããïŒåãªããžã§ã¯ããå«ãïŒãªããžã§ã¯ãè€åãæç»ããããšã§ãã
é¢æ°ã䜿çšããŠåãæããŸã
function drawCircle(R, n, w, flags){ var v = []; var C = new $W.Object($W.GL.LINE_LOOP, flags); C.vertexCount = n; for(var i = 0; i < n; i++){ var a = _2PI/n*i; v = v.concat([R*Math.cos(a), R*Math.sin(a),0.]); } C.fillArray("vertex", v); if(typeof(w) != "undefined") C.WD = w; else C.WD = 1.; C.draw = function(){ // var oldw = $W.GL.getParameter($W.GL.LINE_WIDTH); $W.GL.lineWidth(this.WD); this.drawAt( this.animatedPosition().elements, this.animatedRotation().matrix(), this.animatedScale().elements ); $W.GL.lineWidth(oldw); }; return C; }
åãæãç·ã®å€ªããå€æŽã§ããããã«ããã«ã¯ããã®ãªããžã§ã¯ãã®
draw()
é¢æ°ãåå®çŸ©ããå¿ èŠããããŸãïŒé¢æ°
$W.GL.lineWidth(w)
ã¯ããã®é¢æ°ã®æ¬¡ã®åŒã³åºããŸã§ãç·ã®å€ªããã°ããŒãã«ã«èšå®ããããã§ãïŒã ãã®ãªããžã§ã¯ãã®
$W.GL.LINE_LOOP
ã
$W.GL.LINE_LOOP
ã«å€æŽãããšãåã¯ãããã§æç»ãããŸãã ãã€ã³ãã®ãµã€ãºã¯ãããããªã¢ã«ããã€ã³ãã䜿çšãããšããäºå®ã«ããããªããžã§ã¯ãã®
WD
ããããã£ã«äŸåããŸãã
gl_PointSize = WD;
ããã§ã¯ãããŸããŸãªãµã€ãºã®ãã€ã³ãã衚瀺ããããã®ãã©ã°ã¡ã³ãã·ã§ãŒããŒã³ãŒããèŠãããšãã§ãã ããã§ã¯é ç¹ã·ã§ãŒããŒãèŠãããšãã§ããŸãã
ããã§ããªããžã§ã¯ããäœæããŸããã ç §æãèšç®ããããã«ã·ã§ãŒããŒãäœæããçªã§ããã è€æ°ã®å æºã§ç §ãããããšãã®é ç¹ã®æçµçãªè²ãèšç®ããããã®OpenGLãã³ãããã¯ã«ã¯ã次ã®åŒããããŸãã
result_Color = mat_emission + lmodel_ambient * mat_ambient Sum_i(D * S * [l_ambient * mat_ambient + max{dot(L,n),0}*l_diffuse*mat_diffuse + max{dot(s,n),0}^mat_shininess * l_specular * mat_specular )
ããã«
- result_Color-é ç¹ã®æçµè²ã
- mat_X-ææç¹æ§ã
- l_X-içªç®ã®å æºã®ããããã£
- XïŒ
- æŸå°-æŸå°å ïŒã€ãŸããææã¯å æºã§ãïŒã
- lmodel_ambient-ç §æã¢ãã«ã®åšå²å ã®åèšïŒå æºã«äŸåããªããã€ãŸãèæ¯å ã§ãïŒ
- ambient-èæ¯å ïŒå æºã®å€åŽã®ãããªã¢ã«ã®è²ãiçªç®ã®å æºã®æ¡æ£å æåïŒ
- D-å
æžè¡°ä¿æ°ãD = 1 /ïŒkc + kl * d + kq * d ^ 2ïŒã
- dã¯ããœãŒã¹ããããŒã¯ãŸã§ã®è·é¢ã§ãã
- kc-äžå®ã®æžè¡°ä¿æ°ïŒãã°ã¬ãŒãã£ã«ã¿ãŒãïŒã
- klã¯ç·åœ¢æžè¡°ä¿æ°ã
- kqã¯2次æžè¡°ä¿æ°ã§ãã
- S-次ã®ããã«èšç®ãããã¹ãããã©ã€ãå¹æïŒ
= 1ãå æºãã¹ãããã©ã€ãïŒç¡éé ã®å¹³è¡ããŒã ïŒã§ãªãå Žåã
= 0ããœãŒã¹ããµãŒãã©ã€ãã§ããããé ç¹ãæŸå°ã³ãŒã³ã®å€åŽã«ããå Žåã
= max {dotïŒvãddïŒã0} ^ GL_SPOT_EXPONENTä»ã®å Žåãããã§vã¯ã¹ãããã©ã€ãïŒGL_POSITIONïŒããé ç¹ãžã®æ£èŠåããããã¯ãã«ãddïŒGL_SPOT_DIRECTIONïŒã¯ã¹ãããã©ã€ãã®æ¹åã§ã - L = -vïŒäžãããœãŒã¹ãžã®æ£èŠåãã¯ãã«ïŒã
- nã¯é ç¹ã®æ³ç·ã
- æ¡æ£-æ¡æ£å ãå ¥å°/åå°ã®è§åºŠã«äŸåããªãã
- sã¯ãLãšé ç¹ããç®ãŸã§ã®ãã¯ãã«ã®åèšã«çããæ£èŠåããããã¯ãã«ã§ãã
- å æ²¢-å 沢床ïŒ0ãã128ãŸã§ãè¡šé¢ããå æ²¢ããããã»ã©ïŒ
- specular-ãã©ãŒã³ã³ããŒãã³ãã®è²ã
éåžžããããªã¢ã«ã®æ¡æ£è²ãšèæ¯è²ãäžèŽããã¹ãããã©ã€ãã®èæ¯è²ãäžè¬çãªèæ¯ç §æã®æ¡æ£è²ãšäžèŽããå®æ°æžè¡°ä¿æ°ãšç·åœ¢æžè¡°ä¿æ°ãçç¥ã§ãããšããäºå®ãèæ ®ãããšããã®åŒãç°¡ç¥åã§ããŸãã ãã®çµæã ãã®ãããªãã©ã°ã¡ã³ãã·ã§ãŒããŒãååŸããŸãã é ç¹è¡šç€ºã®åº§æšã®èšç®ã«å ããŠãé ç¹ã·ã§ãŒããŒã¯ãçŸåšã®ã¢ãã«åºæã®ãããªãã¯ã¹ã«åºã¥ããŠã空éå ã®é ç¹ã®äœçœ®ãšãã®æ³ç·ã®æ¹åãåèšç®ããå¿ èŠããããŸãïŒåãªããžã§ã¯ãã移åãã¹ã±ãŒãªã³ã°ãå転ã§ããããããããè¡ãå¿ èŠããããŸãïŒã
ããã§ããæäžé»ç¯ãã®ç¹æ§ãå€æããå¿ èŠããããŸãã
light = { position: [0.,2.,1.5], target: [0.,0.,-2.], color: [1.,.5,0.,1.], fieldAngle: 60., exponent: 5., distanceFalloffRatio: .02 };
setUniformAction(âŠ)
ã䜿çšããŠããã©ã³ã¿ã³ãããããã£ãšãªããžã§ã¯ãããããã£ãã·ã§ãŒããŒå€æ°
setUniformAction(âŠ)
é¢é£ä»ãããã®ããããªã¢ã«ãã䜿çšããŠåãªããžã§ã¯ãã®åã ã®ããããã£ãèšå®ããŸãã
ããããã¹ãŠè¡ã£ãåŸã
$W.start(T);
é¢æ°ã䜿çšããŠã·ãŒã³ãã¢ãã¡ãŒã·ã§ã³åããŸã
$W.start(T);
ããã§ãTã¯ã·ãŒã³ã¬ã³ããªã³ã°éã®æå°ééã§ãã ã·ãŒã³ãè€éãããå Žåã¯ãé¢æ°
$W.util.defaultUpdate();
ã䜿çšããŠãå€æŽãããã³ã«æåã§æç»ããå¿ èŠããã
$W.util.defaultUpdate();
ããã³
$W.util.defaultDraw();
ã ãããã®é¢æ°ã¯ã·ã§ãŒããŒã®ã³ã³ãã€ã«ã«ã¯åœ±é¿ããŸããïŒã·ã§ãŒããŒèªäœã«å€§å¹ ãªå€æŽãè¡ãå Žåã«ã®ã¿å®è¡ããå¿ èŠããããŸãïŒããããã£ãŠãã·ãŒã³ã¯åæèªã¿èŸŒã¿æïŒåæåäžïŒã«ã®ã¿ãããªãŒãºããããŠã£ã³ããŠã®ãµã€ãºãå€æŽããããšå°ãé ããªããŸãã
æåŸã«ãWebGLUããã®ã·ãŒã³ã®å転ïŒæ£ç¢ºã«ã¯ãã·ãŒã³ã®åšãã§ã®ã«ã¡ã©ã®ç§»åïŒæ©èœã¯ããŸã䟿å©ã§ã¯ãªããããç¬èªã®ç§»åæ©èœãå®çŸ©ããå¿ èŠããããŸãã ãã ïŒããã³æåã«ç€ºããäŸã®ã¢ãã¬ã¹ïŒã§ãæçµçãªhtmlãã¡ã€ã«ãã©ã®ããã«èŠãããã確èªã§ããŸãã
éžæãããã¡ãŒã®æäœïŒããŠã¹ã¯ãªãã¯ã§ãªããžã§ã¯ããèå¥ããå¿ èŠãããå ŽåïŒãããã·ã³ã°æã«ãªããžã§ã¯ãã衚瀺ããïŒãªããžã§ã¯ãã®è²ã®éæ床ã³ã³ããŒãã³ãã䜿çšããããã«å¿ èŠïŒãããã³ãæ»ãããã¯ãªããã³ã°ããããšã«ã€ããŠã¯èšåããªãã£ãã«ãããããããèšäºã¯éåžžã«å€§ãããªããŸããè¡šé¢åœ¢ç¶ãªã©ã ãããç§ã®WebGLã®èšäºãæåŸã§ã¯ãªãããšãé¡ã£ãŠããŸãïŒãããã¯ã誰ããç§ã®ä»äºãç¶ããã§ãããïŒã
UPDïŒéžæè¢ãæäŸããããã«ãcrazyglu.jsãã¡ã€ã«ã®getObjectIDAtããã³usePickingé¢æ°ã䜿çšããããšããŸãããããããã®è©Šã¿ã¯å€±æããŸãããã©ã€ãã©ãªãŒã¯æ¹¿ã£ãŠããŸãã ãããã£ãŠãé¢æ°ã¯ç¬ç«ããŠäœæãããŸããã
ã ãããéžæãããã¡ãå®è£ ããããã«å¿ èŠãªãã®ã
ãŸãã
$W.start(p)
é¢æ°ã䜿çšã§ããŸãããããããªããšãéžæãããã¡ãŒã«æžã蟌ããšãã«ã¿ã€ããŒã€ã¡ãŒãžã®æŽæ°ã "å®è¡"ããããªã¹ã¯ããããŸãã ãããã£ãŠãã·ãŒã³ãå€æŽãããã³ã«ã·ãŒã³ãã¬ã³ããªã³ã°ããå¿ èŠããããŸãã
次ã«ãéžæãããã¡ã®ãã°ããŒãã«ãããªã¢ã«ãïŒ
MatPick
ïŒãäœæããŠåæåãããããã¡èªäœãåæåããå¿ èŠããããŸãã
// "" MatPick = new $W.Material({path:$W.paths.materials + "pick.json"}); MatPick.setUniformAction('pickColor', function(uniform, object, material){ var colr = [0.,0.,0.,0.]; var id = object.id; for(var i = 0; i < 4; i++){ colr[i] = (id & 0xff)/256.; id >>= 8; } $W.GL.uniform4fv(uniform.location, colr); } ); // try{ $W.pickBuffer = new $W.Framebuffer(); $W.pickBuffer.attachTexture($W.GL.RGBA, $W.canvas.width, $W.canvas.height, $W.GL.COLOR_ATTACHMENT0); $W.pickBuffer.attachRenderbuffer($W.GL.DEPTH_COMPONENT16, $W.canvas.width, $W.canvas.height, $W.GL.DEPTH_ATTACHMENT); }catch (e) { console.error(e); }
æšæºã¹ããŒã ã«åŸã£ãŠéžæãè¡ããŸãïŒãªããžã§ã¯ãèå¥åïŒ32ãããæŽæ°ãã§ããã°æ£ãè² ããã ã-1ã«çãããªãïŒã¯ãã€ãã«åå²ãããæµ®åå°æ°ç¹æ°ïŒãªããžã§ã¯ãã®è²æåïŒã«å€æãããŸãã floatã¯ãäžžã誀差ãã©ã³ãã 誀差ã蚱容ããªãããšãä¿èšŒããŸãïŒèå¥åaã±1ã«å€æãããå ŽåïŒã éžæãããã¡ãŒã衚瀺ããããã®ã·ã§ãŒããŒã¯éåžžã«åçŽãªã®ã§ãããã§ã¯ãããã説æããŸããã
3çªç®ã«ãObjects.jsãã¡ã€ã«ã®ã³ãŒããè¥å¹²å€æŽããå¿ èŠããããŸãïŒããã©ã«ãã§èå¥åãåæåããããïŒãããã³Util.jsïŒç»é¢ã«ã³ã³ãã³ãã衚瀺ããåŸã«æç»ãããã¡ãŒã®å 容ãä¿åãããããããããªããšéžæã¯æ©èœããŸããïŒã
æåŸã«ããªããžã§ã¯ããéžæãããã¡ãŒã«æç»ããã«ãŒãœã«ã®äžã®è²ã®å€ãååŸããéžæé¢æ°ãäœæããå¿ èŠããããŸãã ãããè¡ãã«ã¯ããã¹ãŠã®ãªããžã§ã¯ãã衚瀺ããåŸããããã®ããããªã¢ã«ãã
oldmat
é åã«ä¿åããŸããéžæãããã¡ãŒãæç»ãããšãã¯ãäžæçã«ãã¹ãŠã®ããããªã¢ã«ãã
MatPick
ã«å€æŽãããããã¡ãŒãåããŠãé©åãªãã€ã³ãã§ã«ã©ãŒå€ãååŸããŸãã ãããŠããã§ã«è²ã®å€ã«ãã£ãŠããªããžã§ã¯ãã®èå¥åã決å®ããŸãïŒãŸãã¯id ==-1ã®å Žåããªããžã§ã¯ãã®æ¬ åŠïŒã
function pick(X,Y){ // var ccolr = $W.GL.getParameter($W.GL.COLOR_CLEAR_VALUE); var blend = $W.GL.getParameter($W.GL.BLEND); if(blend) $W.GL.disable($W.GL.BLEND); $W.GL.clearColor(1., 1., 1., 1.); // "-1" $W.pickBuffer.bind(); // "" $W.util.defaultUpdate(); $W.util.clear(); $W.util.setupMatrices(); for (var i = 0; i < $W.objects.length; i++) { $W.objects[i].material = MatPick; // "" } $W.util.defaultDraw(); // for (var i = 0; i < $W.objects.length; i++) { $W.objects[i].material = oldmat[i]; // "" } var pix = new Uint8Array(4); $W.GL.readPixels(X,$W.canvas.height-Y,1,1,$W.GL.RGBA, $W.GL.UNSIGNED_BYTE, pix); $W.pickBuffer.unbind(); // if(blend) $W.GL.enable($W.GL.BLEND); $W.GL.clearColor(ccolr[0],ccolr[1],ccolr[2],ccolr[3]); var id = pix[0]+(pix[1]<<8)+(pix[2]<<16)+(pix[3]<<24); delete pix; var str = "X=" + X + ", Y=" + Y+ ", ID="+id; alert(str); } </code> <br><br> : . <br><br> : google-chrome ( ).