Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline grayscaling in materials #11308

Closed
Jaliborc opened this issue May 9, 2017 · 8 comments
Closed

Inline grayscaling in materials #11308

Jaliborc opened this issue May 9, 2017 · 8 comments

Comments

@Jaliborc
Copy link

Jaliborc commented May 9, 2017

Description of the problem

It would be great to have the option to render in grayscale. It could be implemented as a new tonemapping type and set in renderer.tonemapping.

@looeee
Copy link
Collaborator

looeee commented May 9, 2017

That is implemented with postprocessing. See this example, and set saturation to 0.

@Mugen87
Copy link
Collaborator

Mugen87 commented May 9, 2017

@looeee is right. Such features should be implemented with post processing.

@Mugen87 Mugen87 closed this as completed May 9, 2017
@Jaliborc
Copy link
Author

Of course it can be implemented in postprocessing. But would be much more efficient to have the option to do it inline. Tone-mapping could also be done in postprocessing, yet you still made it available inline.

@WestLangley WestLangley reopened this May 10, 2017
@WestLangley
Copy link
Collaborator

For reference, adding inline color-correction -- and possibly LUTs -- was mentioned in #11076 (comment). Perhaps we could call it color-remapping.

@Mugen87
Copy link
Collaborator

Mugen87 commented May 10, 2017

Sry, i've missed this in the mentioned discussion.

@Jaliborc
Copy link
Author

color-remapping sounds like a good term for it.

@pailhead
Copy link
Contributor

pailhead commented May 11, 2017

Another thing that can easily be done with:

#10791

for example something like:

myMaterial = new MeshPhongMaterial( params );

myMaterial.shaderIncludes = {
  fog_fragment: THREE.ShaderChunks.fog_fragment + 
    "\n" +
    "gl_FragColor.xyz = mix( gl_FragColor.xyz, dot( gl_FragColor.xyz , 1.) , uMyGrayScaleUniform );"
  uv_pars_fragment: THREE.ShaderChunks.uv_pars_fragment + 
    "\n" +
    "uniform float uMyGrayScaleUniform;"
}

myMaterial.shaderUniforms = {
  uMyGrayScaleUniform: { value: 0 }
}

The idea being - you should be able to decorate any material and modify the glsl to your liking. Current materials have not been built with that in mind. For example, this works, but uv_pars_vertex and uv_pars_fragment are the most convenient places to attach uniforms and attributes, only because they happened to be included in most if not all of the material templates. My guess is that it's a coincidence of having to deal with displacement for everything. Also fog_fragment may not be the best place, to do the grayscale, but my guess is that it is included in anything that needs to be rendered with color / lights, and since it happens very very late and already operates on gl_FragColor adding any logic after it will most likely happen last.

While it gives flexibility, you need to know glsl and go through three's shader library. If it were further abstracted, that you have meaningful hooks, it would be much easier to work with.

Anyway it doesn't have to be this ugly. You'd do something like this, or a factory or something:

class MeshPhongMaterialGS extends MeshPhongMaterial {
	
	constructor(){
		
		super(...args)

		this.shaderUniforms = { 

			uGrayscale: { value: 0 }

		}

		this.shaderIncludes = {
			uv_pars_fragment: "...",
			fog_fragment: "..."
		}

	}

	set grayscale ( v ) { this.shaderUniforms.uGrayscale = v }

	get grayscale () { return this.shaderUniforms.uGrayscale }

}

The main benefit is - now you're relying on the community to patch the library and add a feature to cover what so far may be a single use case ( can already be done with post processing ). Even if that happens, you might find the grays too washed out, or too having too much contrast, and you can't even use it. With this api, you could go here and grab any algorithm, and apply it yourself. Even if you don't know glsl, you might find an npm module that does just this, because someone else solved the problem without altering the library but using it instead.

@Mugen87
Copy link
Collaborator

Mugen87 commented Jul 24, 2020

Duplicate of #19457.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants