-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.js
203 lines (172 loc) · 8.59 KB
/
index.js
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
let modelInfo = ModelIndex.getCurrentModel();
if (!modelInfo) {
modelInfo = TutorialModelIndex.getCurrentModel();
}
if (!modelInfo) {
modelInfo = TutorialPbrModelIndex.getCurrentModel();
}
if (!modelInfo) {
modelInfo = TutorialFurtherPbrModelIndex.getCurrentModel();
}
if (!modelInfo) {
modelInfo = TutorialFeatureTestModelIndex.getCurrentModel();
}
if (!modelInfo) {
modelInfo = TutorialExtensionTestModelIndex.getCurrentModel();
}
if (!modelInfo) {
document.getElementById('container').innerHTML = 'Please specify a model to load';
throw new Error('Model not specified or not found in list.');
}
var ROTATE = true;
var BOUNDING_BOX = false;
var CUBEMAP = true;
var IBL = true;
var LIGHTS = false; // The default is to use IBL instead of lights
var DEBUG = false;
var VARIANT = "";
var CAMERA = "";
let createScene = function(engine) {
let scene = new BABYLON.Scene(engine);
let mesh;
scene.clearColor = new BABYLON.Color3(1, 1, 1);
let scale = modelInfo.scale;
//let path = "../../sampleModels/" + modelInfo.path;
let path = "../../" + modelInfo.category + "/" + modelInfo.path;
if(modelInfo.url) {
path = modelInfo.url;
}
let base = path.substr(0, path.lastIndexOf("/") + 1);
let file = path.substr(path.lastIndexOf("/") + 1);
// GUI
let gui = new dat.GUI();
let guiRotate = gui.add(window, 'ROTATE').name('Rotate');
let guiBoundingBox = gui.add(window, 'BOUNDING_BOX').name('Bounding Box');
let guiCubeMap = gui.add(window, 'CUBEMAP').name('CubeMap');
let guiIbl = gui.add(window, 'IBL').name('IBL');
let guiLights = gui.add(window, 'LIGHTS').name('Lights');
let guiDebug = gui.add(window, 'DEBUG').name('Debug');
let guiVariants = null;
let guiCameras = null;
let variants = null;
let variantsExtension = null;
BABYLON.SceneLoader.OnPluginActivatedObservable.addOnce(function (loader) {
loader.animationStartMode = modelInfo.allAnimations ? BABYLON.GLTFLoaderAnimationStartMode.ALL : BABYLON.GLTFLoaderAnimationStartMode.FIRST;
loader.onExtensionLoadedObservable.add(function (extension) {
if (extension.name === "KHR_materials_variants") {
variantsExtension = extension;
}
});
});
return BABYLON.SceneLoader.LoadAsync(path).then(function (newScene) {
scene = newScene;
let parentMesh = scene.rootNodes[0];
if ( variantsExtension != null ) {
variants = variantsExtension.getAvailableVariants(parentMesh);
if (variants.length > 0 ) {
VARIANT = modelInfo.variant == undefined ? variants[0] : modelInfo.variant;
let variantNames = variants.reduce(function (allNames, name) {
allNames[name] = name;
return allNames
}, {});
guiVariants = gui.add(window, 'VARIANT', variantNames).name("Variant");
variantsExtension.selectVariant(parentMesh, VARIANT)
guiVariants.onChange(function (value) {
variantsExtension.selectVariant(parentMesh, value);
});
}
}
if ( scene.cameras.length > 0 ) {
scene.cameras.forEach(camera => camera.minZ /= 1000); // TODO: If near is 1, the model is missing, so adjusted
let cameraNames = scene.cameras.map(camera => camera.name).reverse();
guiCameras = gui.add(window, 'CAMERA', cameraNames).name("Camera");
guiCameras.onChange(function (value) {
var camera = scene.cameras.find(function(camera) {
return camera.name === value;
});
scene.activeCamera = camera;
});
}
if ( modelInfo.name == "GearboxAssy" ) {
// TODO: Position adjustment required
parentMesh.position.x += 159.20 * scale;
parentMesh.position.y -= 17.02 * scale;
parentMesh.position.z += 3.21 * scale;
} else if ( modelInfo.name == "Fox" ) {
scene.animationGroups[2].play(true); // 0:Survey, 1:Walk, 2:Run
}
let modelScaling = parentMesh.scaling;
parentMesh.scaling = new BABYLON.Vector3(modelScaling.x * scale, modelScaling.y * scale, modelScaling.z * scale);
let camera = new BABYLON.ArcRotateCamera("camera", 0, 1, 5, BABYLON.Vector3.Zero(), scene);
camera.setPosition( new BABYLON.Vector3(0, 3, -5) );
camera.attachControl(canvas, false, false);
camera.wheelDeltaPercentage = 0.005;
scene.activeCamera = camera;
let light1 = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(0.0, -1.0, 0.5), scene);
let light2 = new BABYLON.DirectionalLight("dir02", new BABYLON.Vector3(-0.5, -0.5, -0.5), scene);
light1.setEnabled(LIGHTS);
light2.setEnabled(LIGHTS);
let environmentTexture;
let skybox;
if (scene.environmentTexture) {
// Models with "EXT_lights_image_based" can supply their own environmentTexture.
environmentTexture = scene.environmentTexture;
let skyboxBlur = 0;
skybox = scene.createDefaultSkybox(environmentTexture.clone(), true,
(scene.activeCamera.maxZ - scene.activeCamera.minZ) / 2, skyboxBlur);
} else {
// Skybox
// Using CubeTexture can reduce the size of the texture, but it will take longer to load because of the need to calculate spherical harmonics.
//cubeTexture = new BABYLON.CubeTexture("../../textures/cube/skybox/", scene, ["px.jpg", "py.jpg", "pz.jpg", "nx.jpg", "ny.jpg", "nz.jpg"]);
//cubeTexture = new BABYLON.CubeTexture("../../textures/papermill/environment/", scene, ["environment_right_0.jpg", "environment_top_0.jpg", "environment_front_0.jpg", "environment_left_0.jpg", "environment_bottom_0.jpg", "environment_back_0.jpg"]);
// If you are worried about performance, you can speed up reading by using a .dds or .env file.
// https://doc.babylonjs.com/how_to/use_hdr_environment
cubeTexture = new BABYLON.CubeTexture("../../textures/env/papermillSpecularHDR.env", scene);
//cubeTexture = new BABYLON.CubeTexture.CreateFromPrefilteredData("../../textures/dds/papermillSpecularHDR.dds", scene);
//cubeTexture.gammaSpace = true;
skybox = scene.createDefaultSkybox(cubeTexture, true, 10000);
/*
// If you care about the performance of createDefaultSkybox(), The following code can be used to avoid this. However, the environmental texture will not be applied.
// http://www.html5gamedevs.com/topic/36997-using-skybox-takes-time-to-display-is-it-a-usage-problem/?tab=comments#comment-211765
let skybox = BABYLON.Mesh.CreateBox("skyBox", 10000, scene);
let skyboxMaterial = new BABYLON.StandardMaterial("skyBoxMaterial", scene);
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.reflectionTexture = cubeTexture;
skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
skyboxMaterial.disableLighting = true;
skybox.material = skyboxMaterial;
*/
environmentTexture = cubeTexture;
}
//scene.debugLayer.show(true, camera);
guiBoundingBox.onChange(function (value) {
scene.forceShowBoundingBoxes = value;
});
guiCubeMap.onChange(function (value) {
skybox.visibility = value ? 1 : 0;
});
guiIbl.onChange(function (value) {
scene.environmentTexture = value ? environmentTexture : null;
});
guiLights.onChange(function (value) {
light1.setEnabled(value);
light2.setEnabled(value);
});
guiDebug.onChange(function (value) {
scene.debugLayer.show({popup: value});
});
engine.runRenderLoop(function() {
scene.activeCamera.alpha += ROTATE ? 0.005 : 0;
scene.render();
});
});
return scene;
};
let canvas = document.querySelector("#renderCanvas");
let engine = new BABYLON.Engine(canvas, true);
engine.enableOfflineSupport = false; // Suppress manifest reference
window.addEventListener('resize', function() {
engine.resize();
});
let scene = createScene(engine);