diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/utilities.js')
-rwxr-xr-x | js/helper-classes/RDGE/src/core/script/utilities.js | 152 |
1 files changed, 76 insertions, 76 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/utilities.js b/js/helper-classes/RDGE/src/core/script/utilities.js index dcc61d45..85190097 100755 --- a/js/helper-classes/RDGE/src/core/script/utilities.js +++ b/js/helper-classes/RDGE/src/core/script/utilities.js | |||
@@ -11,26 +11,26 @@ var RDGE = RDGE || {}; | |||
11 | /* | 11 | /* |
12 | // Methods not currently used in Ninja | 12 | // Methods not currently used in Ninja |
13 | RDGE.getRandColor = function () { | 13 | RDGE.getRandColor = function () { |
14 | var r = Math.random(); | 14 | var r = Math.random(); |
15 | var g = Math.random(); | 15 | var g = Math.random(); |
16 | var b = Math.random(); | 16 | var b =Math.random(); |
17 | 17 | ||
18 | return [r, g, b, 1.0]; | 18 | return [r, g, b, 1.0]; |
19 | }; | 19 | }; |
20 | 20 | ||
21 | RDGE.unProject = function (winx, winy, winz, modelMatrix, projMatrix, viewport) { | 21 | RDGE.unProject = function (winx, winy, winz, modelMatrix, projMatrix, viewport) { |
22 | var inVal = [0, 0, 0, 0]; | 22 | var inVal = [0,0,0,0]; |
23 | 23 | ||
24 | var finalMatrix = RDGE.mat4.mul(modelMatrix, projMatrix); | 24 | var finalMatrix = RDGE.mat4.mul(modelMatrix, projMatrix); |
25 | 25 | ||
26 | finalMatrix = RDGE.mat4.inverse(finalMatrix); | 26 | finalMatrix = RDGE.mat4.inverse(finalMatrix); |
27 | if (!finalMatrix) | 27 | if(!finalMatrix) |
28 | return null; | 28 | return null; |
29 | 29 | ||
30 | inVal[0] = winx; | 30 | inVal[0]=winx; |
31 | inVal[1] = winy; | 31 | inVal[1]=winy; |
32 | inVal[2] = winz; | 32 | inVal[2]=winz; |
33 | inVal[3] = 1.0; | 33 | inVal[3]=1.0; |
34 | 34 | ||
35 | // Map x and y from window coordinates | 35 | // Map x and y from window coordinates |
36 | inVal[0] = (inVal[0] - viewport[0]) / viewport[2]; | 36 | inVal[0] = (inVal[0] - viewport[0]) / viewport[2]; |
@@ -42,15 +42,15 @@ RDGE.unProject = function (winx, winy, winz, modelMatrix, projMatrix, viewport) | |||
42 | inVal[2] = inVal[2] * 2 - 1; | 42 | inVal[2] = inVal[2] * 2 - 1; |
43 | 43 | ||
44 | var v4Out = RDGE.mat4.transformPoint(finalMatrix, inVal); | 44 | var v4Out = RDGE.mat4.transformPoint(finalMatrix, inVal); |
45 | 45 | ||
46 | if (v4Out[3] <= 0.0001) | 46 | if (v4Out[3] <= 0.0001) |
47 | return null; | 47 | return null; |
48 | 48 | ||
49 | v4Out[0] /= v4Out[3]; | 49 | v4Out[0] /= v4Out[3]; |
50 | v4Out[1] /= v4Out[3]; | 50 | v4Out[1] /= v4Out[3]; |
51 | v4Out[2] /= v4Out[3]; | 51 | v4Out[2] /= v4Out[3]; |
52 | 52 | ||
53 | return [v4Out[0], v4Out[1], v4Out[2]]; | 53 | return [ v4Out[0], v4Out[1], v4Out[2] ]; |
54 | }; | 54 | }; |
55 | 55 | ||
56 | RDGE.AABB2LineSegment = function (box, startPoint, endPoint) { | 56 | RDGE.AABB2LineSegment = function (box, startPoint, endPoint) { |
@@ -61,42 +61,42 @@ RDGE.AABB2LineSegment = function (box, startPoint, endPoint) { | |||
61 | m = RDGE.vec3.sub(m, box.min), | 61 | m = RDGE.vec3.sub(m, box.min), |
62 | m = RDGE.vec3.sub(m, box.max); | 62 | m = RDGE.vec3.sub(m, box.max); |
63 | 63 | ||
64 | var adx = Math.abs(d[0]); | 64 | var adx = Math.abs(d[0]); |
65 | if (Math.abs(m[0]) > e[0] + adx) return false; | 65 | if( Math.abs(m[0]) > e[0] + adx ) return false; |
66 | |||
67 | var ady = Math.abs(d[1]); | ||
68 | if( Math.abs(m[1]) > e[1] + ady ) return false; | ||
66 | 69 | ||
67 | var ady = Math.abs(d[1]); | 70 | var adz = Math.abs(d[2]); |
68 | if (Math.abs(m[1]) > e[1] + ady) return false; | 71 | if( Math.abs(m[2]) > e[2] + adz ) return false; |
69 | 72 | ||
70 | var adz = Math.abs(d[2]); | 73 | adx += 1.192092896e-07; |
71 | if (Math.abs(m[2]) > e[2] + adz) return false; | 74 | ady += 1.192092896e-07; |
75 | adz += 1.192092896e-07; | ||
72 | 76 | ||
73 | adx += 1.192092896e-07; | 77 | if( Math.abs(m[1] * d[2] - m[2] * d[1]) > e[1] * adz + e[2] * ady ) return false; |
74 | ady += 1.192092896e-07; | 78 | if( Math.abs(m[2] * d[0] - m[0] * d[2]) > e[0] * adz + e[2] * adx ) return false; |
75 | adz += 1.192092896e-07; | 79 | if( Math.abs(m[0] * d[1] - m[1] * d[0]) > e[0] * ady + e[1] * adx ) return false; |
76 | 80 | ||
77 | if (Math.abs(m[1] * d[2] - m[2] * d[1]) > e[1] * adz + e[2] * ady) return false; | 81 | return true; |
78 | if (Math.abs(m[2] * d[0] - m[0] * d[2]) > e[0] * adz + e[2] * adx) return false; | ||
79 | if (Math.abs(m[0] * d[1] - m[1] * d[0]) > e[0] * ady + e[1] * adx) return false; | ||
80 | |||
81 | return true; | ||
82 | }; | 82 | }; |
83 | 83 | ||
84 | RDGE.hitTest = function (mesh, near, far) { | 84 | RDGE.hitTest = function (mesh, near, far) { |
85 | // holds distance to the nearest BV | 85 | // holds distance to the nearest BV |
86 | var dist = null; | 86 | var dist = null; |
87 | var BV = null; | 87 | var BV = null; |
88 | 88 | ||
89 | for (var index = 0; index < mesh.BVL.length; index++) { | 89 | for (var index = 0; index < mesh.BVL.length; index++) { |
90 | if (AABB2LineSegment(mesh.BVL[index], near, far)) { | 90 | if (AABB2LineSegment(mesh.BVL[index], near, far)) { |
91 | var center = RDGE.vec3.scale(RDGE.vec3.add(mesh.BVL[index].min, mesh.BVL[index].max), 0.5); | 91 | var center = RDGE.vec3.scale(RDGE.vec3.add(mesh.BVL[index].min, mesh.BVL[index].max), 0.5); |
92 | var newDist = RDGE.vec3.dot(RDGE.mat4.row(RDGE.globals.cam.world, 2), center); | 92 | var newDist = RDGE.vec3.dot(RDGE.mat4.row(RDGE.globals.cam.world, 2), center); |
93 | if (newDist < dist || dist == null) { | 93 | if (newDist < dist || dist == null) { |
94 | dist = newDist; | 94 | dist = newDist; |
95 | BV = mesh.BVL[index]; | 95 | BV = mesh.BVL[index]; |
96 | } | 96 | } |
97 | } | ||
98 | } | 97 | } |
99 | return BV; | 98 | } |
99 | return BV; | ||
100 | }; | 100 | }; |
101 | 101 | ||
102 | 102 | ||
@@ -111,19 +111,19 @@ RDGE.hitTest = function (mesh, near, far) { | |||
111 | 111 | ||
112 | 112 | ||
113 | // loadShader | 113 | // loadShader |
114 | // 'shaderId' is the id of a <script> element containing the shader source string. | 114 | // 'shaderId' is the id of a <script> element containing the shader source string. |
115 | // Load this shader and return the WebGLShader object corresponding to it. | 115 | // Load this shader and return the WebGLShader object corresponding to it. |
116 | RDGE.loadShader = function (ctx, shaderType, shaderStr) { | 116 | RDGE.loadShader = function (ctx, shaderType, shaderStr) { |
117 | // pre-pend preprocessor settings | 117 | // pre-pend preprocessor settings |
118 | var preProcessor = "#define PC\n" | 118 | var preProcessor = "#define PC\n" |
119 | preProcessor += shaderStr; | 119 | preProcessor += shaderStr; |
120 | shaderStr = preProcessor; | 120 | shaderStr = preProcessor; |
121 | 121 | ||
122 | // Create the shader object | 122 | // Create the shader object |
123 | var shader = ctx.createShader(shaderType); | 123 | var shader = ctx.createShader(shaderType); |
124 | if (shader == null) { | 124 | if (shader == null) { |
125 | 125 | ||
126 | ctx.console.log("*** Error: unable to create shader '" + shaderType + "'"); | 126 | ctx.console.log("*** Error: unable to create shader '"+shaderType+"'"); |
127 | 127 | ||
128 | return null; | 128 | return null; |
129 | } | 129 | } |
@@ -140,7 +140,7 @@ RDGE.loadShader = function (ctx, shaderType, shaderStr) { | |||
140 | // Something went wrong during compilation; get the error | 140 | // Something went wrong during compilation; get the error |
141 | var error = ctx.getShaderInfoLog(shader); | 141 | var error = ctx.getShaderInfoLog(shader); |
142 | 142 | ||
143 | ctx.console.log("*** Error compiling shader '" + shaderType + "':" + error); | 143 | ctx.console.log("*** Error compiling shader '"+shaderType+"':"+error); |
144 | 144 | ||
145 | ctx.deleteShader(shader); | 145 | ctx.deleteShader(shader); |
146 | return null; | 146 | return null; |
@@ -152,28 +152,28 @@ RDGE.loadShader = function (ctx, shaderType, shaderStr) { | |||
152 | // creates id for shader | 152 | // creates id for shader |
153 | RDGE.g_shaderCounter = 0; | 153 | RDGE.g_shaderCounter = 0; |
154 | RDGE.createShader = function (ctx, strVertShaderName, strFragShaderName, attribs) { | 154 | RDGE.createShader = function (ctx, strVertShaderName, strFragShaderName, attribs) { |
155 | var vShader = '', fShader = ''; | 155 | var vShader = '', fShader = ''; |
156 | 156 | ||
157 | if (strVertShaderName.indexOf('{') != -1) { | 157 | if (strVertShaderName.indexOf('{') != -1) { |
158 | vShader = strVertShaderName; | 158 | vShader = strVertShaderName; |
159 | } else { | 159 | } else { |
160 | var vshaderRequest = new XMLHttpRequest(); | 160 | var vshaderRequest = new XMLHttpRequest(); |
161 | vshaderRequest.open("GET", 'assets/shaders/' + strVertShaderName + '.glsl', false); | 161 | vshaderRequest.open("GET", g_Engine._assetPath+'shaders/' + strVertShaderName + '.glsl', false); |
162 | vshaderRequest.send(null); | 162 | vshaderRequest.send(null); |
163 | vShader = vshaderRequest.responseText; | 163 | vShader = vshaderRequest.responseText; |
164 | } | 164 | } |
165 | 165 | ||
166 | if (strFragShaderName.indexOf('{') != -1) { | 166 | if (strFragShaderName.indexOf('{') != -1) { |
167 | fShader = strFragShaderName; | 167 | fShader = strFragShaderName; |
168 | } else { | 168 | } else { |
169 | var fshaderRequest = new XMLHttpRequest(); | 169 | var fshaderRequest = new XMLHttpRequest(); |
170 | fshaderRequest.open("GET", 'assets/shaders/' + strFragShaderName + '.glsl', false); | 170 | fshaderRequest.open("GET", g_Engine._assetPath+'shaders/' + strFragShaderName + '.glsl', false); |
171 |