WebGLãç°¡åã«ä½¿ãããã®zero.jsãä½æãã¾ãã
WebGLã®ç»å ´ã§æããã¦ã§ãæ¥çã«ãå¾ã ã«3Dã®æ³¢ãæ¼ãå¯ãã¦ãããã¹ãã©ã¤ããå©ç¨ãã2Dã²ã¼ã ããã¤ã®ã¾ã«ã3Dã«é§éãããããã«ã3Dã®ã§ããªãã¦ã§ãããã°ã©ããé§éãããã®ãæéã®åé¡ã®ããã«ããæãããã¾ãã
ãããããWebGLãå§ãããã¨æã£ã¦ãããã¨ãã°ç»é¢ã«ãããã1ã¤è¡¨ç¤ºãããã¨æã£ãã ãã§
<html> <head> <script src="CanvasMatrix.js"></script> <script> function main() { var domElement = document.createElement('canvas'); domElement.width = 500; domElement.height = 500; document.body.appendChild(domElement); var gl = domElement.getContext('webgl') || domElement.getContext('experimental-webgl'); if (!gl) throw 'WebGL is not supported.'; var positions = [ -0.5, 0.5, 0.0, 0.5, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0 ]; var indices = [ 0, 1, 2, 2, 1, 3 ]; var numIndices = indices.length; var vbuffers = [positions, positions]; for (var i = 0; i < vbuffers.length; i++) { var data = vbuffers[i]; var vbuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW); vbuffers[i] = vbuffer; } gl.bindBuffer(gl.ARRAY_BUFFER, null); var ibuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Int16Array(indices), gl.STATIC_DRAW); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null); var vshader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vshader, "#ifdef GL_ES\n" + "precision highp float;\n" + "#endif\n" + "uniform mat4 mvpMatrix;\n" + "uniform mat4 normalMatrix;\n" + "uniform vec4 lightVec;\n" + "uniform vec4 lightColor;\n" + "uniform vec4 materialColor;\n" + "attribute vec3 position;\n" + "attribute vec3 normal;\n" + "varying vec4 color;\n" + "void main() {\n" + " float light = clamp(dot(vec3(0.0, 0.0, 1.0), lightVec.xyz), 0.0, 1.0) * 0.8 + 0.2;\n" + " color = min(min(materialColor, lightColor), vec4(light, light, light, 1.0));\n" + " gl_Position = mvpMatrix * vec4(position, 1.0);\n" + "}" ); gl.compileShader(vshader); if (!gl.getShaderParameter(vshader, gl.COMPILE_STATUS)) { throw gl.getShaderInfoLog(vshader); } var fshader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fshader, "#ifdef GL_ES\n" + "precision highp float;\n" + "#endif\n" + "varying vec4 color;\n" + "void main() {\n" + " gl_FragColor = color;\n" + "}" ); gl.compileShader(fshader); if (!gl.getShaderParameter(fshader, gl.COMPILE_STATUS)) { throw gl.getShaderInfoLog(fshader); } var program = gl.createProgram(); gl.attachShader(program, vshader); gl.attachShader(program, fshader); gl.bindAttribLocation(program, 0, 'position'); gl.bindAttribLocation(program, 1, 'normal'); gl.linkProgram(program); if (!gl.getProgramParameter(program, gl.LINK_STATUS)) { throw gl.getProgramInfoLog(program); } var uniformVars = [ gl.getUniformLocation(program, 'mvpMatrix'), gl.getUniformLocation(program, 'normalMatrix'), gl.getUniformLocation(program, 'lightVec'), gl.getUniformLocation(program, 'lightColor'), gl.getUniformLocation(program, 'materialColor') ]; gl.clearColor(0, 0, 0, 1); gl.clearDepth(1000); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); gl.enable(gl.DEPTH_TEST); gl.useProgram(program); var lightVec = [0.0, 0.0, 1.0, 0.0]; var lightColor = [1.0, 1.0, 1.0, 1.0]; var modelMatrix = new CanvasMatrix4(); var mvpMatrix = new CanvasMatrix4(modelMatrix); mvpMatrix.translate(0, 0, -500); mvpMatrix.perspective(30, 1.0, 0.1, 1000); var normalMatrix = new CanvasMatrix4(modelMatrix); normalMatrix.invert(); normalMatrix.transpose(); var materialColor = [1.0, 1.0, 1.0, 1.0]; var values = [mvpMatrix, normalMatrix, lightVec, lightColor, materialColor]; for (var i = 0; i < values.length; i++) { var value = values[i]; if (value instanceof CanvasMatrix4) { gl.uniformMatrix4fv(uniformVars[i], false, value.getAsWebGLFloatArray()); } else { gl.uniform4fv(uniformVars[i], new Float32Array(value)); } } var strides = [3, 3]; for (var i = 0; i < strides.length; i++) { var stride = strides[i]; gl.enableVertexAttribArray(i); gl.bindBuffer(gl.ARRAY_BUFFER, vbuffers[i]); gl.vertexAttribPointer(i, stride, gl.FLOAT, false, 0, 0); } gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibuffer); gl.drawElements(gl.TRIANGLES, numIndices, gl.UNSIGNED_SHORT, 0); gl.flush(); } document.addEventListener('DOMContentLoaded', main, false); </script> </head> <body> </body> </html>
ä¿¡ãããããã¨ã«ãã®ãããªã³ã¼ããè¦æ±ããã¾ã*1ã
ç¹ã«Cè¨èªæ§ã®GLSLã³ã¼ããæååã¨ãã¦æ¸¡ããªãã¨ç»é¢ã«ä½ã表示ãããããã®GLSLã®ã³ã¼ãã«JSå´ããå¤ãä¸ããã«ã¯ãGLSLã³ã¼ãå ã§ä½çªç®ã«å®£è¨ãããå¤æ°ããã¨ããæ å ±ãå¿ è¦ã ã£ããããã¨ãããªã©ãã¾ãã§ä½ãæªã夢ãè¦ã¦ããããã§ããããã¾ãã
WebGLã®é£ããã¯ãã®ãããªWebGLèªä½ã®è¤éãã«å ãã¦ãããã«3Dã®ç¥èã¾ã§ããè¦æ±ãããã¨ããã«ããã¨ãããã§ãããã
ãã¦ããã®ããã«è¤ééããåé¡ã«ç´é¢ããæã«æã ããã°ã©ããåãã¹ãæ段ã¨ã¯ãªãã§ããããï¼ããã§ããåå²çµ±æ²»ã§ããããã§ç§ã¯ãã®åé¡ãã3Dã¨ããåé¡ãé¤ããã¾ãã¯ãWebGLèªä½ã®è¤éããã ãã解決ããããã®ã©ã¤ãã©ãªãéçºãã¾ããã
http://technohippy.github.io/zero.js/
ããããã®zero.jsã§ããzero.jsã¯ååã®éãWebGLã使ç¨ãã¦åç¹ãããªãã¡ã¼ã次å ãæç»ããããã®ã©ã¤ãã©ãªã§ãããã®ã©ã¤ãã©ãªã使ç¨ããã°ç ©ããã3Dã«ã¤ãã¦èãããã¨ãªããWebGLãå©ç¨ãããã¨ã«éä¸ã§ãã¾ããä¾ãã°æåã«ä¸ããã³ã¼ãã¨åæ§ã®è¡¨ç¤ºã次ã®ããã«é常ã«ã·ã³ãã«ã«è¨è¿°ãããã¨ãã§ãã¾ãã
function main() { var scene = new ZERO.Scene(); var geometry = new ZERO.PointGeometry(); var material = new ZERO.MeshBasicMaterial({color: 0xffffff}); var mesh = new ZERO.Mesh(geometry, material); scene.add(mesh); var width = 500; var height = 500; var fov = 30; var aspect = width / height; var near = 0.1; var far = 1000; var camera = new ZERO.PerspectiveCamera(fov, aspect, near, far); camera.position.set(500); var directionalLight = new ZERO.DirectionalLight(0xffffff); directionalLight.position.set(1.0); scene.add(directionalLight); var renderer = new ZERO.WebGLRenderer(); renderer.setSize(width, height); document.getElementById('demo').appendChild(renderer.domElement); renderer.render(scene, camera); } document.addEventListener('DOMContentLoaded', main, false);
ãã以ä¸ãªãã·ã³ãã«ãªã³ã¼ãã§åç¹ãæç»ã§ãã¦ãããã¨ãåããã§ãããã
ãã¡ããããã§ã©ã¤ãã©ãªã«åå¨ããå¯ä¸ã®ã¸ãªã¡ããªã§ããZERO.PointGeometryã¯åç¹ã§ããããä¸åã®åº§æ¨ãæå®ã§ãã¾ãããããã¯å éã観測è ã«ãããä¸å®ã§ãããã¨ãèããã¨åãããããã®ã§ã¯ãªãã§ããããã
ã¾ãã«ã¡ã©ï¼è¦ç¹ï¼ã«ã¤ãã¦ãæå®ã§ããã®ã¯åºæ¬çã«åç¹ããã®è·é¢ã ãã§ãããã ãåç¹ã¯ã¼ã次å ã§ããããå½ç¶"大ãã"ã¨ããæ¦å¿µãåå¨ãããã«ã¡ã©ã®ä½ç½®ã«é¢ãããè¦è§ã¤ã¾ã表示ä¸ã®ãµã¤ãºã¯ä¸å®ã«ãªãã¾ãã
ãã®ããã«zero.jsã使ç¨ããã°åº§æ¨ãã»ã¼æèããã«WebGLãå©ç¨ã§ãããã¨ãåãã£ã¦ããã ããã§ããããzero.jsãçãããWebGLã使ãå§ããé¶æ©ç®ã«ãªããã¨ãé¡ã£ã¦ãã¾ãã
ã»ã»ã»
ãªããå§å¦¹åã¨ãã¦1次å ãæç»ããããã®one.jsãåããã¦ä½æãã¾ãããzero.jsã«ç¿çããæ¹ã®æ¬¡ã®ä¸æ©ã¨ãã¦ãããããã°ãã¡ãããå©ç¨ãã ããã