forked from LasseD/buildinginstructions.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_part_standard.htm
177 lines (153 loc) · 6.58 KB
/
sample_part_standard.htm
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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>Sample render of a part where part files are found in the official and unofficial folders, and LDConfig.ldr is used to determine colors</title>
<meta name="viewport" content="width=device-width" />
</head>
<body>
<!-- jquery is used for the early document ready loading. This is used for loading the rendering components after the ldconfig.ldr file has been parsed -->
<script src="js/jquery.min.js"></script>
<!-- Common libraries from three.js. OrbitControls.js is used for user input -->
<script src="js/three.min.js"></script>
<script src="js/OrbitControls.js"></script>
<!-- LDConfig.js can be used instead of colors.js to load colors from ldconfig.ldr -->
<script src="js/LDConfig.js"></script>
<script src="js/LDROptions.js"></script>
<script src="js/LDRShaders.js"></script>
<script src="js/LDRColorMaterials.js"></script>
<script src="js/LDRGeometries.js"></script>
<script src="js/LDRMeasurer.js"></script>
<script src="js/ClientStorage.js"></script>
<script src="js/LDRLoader.js"></script>
<script src="js/LDRGenerator.js"></script>
<script src="js/LDRStuds.js"></script>
<script src="js/StudioTexmap.js"></script>
<script>
let partID = '44375bps0.dat'; // Set the part to load here.
//let partID = 'stud-logo.dat';
//let partID = '18675ps2.dat';
let color = 40;
// Set up camera:
var camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 1000000);
// Set up scene:
var scene = new THREE.Scene();
var baseObject;
scene.background = new THREE.Color(0xFFFFFF); // Set the background color of the renderer here!
// Set up renderer:
var renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setPixelRatio(window.devicePixelRatio);
function render() {
renderer.render(scene, camera);
}
document.body.appendChild(renderer.domElement);
function onWindowResize(){
camera.left = -window.innerWidth;
camera.right = window.innerWidth;
camera.top = window.innerHeight;
camera.bottom = -window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
render();
}
// React to user input:
var controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', render);
window.addEventListener('resize', onWindowResize, false);
// Three.js loader for LDraw models:
var ldrLoader;
let mc;
var onLoad = function() { // Notice that onLoad might be called more than once.
if(mc) {
onWindowResize();
return;
}
if(!ldrLoader.mainModel) {
ldrLoader.mainModel = partID;
}
var pt = ldrLoader.getMainModel();
// Place model in scene:
var origo = new THREE.Vector3();
var inv = new THREE.Matrix3();
inv.set(1,0,0, 0,-1,0, 0,0,-1); // Invert Y, and Z-axis for LDraw
// Set up objects for proper rendering order (when parts contain both transparent and opaque parts, the transparent parts should be rendered last:
if(baseObject) {
scene.remove(baseObject); // Remove existing geometries in case the user calls load more than once.
}
baseObject = new THREE.Group()
let opaqueObject = new THREE.Group(), sixteenObject = new THREE.Group(); transObject = new THREE.Group();
baseObject.add(opaqueObject);
baseObject.add(sixteenObject);
baseObject.add(transObject);
scene.add(baseObject);
mc = new LDR.MeshCollector(opaqueObject, sixteenObject, transObject);
// Generate the mesh:
pt.generateThreePart(ldrLoader, color, origo, inv, true, false, mc);
// Find center of drawn model:
let b = mc.boundingBox;
let elementCenter = new THREE.Vector3();
b.getCenter(elementCenter);
baseObject.position.set(-elementCenter.x, -elementCenter.y, -elementCenter.z);
let size = new THREE.Vector3();
b.getSize(size);
size = Math.sqrt(size.x*size.x + size.y*size.y + size.z*size.z);
camera.position.set(size*10/7, size, size*10/7);
camera.lookAt(new THREE.Vector3());
let measurer = new LDR.Measurer(camera); // Used for making the shown part fill the renderer.
let [dx,dy] = measurer.measure(b, baseObject.matrixWorld);
camera.zoom = 1.6*Math.min(window.innerWidth/dx, window.innerHeight/dy); // 1.6 to make the part not fill the full element. Use 2.0 to max it out.
onWindowResize();
}
// The following function makes the ldrLoader search through the official
// and unofficial file structures (unofficial/ and unofficial/) for parts.
const OFFICIAL_DIR = 'official', UNOFFICIAL_DIR = 'unofficial';
var idToUrl = function(id) {
if(id.startsWith('48/') || id.startsWith('8/')) {
return [OFFICIAL_DIR + '/p/' + id,
UNOFFICIAL_DIR + '/p/' + id];
}
else if(id.startsWith('s/')) {
return [OFFICIAL_DIR + '/parts/' + id,
UNOFFICIAL_DIR + '/parts/' + id];
}
else if(id.match(/[a-z][a-z].*\.dat/)) {
return [OFFICIAL_DIR + '/p/' + id,
OFFICIAL_DIR + '/parts/' + id,
UNOFFICIAL_DIR + '/p/' + id,
UNOFFICIAL_DIR + '/parts/' + id];
}
else {
return [OFFICIAL_DIR + '/parts/' + id,
OFFICIAL_DIR + '/p/' + id,
UNOFFICIAL_DIR + '/parts/' + id,
UNOFFICIAL_DIR + '/p/' + id];
}
}
// The section below ensures studs are shown:
LDR.Studs.makeGenerators('', 0, 1);
let storage = {
retrievePartsFromStorage: (loader, toBeFetched, onDone) => {
let retrieved = [];
function attemptGeneratorFirst(id) {
let pt = LDR.Generator.make(id);
if(pt) {
loader.partTypes[id] = pt;
pt.steps.forEach(step => step.subModels.forEach(sm => attemptGeneratorFirst(sm.ID)));
retrieved.push(id);
}
else {
loader.load(id);
}
}
toBeFetched.forEach(attemptGeneratorFirst);
onDone(retrieved);
}
};
ldrLoader = new THREE.LDRLoader(onLoad, storage, {idToUrl:idToUrl});
function onColorsLoaded() {
ldrLoader.load(partID);
}
$(document).ready(() => LDR.Colors.load(onColorsLoaded));
</script>
</body>
</html>