-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample_pmodel.html
251 lines (233 loc) · 6.47 KB
/
sample_pmodel.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width" />
<title>WWG (parametric model)</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 ;
var PHI = Math.PI *2 ;
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.6,0.6,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:{
bcolor:[0.2,0.2,0.4,1.0],
mode:0
},
model:[
{name:"torus",
geo:new WWModel().primitive("torus",{div:20,sr:0.4}).objModel(),
bm:new CanvasMatrix4().translate(0,2,0),
fs_uni:{bcolor:[1.0,1.0,1.0,1.0],mode:2}},
{name:"wave",
geo:new WWModel().parametricModel(function(u,v){
var r = Math.sqrt(u*u+v*v)
var d = Math.cos(r/2*PHI)+1
var cv = Math.cos(r*4*PHI)*d*0.5
return {
px:u*4,
py:cv*0.4,
pz:-v*4,
nx:0,ny:0,nz:0,mu:u,mv:v}
},{start:-1,end:1.0,div:100},{start:-1,end:1,div:100}).objModel(),
fs_uni:{bcolor:[1.0,0.2,0.2,1.0],mode:2}
},
{name:"axis",
geo:{mode:"lines",
vtx_at:["position"],
vtx:[0,0,0, 200,0,0, 0,0,0, 0,200,0, 0,0,0, 0,0,200]},
fs_uni:{bcolor:[1.0,1.0,1.0,1.0],mode:1}},
]
}
// 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)) ;
var ret = {};
// 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 ;
// view light
// ret.vs_uni = {lightVec:[camX,camY,camZ]}
// 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(isStereo?70:60,aspect, 0.1, 300)
// 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()}
}
}
ret.model = mod ;
return ret ;
}
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*pixRatio/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 ;
attribute vec3 ipos ;
attribute vec4 icolor ;
uniform mat4 mvpMatrix;
uniform mat4 invMatrix;
uniform vec3 lightVec ;
varying vec2 tuv ;
varying float light ;
varying vec4 color ;
void main() {
color = icolor ;
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+ipos, 1.0) ;
}
</script>
<! --------------- Fragment Shader ---------------- ->
<script id="fshader" type="x-shader/x-fragment">
precision highp float;
uniform int mode ;
uniform vec4 bcolor ;
uniform sampler2D tex ;
varying vec2 tuv ;
varying float light ;
varying vec4 color ;
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(bcolor.x*t,bcolor.y*t,bcolor.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(bcolor.x,bcolor.y,bcolor.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>