-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
166 lines (132 loc) · 4.62 KB
/
init.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
/*
Author: <Anthony Sychev> (hello at dm211 dot com | a.sychev at jfranc dot studio)
Buy me a coffe: https://www.buymeacoffee.com/twooneone
init.js (c) 2022
Created: 2022-04-07 01:16:02
Desc: description
*/
import * as THREE from "three";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass";
//https://github.com/mrdoob/three.js/blob/master/examples/jsm/loaders/GLTFLoader.js
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader";
//HDR
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader";
import { KTX2Loader } from "three/examples/jsm/loaders/KTX2Loader";
import { MeshoptDecoder } from "three/examples/jsm/libs/meshopt_decoder.module";
import { BasisTextureLoader } from "three/examples/jsm/loaders/BasisTextureLoader";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
export default class init {
static instance;
constructor(options = {}) {
//this.fileToLoad = "static/DamagedHelmetEmmbed.gltf";
//this.fileToLoad = "static/DamagedHelmetDraco.gltf";
//this.fileToLoad = "static/DamagedHelmetMeshopt.gltf";
//this.fileToLoad = "static/DamagedHelmetDracoUltra.gltf";
//this.fileToLoad = "static/DamagedHelmetKTX-etc1s.gltf";
this.fileToLoad = "static/DamagedHelmetKTX-uastc.gltf";
this.targetElement = options.targetElement;
this.config = {};
// Width and height
const boundings = this.targetElement.getBoundingClientRect();
this.config.width = boundings.width || window.innerWidth;
this.config.height = boundings.height || window.innerHeight;
this.scene = new THREE.Scene();
//camera
this.camera = new THREE.PerspectiveCamera(
25,
this.config.width / this.config.height,
0.1,
150
);
this.camera.rotation.reorder("YXZ");
this.camera.position.set(2, 2, 5);
this.scene.add(this.camera);
//render
this.clearColor = "#cccccc";
this.renderer = new THREE.WebGLRenderer({
alpha: false,
antialias: true,
});
this.renderer.setClearColor(this.clearColor, 1);
this.renderer.setSize(this.config.width, this.config.height);
this.renderer.setPixelRatio(1);
this.context = this.renderer.getContext();
this.targetElement.appendChild(this.renderer.domElement);
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
this.renderPass = new RenderPass(this.scene, this.camera);
const RenderTargetClass = THREE.WebGLRenderTarget;
// const RenderTargetClass = THREE.WebGLRenderTarget
this.renderTarget = new RenderTargetClass(
this.config.width,
this.config.height,
{
generateMipmaps: false,
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
format: THREE.RGBFormat,
encoding: THREE.sRGBEncoding,
}
);
this.composer = new EffectComposer(this.renderer, this.renderTarget);
this.composer.setSize(this.config.width, this.config.height);
this.composer.setPixelRatio(this.config.pixelRatio);
this.composer.addPass(this.renderPass);
//resources
this.loadAssets();
//lights
const hemiLight = new THREE.HemisphereLight();
this.scene.add(hemiLight);
window.scene = this.scene;
this.update();
}
update() {
this.camera.updateMatrixWorld();
this.composer.render();
window.requestAnimationFrame(() => {
this.update();
});
}
loadAssets() {
//NOTE: if you use loader manager add header mime type
//loadingManager.addHandler(/\.basis$/i, basisLoader);
//THREE.DefaultLoadingManager.addHandler( /\.basis$/, basisLoader );
/*
//This loader is Depricated
const basisLoader = new BasisTextureLoader();
basisLoader.setTranscoderPath("static/basis/");
basisLoader.detectSupport(this.renderer);
*/
/*
basisLoader.load("static/texture.basis", (data) =>
{
console.log(data);
})
*/
// Draco
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath("static/draco/");
dracoLoader.setDecoderConfig({ type: "js" });
/*
dracoLoader.load(_resource.source, (_data) =>
{
this.fileLoadEnd(_resource, _data)
DRACOLoader.releaseDecoderModule()
})
*/
// KTX2
const ktxLoader = new KTX2Loader();
ktxLoader.setTranscoderPath("static/basis/");
ktxLoader.detectSupport(this.renderer);
// GLTF
const gltfLoader = new GLTFLoader();
gltfLoader.setCrossOrigin("anonymous");
gltfLoader.setDRACOLoader(dracoLoader);
gltfLoader.setKTX2Loader(ktxLoader);
gltfLoader.setMeshoptDecoder(MeshoptDecoder);
gltfLoader.load(this.fileToLoad, (data) => {
this.scene.add(data.scene);
});
}
}