Jump to content

  • Log In with Google      Sign In   
  • Create Account


Adding separate uv coordinates to heightmap color channels


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 football94   Members   -  Reputation: 194

Like
0Likes
Like

Posted 26 July 2013 - 01:56 PM

Hi guys

 

Ive been using riemers heightmap sample(http://www.riemers.net/eng/Tutorials/XNA/Csharp/series4.php) as a platform for learning different programming techniques and my latest project has been  teaching myself  the art of terrain multitexturing but most of the texturing techiniques Ive seen had to do with height, and what I wanted to do is modify riemers heightbased textured terrain and create a simple two textured terrain using the blue and green channels of the heightmap along with implementing two different uv coordinates for each channel. the first step I took was to create a custom vertex struct(code below) that holds more than two texture coordinates but after that Im not sure how to go about a second step and this is where I seem to stuck at.  but despite my many searches I have yet find info on how I would access the blue channel and apply the uv coordinates of a grass texture to it. if anyone could help me to move on to a second step it would much appreciated.

 

   

                                                                                 Thankyou

 

 

 

 

code for custom vertex struct

  /*
         * Struct to hold vertex information with pos,normal,texcoord and texweights
         */
        public struct VertexMultitextured
        {
            public Vector3 Position;
            public Vector3 Normal;
            public Vector4 TextureCoordinate;
            public Vector4 TexWeights;
            public Vector4 TexWeights1;
            public Vector4 TexWeights2;
            public Vector4 TexWeights3;
            public Vector4 TexWeights4;

            public static int SizeInBytes = (3 + 3 + 4 + 4 + 4 + 4 + 4 + 4) * sizeof(float); // vec3 + vec3 + vec4 +vec4
            public static VertexElement[] VertexElements = new VertexElement[]
            {
                // stream, offset, type, method, usage and usage index
                new VertexElement( 0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0 ),
                new VertexElement( 0, sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Normal, 0 ),
                new VertexElement( 0, sizeof(float) * 6, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 0 ),
                new VertexElement( 0, sizeof(float) * 10, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 1 ),
                 new VertexElement( 0, sizeof(float) * 14, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 2 ), 
                new VertexElement( 0, sizeof(float) * 18, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 3 ), 
                new VertexElement( 0, sizeof(float) * 22, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 4 ),
                 new VertexElement( 0, sizeof(float) *26, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 5 ),
            };
        }

also the terrain Ive been working

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace TRIANGLE
{
    public class Terrain
    {
        /*
         * Struct to hold vertex information with pos,normal,texcoord and texweights
         */
        public struct VertexMultitextured
        {
            public Vector3 Position;
            public Vector3 Normal;
            public Vector4 TextureCoordinate;
            public Vector4 TexWeights;
            public Vector4 TexWeights1;
            public Vector4 TexWeights2;
            public Vector4 TexWeights3;
            public Vector4 TexWeights4;

            public static int SizeInBytes = (3 + 3 + 4 + 4 + 4 + 4 + 4 + 4) * sizeof(float); // vec3 + vec3 + vec4 +vec4
            public static VertexElement[] VertexElements = new VertexElement[]
            {
                // stream, offset, type, method, usage and usage index
                new VertexElement( 0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0 ),
                new VertexElement( 0, sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Normal, 0 ),
                new VertexElement( 0, sizeof(float) * 6, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 0 ),
                new VertexElement( 0, sizeof(float) * 10, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 1 ),
                 new VertexElement( 0, sizeof(float) * 14, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 2 ), 
                new VertexElement( 0, sizeof(float) * 18, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 3 ), 
                new VertexElement( 0, sizeof(float) * 22, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 4 ),
                 new VertexElement( 0, sizeof(float) *26, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 5 ),
            };
        }

        Game1 GameClass;
        GraphicsDevice device;

        public int terrainWidth;
        public int terrainLength;
        public float[,] heightData;
        public float[,] TerrainData;
        Vector3 diffuseColor;
        VertexBuffer terrainVertexBuffer;
        IndexBuffer terrainIndexBuffer;
        VertexDeclaration terrainVertexDeclaration;
        VertexMultitextured[] terrainVertices;
        int[] terrainIndices;

        public Texture2D grassTexture;
        public Texture2D sandTexture;
        public Texture2D rockTexture;
        public Texture2D snowTexture;
       

        public float grassv;
        public float grassu ; 
        public float sandv;
        public float sandu;
        Vector2 grasss;
       

        public VertexMultitextured[] TerrainVertices
        {
            get
            {
                return terrainVertices;
            }

        }
        public int Twidth
        {
            get
            {
                return terrainWidth;
            }

        }
        public int Tlength
        {
            get
            {
                return terrainLength;
            }

        }
       

        
        public Terrain(Game1 reference,GraphicsDevice device)
        {
            this.GameClass = reference;
            this.device = device;
            
            LoadVertices();
            LoadTextures();
        }

        private void LoadTextures()
        {
            grassTexture = GameClass.Content.Load<Texture2D>("grass");
            sandTexture = GameClass.Content.Load<Texture2D>("sand");
            rockTexture = GameClass.Content.Load<Texture2D>("rock");
            snowTexture = GameClass.Content.Load<Texture2D>("snow");

           

        }

       public void LoadVertices()
        {

            Texture2D TerrainMap = GameClass.Content.Load<Texture2D>("heightmap1");
            Texture2D heightMap = GameClass.Content.Load<Texture2D>("heightmap");
            LoadHeightData(heightMap, TerrainMap);

            VertexMultitextured[] terrainVertices = SetUpTerrainVertices();
            int[] terrainIndices = SetUpTerrainIndices();
            terrainVertices = CalculateNormals(terrainVertices, terrainIndices);
            CopyToTerrainBuffers(terrainVertices, terrainIndices);
            terrainVertexDeclaration = new VertexDeclaration(device, VertexMultitextured.VertexElements);

          

        }
        
        
        
        public int[] TerrainIndices
        {
            get
            {
                return terrainIndices;
            }

        }


        public void LoadHeightData(Texture2D heightMap, Texture2D TerrainMap)
        {
            float minimumHeight = float.MaxValue;
            float maximumHeight = float.MinValue;

            terrainWidth = heightMap.Width;
            terrainLength = heightMap.Height;

            // get rgb values for each pixel of hightmap
            Color[] heightMapColors = new Color[terrainWidth * terrainLength];
            heightMap.GetData(heightMapColors);

            Color[] TerrainMapColors = new Color[terrainWidth * terrainLength];
            TerrainMap.GetData(TerrainMapColors);

            heightData = new float[terrainWidth, terrainLength];
            TerrainData = new float[terrainWidth, terrainLength];
            for (int x = 0; x < terrainWidth; x++)
                for (int y = 0; y < terrainLength; y++)
                {
                    heightData[x, y] = heightMapColors[x + (y * terrainWidth)].R; // read r value
                    heightData[x, y] = heightMapColors[x + (y * terrainWidth)].B ;
                    heightData[x, y] = heightMapColors[x + (y * terrainWidth)].G;
                   
                    // determine minimum and maximum height value in terrain
                    if (heightData[x, y] < minimumHeight) minimumHeight = heightData[x, y];
                    if (heightData[x, y] > maximumHeight) maximumHeight = heightData[x, y];
                    //experimental - this reads the TerrainMap(colormap) and you decode the rgba values to determine the texture to paint.  
                    //right now its limited to the reimers example of 1 TextWeight vector 4 but I will be adding probably 9 more to the VertixMultiTextured  
                    //struc so that 40 total textures can be used - and changing the shader.    
                    //You are really unlimeted, hell add 100 TextWeights[99] for 400 textures! :)  
                    //see up above for setting teh texture weights.  Along with the yet to be implemented groundCursorStrength var, the 1 value will be  
                    //varied(set to the strength rating(0-1) range.  Then it will blend with whats already on the terrain rather than full strength value  
                    //of 1 if needed.  
                    if (TerrainMapColors[x + y * terrainWidth].R == 255)
                        if (TerrainMapColors[x + y * terrainWidth].G == 255)
                            if (TerrainMapColors[x + y * terrainWidth].B == 255)
                                //sand  
                                TerrainData[x, y] = 1;

                    if (TerrainMapColors[x + y * terrainWidth].R == 0)
                        if (TerrainMapColors[x + y * terrainWidth].G == 255)
                            if (TerrainMapColors[x + y * terrainWidth].B == 0)
                                //grass  
                                TerrainData[x, y] = 2;

                    if (TerrainMapColors[x + y * terrainWidth].R == 55)
                        if (TerrainMapColors[x + y * terrainWidth].G == 55)
                            if (TerrainMapColors[x + y * terrainWidth].B == 55)
                                //rock  
                                TerrainData[x, y] = 3;

                    if (TerrainMapColors[x + y * terrainWidth].R == 0)
                        if (TerrainMapColors[x + y * terrainWidth].G == 0)
                            if (TerrainMapColors[x + y * terrainWidth].B == 0)
                                //road  
                                TerrainData[x, y] = 4;
                }

            for (int x = 0; x < terrainWidth; x++)
                for (int y = 0; y < terrainLength; y++)
                {
                    heightData[x, y] = (heightData[x, y] - minimumHeight) / (maximumHeight - minimumHeight) * 30;
                }
        } 
 
       
        /*
         * Define Vertices
         */
        /*
          * Define Vertices
          */
        private VertexMultitextured[] SetUpTerrainVertices()
        {
            terrainVertices = new VertexMultitextured[terrainWidth * terrainLength];



            for (int x = 0; x < terrainWidth; x++)
            {
                for (int y = 0; y < terrainLength; y++)
                {
                    // generate a vertex for each pixel of the heightmap
                    // a terrain is generated in x,-y direction
                    terrainVertices[x + (y * terrainWidth)].Position = new Vector3(x, heightData[x, y], -y);
                    terrainVertices[x + (y * terrainWidth)].TextureCoordinate.X = (float)x / 30.0f; // /30 to stretch texture so it looks realistic
                    terrainVertices[x + (y * terrainWidth)].TextureCoordinate.Y = (float)y / 30.0f;

                   



                    // X = Sand, Y = Grass, Z = Stone and W = Snow


                    if (TerrainData[x, y] == 1)
                    {

                        terrainVertices[x + y * terrainWidth].TexWeights.X = 1;
                        terrainVertices[x + y * terrainWidth].TexWeights.Y = 0;
                        terrainVertices[x + y * terrainWidth].TexWeights.Z = 0;
                        terrainVertices[x + y * terrainWidth].TexWeights.W = 0;
                    }

                    if (TerrainData[x, y] == 2)
                    {
                        terrainVertices[x + y * terrainWidth].TexWeights2.X = 0;
                        terrainVertices[x + y * terrainWidth].TexWeights2.Y = 1;
                        terrainVertices[x + y * terrainWidth].TexWeights2.Z = 0;
                        terrainVertices[x + y * terrainWidth].TexWeights2.W = 0;
                    }

                    if (TerrainData[x, y] == 3)
                    {
                        terrainVertices[x + y * terrainWidth].TexWeights3.X = 0;
                        terrainVertices[x + y * terrainWidth].TexWeights3.Y = 0;
                        terrainVertices[x + y * terrainWidth].TexWeights3.Z = 1;
                        terrainVertices[x + y * terrainWidth].TexWeights3.W = 0;
                    }

                    if (TerrainData[x, y] == 4)
                    {


                        terrainVertices[x + y * terrainWidth].TexWeights1.X = 0;
                        terrainVertices[x + y * terrainWidth].TexWeights1.Y = 0;
                        terrainVertices[x + y * terrainWidth].TexWeights1.Z = 0;
                        terrainVertices[x + y * terrainWidth].TexWeights1.W = 1;
                    }  
                  



                    float total1 = terrainVertices[x + y * terrainWidth].TexWeights.X;
                    total1 += terrainVertices[x + y * terrainWidth].TexWeights.Y;
                    total1 += terrainVertices[x + y * terrainWidth].TexWeights.Z;
                    total1 += terrainVertices[x + y * terrainWidth].TexWeights.W;

                    terrainVertices[x + y * terrainWidth].TexWeights.X /= total1;
                    terrainVertices[x + y * terrainWidth].TexWeights.Y /= total1;
                    terrainVertices[x + y * terrainWidth].TexWeights.Z /= total1;
                    terrainVertices[x + y * terrainWidth].TexWeights.W /= total1; 


                   







                }
            }

            return terrainVertices;
        }

        /*
         * Set indices clockwise to build faces
         */
        private int[] SetUpTerrainIndices()
        {
            /*
             * new int[(terrainWidth - 1) * (terrainLength - 1) * 6]
             * Example, plane consisting of 4x3 points: first row is build out of 3 quads, second one also.
             *                                        row   col    3 points are needed for each triangle, 6 for one quad
             * so for a 4x3 points plane one can say (4-1)*(3-1) * 6
             */
            int[] indices = new int[(terrainWidth - 1) * (terrainLength - 1) * 6];
            int counter = 0;
            for (int y = 0; y < terrainLength - 1; y++)
            {
                for (int x = 0; x < terrainWidth - 1; x++)
                {
                    int lowerLeft = x + y * terrainWidth;
                    int lowerRight = (x + 1) + y * terrainWidth;
                    int topLeft = x + (y + 1) * terrainWidth;
                    int topRight = (x + 1) + (y + 1) * terrainWidth;

                    // order clockwise
                    indices[counter++] = topLeft;
                    indices[counter++] = lowerRight;
                    indices[counter++] = lowerLeft;

                    indices[counter++] = topLeft;
                    indices[counter++] = topRight;
                    indices[counter++] = lowerRight;
                }
            }

            return indices;
        }

        /* 
         * Calculate Normals on Terrain for realistic shadows
         */
        private VertexMultitextured[] CalculateNormals(VertexMultitextured[] vertices, int[] indices)
        {
            // initialise normals with 0 0 0
            for (int i = 0; i < vertices.Length; i++)
                vertices[i].Normal = new Vector3(0, 0, 0);

            for (int i = 0; i < indices.Length / 3; i++)
            {
                // Calculate Triangle
                int index1 = indices[i * 3];
                int index2 = indices[i * 3 + 1];
                int index3 = indices[i * 3 + 2];

                // Calculate normal on triangle
                Vector3 side1 = vertices[index1].Position - vertices[index3].Position;
                Vector3 side2 = vertices[index1].Position - vertices[index2].Position;
                Vector3 normal = Vector3.Cross(side1, side2);

                // apply normal on all three vertices of triangle
                vertices[index1].Normal += normal;
                vertices[index2].Normal += normal;
                vertices[index3].Normal += normal;
            }

            // normalize all normals
            for (int i = 0; i < vertices.Length; i++)
                vertices[i].Normal.Normalize();

            return vertices;
        }

        /*
         * Copy vertices and indices onto grafikcard buffer to save performance (so this data has to be transferred ONLY ONCE)
         */
        private void CopyToTerrainBuffers(VertexMultitextured[] vertices, int[] indices)
        {
            terrainVertexBuffer = new VertexBuffer(device, vertices.Length * VertexMultitextured.SizeInBytes, BufferUsage.WriteOnly);
            terrainVertexBuffer.SetData(vertices);

            terrainIndexBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.WriteOnly);
            terrainIndexBuffer.SetData(indices);
        }

        /* 
         * This draws the terrain
         */
        public void DrawTerrain(Effect effect)
        {
            effect.CurrentTechnique = effect.Techniques["MultiTextured"];
            effect.Parameters["SandTexture"].SetValue(sandTexture);
            effect.Parameters["GrassTexture"].SetValue(grassTexture);
            effect.Parameters["RockTexture"].SetValue(rockTexture);
            effect.Parameters["SnowTexture"].SetValue(snowTexture);


            Matrix world = Matrix.CreateTranslation(new Vector3(77, 0, 0));
            Matrix worldMatrix = Matrix.Identity;
            effect.Parameters["World"].SetValue(worldMatrix);
            effect.Parameters["EnableLighting"].SetValue(true);
            effect.Parameters["Ambient"].SetValue(0.4f);
            effect.Parameters["LightDirection"].SetValue(new Vector3(-0.5f, -1, -0.5f));

            effect.Begin();
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();

                device.Vertices[0].SetSource(terrainVertexBuffer, 0, VertexMultitextured.SizeInBytes); // tell it that this data is in graka buffer
                device.Indices = terrainIndexBuffer;
                device.VertexDeclaration = terrainVertexDeclaration;

                int noVertices = terrainVertexBuffer.SizeInBytes / VertexMultitextured.SizeInBytes;
                int noTriangles = terrainIndexBuffer.SizeInBytes / sizeof(int) / 3;
                device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, noVertices, 0, noTriangles);

                pass.End();
            }
            effect.End();
        }
        public float GetHeight(float x, float z)
        {
            int xmin = (int)Math.Floor(x);
            int xmax = xmin + 1;
            int zmin = (int)Math.Floor(z);
            int zmax = zmin + 1;

            if (
                (xmin < 0) || (zmin < 0) ||
                (xmax > heightData.GetUpperBound(0)) ||
                (zmax > heightData.GetUpperBound(1)))
            {
                return 0;
            }

            Vector3 p1 = new Vector3(xmin, heightData[xmin, zmax], zmax);
            Vector3 p2 = new Vector3(xmax, heightData[xmax, zmin], zmin);
            Vector3 p3;

            if ((x - xmin) + (z - zmin) <= 1)
            {
                p3 = new Vector3(xmin, heightData[xmin, zmin], zmin);
            }
            else
            {
                p3 = new Vector3(xmax, heightData[xmax, zmax], zmax);
            }

            Plane plane = new Plane(p1, p2, p3);

            Ray ray = new Ray(new Vector3(x, 0, z), Vector3.Up);

            float? height = ray.Intersects(plane);

            return height.HasValue ? height.Value : 0f;
        }
       
    }
}

