Jump to content
  • Advertisement
Sign in to follow this  

Planet HLSL "Perlin Style" Noise

This topic is 3925 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Thanks for all your help, I thought I'd post some code back for others in the future. Here's the Shader file
// File: Planet.vsh

// Global variables
float4x4 mWorldViewProj;  // World * View * Projection transformation

// Vertex shader output structure

struct VSPlanet_INPUT
    float4 Position   : POSITION;   // vertex position 
    float3 Normal     : NORMAL;     // Normal
    float2 Tex        : TEXCOORD0;  // Texture

struct VSPlanet_OUTPUT
    float4 Position   : POSITION;   // vertex position 
    float4 Original   : TEXCOORD0;  // original position of vertex
    float2 Tex        : TEXCOORD1;  // texture coords

VSPlanet_OUTPUT VSPlanet( in VSPlanet_INPUT VSIn ){
VSPlanet_OUTPUT Output;
    Output.Position = mul( VSIn.Position, mWorldViewProj );
    Output.Original = VSIn.Position;
    Output.Tex = VSIn.Tex;

    return Output;

texture g_tex;
sampler3D sampPlanet= sampler_state
texture = <g_tex>;
AddressU = wrap;
AddressV = wrap;
AddressW = wrap;

float4 PSPlanet(VSPlanet_OUTPUT PSIn): COLOR{

float4 PlanetColor;
float4 SampleColor;
	PlanetColor.rgba = 0.0f;
	SampleColor = tex3D(sampPlanet, PSIn.Original);
	PlanetColor.r = SampleColor.a;

	SampleColor = tex3D(sampPlanet, PSIn.Original*2.0f);
	PlanetColor.r += (SampleColor.a / 2.0f);

	SampleColor = tex3D(sampPlanet, PSIn.Original*4.0f);
	PlanetColor.r += (SampleColor.a / 4.0f);

	SampleColor = tex3D(sampPlanet, PSIn.Original*8.0f);
	PlanetColor.r += (SampleColor.a / 8.0f);

	PlanetColor.rgb = pow(cos((PlanetColor.r/1.875f) * 5.0f), 15.0f);
	return PlanetColor;
Here's the code to create a 3D Noise texture
#define PERLINSIZE 32
	V_RETURN(D3DXCreateVolumeTexture(pd3dDevice, PERLINSIZE, PERLINSIZE, PERLINSIZE, 1, 0, D3DFMT_A8, D3DPOOL_SYSTEMMEM, &pNoiseTexture ));
	V_RETURN(pNoiseTexture->LockBox(0, &d3dBOX, NULL, 0));
int i;
unsigned char* pixel;
	pixel = (unsigned char*)d3dBOX.pBits;
	i = 0;
		*pixel = (char)(rand()/(RAND_MAX/256));
	V_RETURN(D3DXCreateVolumeTexture(pd3dDevice, PERLINSIZE, PERLINSIZE, PERLINSIZE, 1, 0, D3DFMT_A8, D3DPOOL_DEFAULT, &g_pNoiseTexture ));
	V_RETURN(pd3dDevice->UpdateTexture(pNoiseTexture, g_pNoiseTexture));
	//V_RETURN(D3DXSaveTextureToFile(L"noise.png", D3DXIFF_PNG, pNoiseTexture, NULL));
Here's what you have to do to use it.
		pd3dDevice->SetTexture(0, g_pNoiseTexture );
		pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
		pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
		pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
Hope you have some fun with it. I also have a shader for doing really nice Saturn style Rings. EDIT: Please remember to use 'source' tags in future. [Edited by - jollyjeffers on September 19, 2007 3:13:47 PM]

Share this post

Link to post
Share on other sites
That code has an error in it.

You use the size of the texture when writing the bits to the volume texture, instead of using the SlicePitch and RowPitch members. This code will likely break on any texture that isn't a multiple of 4 (or 8) and might also randomly break with some driver/hardware setups.

Hope this helps.

Share this post

Link to post
Share on other sites
I assumed that since I'm creating the Texture using D3DPOOL_SYSTEMMEM it would be continuous and that the DirectX/driver would sort out the format when it went called UpdateTexture.

Perhaps that's wrong...

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!