SiliconStudio / xenko

Old repo for Xenko Game Engine. Please use https://github.com/xenko3d/xenko instead.
http://xenko.com
1.54k stars 346 forks source link

Texture cannot be setted into custom shader #631

Open Chaosus opened 6 years ago

Chaosus commented 6 years ago

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 commented 6 years ago

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

siliconvoodoo commented 6 years ago

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.

Chaosus commented 6 years ago

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

siliconvoodoo commented 6 years ago

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

Chaosus commented 6 years ago

@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 commented 6 years ago

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 ?

stefnotch commented 6 years ago

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 commented 6 years ago

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();
        }
    };