shader info for terrain

//----------------------------------------------------
//--                                                --
//--             www.riemers.net                 --
//--         Series 4: Advanced terrain             --
//--                 Shader code      
//--             Adapted to work with this project  --
//--                                                --
//----------------------------------------------------

//------- Constants --------
float4x4 View;
float4x4 Projection;
float4x4 World;
float4x4 DecalViewProj;  
float3 decalPosition;  
float3 LightDirection;
float Ambient;
bool EnableLighting;
float3 groundCursorPosition;  
int groundCursorSize;  
int groundCursorScale;  
//bool bShowCursor;  



// Textures  
Texture SandTexture;  
Texture GrassTexture;  
Texture RockTexture;  
Texture SnowTexture;  
Texture GroundCursorTexture;  
 
 
 
// Samplers  
sampler SandTextureSampler = sampler_state   
{  
    texture = <SandTexture>;   
    magfilter = LINEAR;   
    minfilter = LINEAR;   
    mipfilter=LINEAR;   
    AddressU = mirror;   
    AddressV = mirror;  
};  
 
sampler GrassTextureSampler = sampler_state   
{  
    texture = <GrassTexture>;   
    magfilter = LINEAR;   
    minfilter = LINEAR;   
    mipfilter=LINEAR;   
    AddressU = mirror;   
    AddressV = mirror;  
};  
 
