hey for some days now I’m trying to extract the position of the vertices animated by Unity’s FX/Water4 shader. Failing big time (nooby beginner level
). all i get back is 0 or (0,0,0,1) ![]()
Tried it in vert function as wel as in the frag function. to simplify things i wrote a small shader that just moves a plane up and down and tried the same there, fail again…
any help would be greatly appreciated!
my last attempt was trying to convert fragment into world position…
Shader "Custom/GetHeight" {
Properties {
_MainTex("Main texture", 2D) = "white" {}
_Scale("Scale", float) = 2
_Speed ("Speed", float) = 0.5
_VertPos ("Vertex Position", Vector) = (0.0 ,0.0, 0.0, 1)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Pass
{
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
float _Speed;
float _Scale;
float4 _VertPos;
struct Input {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float4 worldSpacePosition : TEXCOORD1;
};
v2f vert(appdata_full v)
{
v2f o;
//o.pos = UnityObjectToClipPos(v.vertex);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.pos.y += sin(_Time.w * _Speed )*_Scale;
o.worldSpacePosition = mul( unity_ObjectToWorld, o.pos);
o.uv = v.vertex.xz;
return o;
}
fixed4 frag (v2f i) : SV_TARGET
{
float4 pixelWorldSpacePosition = i.worldSpacePosition;
_VertPos = pixelWorldSpacePosition;
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
also last attempt was to get it in the OnRenderObject function instead of Update
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetWaterPlaneHeight : MonoBehaviour {
public Material mat;
public Vector4 waterPlaneHeight;
public float height;
// Use this for initialization
void Start ()
{
if(mat == null)
mat = GetComponentInChildren<Renderer>().sharedMaterial;
}
// Update is called once per frame
void OnRenderObject ()
{
waterPlaneHeight = mat.GetVector("_VertPos");
height = mat.GetVector("_VertPos").y+transform.position.y;
}
}