21
2015
Matrix Playground Shader
Finally decided to try shader matrices.. (because having issues with custom billboard shader)
Doing Translate, Scale, Rotations with matrix operations seems quite simple actually!
Would had been totally lost without these two links:
http://www.codinglabs.net/article_world_view_projection_matrix.aspx
http://www.gamedev.net/topic/610115-solved-rotation-deforming-mesh-opengl-es-20/#entry4859756
How to use the shader:
– Get the source below, copy to your project
– Create new material, assign this shader to that material
– Create default box 3D object, assign the material to that
– Try adjusting the shader values on that material
Current shader source: *Annoying how github gist breaks the formatting.. click the filename below the source to view in github
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Matrix PlayGround Shader – UnityCoder.com | |
// References: | |
// Matrices http://www.codinglabs.net/article_world_view_projection_matrix.aspx | |
// Rotation: http://www.gamedev.net/topic/610115-solved-rotation-deforming-mesh-opengl-es-20/#entry4859756 | |
Shader "UnityCoder/MatrixPlayground" | |
{ | |
Properties | |
{ | |
_MainTex ("Texture", 2D) = "white" {} | |
_tX ("TranslateX", float) = 0 | |
_tY ("TranslateY", float) = 0 | |
_tZ ("TranslateZ", float) = 0 | |
_sX ("ScaleX", float) = 1 | |
_sY ("ScaleY", float) = 1 | |
_sZ ("ScaleZ", float) = 1 | |
_rX ("RotateX", float) = 0 | |
_rY ("RotateY", float) = 0 | |
_rZ ("RotateZ", float) = 0 | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" } | |
//Cull Off | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float2 uv : TEXCOORD0; | |
UNITY_FOG_COORDS(1) | |
float4 vertex : SV_POSITION; | |
}; | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
float _tX,_tY,_tZ; | |
float _sX,_sY,_sZ; | |
float _rX,_rY,_rZ; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
float4x4 translateMatrix = float4x4(1, 0, 0, _tX, | |
0, 1, 0, _tY, | |
0, 0, 1, _tZ, | |
0, 0, 0, 1); | |
float4x4 scaleMatrix = float4x4(_sX, 0, 0, 0, | |
0, _sY,0, 0, | |
0, 0, _sZ,0, | |
0, 0, 0, 1); | |
float angleX = radians(_rX); | |
float c = cos(angleX); | |
float s = sin(angleX); | |
float4x4 rotateXMatrix = float4x4( 1, 0, 0, 0, | |
0, c, -s, 0, | |
0, s, c, 0, | |
0, 0, 0, 1); | |
float angleY = radians(_rY); | |
c = cos(angleY); | |
s = sin(angleY); | |
float4x4 rotateYMatrix = float4x4( c, 0, s, 0, | |
0, 1, 0, 0, | |
-s, 0, c, 0, | |
0, 0, 0, 1); | |
float angleZ = radians(_rZ); | |
c = cos(angleZ); | |
s = sin(angleZ); | |
float4x4 rotateZMatrix = float4x4( c, -s, 0, 0, | |
s, c, 0, 0, | |
0, 0, 1, 0, | |
0, 0, 0, 1); | |
float4 localVertexPos = v.vertex; | |
// NOTE: the order matters, try scaling first before translating, different results | |
float4 localTranslated = mul(translateMatrix,localVertexPos); | |
float4 localScaledTranslated = mul(localTranslated,scaleMatrix); | |
float4 localScaledTranslatedRotX = mul(localScaledTranslated,rotateXMatrix); | |
float4 localScaledTranslatedRotXY = mul(localScaledTranslatedRotX,rotateYMatrix); | |
float4 localScaledTranslatedRotXYZ = mul(localScaledTranslatedRotXY,rotateZMatrix); | |
o.vertex = mul(UNITY_MATRIX_MVP, localScaledTranslatedRotXYZ); | |
o.uv = TRANSFORM_TEX(v.uv, _MainTex); | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
fixed4 col = tex2D(_MainTex, i.uv); | |
return col; | |
} | |
ENDCG | |
} | |
} | |
} |
Related Posts
12 Comments + Add Comment
Leave a comment
Recent posts
- MotionVector Effect: Object “disappears” when paused
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
Recent Comments
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
Is it possible to combine this shader with the unity 5 standard shader?
I could get all of the properties in the shader, but don’t know where to put the structs anf v2f vert function, I guess it should be placed in one of the cginc files.
Maybe looking from this could work, since they have added vertex colors there:
https://forum.unity3d.com/threads/standard-shader-with-vertex-colors.316529/
The Transformation Matrix for 2D Games
http://www.alanzucconi.com/2016/02/10/tranfsormation-matrix/
decoding projection matrix
http://xdpixel.com/decoding-a-projection-matrix/
Minimal project to outline matrix application in Unity
https://github.com/KoltesDigital/unity-matrix-order
(soon) cheat sheet blog post for matrices for tech arts
https://twitter.com/IRCSS/status/1202868860631887872
look at transformation matrix (c#)
https://twitter.com/IRCSS/status/1224826249262006277
Transformation Matrices (mostly in Maya)
https://twitter.com/troll_lock/status/1289127678390472704
Matrix Viewer
https://github.com/lujian101/UnityToolDist
Matrix operations for HLSL
https://gist.github.com/mattatz/86fff4b32d198d0928d0fa4ff32cf6fa
why did you do mul (vector, matrix) instead of mul(matrix, vector)? except for the Translation where you did mul (matrix, vector)
oh probably didn’t even realize that part..since it works..
more info:
https://forum.unity.com/threads/mul-function-and-matrices.445266/#post-2880535