sampler RockTextureSampler = sampler_state   
{  
    texture = <RockTexture>;  
    magfilter = LINEAR;   
    minfilter = LINEAR;   
    mipfilter=LINEAR;   
    AddressU = mirror;   
    AddressV = mirror;  
};  
 
sampler SnowTextureSampler = sampler_state   
{  
    texture = <SnowTexture>;  
    magfilter = LINEAR;   
    minfilter = LINEAR;   
    mipfilter=LINEAR;   
    AddressU = mirror;   
    AddressV = mirror;  
};  
 
sampler CursorTextureSampler = sampler_state   
{  
    texture = <GroundCursorTexture>;  
    magfilter = LINEAR;   
    minfilter = LINEAR;   
    mipfilter=LINEAR;   
    AddressU = clamp;   
    AddressV = clamp;  
};  

//------- Texture Samplers --------

Texture xTexture;

sampler TextureSampler = sampler_state { texture = <xTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};



// Technique: Multitextured (= terrain)

struct  MultiTexVertexToPixel  
{
    float4 Position         : POSITION;    
    float4 Color            : COLOR0;
    float3 Normal            : TEXCOORD0;
    float2 TextureCoords    : TEXCOORD1;
    float4 LightDirection    : TEXCOORD2;
    float4 TextureWeights    : TEXCOORD3;
    float4 TextureWeights1    : TEXCOORD4;
    float  Depth : TEXCOORD5;
};

