Gradient on Platform
Hi All,
I am trying to create a color gradient on a platform, see picture 1 for example, that is what i currently have but i have just used multiple mesh's and gameObject.renderer.material.color = Color.green; using OnTriggerEnter2D, also i have copy's of those using the same method but for score.!
Me and a friend are in the middle of changing this but are getting position problems (see Debug.Log IMG)
We have the gradient all working! but the error is the positioning of the gradient, the debug is showing all different positions meaning that the gradient only changes part of the platform, (which will now be 1 single mesh).
Below is the Shader/s used and the new color change script.
using UnityEngine; using System.Collections;
public class ColorChange : MonoBehaviour {
public bool changed = false;
private Mesh mesh;
private Vector3 childTransform;
private Vector3[] worldPositions;
private Color[] colors;
void Start()
{
this.mesh = GetComponentInChildren<MeshFilter>().mesh;
childTransform = GetComponentInChildren<Transform>().position;
colors = new Color[mesh.vertexCount];
for(int i = 0; i < mesh.vertexCount; i++)
{
colors[i] = Color.white;
worldPositions = new Vector3[mesh.vertexCount];
worldPositions[i] = childTransform + mesh.vertices[i];
Debug.LogWarning (string.Format ("Object Position: {0} || Vertex Position: {1} || New Position: {2}", childTransform, mesh.vertices[i], worldPositions[i]));
}
}
void OnCollisionStay2D(Collision2D other)
{
if(other.gameObject.tag == "Player")
{
int closestVertex = 0;
for(int i = 1; i < worldPositions.Length; i++)
{
if (Vector3.Distance(worldPositions[i], other.transform.position) < Vector3.Distance(worldPositions[closestVertex], other.transform.position))
{
closestVertex = i;
Debug.Log (string.Format("Closest vertice is: {0}",i));
}
}
colors[closestVertex] = Color.green;
Debug.Log (string.Format ("Vertex Color changed: {0} on {1}", colors[closestVertex], mesh.vertices[closestVertex]));
mesh.colors = colors;
if(changed)
return;
for(int i = 0; i < mesh.vertexCount; i++)
{
if(colors[i] != Color.green)
{
changed = false;
break;
}
changed = true;
}
}
}
}
Shader "Custom/VertexColor" { Properties { _Color ("Color", Color) = (1.00, 1.00, 1.00, 1.00) } SubShader { Tags { "RenderType"="Opaque" } LOD 200
CGPROGRAM
#pragma surface surf Lambert
fixed4 _Color;
struct Input {
float2 uv_MainTex;
float4 color : COLOR;
};
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = IN.color.rgb * _Color.rgb;
o.Alpha = IN.color.a * _Color.a;
}
ENDCG
} FallBack "Diffuse" }
Shader 2 Shader "Custom/VertexColor2" { Properties {
} SubShader { Tags { "RenderType"="Opaque" } Pass {
CGPROGRAM
#pragma vertex wfiVertCol
#pragma fragment passThrough
#include "UnityCG.cginc"
struct VertOut
{
float4 position : POSITION;
float4 color : COLOR;
};
struct VertIn
{
float4 vertex : POSITION;
float4 color : COLOR;
};
VertOut wfiVertCol(VertIn input, float3 normal : NORMAL)
{
VertOut output;
output.position = mul(UNITY_MATRIX_MVP, input.vertex);
output.color = input.color;
return output;
}
struct FragOut
{
float4 color : COLOR;
};
FragOut passThrough(float4 color : COLOR)
{
FragOut output;
output.color = color;
return output;
}
ENDCG
}
}
There's not enough information to answer. You say the gradient is all working, but then say there's a problem. Perhaps paste the script here or show some screenshots of what is happening and what you actually want to happen.
Hi @Jessespike, i will update the question, thanks for letting me know.
https://play.google.com/store/apps/details?id=com.HappyChappyGames.Bounce_E&hl=en_GB
Link to my game on android, $$anonymous$$us the new art work, added in next update.