2
0
Эх сурвалжийг харах

Core: allow tree-shaking again for materials (#24094)

* Core: Replace Materia.fromType with MaterialLoader.createMaterialFromType

* Remove unused import

* Add missing materialLib

* Add missing import

* Fix import

* Add missing SpriteNodeMaterial
Marco Fugaro 3 жил өмнө
parent
commit
bff54c419b

+ 31 - 0
examples/jsm/nodes/loaders/NodeMaterialLoader.js

@@ -1,4 +1,35 @@
 import { MaterialLoader } from 'three';
+import {
+	NodeMaterial,
+	LineBasicNodeMaterial,
+	MeshBasicNodeMaterial,
+	MeshStandardNodeMaterial,
+	PointsNodeMaterial,
+	SpriteNodeMaterial
+} from '../materials/Materials.js';
+
+const superFromTypeFunction = MaterialLoader.createMaterialFromType;
+
+MaterialLoader.createMaterialFromType = function ( type ) {
+
+	const materialLib = {
+		NodeMaterial,
+		LineBasicNodeMaterial,
+		MeshBasicNodeMaterial,
+		MeshStandardNodeMaterial,
+		PointsNodeMaterial,
+		SpriteNodeMaterial,
+	};
+
+	if ( materialLib[ type ] !== undefined ) {
+
+		return new materialLib[ type ]();
+
+	}
+
+	return superFromTypeFunction.call( this, type );
+
+};
 
 class NodeMaterialLoader extends MaterialLoader {
 

+ 9 - 24
examples/jsm/nodes/materials/Materials.js

@@ -4,7 +4,6 @@ import MeshBasicNodeMaterial from './MeshBasicNodeMaterial.js';
 import MeshStandardNodeMaterial from './MeshStandardNodeMaterial.js';
 import PointsNodeMaterial from './PointsNodeMaterial.js';
 import SpriteNodeMaterial from './SpriteNodeMaterial.js';
-import { Material } from 'three';
 
 export {
 	NodeMaterial,
@@ -15,31 +14,17 @@ export {
 	SpriteNodeMaterial
 };
 
-const materialLib = {
-	NodeMaterial,
-	LineBasicNodeMaterial,
-	MeshBasicNodeMaterial,
-	MeshStandardNodeMaterial,
-	PointsNodeMaterial,
-	SpriteNodeMaterial
-};
-
-const fromTypeFunction = Material.fromType;
-
-Material.fromType = function ( type ) {
-
-	if ( materialLib[ type ] !== undefined ) {
-
-		return new materialLib[ type ]();
-
-	}
-
-	return fromTypeFunction.call( this, type );
-
-};
-
 NodeMaterial.fromMaterial = function ( material ) {
 
+	const materialLib = {
+		NodeMaterial,
+		LineBasicNodeMaterial,
+		MeshBasicNodeMaterial,
+		MeshStandardNodeMaterial,
+		PointsNodeMaterial,
+		SpriteNodeMaterial,
+	};
+
 	const type = material.type.replace( 'Material', 'NodeMaterial' );
 
 	if ( materialLib[ type ] === undefined ) {

+ 47 - 2
src/loaders/MaterialLoader.js

@@ -6,7 +6,26 @@ import { Matrix3 } from '../math/Matrix3.js';
 import { Matrix4 } from '../math/Matrix4.js';
 import { FileLoader } from './FileLoader.js';
 import { Loader } from './Loader.js';
-import { Material } from '../materials/Material.js';
+import {
+	ShadowMaterial,
+	SpriteMaterial,
+	RawShaderMaterial,
+	ShaderMaterial,
+	PointsMaterial,
+	MeshPhysicalMaterial,
+	MeshStandardMaterial,
+	MeshPhongMaterial,
+	MeshToonMaterial,
+	MeshNormalMaterial,
+	MeshLambertMaterial,
+	MeshDepthMaterial,
+	MeshDistanceMaterial,
+	MeshBasicMaterial,
+	MeshMatcapMaterial,
+	LineDashedMaterial,
+	LineBasicMaterial,
+	Material,
+} from '../materials/Materials.js';
 
 class MaterialLoader extends Loader {
 
@@ -67,7 +86,7 @@ class MaterialLoader extends Loader {
 
 		}
 
-		const material = Material.fromType( json.type );
+		const material = MaterialLoader.createMaterialFromType( json.type );
 
 		if ( json.uuid !== undefined ) material.uuid = json.uuid;
 		if ( json.name !== undefined ) material.name = json.name;
@@ -309,5 +328,31 @@ class MaterialLoader extends Loader {
 
 }
 
+MaterialLoader.createMaterialFromType = function ( type ) {
+
+	const materialLib = {
+		ShadowMaterial,
+		SpriteMaterial,
+		RawShaderMaterial,
+		ShaderMaterial,
+		PointsMaterial,
+		MeshPhysicalMaterial,
+		MeshStandardMaterial,
+		MeshPhongMaterial,
+		MeshToonMaterial,
+		MeshNormalMaterial,
+		MeshLambertMaterial,
+		MeshDepthMaterial,
+		MeshDistanceMaterial,
+		MeshBasicMaterial,
+		MeshMatcapMaterial,
+		LineDashedMaterial,
+		LineBasicMaterial,
+		Material
+	};
+
+	return new materialLib[ type ]();
+
+};
 
 export { MaterialLoader };

+ 0 - 8
src/materials/Material.js

@@ -499,12 +499,4 @@ class Material extends EventDispatcher {
 
 }
 
-Material.fromType = function ( /*type*/ ) {
-
-	// TODO: Behavior added in Materials.js
-
-	return null;
-
-};
-
 export { Material };

+ 0 - 27
src/materials/Materials.js

@@ -37,30 +37,3 @@ export {
 	LineBasicMaterial,
 	Material
 };
-
-const materialLib = {
-	ShadowMaterial,
-	SpriteMaterial,
-	RawShaderMaterial,
-	ShaderMaterial,
-	PointsMaterial,
-	MeshPhysicalMaterial,
-	MeshStandardMaterial,
-	MeshPhongMaterial,
-	MeshToonMaterial,
-	MeshNormalMaterial,
-	MeshLambertMaterial,
-	MeshDepthMaterial,
-	MeshDistanceMaterial,
-	MeshBasicMaterial,
-	MeshMatcapMaterial,
-	LineDashedMaterial,
-	LineBasicMaterial,
-	Material
-};
-
-Material.fromType = function ( type ) {
-
-	return new materialLib[ type ]();
-
-};