struct MultiTexPixelToFrame 
{
    float4 Color : COLOR0;
};

// Vertexshader
MultiTexVertexToPixel MultiTexturedVS( float4 inPos : POSITION, float3 inNormal: NORMAL, float2 inTexCoords: TEXCOORD0, float4 inTexWeights: TEXCOORD1,float4 inTexWeights1: TEXCOORD2)
{    
    MultiTexVertexToPixel Output = (MultiTexVertexToPixel)0;
    float4x4 preViewProjection = mul (View, Projection);
    float4x4 preWorldViewProjection = mul (World, preViewProjection);
    
    Output.Position = mul(inPos, preWorldViewProjection);
    Output.Normal = mul(normalize(inNormal), World);
    Output.TextureCoords = inTexCoords;
    Output.LightDirection.xyz = - LightDirection;
    Output.LightDirection.w = 1;    
    Output.TextureWeights = inTexWeights;
    Output.TextureWeights1 = inTexWeights1;
    Output.Depth = Output.Position.z;
    
    return Output;    
}

// Pixelshader
MultiTexPixelToFrame MultiTexturedPS(MultiTexVertexToPixel PSIn)
{
    MultiTexPixelToFrame Output = (MultiTexPixelToFrame)0;
    
    // Depth details - see http://blog.goltergaul.de/2009/10/xna-directx-c-game-project-%E2%80%93-depth-details/
    float blendDistance = 30;
	float blendWidth = 100;
	float blendFactor = clamp((PSIn.Depth-blendDistance)/blendWidth, 0, 1);        
    
    float lightingFactor = 1;
    if (EnableLighting)
        lightingFactor = saturate(saturate(dot(PSIn.Normal, PSIn.LightDirection)) + Ambient);
	
	 float4 farColor;  
    farColor = tex2D(SandTextureSampler, PSIn.TextureCoords)*PSIn.TextureWeights.x;  
    farColor += tex2D(GrassTextureSampler, PSIn.TextureCoords)*PSIn.TextureWeights.y;  
    farColor += tex2D(RockTextureSampler, PSIn.TextureCoords)*PSIn.TextureWeights.z;  
    farColor += tex2D(SnowTextureSampler, PSIn.TextureCoords)*PSIn.TextureWeights.w;  
   
    float4 nearColor;  
    float2 nearTextureCoords = PSIn.TextureCoords*3;  
    nearColor = tex2D(SandTextureSampler, nearTextureCoords)*PSIn.TextureWeights.x;  
    nearColor += tex2D(GrassTextureSampler, nearTextureCoords)*PSIn.TextureWeights.y;  
    nearColor += tex2D(RockTextureSampler, nearTextureCoords)*PSIn.TextureWeights.z;  
    nearColor += tex2D(SnowTextureSampler, nearTextureCoords)*PSIn.TextureWeights.w;  

	  Output.Color = farColor*blendFactor + nearColor*(1-blendFactor);  
 
    // Ground Cursor  
    //if(bShowCursor)  
    //{  
        float4 CursorColor = tex2D(CursorTextureSampler, (PSIn.TextureCoords * (groundCursorScale / groundCursorSize)) - (groundCursorPosition.xz * (groundCursorScale / groundCursorSize)) + 0.5f);  
        Output.Color += CursorColor;  
    //}  
 
    Output.Color *= saturate(lightingFactor + Ambient);  

    return Output;
}

