-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmodel.js
151 lines (122 loc) · 4.79 KB
/
model.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
import * as THREE from 'https://cdn.skypack.dev/[email protected]/build/three.module.js'
import { OrbitControls } from 'https://cdn.skypack.dev/[email protected]/examples/jsm/controls/OrbitControls.js';
import { GLTFLoader } from 'https://cdn.skypack.dev/[email protected]/examples/jsm/loaders/GLTFLoader.js';
window.model = {
initialize3D: () => { initialize3D(); },
initializeEditor: () => { initializeEditor(); },
loadModel: (glb) => { loadModel(glb) },
};
const scene = new THREE.Scene();
var gltfScene = null;
var editor = null;
var directionalLight = null;
var renderer = null;
function loadModel(glb) {
const contentArray = Blazor.platform.toUint8Array(glb);
const blob = new Blob([new Uint8Array(contentArray)], { type: "application/octet-stream" });
const blobUrl = URL.createObjectURL(blob);
const loader = new GLTFLoader();
loader.load(
blobUrl,
function (gltf) {
if (gltfScene != null) {
scene.remove(gltfScene);
}
gltf.scene.traverse((o) => {
if (o instanceof THREE.Mesh) {
o.castShadow = true;
o.receiveShadow = true;
}
})
scene.add(gltf.scene);
gltfScene = gltf.scene;
URL.revokeObjectURL(blobUrl);
return true;
},
function (xhr) {
console.log((xhr.loaded / xhr.total * 100) + '% loaded');
},
function (error) {
console.log('An error happened');
console.log(error)
}
);
}
function initializeEditor() {
ace.config.set("packaged", true)
ace.config.set("basePath", "https://pagecdn.io/lib/ace/1.4.12/")
ace.require("ace/ext/language_tools");
editor = ace.edit("editor");
editor.setTheme("ace/theme/tomorrow");
editor.session.setMode("ace/mode/csharp");
editor.setOptions({
fontSize: "10pt",
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: true
});
editor.getSession().on('change', function () {
var code = editor.getValue();
DotNet.invokeMethod('Elements.Playground', 'SetCodeValue', code)
});
}
function initialize3D() {
const div = document.getElementById("model");
const camera = new THREE.PerspectiveCamera(75, div.clientWidth / div.clientHeight, 0.1, 300);
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap
renderer.physicallyCorrectLights = true;
renderer.outputEncoding = THREE.sRGBEncoding
renderer.toneMappingExposure = 0.9
renderer.setSize(div.clientWidth, div.clientHeight);
div.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
directionalLight = new THREE.DirectionalLight(0xffffff, 0.8 * Math.PI);
directionalLight.position.set(-2, 10, 0);
directionalLight.castShadow = true; // default false
// directionalLight.shadow.radius = 12
scene.add(directionalLight);
var side = 30
directionalLight.shadow.mapSize.width = 4096;
directionalLight.shadow.mapSize.height = 4096;
// directionalLight.shadow.camera.near = 1;
// directionalLight.shadow.camera.far = 20;
directionalLight.shadow.camera.left = -side
directionalLight.shadow.camera.right = side
directionalLight.shadow.camera.top = side
directionalLight.shadow.camera.bottom = -side
directionalLight.shadow.bias = -0.00001 // -r / 2000000.0
// const size = 100;
// const divisions = 20;
// const gridHelper = new THREE.GridHelper(size, divisions, "darkgray", "lightgray");
// scene.add(gridHelper);
// const helper = new THREE.CameraHelper(directionalLight.shadow.camera);
// scene.add(helper);
// Create a shadow plane
var shadowMaterial = new THREE.ShadowMaterial();
shadowMaterial.opacity = 0.5;
const planeGeometry = new THREE.PlaneGeometry(50, 50, 32, 32);
const plane = new THREE.Mesh(planeGeometry, shadowMaterial);
plane.rotateX(-Math.PI / 2);
plane.receiveShadow = true;
scene.add(plane);
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
const light = new THREE.AmbientLight(0x404040); // soft white light
scene.add(light);
camera.position.z = 5;
camera.position.y = 10;
controls.update();
const animate = function () {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
};
window.addEventListener('resize', () => {
camera.aspect = div.clientWidth / div.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(div.clientWidth, div.clientHeight);
}, false);
animate();
}