xenko icon indicating copy to clipboard operation
xenko copied to clipboard

Texture cannot be setted into custom shader

Open Chaosus opened this issue 8 years ago • 8 comments

Hi Xenko team. I tried several ways and it seems to be impossible.

The following code compiles, but fails at runtime -

shader TextureColor<Texture2D myTexture> : ComputeColor, Texturing
{
    override float4 Compute()
    {
        return myTexture.Sample(LinearSampler, streams.TexCoord);
    }
}; 

or

shader TextureColor<Texture2D myTexture> : ComputeColorTexture<myTexture, TEXCOORD0>
{
    override float4 Compute()
    {
        return base.Compute();
    }
}; 
shader TextureColor : ComputeColor, Texturing
{
    cbuffer PerMaterial
    {
       stage Texture2D MyTexture;
    }

    override float4 Compute()
    {
        return MyTexture.Sample(Texturing.LinearSampler, streams.TexCoord);
    }
}; 

following by this code in scripts

using SiliconStudio.Xenko.Engine;
using SiliconStudio.Xenko.Rendering;
using SiliconStudio.Xenko.Graphics;

namespace EffectTest
{
    public class SetupTextureScript : SyncScript
    {
        public Texture CustomTexture;

        private Material material;

        public override void Start()
        {
            var model = Entity.Get<ModelComponent>();
            material = model.GetMaterial(0);
        }

        public override void Update()
        {
            material.Passes[0].Parameters.Set(TextureColorKeys.MyTexture, CustomTexture);
        }
    }
}

doesnt work either. Please help !

Chaosus avatar Sep 29 '17 20:09 Chaosus

A week passed and still no comments ? This is serious issue cuz its vastly limit developers to create own custom shaders. Here is the error log for first code sample image

Chaosus avatar Oct 06 '17 09:10 Chaosus

shader TextureColor<Texture2D myTexture> I don't think you can pass textures this way since that means a compile constant. use a simple uniform declaration as a member of the shader like a field in a class rather.

siliconvoodoo avatar Oct 06 '17 10:10 siliconvoodoo

okay - the last sample with cbuffer used texture as member and I simply got a black surface even if I pass correct texture.

Chaosus avatar Oct 06 '17 10:10 Chaosus

I would suggest firing up renderdoc to see if the texture is bound, or has really something else than black. maybe it's the texcoords. first thing first, try outputting a hard white color as a return value instead of a mytexture.sample

siliconvoodoo avatar Oct 06 '17 10:10 siliconvoodoo

@siliconvoodoo Have you tried pass texture yourself ? It simply doesnt work. texcoords is okay(for sample sphere)

image

image - why its empty when I set it in script ? - material.Passes[0].Parameters.Set(TextureColorKeys.MyTexture, MyTexture);

Chaosus avatar Oct 06 '17 10:10 Chaosus

I don't think you can pass textures this way since that means a compile constant. - this is generic, and its the only visible way to pass parameter from Game Studio(not through script). Why Unity does not have such problem and you could simply pass textures from editor ?

Chaosus avatar Oct 06 '17 10:10 Chaosus

https://forums.xenko.com/t/sampling-a-texture-in-a-custom-shader/1225/4

Well that seems to work ^ However, it requires an extra script, which isn't optimal.

stefnotch avatar Feb 24 '18 19:02 stefnotch

If you wish to create a shader where you can set a texture in the game studio, the following code should work:

shader TextureShader: ComputeColor
    {
        //Not necessarily a texture, it can also be a different shader, a color, etc.
        compose ComputeColor MatrixTexture; 

        override float4 Compute()
        {
            return MatrixTexture.Compute();
        }
    };

stefnotch avatar Feb 25 '18 10:02 stefnotch