technique MultiTextured
{
    pass Pass0
    {
        VertexShader = compile vs_1_1 MultiTexturedVS();
        PixelShader = compile ps_2_0 MultiTexturedPS();
    }
}

/////////////////////// Technique simplest (= car effect)

float4x4 xWorldViewProjection; // same with world transformation

struct VertexToPixel
{
    float4 Position     : POSITION;    
    float2 TexCoords    : TEXCOORD0;
};

struct PixelToFrame // this is outputted by the pixel shader
{
    float4 Color        : COLOR0;
};

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float2 inTexCoords : TEXCOORD0)
{
    VertexToPixel Output = (VertexToPixel)0;
    
	Output.Position = mul(inPos, xWorldViewProjection);
    Output.TexCoords = inTexCoords;

    return Output;
}
/*
VertexToPixel SimplestVertexShader(float4 inPos : POSITION, float4 inColor : COLOR0)
{
    VertexToPixel Output = (VertexToPixel)0; // create empty output struct

    Output.Position = mul(inPos, ViewProjection); // conversion from 3d in 2d 
   // Output.Color = inColor; // pass color to the pixel shader
    Output.Color.rgb = inPos.xyz; // Let the color indicate xyz axis
    Output.Position3D = inPos; // pass position to pixel shader
    
    return Output;
}*/

