@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
706 lines (705 loc) • 23.5 kB
JavaScript
"use strict";
import {
UVMapping,
CubeReflectionMapping,
CubeRefractionMapping,
EquirectangularReflectionMapping,
EquirectangularRefractionMapping,
CubeUVReflectionMapping,
RepeatWrapping,
ClampToEdgeWrapping,
MirroredRepeatWrapping,
NearestFilter,
NearestMipmapNearestFilter,
NearestMipmapLinearFilter,
LinearFilter,
LinearMipmapNearestFilter,
LinearMipmapLinearFilter,
InstancedBufferAttribute,
Object3D,
Group,
Color,
InstancedMesh,
Sprite,
Points,
Line,
LineLoop,
LineSegments,
LOD,
Mesh,
SkinnedMesh,
Bone,
Skeleton,
Shape,
Fog,
FogExp2,
HemisphereLight,
SpotLight,
PointLight,
DirectionalLight,
AmbientLight,
RectAreaLight,
LightProbe,
OrthographicCamera,
PerspectiveCamera,
Scene,
CubeTexture,
Texture,
Source,
DataTexture,
ImageLoader,
LoadingManager,
AnimationClip,
MaterialLoader,
LoaderUtils,
BufferGeometryLoader,
Loader,
FileLoader
} from "three";
import * as Geometries from "three/src/geometries/Geometries.js";
import { getTypedArray } from "three/src/utils.js";
class ObjectLoader extends Loader {
constructor(manager) {
super(manager);
}
load(url, onLoad, onProgress, onError) {
const scope = this;
const path = this.path === "" ? LoaderUtils.extractUrlBase(url) : this.path;
this.resourcePath = this.resourcePath || path;
const loader = new FileLoader(this.manager);
loader.setPath(this.path);
loader.setRequestHeader(this.requestHeader);
loader.setWithCredentials(this.withCredentials);
loader.load(
url,
function(text) {
let json = null;
try {
json = JSON.parse(text);
} catch (error) {
if (onError !== void 0)
onError(error);
console.error("THREE:ObjectLoader: Can't parse " + url + ".", error.message);
return;
}
const metadata = json.metadata;
if (metadata === void 0 || metadata.type === void 0 || metadata.type.toLowerCase() === "geometry") {
if (onError !== void 0)
onError(new Error("THREE.ObjectLoader: Can't load " + url));
console.error("THREE.ObjectLoader: Can't load " + url);
return;
}
scope.parse(json, onLoad);
},
onProgress,
onError
);
}
async loadAsync(url, onProgress) {
const scope = this;
const path = this.path === "" ? LoaderUtils.extractUrlBase(url) : this.path;
this.resourcePath = this.resourcePath || path;
const loader = new FileLoader(this.manager);
loader.setPath(this.path);
loader.setRequestHeader(this.requestHeader);
loader.setWithCredentials(this.withCredentials);
const text = await loader.loadAsync(url, onProgress);
const json = JSON.parse(text);
const metadata = json.metadata;
if (metadata === void 0 || metadata.type === void 0 || metadata.type.toLowerCase() === "geometry") {
throw new Error("THREE.ObjectLoader: Can't load " + url);
}
return await scope.parseAsync(json);
}
parse(json, onLoad) {
const animations = this.parseAnimations(json.animations);
const shapes = this.parseShapes(json.shapes);
const geometries = this.parseGeometries(json.geometries, shapes);
const images = this.parseImages(json.images, function() {
if (onLoad !== void 0)
onLoad(object);
});
const textures = this.parseTextures(json.textures, images);
const materials = this.parseMaterials(json.materials, textures);
const object = this.parseObject(json.object, geometries, materials, textures, animations);
const skeletons = this.parseSkeletons(json.skeletons, object);
this.bindSkeletons(object, skeletons);
if (onLoad !== void 0) {
let hasImages = false;
for (const uuid in images) {
if (images[uuid].data instanceof HTMLImageElement) {
hasImages = true;
break;
}
}
if (hasImages === false)
onLoad(object);
}
return object;
}
async parseAsync(json) {
const animations = this.parseAnimations(json.animations);
const shapes = this.parseShapes(json.shapes);
const geometries = this.parseGeometries(json.geometries, shapes);
const images = await this.parseImagesAsync(json.images);
const textures = this.parseTextures(json.textures, images);
const materials = this.parseMaterials(json.materials, textures);
const object = this.parseObject(json.object, geometries, materials, textures, animations);
const skeletons = this.parseSkeletons(json.skeletons, object);
this.bindSkeletons(object, skeletons);
return object;
}
parseShapes(json) {
const shapes = {};
if (json !== void 0) {
for (let i = 0, l = json.length; i < l; i++) {
const shape = new Shape().fromJSON(json[i]);
shapes[shape.uuid] = shape;
}
}
return shapes;
}
parseSkeletons(json, object) {
const skeletons = {};
const bones = {};
object.traverse(function(child) {
if (child.isBone)
bones[child.uuid] = child;
});
if (json !== void 0) {
for (let i = 0, l = json.length; i < l; i++) {
const skeleton = new Skeleton().fromJSON(json[i], bones);
skeletons[skeleton.uuid] = skeleton;
}
}
return skeletons;
}
parseGeometries(json, shapes) {
const geometries = {};
if (json !== void 0) {
const bufferGeometryLoader = new BufferGeometryLoader();
for (let i = 0, l = json.length; i < l; i++) {
let geometry;
const data = json[i];
switch (data.type) {
case "BufferGeometry":
case "InstancedBufferGeometry":
geometry = bufferGeometryLoader.parse(data);
break;
default:
if (data.type in Geometries) {
geometry = Geometries[data.type].fromJSON(data, shapes);
} else {
console.warn(`THREE.ObjectLoader: Unsupported geometry type "${data.type}"`);
}
}
geometry.uuid = data.uuid;
if (data.name !== void 0)
geometry.name = data.name;
if (geometry.isBufferGeometry === true && data.userData !== void 0)
geometry.userData = data.userData;
geometries[data.uuid] = geometry;
}
}
return geometries;
}
parseMaterials(json, textures) {
const cache = {};
const materials = {};
if (json !== void 0) {
const loader = new MaterialLoader();
loader.setTextures(textures);
for (let i = 0, l = json.length; i < l; i++) {
const data = json[i];
if (cache[data.uuid] === void 0) {
cache[data.uuid] = loader.parse(data);
}
materials[data.uuid] = cache[data.uuid];
}
}
return materials;
}
parseAnimations(json) {
const animations = {};
if (json !== void 0) {
for (let i = 0; i < json.length; i++) {
const data = json[i];
const clip = AnimationClip.parse(data);
animations[clip.uuid] = clip;
}
}
return animations;
}
parseImages(json, onLoad) {
const scope = this;
const images = {};
let loader;
function loadImage(url) {
scope.manager.itemStart(url);
return loader.load(
url,
function() {
scope.manager.itemEnd(url);
},
void 0,
function() {
scope.manager.itemError(url);
scope.manager.itemEnd(url);
}
);
}
function deserializeImage(image) {
if (typeof image === "string") {
const url = image;
const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test(url) ? url : scope.resourcePath + url;
return loadImage(path);
} else {
if (image.data) {
return {
data: getTypedArray(image.type, image.data),
width: image.width,
height: image.height
};
} else {
return null;
}
}
}
if (json !== void 0 && json.length > 0) {
const manager = new LoadingManager(onLoad);
loader = new ImageLoader(manager);
loader.setCrossOrigin(this.crossOrigin);
for (let i = 0, il = json.length; i < il; i++) {
const image = json[i];
const url = image.url;
if (Array.isArray(url)) {
const imageArray = [];
for (let j = 0, jl = url.length; j < jl; j++) {
const currentUrl = url[j];
const deserializedImage = deserializeImage(currentUrl);
if (deserializedImage !== null) {
if (deserializedImage instanceof HTMLImageElement) {
imageArray.push(deserializedImage);
} else {
imageArray.push(
new DataTexture(
deserializedImage.data,
deserializedImage.width,
deserializedImage.height
)
);
}
}
}
images[image.uuid] = new Source(imageArray);
} else {
const deserializedImage = deserializeImage(image.url);
images[image.uuid] = new Source(deserializedImage);
}
}
}
return images;
}
async parseImagesAsync(json) {
const scope = this;
const images = {};
let loader;
async function deserializeImage(image) {
if (typeof image === "string") {
const url = image;
const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test(url) ? url : scope.resourcePath + url;
return await loader.loadAsync(path);
} else {
if (image.data) {
return {
data: getTypedArray(image.type, image.data),
width: image.width,
height: image.height
};
} else {
return null;
}
}
}
if (json !== void 0 && json.length > 0) {
loader = new ImageLoader(this.manager);
loader.setCrossOrigin(this.crossOrigin);
for (let i = 0, il = json.length; i < il; i++) {
const image = json[i];
const url = image.url;
if (Array.isArray(url)) {
const imageArray = [];
for (let j = 0, jl = url.length; j < jl; j++) {
const currentUrl = url[j];
const deserializedImage = await deserializeImage(currentUrl);
if (deserializedImage !== null) {
if (deserializedImage instanceof HTMLImageElement) {
imageArray.push(deserializedImage);
} else {
imageArray.push(
new DataTexture(
deserializedImage.data,
deserializedImage.width,
deserializedImage.height
)
);
}
}
}
images[image.uuid] = new Source(imageArray);
} else {
const deserializedImage = await deserializeImage(image.url);
images[image.uuid] = new Source(deserializedImage);
}
}
}
return images;
}
parseTextures(json, images) {
function parseConstant(value, type) {
if (typeof value === "number")
return value;
console.warn("THREE.ObjectLoader.parseTexture: Constant should be in numeric form.", value);
return type[value];
}
const textures = {};
if (json !== void 0) {
for (let i = 0, l = json.length; i < l; i++) {
const data = json[i];
if (data.image === void 0) {
console.warn('THREE.ObjectLoader: No "image" specified for', data.uuid);
}
if (images[data.image] === void 0) {
console.warn("THREE.ObjectLoader: Undefined image", data.image);
}
const source = images[data.image];
const image = source.data;
let texture;
if (Array.isArray(image)) {
texture = new CubeTexture();
if (image.length === 6)
texture.needsUpdate = true;
} else {
if (image && image.data) {
texture = new DataTexture();
} else {
texture = new Texture();
}
if (image)
texture.needsUpdate = true;
}
texture.source = source;
texture.uuid = data.uuid;
if (data.name !== void 0)
texture.name = data.name;
if (data.mapping !== void 0)
texture.mapping = parseConstant(data.mapping, TEXTURE_MAPPING);
if (data.offset !== void 0)
texture.offset.fromArray(data.offset);
if (data.repeat !== void 0)
texture.repeat.fromArray(data.repeat);
if (data.center !== void 0)
texture.center.fromArray(data.center);
if (data.rotation !== void 0)
texture.rotation = data.rotation;
if (data.wrap !== void 0) {
texture.wrapS = parseConstant(data.wrap[0], TEXTURE_WRAPPING);
texture.wrapT = parseConstant(data.wrap[1], TEXTURE_WRAPPING);
}
if (data.format !== void 0)
texture.format = data.format;
if (data.type !== void 0)
texture.type = data.type;
if (data.encoding !== void 0)
texture.encoding = data.encoding;
if (data.minFilter !== void 0)
texture.minFilter = parseConstant(data.minFilter, TEXTURE_FILTER);
if (data.magFilter !== void 0)
texture.magFilter = parseConstant(data.magFilter, TEXTURE_FILTER);
if (data.anisotropy !== void 0)
texture.anisotropy = data.anisotropy;
if (data.flipY !== void 0)
texture.flipY = data.flipY;
if (data.premultiplyAlpha !== void 0)
texture.premultiplyAlpha = data.premultiplyAlpha;
if (data.unpackAlignment !== void 0)
texture.unpackAlignment = data.unpackAlignment;
if (data.userData !== void 0)
texture.userData = data.userData;
textures[data.uuid] = texture;
}
}
return textures;
}
parseObject(data, geometries, materials, textures, animations) {
let object;
function getGeometry(name) {
if (geometries[name] === void 0) {
console.warn("THREE.ObjectLoader: Undefined geometry", name);
}
return geometries[name];
}
function getMaterial(name) {
if (name === void 0)
return void 0;
if (Array.isArray(name)) {
const array = [];
for (let i = 0, l = name.length; i < l; i++) {
const uuid = name[i];
if (materials[uuid] === void 0) {
console.warn("THREE.ObjectLoader: Undefined material", uuid);
}
array.push(materials[uuid]);
}
return array;
}
if (materials[name] === void 0) {
console.warn("THREE.ObjectLoader: Undefined material", name);
}
return materials[name];
}
function getTexture(uuid) {
if (textures[uuid] === void 0) {
console.warn("THREE.ObjectLoader: Undefined texture", uuid);
}
return textures[uuid];
}
let geometry, material;
switch (data.type) {
case "Scene":
object = new Scene();
if (data.background !== void 0) {
if (Number.isInteger(data.background)) {
object.background = new Color(data.background);
} else {
object.background = getTexture(data.background);
}
}
if (data.environment !== void 0) {
object.environment = getTexture(data.environment);
}
if (data.fog !== void 0) {
if (data.fog.type === "Fog") {
object.fog = new Fog(data.fog.color, data.fog.near, data.fog.far);
} else if (data.fog.type === "FogExp2") {
object.fog = new FogExp2(data.fog.color, data.fog.density);
}
}
if (data.backgroundBlurriness !== void 0)
object.backgroundBlurriness = data.backgroundBlurriness;
break;
case "PerspectiveCamera":
object = new PerspectiveCamera(data.fov, data.aspect, data.near, data.far);
if (data.focus !== void 0)
object.focus = data.focus;
if (data.zoom !== void 0)
object.zoom = data.zoom;
if (data.filmGauge !== void 0)
object.filmGauge = data.filmGauge;
if (data.filmOffset !== void 0)
object.filmOffset = data.filmOffset;
if (data.view !== void 0)
object.view = Object.assign({}, data.view);
break;
case "OrthographicCamera":
object = new OrthographicCamera(data.left, data.right, data.top, data.bottom, data.near, data.far);
if (data.zoom !== void 0)
object.zoom = data.zoom;
if (data.view !== void 0)
object.view = Object.assign({}, data.view);
break;
case "AmbientLight":
object = new AmbientLight(data.color, data.intensity);
break;
case "DirectionalLight":
object = new DirectionalLight(data.color, data.intensity);
break;
case "PointLight":
object = new PointLight(data.color, data.intensity, data.distance, data.decay);
break;
case "RectAreaLight":
object = new RectAreaLight(data.color, data.intensity, data.width, data.height);
break;
case "SpotLight":
object = new SpotLight(
data.color,
data.intensity,
data.distance,
data.angle,
data.penumbra,
data.decay
);
break;
case "HemisphereLight":
object = new HemisphereLight(data.color, data.groundColor, data.intensity);
break;
case "LightProbe":
object = new LightProbe().fromJSON(data);
break;
case "SkinnedMesh":
geometry = getGeometry(data.geometry);
material = getMaterial(data.material);
object = new SkinnedMesh(geometry, material);
if (data.bindMode !== void 0)
object.bindMode = data.bindMode;
if (data.bindMatrix !== void 0)
object.bindMatrix.fromArray(data.bindMatrix);
if (data.skeleton !== void 0)
object.skeleton = data.skeleton;
break;
case "Mesh":
geometry = getGeometry(data.geometry);
material = getMaterial(data.material);
object = new Mesh(geometry, material);
break;
case "InstancedMesh":
geometry = getGeometry(data.geometry);
material = getMaterial(data.material);
const count = data.count;
const instanceMatrix = data.instanceMatrix;
const instanceColor = data.instanceColor;
object = new InstancedMesh(geometry, material, count);
object.instanceMatrix = new InstancedBufferAttribute(new Float32Array(instanceMatrix.array), 16);
if (instanceColor !== void 0)
object.instanceColor = new InstancedBufferAttribute(
new Float32Array(instanceColor.array),
instanceColor.itemSize
);
break;
case "LOD":
object = new LOD();
break;
case "Line":
object = new Line(getGeometry(data.geometry), getMaterial(data.material));
break;
case "LineLoop":
object = new LineLoop(getGeometry(data.geometry), getMaterial(data.material));
break;
case "LineSegments":
object = new LineSegments(getGeometry(data.geometry), getMaterial(data.material));
break;
case "PointCloud":
case "Points":
object = new Points(getGeometry(data.geometry), getMaterial(data.material));
break;
case "Sprite":
object = new Sprite(getMaterial(data.material));
break;
case "Group":
object = new Group();
break;
case "Bone":
object = new Bone();
break;
default:
object = new Object3D();
}
object.uuid = data.uuid;
if (data.name !== void 0)
object.name = data.name;
if (data.matrix !== void 0) {
object.matrix.fromArray(data.matrix);
if (data.matrixAutoUpdate !== void 0)
object.matrixAutoUpdate = data.matrixAutoUpdate;
if (object.matrixAutoUpdate)
object.matrix.decompose(object.position, object.quaternion, object.scale);
} else {
if (data.position !== void 0)
object.position.fromArray(data.position);
if (data.rotation !== void 0)
object.rotation.fromArray(data.rotation);
if (data.quaternion !== void 0)
object.quaternion.fromArray(data.quaternion);
if (data.scale !== void 0)
object.scale.fromArray(data.scale);
}
if (data.castShadow !== void 0)
object.castShadow = data.castShadow;
if (data.receiveShadow !== void 0)
object.receiveShadow = data.receiveShadow;
if (data.shadow) {
if (data.shadow.bias !== void 0)
object.shadow.bias = data.shadow.bias;
if (data.shadow.normalBias !== void 0)
object.shadow.normalBias = data.shadow.normalBias;
if (data.shadow.radius !== void 0)
object.shadow.radius = data.shadow.radius;
if (data.shadow.mapSize !== void 0)
object.shadow.mapSize.fromArray(data.shadow.mapSize);
if (data.shadow.camera !== void 0)
object.shadow.camera = this.parseObject(data.shadow.camera);
}
if (data.visible !== void 0)
object.visible = data.visible;
if (data.frustumCulled !== void 0)
object.frustumCulled = data.frustumCulled;
if (data.renderOrder !== void 0)
object.renderOrder = data.renderOrder;
if (data.userData !== void 0)
object.userData = data.userData;
if (data.layers !== void 0)
object.layers.mask = data.layers;
if (data.children !== void 0) {
const children = data.children;
for (let i = 0; i < children.length; i++) {
object.add(this.parseObject(children[i], geometries, materials, textures, animations));
}
}
if (data.animations !== void 0) {
const objectAnimations = data.animations;
for (let i = 0; i < objectAnimations.length; i++) {
const uuid = objectAnimations[i];
object.animations.push(animations[uuid]);
}
}
if (data.type === "LOD") {
if (data.autoUpdate !== void 0)
object.autoUpdate = data.autoUpdate;
const levels = data.levels;
for (let l = 0; l < levels.length; l++) {
const level = levels[l];
const child = object.getObjectByProperty("uuid", level.object);
if (child !== void 0) {
object.addLevel(child, level.distance);
}
}
}
return object;
}
bindSkeletons(object, skeletons) {
if (Object.keys(skeletons).length === 0)
return;
object.traverse(function(child) {
if (child.isSkinnedMesh === true && child.skeleton !== void 0) {
const skeleton = skeletons[child.skeleton];
if (skeleton === void 0) {
console.warn("THREE.ObjectLoader: No skeleton found with UUID:", child.skeleton);
} else {
child.bind(skeleton, child.bindMatrix);
}
}
});
}
}
const TEXTURE_MAPPING = {
UVMapping,
CubeReflectionMapping,
CubeRefractionMapping,
EquirectangularReflectionMapping,
EquirectangularRefractionMapping,
CubeUVReflectionMapping
};
const TEXTURE_WRAPPING = {
RepeatWrapping,
ClampToEdgeWrapping,
MirroredRepeatWrapping
};
const TEXTURE_FILTER = {
NearestFilter,
NearestMipmapNearestFilter,
NearestMipmapLinearFilter,
LinearFilter,
LinearMipmapNearestFilter,
LinearMipmapLinearFilter
};
export { ObjectLoader };