Home Technical Talk

New at shader programming

Hi everyone,

This is my first post on the forums. :)

Since a week I started with shader programming (HLSL), but allready I ran into a stopping problem.
I followed this tutorial, a tutorial to write a simple ambient shader.
If I apply my shader to a model in Max 2013, or in FX Composer the model just dissapears. :shifty:

This is my shader code:
//===================================================
//	Realtime game shader
//	Bob Adriaenssen (CookieBE)
//	31/05/2012
//	Version 0.1
//	Copyright 2012. All rights reserved.
//===================================================

//===================================================
//	Variables
//===================================================

float4x4 WorldMatrix;
float4x4 ViewMatrix;
float4x4 ProjectionMatrix;

float4 AmbientColor = float4( 1.0, 1.0, 1.0, 1.0);
float AmbientIntensity = 0.1;

//Tweakable parameters//
string UIName = "Test";

//===================================================
//	Data structures
//===================================================

struct VertexShaderInput
{
	float4 Position : POSITION0;
};

struct VertexShaderOutput
{
	float4 Position : POSITION0;
};

//===================================================
//	Vertex shaders
//===================================================

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
	VertexShaderOutput output;
	
	float4 worldPosition = mul(input.Position, WorldMatrix);
	float4 viewPosition = mul(worldPosition, ViewMatrix);
	output.Position = mul(viewPosition, ProjectionMatrix);
	
	return output;
}

//===================================================
//	Pixel shaders
//===================================================

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
	return AmbientColor * AmbientIntensity;
}

//===================================================
//	Techniques
//===================================================

technique Flat
{
	pass Pass0
	{
		CullMode = None;
		VertexShader = compile vs_2_0 VertexShaderFunction();
		PixelShader = compile ps_2_0 PixelShaderFunction();
	}
}

I hope someone can help me with this problem.

Replies

  • JamesWild
    Options
    Offline / Send Message
    JamesWild polycounter lvl 8
    That's HLSL right?
    My only experience is with GLSL but I'm guessing multiplying the ambient of 1 1 1 1 with 0.1 makes 0.1 0.1 0.1 0.1 - a very low alpha value. I'm not sure how you'd do it under HLSL cleanly but set the alpha component to 1 or remove the intensity multiplier and see what happens.
  • CookieBE
    Options
    Offline / Send Message
    @JamesWild Yes, it is HLSL. Forgot to mention that in the first post.
    I removed the intensity component, but the problem remains unfortunately. :(
  • JamesWild
    Options
    Offline / Send Message
    JamesWild polycounter lvl 8
    I don't know much about FX Composer but I guess it could be a problem with the matrices not being populated/properly by Composer.
  • rollin
    Options
    Offline / Send Message
    rollin polycounter
    I'm no shader pro but If your model disappears it's likely that your vertex shader position transformation is not right
  • Xoliul
    Options
    Offline / Send Message
    Xoliul polycounter lvl 14
    Yeah, model disappears = vertex shader is incorrect or not working. It's probably because you have no semantics for your application input matrices.
    These are the definitions from my shader that work, you don't need all of them for the basics:
    float4x4 World : WORLD;
    float4x4 View : VIEW;
    float4x4 Projection : PROJECTION;
    float4x4 WorldInvTrans : WorldIT;
    float4x4 ViewInv : ViewInverse;
    float4 cameraPos : WORLDCAMERAPOSITION;

    Also, you really want to use this line at the very top for 3DS Max:

    string ParamID = "0x003"; //use DXSAS compiler

    It tells it to use the improved DXSAS compiler. You shouldn't use anything else.
  • CookieBE
    Options
    Offline / Send Message
    @Xoliul: Thanks for the help it is all working correct now.

    Everyone thanks for the replies. :)
Sign In or Register to comment.