float DotProduct(float3 lightPos, float3 pos3D, float3 normal)
{
    float3 lightDir = normalize(pos3D - lightPos);
    return dot(-lightDir, normal);    
}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)
{
    //PSIn.TexCoords.y--;
    PixelToFrame Output = (PixelToFrame)0;    

    //Output.Color = PSIn.Color; // assign recieved color to pixel
    //Output.Color.rgb = PSIn.Position3D.xyz; // assign color in relation to position
    Output.Color = tex2D(TextureSampler, PSIn.TexCoords); // output Texture

    return Output;
}

technique Simplest
{
    pass Pass0
    {
        VertexShader = compile vs_1_1 SimplestVertexShader();
        PixelShader = compile ps_1_1 OurFirstPixelShader();
    }
}

//------- Technique: Colored (= Debug triagles)--------

struct VertexToPixelColored
{
    float4 Position   	: POSITION;    
    float4 Color		: COLOR0;
    float LightingFactor: TEXCOORD0;
    float2 TextureCoords: TEXCOORD1;
};

struct PixelToFrameColored
{
    float4 Color : COLOR0;
};


VertexToPixelColored ColoredVS( float4 inPos : POSITION, float4 inColor: COLOR, float3 inNormal: NORMAL)
{	
	VertexToPixelColored Output = (VertexToPixelColored)0;
	float4x4 preViewProjection = mul (View, Projection);
	float4x4 preWorldViewProjection = mul (World, preViewProjection);
    
	Output.Position = mul(inPos, preWorldViewProjection);
	Output.Color = inColor;
	
	float3 Normal = normalize(mul(normalize(inNormal), World));	
	Output.LightingFactor = 1;
	if (EnableLighting)
		Output.LightingFactor = saturate(dot(Normal, -LightDirection));
    
	return Output;    
}

