-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample_prim.html
259 lines (244 loc) · 7.06 KB
/
sample_prim.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width" />
<title>WWG (primitive)</title>
<script src="js/WWG.js"></script>
<script src="js/CanvasMatrix.js"></script>
<script src="js/WWModel.js"></script>
<script src="js/Pointer.js"></script>
<script>
var pixRatio = window.devicePixelRatio ;
var isStereo = false ;
onload = function() {
// canvas initialize
var can = document.getElementById('screen1') ;
can.width= can.offsetWidth*pixRatio ;
can.height = can.offsetHeight*pixRatio ;
console.log("canvas:"+can.width+" x "+can.height);
// wwg initialize
var wwg = new WWG() ;
if(!wwg.init(can)) {
alert("not supported") ;
return ;
}
// scene data
var scene = {
env:{clear_color:[0.6,0.8,1.0,1.0],cull:true},
vshader:{
text: document.getElementById('vshader').innerText
},
fshader:{
text: document.getElementById('fshader').innerText
},
vs_uni:{
lightVec:[1,0.8,0.9],
},
fs_uni:{
color:[1.0,0.2,0.2,1.0],
tex:0,
mode:3
},
texture:[
{src:"res/tex1.png",opt:{flevel:2,anisotropy:4}}
],
model:[
{name:"axis",
geo:{mode:"lines",
vtx_at:["position"],
vtx:[0,0,0, 100,0,0, 0,0,0, 0,100,0, 0,0,0, 0,0,100]},
fs_uni:{color:[1.0,1.0,1.0,1.0],mode:1}},
{name:"base",
geo:new WWModel().primitive("plane",{wx:50,wz:50}).objModel(),
bm:new CanvasMatrix4().translate(0,-0.2,0),
fs_uni:{color:[0.6,0.9,0.6,1.0],mode:2}},
{name:"sph",
geo:new WWModel().primitive("sphere",{div:20}).objModel(),
bm:new CanvasMatrix4().scale(0.75).translate(1,1,0),
fs_uni:{mode:2}},
{name:"box",
geo:new WWModel().primitive("box",{wz:0.5}).objModel(),
bm:new CanvasMatrix4().rotate(45,0,0,1)},
{name:"jiku",
geo:new WWModel().primitive("cylinder",{div:20,wx:0.2,wy:2,wz:0.2}).objModel(),
bm:new CanvasMatrix4().rotate(90,1,0,0),
fs_uni:{color:[0.3,0.3,1.0,1.0],mode:0}},
{name:"futa",
geo:new WWModel().primitive("disc",{div:20,wx:0.2,wy:0.2,wz:0.2}).objModel(),
bm:new CanvasMatrix4().rotate(90,1,0,0).translate(0,0,2),
fs_uni:{color:[0.3,0.3,1.0,1.0],mode:0}}
]
}
// scene update
function calcMatrix(render,p,sf) {
var RAD = Math.PI/180 ;
var dx = sf * 0.1 ; // stereo base
var aspect = can.width/(can.height*((isStereo)?2:1)) ;
// calc camera matrix
// bird camera mode
var mx = my = mz = 0 ;
var camd= 5 ;
var camX = -Math.sin(p.camRY*RAD)*camd*Math.cos(p.camRX*RAD)
var camY = Math.sin(p.camRX*RAD)*camd ;
var camZ = Math.cos(p.camRY*RAD)*camd*Math.cos(p.camRX*RAD)
var cx = 0 ,cy = 0, cz = 0 ;
var upx = 0,upy = 1 ,upz = 0 ;
/*
// self camera mode
var mx = 2 ;
var mz = 2 ;
var my = 0.1
var cx = Math.sin(p.camRY*RAD)*camd*Math.cos(p.camRX*RAD)
var cy = -Math.sin(p.camRX*RAD)*camd ;
var cz = -Math.cos(p.camRY*RAD)*camd*Math.cos(p.camRX*RAD)
var camX = 0 ,camY = 0, camZ = 0 ;
var upx = 0,upy = 1 ,upz = 0 ;
*/
// X vector = Y cross Z
var xx = upy * (camZ-cz) - upz * (camY-cy);
var xy = -upx * (camZ-cz) + upz * (camX-cx);
var xz = upx * (camY-cy) - upy * (camX-cx);
var mag = Math.sqrt(xx * xx + xy * xy + xz * xz);
xx /= mag ; xy /=mag ; xz /= mag ;
var camM = new CanvasMatrix4().lookat(camX+xx*dx+mx,camY+xy*dx+my,camZ+xz*dx+mz,cx+mx,cy+my,cz+mz,upx,upy,upz).
perspective(60,aspect, 0.1, 100)
// calc each model moving matrix
render.getModelData("sph").mm = new CanvasMatrix4().rotate(p.rotY*0.9,0,1,0)
render.getModelData("box").mm = new CanvasMatrix4().rotate(p.rotY,0,0,1)
// calc each mvp matrix and invert matrix
var mod = [] ;
for(var i=0;i<render.modelCount;i++) {
var d = render.getModelData(i) ;
var m = new CanvasMatrix4(d.bm) ;
if(d.mm) m.multRight(d.mm) ;
mod[i] = {
vs_uni:{mvpMatrix:new CanvasMatrix4(m).multRight(camM).getAsWebGLFloatArray(),
invMatrix:new CanvasMatrix4(m).invert().getAsWebGLFloatArray()}
}
}
return {model:mod} ;
}
function update(render,p) {
// draw call
if(isStereo) {
render.gl.viewport(0,0,can.width/2,can.height) ;
render.draw(calcMatrix(render,p,-1),false) ;
render.gl.viewport(can.width/2,0,can.width/2,can.height) ;
render.draw(calcMatrix(render,p,1),true) ;
} else {
render.gl.viewport(0,0,can.width,can.height) ;
render.draw(calcMatrix(render,p,0),false) ;
}
}
var p = {camRX:30,camRY:-30,rotX:0,rotY:0,ofsY:0}
//create render unit
var r = wwg.createRender() ;
r.setRender(scene).then(function() {
console.log(r) ;
var rotX = p.camRX ,rotY = p.camRY ;
//draw first time
update(r,p);
//draw loop
var st = new Date().getTime() ;
var lt = st ;
(function loop(){
window.requestAnimationFrame(loop) ;
var ct = new Date().getTime() ;
var tint = (ct - st) ;
if((ct - lt)<1000/60) return ;
p.rotY = (ct-st)/20 ;
update(r,p) ;
// console.log(Math.floor(1000/(ct - lt)+0.5)) ;
lt = ct ;
})();
//mouse intraction
var mag = 300/can.width;
var keymag= 2 ;
var m = new Pointer(can,{
down:function(d) {
return false ;
},
move:function(d) {
p.camRX = rotX+d.dy*mag ;
p.camRY = rotY+d.dx*mag ;
if(p.camRX>90)p.camRX=90 ;
if(p.camRX<-90)p.camRX=-90 ;
return false ;
},
up:function(d) {
rotX += d.dy*mag ;
rotY += d.dx*mag;
return false ;
}
})
document.body.addEventListener("keydown", function(ev){
switch(ev.keyCode) {
case 37:rotY -= keymag ; break ;
case 38:rotX -= keymag ; if(rotX<-90) rotX = -90 ; break ;
case 39:rotY += keymag ; break ;
case 40:rotX += keymag ; if(rotX>90) rotX = 90 ; break ;
}
p.camRX = rotX ;
p.camRY = rotY ;
})
document.getElementById("isstereo").addEventListener("change", function(ev) {
isStereo = this.checked ;
})
}).catch(function(err){
console.log(err) ;
})
} //onload
</script>
<! --------------- Vertex Shader ---------------- ->
<script id="vshader" type="x-shader/x-vertex">
attribute vec3 position;
attribute vec3 norm;
attribute vec2 uv ;
uniform mat4 mvpMatrix;
uniform mat4 invMatrix;
uniform vec3 lightVec ;
varying vec2 tuv ;
varying float light ;
void main() {
tuv = vec2(uv.x,1.0-uv.y) ;
light =clamp(dot(norm, normalize(invMatrix * vec4(lightVec,0.0)).xyz),0.0,1.0)*0.8+0.2;
gl_Position = mvpMatrix * vec4(position, 1.0) ;
}
</script>
<! --------------- Fragment Shader ---------------- ->
<script id="fshader" type="x-shader/x-fragment">
precision highp float;
uniform vec4 color ;
uniform int mode ;
uniform sampler2D tex ;
varying vec2 tuv ;
varying float light ;
void main() {
vec3 col = color.xyz ;
if(mode==2) { //checker
float dx = mod(tuv.x*10.0,1.0) ;
float dy = mod(tuv.y*10.0,1.0) ;
float t = (dx<0.5) ^^ (dy<0.5)?1.0:0.0 ;
col = vec3(col.x*t,col.y*t,col.z*t) ;
}
if(mode==3) { //texture
col = texture2D(tex, tuv).xyz ;
}
gl_FragColor = (mode!=1) ? vec4(light*col.x, light*col.y, light*col.z, 1.0):vec4(color.x,color.y,color.z,1.0);
}
</script>
<style>
body {
margin:0 ;
background-color:#888 ;
}
canvas {
width:100vw ;
height:60vw ;
}
</style>
</head>
<body>
<canvas id="screen1" ></canvas><br/>
<label><input type=checkbox id=isstereo>stereo mode</label>