Jump to content

Conditional input of texture into a shader


JohnK
 Share

Recommended Posts

Sometimes the shader I am creating will be given an image file to use as a texture sampler and sometimes it will not. 

 

In the shader when the image file is given I need the line

uniform sampler2D refTexture;

and in the main function a line along the lines of

float x = 0.5 * texture2D(refSampler, uv).rgb.x;

and when no image file is given then

float x = 0.5 * position.x

From looking at the code in the samplers in the materialsLibrary I see there are some conditions on the uniforms such as

#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)varying vec4 vPositionFromLight0;uniform sampler2D shadowSampler0;#elseuniform samplerCube shadowSampler0;#endif

1. Is there a condition I can use for

#if ..................uniform sampler2D refTexture;#endif

2. Is there a conditional statement within the main function I can use for

IF texture loaded    float x = 0.5 * texture2D(refSampler, uv).rgb.x;ELSE    float x = 0.5 * position.xENDIF

 

Link to comment
Share on other sites

I think you can define a variable in your material.js file 

 

like : 

Material.prototype.isReady = function (mesh, useInstances) {     if (texture loaded) {        defines.push("#define variable_name");     }}

and in you shader check 

#ifdef variable_name    float x = 0.5 * texture2D(refSampler, uv).rgb.x;#else    float x = 0.5 * position.x;#endif
Link to comment
Share on other sites

@jerome  & @bulisor thanks for suggestions, after  reading these and looking further at the sample files in the materialsLibrary I can see that I can define a public variable in the class defines in the typescript file which can be set as true if there is a texture file given - just got to work out how I actually do this and then use #ifdef in the shader file.

 

More thinking to do yet.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...