-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
200 lines (184 loc) · 8.72 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="shortcut icon" href="">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>CraZyPG</title>
<style>
html, body {
background: black;
padding: 0px;
margin: 0px;
width: 100%;
height: 100%;
}
div#canvas {
display: flex;
width: 100%;
height: 100%;
align-items: center;
justify-content: center;
}
div#container{
width: 100%;
height: 100%;
}
div#footbar{
position: absolute;
bottom: 0px;
right: 20px;
padding: 10px;
font-size: 11px;
font-family: 'Raleway', 'Helvetica', sans-serif;
color: #222222;
font-weight: 700;
}
div#footbar a{
color: #44ABDA
}
</style>
</head>
<body>
<div id="container">
<div id="canvas">
<canvas id="glpaper"></canvas>
</div>
</div>
<div id="footbar">
<p><a href="https://github.com/PrincessGod/CraZyPG">CZPG.js</a></p>
</div>
<script src="./build/czpg.js"></script>
<script src="./sample/js/dat.gui.min.js"></script>
<script>
var sceneNode, ca, sc, or;
window.addEventListener('load', function() {
const renderer = new CZPG.Renderer('glpaper', { antialias: true, preserveDrawingBuffer: true }).setSize('100%', '100%').clear();
const context = renderer.context;
const scene = new CZPG.Scene(renderer);
const controler = scene.controler;
let camera = new CZPG.PerspectiveCamera(45, context.canvas.width/context.canvas.height);
// let camera = new CZPG.OrthographicCamera(10, context.canvas.width/context.canvas.height);
camera.transform.position = [0, 0, 7];
let cameraControler = new CZPG.OrbitControls(camera, context.canvas, controler);
cameraControler.enableDamping = true;
cameraControler.autoRotate = true;
const textures = CZPG.createTextures(context, {
env: {
target: context.TEXTURE_CUBE_MAP,
src: [
'https://raw.githubusercontent.com/PrincessGod/resources/master/EnvTextures/environment/px.jpg',
'https://raw.githubusercontent.com/PrincessGod/resources/master/EnvTextures/environment/nx.jpg',
'https://raw.githubusercontent.com/PrincessGod/resources/master/EnvTextures/environment/py.jpg',
'https://raw.githubusercontent.com/PrincessGod/resources/master/EnvTextures/environment/ny.jpg',
'https://raw.githubusercontent.com/PrincessGod/resources/master/EnvTextures/environment/pz.jpg',
'https://raw.githubusercontent.com/PrincessGod/resources/master/EnvTextures/environment/nz.jpg',
]
},
diffuse: {
target: context.TEXTURE_CUBE_MAP,
src: [
'https://raw.githubusercontent.com/PrincessGod/resources/master/EnvTextures/environment/diffuse.px.jpg',
'https://raw.githubusercontent.com/PrincessGod/resources/master/EnvTextures/environment/diffuse.nx.jpg',
'https://raw.githubusercontent.com/PrincessGod/resources/master/EnvTextures/environment/diffuse.py.jpg',
'https://raw.githubusercontent.com/PrincessGod/resources/master/EnvTextures/environment/diffuse.ny.jpg',
'https://raw.githubusercontent.com/PrincessGod/resources/master/EnvTextures/environment/diffuse.pz.jpg',
'https://raw.githubusercontent.com/PrincessGod/resources/master/EnvTextures/environment/diffuse.nz.jpg',
]
},
lut: {
mag: context.NEAREST,
min: context.NEAREST,
src: 'https://raw.githubusercontent.com/PrincessGod/resources/master/EnvTextures/brdfLUT.png',
}
});
// let file = './resource/gltf/VC/glTF/VC.gltf';
let file = 'https://cdn.rawgit.com/KhronosGroup/glTF-Sample-Models/395358db/2.0/AnimatedMorphSphere/glTF/AnimatedMorphSphere.gltf';
let gltfShader = new CZPG.GLTFShader(context).setUniformObj({
u_lightDirection: [0, 1, 1],
u_lightColor: [1, 1, 1],
u_diffuseEnvMap: textures.diffuse,
u_specularEnvMap: textures.env,
u_brdfLUT: textures.lut,
});
let gltfLoader = new CZPG.GLTFLoader();
let animator;
gltfLoader.load(file)
.then( res => {
const {rootNode, textures, animations, cameras} = res;
sceneNode = rootNode; ca = cameras; sc = scene; or = cameraControler;
const gltfTextures = CZPG.createTextures(context, textures);
rootNode.traverse((node)=> {
if(node.model && node.model.textures !== undefined) {
Object.keys(node.model.textures).forEach(uniformName => node.model.textures[uniformName] = gltfTextures[node.model.textures[uniformName]])
node.model.setUniformObj(node.model.textures);
}
});
if(cameras.length > 0) {
allCamera = [camera, ...cameras];
allCameraName = ['Default', ...cameras.map(c => c.name)];
datgui.add(controlObj, 'camera', allCameraName)
.onFinishChange(value => {
let target = allCamera.filter(c => c.name === value);
scene.setCamera(allCamera[allCameraName.indexOf(value)]);
if(scene.currentCamera === camera) {
cameraControler.enable = true;
} else {
cameraControler.enable = false;
}
});
}
animator = new CZPG.Animator(animations);
let animates = animator.animations;
if(animates.length > 0) {
let animateMap = animates.map(ani => ani.name);
animateMap = ['None'].concat(animateMap);
datgui.add(controlObj, 'animate', animateMap)
.onFinishChange(value => {
animator.resetAll().playAnimation(value);
});
}
// animator.playAll();
scene.add([
{shader: gltfShader, model: rootNode},
]);
});
let gridShader = new CZPG.GridAxisShader(context);
let gridModal = CZPG.GridAxis.createModel()
let skymapShader = new CZPG.DynamicSkyboxShader(context, textures.env);
let skyCubeModal = CZPG.Cube.createModel('sky');
scene.add([
// {shader: gridShader, model: gridModal},
{shader: skymapShader, model: skyCubeModal},
]).setCamera(camera);
let resized = false;
let loop = new CZPG.Render(function(timespan) {
resized = renderer.clear(0.05,0.05,0.05,1).fixCanvasToDisplay(window.devicePixelRatio);
if(resized) scene.cameras.forEach(c => c.updateProjMatrix( context.canvas.width / context.canvas.height ));
cameraControler.update();
if(animator) animator.update(timespan);
scene.render();
}).start();
let controlObj = {
showgrid: false,
autoRotate: true,
animate: 'None',
camera: 'Default',
};
let datgui = new dat.GUI();
datgui.add(controlObj, 'showgrid')
.onFinishChange(value => {
if(value)
scene.add({shader: gridShader, model: gridModal});
else
scene.remove({shader: gridShader, model: gridModal});
});
datgui.add(controlObj, 'autoRotate')
.onFinishChange(value => {
cameraControler.autoRotate = value;
});
});
</script>
</body>
</html>