浏览代码

revert ShaderMaterial and Material

linbingquan 4 年之前
父节点
当前提交
5f6e9efff1
共有 2 个文件被更改,包括 189 次插入189 次删除
  1. 71 67
      src/materials/Material.js
  2. 118 122
      src/materials/ShaderMaterial.js

+ 71 - 67
src/materials/Material.js

@@ -4,94 +4,88 @@ import { MathUtils } from '../math/MathUtils.js';
 
 let materialId = 0;
 
-class Material extends EventDispatcher {
+function Material() {
 
-	constructor() {
+	Object.defineProperty( this, 'id', { value: materialId ++ } );
 
-		super();
+	this.uuid = MathUtils.generateUUID();
 
-		Object.defineProperty( this, 'id', { value: materialId ++ } );
-		Object.defineProperty( this, 'isMaterial', { value: true } );
+	this.name = '';
+	this.type = 'Material';
 
-		this.uuid = MathUtils.generateUUID();
+	this.fog = true;
 
-		this.name = '';
-		this.type = 'Material';
+	this.blending = NormalBlending;
+	this.side = FrontSide;
+	this.vertexColors = false;
 
-		this.fog = true;
+	this.opacity = 1;
+	this.transparent = false;
 
-		this.blending = NormalBlending;
-		this.side = FrontSide;
-		this.flatShading = false;
-		this.vertexColors = false;
+	this.blendSrc = SrcAlphaFactor;
+	this.blendDst = OneMinusSrcAlphaFactor;
+	this.blendEquation = AddEquation;
+	this.blendSrcAlpha = null;
+	this.blendDstAlpha = null;
+	this.blendEquationAlpha = null;
 
-		this.opacity = 1;
-		this.transparent = false;
+	this.depthFunc = LessEqualDepth;
+	this.depthTest = true;
+	this.depthWrite = true;
 
-		this.blendSrc = SrcAlphaFactor;
-		this.blendDst = OneMinusSrcAlphaFactor;
-		this.blendEquation = AddEquation;
-		this.blendSrcAlpha = null;
-		this.blendDstAlpha = null;
-		this.blendEquationAlpha = null;
+	this.stencilWriteMask = 0xff;
+	this.stencilFunc = AlwaysStencilFunc;
+	this.stencilRef = 0;
+	this.stencilFuncMask = 0xff;
+	this.stencilFail = KeepStencilOp;
+	this.stencilZFail = KeepStencilOp;
+	this.stencilZPass = KeepStencilOp;
+	this.stencilWrite = false;
 
-		this.depthFunc = LessEqualDepth;
-		this.depthTest = true;
-		this.depthWrite = true;
+	this.clippingPlanes = null;
+	this.clipIntersection = false;
+	this.clipShadows = false;
 
-		this.stencilWriteMask = 0xff;
-		this.stencilFunc = AlwaysStencilFunc;
-		this.stencilRef = 0;
-		this.stencilFuncMask = 0xff;
-		this.stencilFail = KeepStencilOp;
-		this.stencilZFail = KeepStencilOp;
-		this.stencilZPass = KeepStencilOp;
-		this.stencilWrite = false;
+	this.shadowSide = null;
 
-		this.clippingPlanes = null;
-		this.clipIntersection = false;
-		this.clipShadows = false;
+	this.colorWrite = true;
 
-		this.shadowSide = null;
+	this.precision = null; // override the renderer's default precision for this material
 
-		this.colorWrite = true;
+	this.polygonOffset = false;
+	this.polygonOffsetFactor = 0;
+	this.polygonOffsetUnits = 0;
 
-		this.precision = null; // override the renderer's default precision for this material
+	this.dithering = false;
 
-		this.polygonOffset = false;
-		this.polygonOffsetFactor = 0;
-		this.polygonOffsetUnits = 0;
+	this.alphaTest = 0;
+	this.premultipliedAlpha = false;
 
-		this.dithering = false;
+	this.visible = true;
 
-		this.alphaTest = 0;
-		this.premultipliedAlpha = false;
+	this.toneMapped = true;
 
-		this.visible = true;
+	this.userData = {};
 
-		this.toneMapped = true;
+	this.version = 0;
 
-		this.userData = {};
-
-		this.version = 0;
-
-	}
+}
 
-	set needsUpdate( value ) {
+Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
-		if ( value === true ) this.version ++;
+	constructor: Material,
 
-	}
+	isMaterial: true,
 
-	onBeforeCompile( /* shaderobject, renderer */ ) {}
+	onBeforeCompile: function ( /* shaderobject, renderer */ ) {},
 
-	customProgramCacheKey() {
+	customProgramCacheKey: function () {
 
 		return this.onBeforeCompile.toString();
 
-	}
+	},
 
-	setValues( values ) {
+	setValues: function ( values ) {
 
 		if ( values === undefined ) return;
 
@@ -140,9 +134,9 @@ class Material extends EventDispatcher {
 
 		}
 
-	}
+	},
 
-	toJSON( meta ) {
+	toJSON: function ( meta ) {
 
 		const isRoot = ( meta === undefined || typeof meta === 'string' );
 
@@ -353,15 +347,15 @@ class Material extends EventDispatcher {
 
 		return data;
 
-	}
+	},
 
-	clone() {
+	clone: function () {
 
 		return new this.constructor().copy( this );
 
-	}
+	},
 
-	copy( source ) {
+	copy: function ( source ) {
 
 		this.name = source.name;
 
@@ -437,14 +431,24 @@ class Material extends EventDispatcher {
 
 		return this;
 
-	}
+	},
 
-	dispose() {
+	dispose: function () {
 
 		this.dispatchEvent( { type: 'dispose' } );
 
 	}
 
-}
+} );
+
+Object.defineProperty( Material.prototype, 'needsUpdate', {
+
+	set: function ( value ) {
+
+		if ( value === true ) this.version ++;
+
+	}
+
+} );
 
 export { Material };

+ 118 - 122
src/materials/ShaderMaterial.js

@@ -23,195 +23,191 @@ import default_fragment from '../renderers/shaders/ShaderChunk/default_fragment.
  * }
  */
 