PixelToFrameColored ColoredPS(VertexToPixelColored PSIn) 
{
	PixelToFrameColored Output = (PixelToFrameColored)0;		
    
	Output.Color = PSIn.Color;
	Output.Color.rgb *= saturate(PSIn.LightingFactor + Ambient);
	
	return Output;
}

technique Colored_2_0
{
	pass Pass0
	{   
		VertexShader = compile vs_2_0 ColoredVS();
		PixelShader  = compile ps_2_0 ColoredPS();
	}
}

technique Colored
{
	pass Pass0
	{   
		VertexShader = compile vs_1_1 ColoredVS();
		PixelShader  = compile ps_1_1 ColoredPS();
	}
}




Sponsor:

#2 MiniMigi   Crossbones+   -  Reputation: 1433

Like
0Likes
Like

Posted 26 July 2013 - 02:28 PM

To sample a texture with the intensity based on, example, the red channel of a texture, you could do the following:

 

newColor = lerp(color, t_Grass.Sample(sampler, texcoord), t_channels.Sample(sampler, texcoord).r);

 

The lerp function provides a linear interpolation of the two colors, or pixels.

 

If this is what you're asking for. huh.png


Edited by Migi0027, 26 July 2013 - 02:29 PM.


#3 football94   Members   -  Reputation: 194

Like
0Likes
Like

Posted 26 July 2013 - 04:16 PM

Thanks for the reply

 

the project that Im working on involves collision detection ,what Im trying to do is to modify the terrain code and make it into

a sort of texture splatting(http://en.wikipedia.org/wiki/Texture_splatting)so that for example a model car moving across a multitextured terrain,

as it hits a certain layer of texture coordinates emits certain particles.but I think the way the terrain is setup is that all textures are on one single layer of texture

coordinates(red channel) which is the heightmap itself and what I wanted to do is use the blue channel to implement a layer of uv coordinates(ex grasstexture)

and another layer of uv coordinates for the green channel(ex sand texture) I think the mistake I been making so far is I thought the car was reading the uv coordinates

from the blue and green channels but the whole time it(return ray)was just reading one layer of texture coordinates which belonged to the heightmap.

 

                                                                                                                                                    

                                                                                                 Thanks again

 

 

 

below is a youtube  link to the original project Ive been trying to modify

what I wanted to do is to get the cars highlighted triangles to change color

for every different texture hit(ex triangles turn yellow on sand, turn green on grass etc,...)

I have the collision method setup and an understanding of particles  I just need

to make the multilayer uv coordinate adjustments to the terrain

 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS