- Home /
Swizzling cube face to other faces in the cube
This is a shader which take the front face of a cube and swizzles it to the other faces of the cube, or at least is supposed to. But no matter which directions I input for _WidthDir and _HeightDir it just shows the front face of the cube.
Anyone have any ideas?
Shader "Custom/PlanetShader" {
Properties {
_Position ("Position", Vector) = (0,0,0,0)
_WidthDir ("WidthDir", Vector) = (1,0,0,0)
_HeightDir ("HeightDir", Vector) = (0,1,0,0)
_Width ("Width", Float) = 1
}
SubShader {
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform float4 _Position;
uniform float4 _WidthDir;
uniform float4 _HeightDir;
uniform float _Width;
struct v2f{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(appdata_base v){
v2f o;
v.vertex = float4((_Position.xyz + ( (_WidthDir.xyz * v.vertex.x + _HeightDir.xyz * v.vertex.y) * _Width)).xyz, 1);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
half4 frag(v2f i) : COLOR{
return half4(.1,.1,1,.9);
}
ENDCG
}
}
}
Did you really mean "swizzle" (which has a particular meaning in computer graphics), or did you mean "blend", or "rotate", or perhaps something else?! Note that a shader applies to a material which is assigned to an entire object, not just one face of it, so I'm not quite sure how you're defining what the "front" face of your cube is and how you're going to "swizzle" that separately from the other faces?
I think both your problem description and code are confusing - perhaps you could draw a picture to illustrate what you'd like to see happen, and then it would be easier to help?
$$anonymous$$y mesh is just the front square face of the cube. I then assign this mesh to other mesh renderers and in the shader give each mesh renderer its own parameters to move the vertices to the correct location on the cube. $$anonymous$$y goal is to make a cube out of a single square mesh. This is the beginnings of a shader for a quadtree cubesphere procedural planet. Since I'm just rotating the cube face to a different face I thought the term swizzling applied, but apparently it doesn't since I'm not just rearranging the order of their values.
An update to the problem is when I hardcode the values in my shader I see the correct movement on the screen. It looks like when I call my$$anonymous$$eshRenderer.renderer.material.SetVector/float/whatever nothing actually gets passed to the shader. No idea what I'm doing wrong. Here's an example how I'm doing it:
GameObject prefab = _tree.sphere.GetPrefab();//Returns instantiated prefab with transform and parent set. $$anonymous$$eshRenderer mr =($$anonymous$$eshRenderer)prefab.GetComponent("$$anonymous$$eshRenderer"); mr.renderer.material.SetVector("HeightDir", _tree.heightDir);
The problem with mr.renderer.material.SetVector("HeightDir", _tree.heightDir); is that your Shaderlab property is called _HeightDir, not HeightDir.
I thought that was supposed to match the string..... bummer. Thanks again.
If you cut and paste your comment as an answer I'll mark it as correct for the karma if you want.
Answer by tanoshimi · Sep 17, 2013 at 01:27 PM
The problem with mr.renderer.material.SetVector("HeightDir", _tree.heightDir); is that your Shaderlab property is called _HeightDir, not HeightDir.
Your answer
Follow this Question
Related Questions
Where is model space origin in shaderlab? 1 Answer
Parsing values in multipass cg shader 0 Answers
Vertex Shader Properties Issu 1 Answer