-class ShaderMaterial extends Material {
+function ShaderMaterial( parameters ) {
 
-	constructor( parameters ) {
+	Material.call( this );
 
-		super();
+	this.type = 'ShaderMaterial';
 
-		Object.defineProperty( this, 'isShaderMaterial', { value: true } );
+	this.defines = {};
+	this.uniforms = {};
 
-		this.type = 'ShaderMaterial';
+	this.vertexShader = default_vertex;
+	this.fragmentShader = default_fragment;
 
-		this.defines = {};
-		this.uniforms = {};
+	this.linewidth = 1;
 
-		this.vertexShader = default_vertex;
-		this.fragmentShader = default_fragment;
+	this.wireframe = false;
+	this.wireframeLinewidth = 1;
 
-		this.linewidth = 1;
+	this.fog = false; // set to use scene fog
+	this.lights = false; // set to use scene lights
+	this.clipping = false; // set to use user-defined clipping planes
 
-		this.wireframe = false;
-		this.wireframeLinewidth = 1;
+	this.skinning = false; // set to use skinning attribute streams
+	this.morphTargets = false; // set to use morph targets
+	this.morphNormals = false; // set to use morph normals
 
-		this.fog = false; // set to use scene fog
-		this.lights = false; // set to use scene lights
-		this.clipping = false; // set to use user-defined clipping planes
+	this.extensions = {
+		derivatives: false, // set to use derivatives
+		fragDepth: false, // set to use fragment depth values
+		drawBuffers: false, // set to use draw buffers
+		shaderTextureLOD: false // set to use shader texture LOD
+	};
 
-		this.skinning = false; // set to use skinning attribute streams
-		this.morphTargets = false; // set to use morph targets
-		this.morphNormals = false; // set to use morph normals
+	// When rendered geometry doesn't include these attributes but the material does,
+	// use these default values in WebGL. This avoids errors when buffer data is missing.
+	this.defaultAttributeValues = {
+		'color': [ 1, 1, 1 ],
+		'uv': [ 0, 0 ],
+		'uv2': [ 0, 0 ]
+	};
 
-		this.extensions = {
-			derivatives: false, // set to use derivatives
-			fragDepth: false, // set to use fragment depth values
-			drawBuffers: false, // set to use draw buffers
-			shaderTextureLOD: false // set to use shader texture LOD
-		};
+	this.index0AttributeName = undefined;
+	this.uniformsNeedUpdate = false;
 
-		// When rendered geometry doesn't include these attributes but the material does,
-		// use these default values in WebGL. This avoids errors when buffer data is missing.
-		this.defaultAttributeValues = {
-			'color': [ 1, 1, 1 ],
-			'uv': [ 0, 0 ],
-			'uv2': [ 0, 0 ]
-		};
+	this.glslVersion = null;
 
-		this.index0AttributeName = undefined;
-		this.uniformsNeedUpdate = false;
+	if ( parameters !== undefined ) {
 
-		this.glslVersion = null;
+		if ( parameters.attributes !== undefined ) {
 
-		if ( parameters !== undefined ) {
+			console.error( 'THREE.ShaderMaterial: attributes should now be defined in THREE.BufferGeometry instead.' );
 
-			if ( parameters !== undefined ) {
-
-				if ( parameters.attributes !== undefined ) {
-
-					console.error( 'THREE.ShaderMaterial: attributes should now be defined in THREE.BufferGeometry instead.' );
+		}
 
-				}
+		this.setValues( parameters );
 
-				this.setValues( parameters );
+	}
 
-			}
+}
 
-		}
+ShaderMaterial.prototype = Object.create( Material.prototype );
+ShaderMaterial.prototype.constructor = ShaderMaterial;
 
-	}
+ShaderMaterial.prototype.isShaderMaterial = true;
 
-	copy( source ) {
+ShaderMaterial.prototype.copy = function ( source ) {
 
-		super.copy( source );
+	Material.prototype.copy.call( this, source );
 
-		this.fragmentShader = source.fragmentShader;
-		this.vertexShader = source.vertexShader;
+	this.fragmentShader = source.fragmentShader;
+	this.vertexShader = source.vertexShader;
 
-		this.uniforms = cloneUniforms( source.uniforms );
+	this.uniforms = cloneUniforms( source.uniforms );
 
-		this.defines = Object.assign( {}, source.defines );
+	this.defines = Object.assign( {}, source.defines );
 
-		this.wireframe = source.wireframe;
-		this.wireframeLinewidth = source.wireframeLinewidth;
+	this.wireframe = source.wireframe;
+	this.wireframeLinewidth = source.wireframeLinewidth;
 
-		this.lights = source.lights;
-		this.clipping = source.clipping;
+	this.lights = source.lights;
+	this.clipping = source.clipping;
 
-		this.skinning = source.skinning;
+	this.skinning = source.skinning;
 
-		this.morphTargets = source.morphTargets;
-		this.morphNormals = source.morphNormals;
+	this.morphTargets = source.morphTargets;
+	this.morphNormals = source.morphNormals;
 
-		this.extensions = Object.assign( {}, source.extensions );
+	this.extensions = Object.assign( {}, source.extensions );
 
-		this.glslVersion = source.glslVersion;
+	this.glslVersion = source.glslVersion;
 
-		return this;
+	return this;
 
-	}
+};
 
-	toJSON( meta ) {
+ShaderMaterial.prototype.toJSON = function ( meta ) {
 
-		const data = super.toJSON( meta );
+	const data = Material.prototype.toJSON.call( this, meta );
 
-		data.glslVersion = this.glslVersion;
-		data.uniforms = {};
+	data.glslVersion = this.glslVersion;
+	data.uniforms = {};
 
-		for ( const name in this.uniforms ) {
+	for ( const name in this.uniforms ) {
 
-			const uniform = this.uniforms[ name ];
-			const value = uniform.value;
+		const uniform = this.uniforms[ name ];
+		const value = uniform.value;
 
-			if ( value && value.isTexture ) {
+		if ( value && value.isTexture ) {
 
-				data.uniforms[ name ] = {
-					type: 't',
-					value: value.toJSON( meta ).uuid
-				};
+			data.uniforms[ name ] = {
+				type: 't',
+				value: value.toJSON( meta ).uuid
+			};
 
-			} else if ( value && value.isColor ) {
+		} else if ( value && value.isColor ) {
 
-				data.uniforms[ name ] = {
-					type: 'c',
-					value: value.getHex()
-				};
+			data.uniforms[ name ] = {
+				type: 'c',
+				value: value.getHex()
+			};
 
-			} else if ( value && value.isVector2 ) {
+		} else if ( value && value.isVector2 ) {
 
-				data.uniforms[ name ] = {
-					type: 'v2',
-					value: value.toArray()
-				};
+			data.uniforms[ name ] = {
+				type: 'v2',
+				value: value.toArray()
+			};
 
-			} else if ( value && value.isVector3 ) {
+		} else if ( value && value.isVector3 ) {
 
-				data.uniforms[ name ] = {
-					type: 'v3',
-					value: value.toArray()
-				};
+			data.uniforms[ name ] = {
+				type: 'v3',
+				value: value.toArray()
+			};
 
-			} else if ( value && value.isVector4 ) {
+		} else if ( value && value.isVector4 ) {
 
-				data.uniforms[ name ] = {
-					type: 'v4',
-					value: value.toArray()
-				};
+			data.uniforms[ name ] = {
+				type: 'v4',
+				value: value.toArray()
+			};
 
-			} else if ( value && value.isMatrix3 ) {
+		} else if ( value && value.isMatrix3 ) {
 
-				data.uniforms[ name ] = {
-					type: 'm3',
-					value: value.toArray()
-				};
+			data.uniforms[ name ] = {
+				type: 'm3',
+				value: value.toArray()
+			};
 
-			} else if ( value && value.isMatrix4 ) {
+		} else if ( value && value.isMatrix4 ) {
 
-				data.uniforms[ name ] = {
-					type: 'm4',
-					value: value.toArray()
-				};
+			data.uniforms[ name ] = {
+				type: 'm4',
+				value: value.toArray()
+			};
 
-			} else {
+		} else {
 
-				data.uniforms[ name ] = {
-					value: value
-				};
+			data.uniforms[ name ] = {
+				value: value
+			};
 
-				// note: the array variants v2v, v3v, v4v, m4v and tv are not supported so far
-
-			}
+			// note: the array variants v2v, v3v, v4v, m4v and tv are not supported so far
 
 		}
 
-		if ( Object.keys( this.defines ).length > 0 ) data.defines = this.defines;
+	}
 
-		data.vertexShader = this.vertexShader;
-		data.fragmentShader = this.fragmentShader;
+	if ( Object.keys( this.defines ).length > 0 ) data.defines = this.defines;
 
-		const extensions = {};
+	data.vertexShader = this.vertexShader;
+	data.fragmentShader = this.fragmentShader;
 
-		for ( const key in this.extensions ) {
+	const extensions = {};
 
-			if ( this.extensions[ key ] === true ) extensions[ key ] = true;
+	for ( const key in this.extensions ) {
 
-		}
+		if ( this.extensions[ key ] === true ) extensions[ key ] = true;
 
-		if ( Object.keys( extensions ).length > 0 ) data.extensions = extensions;
+	}
 
-		return data;
+	if ( Object.keys( extensions ).length > 0 ) data.extensions = extensions;
 
-	}
+	return data;
+
+};
 
-}
 
 export { ShaderMaterial };