<commit_before>define [
	'goo/loaders/handlers/ConfigHandler'
	
	'goo/renderer/Material'
	'goo/renderer/Util'
	'goo/renderer/shaders/ShaderLib'
	
	'goo/util/rsvp'
	'goo/util/PromiseUtil'
	'goo/util/ObjectUtil'

], (
	ConfigHandler,
	Material,
	Util, 
	ShaderLib,
	RSVP,
	pu,
	_
) ->
	class MaterialHandler extends ConfigHandler			
		@_register('material')		
		
		constructor: (@world, @getConfig, @updateObject, @options)->
			@_objects = {}
		
		_prepare: (config)->
			config.blendState ?= {}
			_.defaults config.blendState,
					blending: 'NoBlending'
					blendEquation: 'AddEquation'
					blendSrc: 'SrcAlphaFactor'
					blendDst: 'OneMinusSrcAlphaFactor'
			config.cullState ?= {}
			_.defaults config.cullState,
				enabled : true
				cullFace : 'Back'
				frontFace : 'CCW'
			config.depthState ?= {}
			_.defaults config.depthState,
				enabled: true
				write: true

			config.renderQueue ?= -1
		
		_create: (ref)->
			@_objects ?= {}
			@_objects[ref] = new Material(ref)
		
		
		update: (ref, config)->
			@_prepare(config)
			if not @_objects?[ref] 
				@_create(ref)
				object = @_objects[ref]

				@_getShaderObject(config.shaderRef, config.wireframe).then (shader)=>
					#console.log "Got shader"
					
					if not shader
						console.warn 'Unknown shader', config.shaderRef, '- not updating material', ref
						return
		
					if config.wireframe
						object.wireframe = config.wireframe
					if config.wireframeColor
						object.wireframeColor = Util.clone config.wireframeColor
		
					object.blendState = Util.clone config.blendState
					object.cullState = Util.clone config.cullState
					object.depthState = Util.clone config.depthState
					if config.renderQueue == -1 then object.renderQueue = null
					else object.renderQueue = config.renderQueue
		

					object.shader = shader
					object.uniforms = {}
					for name, value of config.uniforms
						object.uniforms[name] = _.clone(value)
		

					#console.log "#{config.textureRefs?.length or 0} textures"
					promises = []

					for textureType, textureRef of config.texturesMapping
						do (textureType, textureRef)=>
							promises.push @getConfig(textureRef).then (textureConfig)=>
								@updateObject(textureRef, textureConfig, @options).then (texture)=>
									#console.log "Got texture: #{textureRef} #{texture}"
									type: textureType
									ref: textureRef 
									texture: texture
					
					if promises?.length 
						return RSVP.all(promises).then (textures)-> 
							for texture in textures
								object.setTexture(texture.type, texture.texture)

							return object
						.then null, (err)->
							console.error "Error loading textures: #{err}"
					else 
						return object
			else
				# Already loaded this material
				pu.createDummyPromise(@_objects[ref])				
		
		remove: (ref)->
			delete @_objects[ref]

		_getShaderObject: (ref, wireframe)->
			if wireframe
				promise = new RSVP.Promise()
				shader = Material.createShader ShaderLib.simple
				promise.resolve(shader)
				return promise
			else if ref?
				@getConfig(ref).then (config)=>
					@updateObject(ref, config, @options)
			else
				defaultShader = Material.createShader(ShaderLib.texturedLit, 'DefaultShader')
				pu.createDummyPromise(defaultShader)



<commit_msg>Minor bugfix, unclear if it makes any difference<commit_after>define [
	'goo/loaders/handlers/ConfigHandler'
	
	'goo/renderer/Material'
	'goo/renderer/Util'
	'goo/renderer/shaders/ShaderLib'
	
	'goo/util/rsvp'
	'goo/util/PromiseUtil'
	'goo/util/ObjectUtil'

], (
	ConfigHandler,
	Material,
	Util, 
	ShaderLib,
	RSVP,
	pu,
	_
) ->
	class MaterialHandler extends ConfigHandler			
		@_register('material')		
		
		constructor: (@world, @getConfig, @updateObject, @options)->
			@_objects = {}
		
		_prepare: (config)->
			config.blendState ?= {}
			_.defaults config.blendState,
					blending: 'NoBlending'
					blendEquation: 'AddEquation'
					blendSrc: 'SrcAlphaFactor'
					blendDst: 'OneMinusSrcAlphaFactor'
			config.cullState ?= {}
			_.defaults config.cullState,
				enabled : true
				cullFace : 'Back'
				frontFace : 'CCW'
			config.depthState ?= {}
			_.defaults config.depthState,
				enabled: true
				write: true

			config.renderQueue ?= -1
		
		_create: (ref)->
			@_objects ?= {}
			@_objects[ref] = new Material(ref)
		
		
		update: (ref, config)->
			@_prepare(config)
			if not @_objects?[ref] 
				@_create(ref)
				object = @_objects[ref]

				@_getShaderObject(config.shaderRef, config.wireframe).then (shader)=>
					#console.log "Got shader"
					
					if not shader
						console.warn 'Unknown shader', config.shaderRef, '- not updating material', ref
						return
		
					if config.wireframe
						object.wireframe = config.wireframe
					if config.wireframeColor
						object.wireframeColor = Util.clone config.wireframeColor
		
					object.blendState = Util.clone config.blendState
					object.cullState = Util.clone config.cullState
					object.depthState = Util.clone config.depthState
					if config.renderQueue == -1 then object.renderQueue = null
					else object.renderQueue = config.renderQueue
		

					object.shader = shader
					object.uniforms = {}
					for name, value of config.uniforms
						object.uniforms[name] = _.clone(value)
		

					#console.log "#{config.textureRefs?.length or 0} textures"
					promises = []

					for textureType, textureRef of config.texturesMapping
						do (textureType, textureRef)=>
							promises.push @getConfig(textureRef).then (textureConfig)=>
								@updateObject(textureRef, textureConfig, @options).then (texture)=>
									#console.log "Got texture: #{textureRef} #{texture}"
									type: textureType
									ref: textureRef 
									texture: texture
					
					if promises?.length 
						return RSVP.all(promises).then (textures)-> 
							for texture in textures
								if texture.texture?
									object.setTexture(texture.type, texture.texture)

							return object
						.then null, (err)->
							console.error "Error loading textures: #{err}"
					else 
						return object
			else
				# Already loaded this material
				pu.createDummyPromise(@_objects[ref])				
		
		remove: (ref)->
			delete @_objects[ref]

		_getShaderObject: (ref, wireframe)->
			if wireframe
				promise = new RSVP.Promise()
				shader = Material.createShader ShaderLib.simple
				promise.resolve(shader)
				return promise
			else if ref?
				@getConfig(ref).then (config)=>
					@updateObject(ref, config, @options)
			else
				defaultShader = Material.createShader(ShaderLib.texturedLit, 'DefaultShader')
				pu.createDummyPromise(defaultShader)




{'fraction_non_alphanumeric': 0.11197204497113339, 'fraction_numerical': 0.0009115770282588879, 'mean_word_length': 2.970446320868516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2643605', 'n_tokens_mistral': 2759, 'n_tokens_neox': 2036, 'n_words': 449}