textures, animations
This commit is contained in:
parent
010cf6534b
commit
4d5ef5fa63
@ -69,6 +69,13 @@
|
|||||||
|
|
||||||
for (let [key, value] of Object.entries(data.materials)) {
|
for (let [key, value] of Object.entries(data.materials)) {
|
||||||
materials[key] = new materialTypes[value.type](value.options);
|
materials[key] = new materialTypes[value.type](value.options);
|
||||||
|
if (value.texture) {
|
||||||
|
materials[key].map = new THREE.TextureLoader().load(value.texture.path);
|
||||||
|
if (value.texture.repeat == true) {
|
||||||
|
materials[key].map.wrapS = THREE.RepeatWrapping;
|
||||||
|
materials[key].map.wrapT = THREE.RepeatWrapping;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createObject(data, name) {
|
function createObject(data, name) {
|
||||||
@ -139,6 +146,7 @@
|
|||||||
if (data.scale) object.scale.set(...data.scale);
|
if (data.scale) object.scale.set(...data.scale);
|
||||||
if (data.castShadow) object.castShadow = data.castShadow;
|
if (data.castShadow) object.castShadow = data.castShadow;
|
||||||
if (data.receiveShadow) object.receiveShadow = data.receiveShadow;
|
if (data.receiveShadow) object.receiveShadow = data.receiveShadow;
|
||||||
|
if (data.animation) object.animation = data.animation;
|
||||||
|
|
||||||
// add children to parent
|
// add children to parent
|
||||||
if (data.children) {
|
if (data.children) {
|
||||||
@ -166,7 +174,7 @@
|
|||||||
|
|
||||||
const loop = true;
|
const loop = true;
|
||||||
|
|
||||||
let canvas, renderer, scene, camera, controls;
|
let canvas, renderer, scene, camera, controls, clock;
|
||||||
|
|
||||||
init().then(loop && animate());
|
init().then(loop && animate());
|
||||||
|
|
||||||
@ -184,6 +192,8 @@
|
|||||||
controls.rotateSpeed = rotateSpeed;
|
controls.rotateSpeed = rotateSpeed;
|
||||||
controls.panSpeed = panSpeed;
|
controls.panSpeed = panSpeed;
|
||||||
|
|
||||||
|
clock = new THREE.Clock();
|
||||||
|
|
||||||
scene = await fetch("scene.json")
|
scene = await fetch("scene.json")
|
||||||
.then((response) => response.json())
|
.then((response) => response.json())
|
||||||
.catch(reason => console.log(`fetch failed: ${reason}`))
|
.catch(reason => console.log(`fetch failed: ${reason}`))
|
||||||
@ -207,6 +217,19 @@
|
|||||||
function animate() {
|
function animate() {
|
||||||
requestAnimationFrame(animate);
|
requestAnimationFrame(animate);
|
||||||
controls.update();
|
controls.update();
|
||||||
|
|
||||||
|
let delta = clock.getDelta();
|
||||||
|
let time = clock.getElapsedTime();
|
||||||
|
|
||||||
|
scene.traverse((element)=>{
|
||||||
|
if (element.animation) {
|
||||||
|
(() => {
|
||||||
|
eval(element.animation);
|
||||||
|
})
|
||||||
|
.call({ element: element, delta: delta, time: time });
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
render();
|
render();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
38
scene.json
38
scene.json
@ -145,7 +145,18 @@
|
|||||||
"options": {
|
"options": {
|
||||||
"color": "rgb(94, 85, 56)",
|
"color": "rgb(94, 85, 56)",
|
||||||
"roughness": 0.8
|
"roughness": 0.8
|
||||||
}
|
},
|
||||||
|
"texture": {
|
||||||
|
"path": "textures/mud.jpg",
|
||||||
|
"repeat": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"moon": {
|
||||||
|
"type": "MeshBasicMaterial",
|
||||||
|
"options": { },
|
||||||
|
"texture": {
|
||||||
|
"path": "textures/moon.jpg"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"objects": {
|
"objects": {
|
||||||
@ -163,7 +174,8 @@
|
|||||||
10,
|
10,
|
||||||
10
|
10
|
||||||
],
|
],
|
||||||
"castShadow": true
|
"castShadow": true,
|
||||||
|
"animation": "element.rotation.y += delta"
|
||||||
},
|
},
|
||||||
"batman": {
|
"batman": {
|
||||||
"children": {
|
"children": {
|
||||||
@ -173,6 +185,7 @@
|
|||||||
"material": "black",
|
"material": "black",
|
||||||
"children": {
|
"children": {
|
||||||
"r_thigh": {
|
"r_thigh": {
|
||||||
|
"animation": "element.rotation.x = Math.sin(time*10)",
|
||||||
"castShadow": true,
|
"castShadow": true,
|
||||||
"geometry": "thigh",
|
"geometry": "thigh",
|
||||||
"material": "black",
|
"material": "black",
|
||||||
@ -222,6 +235,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"l_thigh": {
|
"l_thigh": {
|
||||||
|
"animation": "element.rotation.x = -Math.sin(time*10)",
|
||||||
"castShadow": true,
|
"castShadow": true,
|
||||||
"geometry": "thigh",
|
"geometry": "thigh",
|
||||||
"material": "black",
|
"material": "black",
|
||||||
@ -274,6 +288,7 @@
|
|||||||
"type": "empty",
|
"type": "empty",
|
||||||
"children": {
|
"children": {
|
||||||
"r_shoulder": {
|
"r_shoulder": {
|
||||||
|
"animation": "element.rotation.x = -Math.sin(time*10)",
|
||||||
"castShadow": true,
|
"castShadow": true,
|
||||||
"geometry": "arm_joint",
|
"geometry": "arm_joint",
|
||||||
"material": "black",
|
"material": "black",
|
||||||
@ -372,6 +387,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"l_shoulder": {
|
"l_shoulder": {
|
||||||
|
"animation": "element.rotation.x = Math.sin(time*10)",
|
||||||
"castShadow": true,
|
"castShadow": true,
|
||||||
"geometry": "arm_joint",
|
"geometry": "arm_joint",
|
||||||
"material": "black",
|
"material": "black",
|
||||||
@ -577,6 +593,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ground": {
|
"ground": {
|
||||||
|
"animation": "element.material.map.offset.y -= delta",
|
||||||
"geometry": "ground",
|
"geometry": "ground",
|
||||||
"material": "ground",
|
"material": "ground",
|
||||||
"rotation": [
|
"rotation": [
|
||||||
@ -603,6 +620,23 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"moon": {
|
||||||
|
"geometry": "ground",
|
||||||
|
"material": "moon",
|
||||||
|
"scale": [
|
||||||
|
0.7,
|
||||||
|
0.7,
|
||||||
|
0.7
|
||||||
|
],
|
||||||
|
"position": [
|
||||||
|
300,
|
||||||
|
300,
|
||||||
|
-300
|
||||||
|
],
|
||||||
|
"rotation": [
|
||||||
|
45, -45, 0
|
||||||
|
]
|
||||||
|
},
|
||||||
"lights": {
|
"lights": {
|
||||||
"children": {
|
"children": {
|
||||||
"front_light": {
|
"front_light": {
|
||||||
|
BIN
textures/moon.jpg
Normal file
BIN
textures/moon.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 MiB |
BIN
textures/mud.jpg
Normal file
BIN
textures/mud.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 28 KiB |
Loading…
Reference in New Issue
Block a user