Skip to content

Instantly share code, notes, and snippets.

@wblackall
Last active December 11, 2015 06:48
Show Gist options
  • Save wblackall/4561741 to your computer and use it in GitHub Desktop.
Save wblackall/4561741 to your computer and use it in GitHub Desktop.
This is a class I wrote based on the FWipeSprite in Futile's development branch (https://github.com/MattRix/Futile/blob/development/BananaDemoProject/Assets/Plugins/Futile/Extras/FWipeSprite.cs). It's just a proof of concept. I wanted to learn not only how to draw things, but also how to keep a texture still while moving a cropped "window" aroun…
using UnityEngine;
using System;
// this Point class is just so I can use it with GoKit
public class Point {
private float x_;
private float y_;
public Point(float x, float y) {
this.x = x;
this.y = y;
}
public float x {
get {return x_;}
set {x_ = value;}
}
public float y {
get {return y_;}
set {y_ = value;}
}
}
public class CroppedSprite : FSprite
{
float quadSize_ = 400f;
float angle_;
float sourceScale = 1f;
Point squareOffset = new Point(0, 0);
Point sourceOffset = new Point(0, 0);
public CroppedSprite (string elementName) : base()
{
Init(FFacetType.Quad, Futile.atlasManager.GetElementWithName(elementName),1);
_isAlphaDirty = true;
UpdateLocalVertices();
// GoKit stuff (making it move up and down as well as shrink, grow, and rotate)
// if you were going to use this class in something, I'd recommend not
// doing this stuff inside the class. It should be as abstract as possible.
// But it's here for simplicity and demonstration purposes
// move the square up and down
float duration1 = 2;
Tween tween1 = new Tween(squareOffset, duration1, new TweenConfig().floatProp("y", 300, true));
Tween tween2 = new Tween(squareOffset, duration1 * 2, new TweenConfig().floatProp("y", -600, true));
Tween tween3 = new Tween(squareOffset, duration1, new TweenConfig().floatProp("y", 300, true));
TweenChain chain1 = new TweenChain();
chain1.setIterations(-1);
chain1.append(tween1).append(tween2).append(tween3);
Go.addTween(chain1);
chain1.play();
// shrink and grow
float duration2 = 0.8f;
Tween tween4 = new Tween(this, duration2, new TweenConfig().floatProp("quadSize", 0).setEaseType(EaseType.SineOut));
Tween tween5 = new Tween(this, duration2, new TweenConfig().floatProp("quadSize", 400, true).setEaseType(EaseType.SineIn));
TweenChain chain2 = new TweenChain();
chain2.setIterations(-1);
chain2.append(tween4).append(tween5);
Go.addTween(chain2);
chain2.play();
// rotate
Go.to(this, 3, new TweenConfig().setIterations(-1).floatProp("angle", 360, true));
}
override public void PopulateRenderLayer()
{
if(_isOnStage && _firstFacetIndex != -1)
{
_isMeshDirty = false;
int vertexIndex0 = _firstFacetIndex * 4;
Vector3[] vertices = _renderLayer.vertices;
Vector2[] uvs = _renderLayer.uvs;
Vector2[] localVertices = new Vector2[4];
Vector2[] localUVVertices = new Vector2[4];
Color[] colors = _renderLayer.colors;
// this part figures out where square's the vertices are depending on the angle and size
localVertices[0] = new Vector2 // top right vertex
(
quadSize / Mathf.Sqrt(2) * Mathf.Cos(angle * Mathf.Deg2Rad) + squareOffset.x,
quadSize / Mathf.Sqrt(2) * Mathf.Sin(angle * Mathf.Deg2Rad) + squareOffset.y
);
localVertices[1] = new Vector2 // bottom right vertex
(
quadSize / Mathf.Sqrt(2) * Mathf.Cos((angle + 90) * Mathf.Deg2Rad) + squareOffset.x,
quadSize / Mathf.Sqrt(2) * Mathf.Sin((angle + 90) * Mathf.Deg2Rad) + squareOffset.y
);
localVertices[2] = new Vector2 // bottom left vertex
(
quadSize / Mathf.Sqrt(2) * Mathf.Cos((angle + 180) * Mathf.Deg2Rad) + squareOffset.x,
quadSize / Mathf.Sqrt(2) * Mathf.Sin((angle + 180) * Mathf.Deg2Rad) + squareOffset.y
);
localVertices[3] = new Vector2 // top left vertex
(
quadSize / Mathf.Sqrt(2) * Mathf.Cos((angle + 270) * Mathf.Deg2Rad) + squareOffset.x,
quadSize / Mathf.Sqrt(2) * Mathf.Sin((angle + 270) * Mathf.Deg2Rad) + squareOffset.y
);
float uvWidth = (_element.uvTopRight.x - _element.uvTopLeft.x);
float uvHeight = (_element.uvTopRight.y - _element.uvBottomRight.y);
// this for loop creates vertices on the texture so it knows where to apply it to the square's vertices
for (int i = 0; i < 4; i++) {
localUVVertices[i] = new Vector2
(
_element.uvBottomLeft.x + uvWidth / 2f + localVertices[i].x * uvWidth / (_element.sourceSize.x * sourceScale) - sourceOffset.x / _element.sourceSize.x,
_element.uvBottomLeft.y + uvHeight / 2f + localVertices[i].y * uvHeight / (_element.sourceSize.y * sourceScale) - sourceOffset.y / _element.sourceSize.y
);
}
// apply the colors
for (int i = 0; i < 4; i++) {
colors[vertexIndex0 + i] = _alphaColor;
}
// apply the vertice locations to the actual locations on the 3D mesh
_concatenatedMatrix.ApplyVector3FromLocalVector2(ref vertices[vertexIndex0 + 0], localVertices[0],0);
_concatenatedMatrix.ApplyVector3FromLocalVector2(ref vertices[vertexIndex0 + 1], localVertices[1],0);
_concatenatedMatrix.ApplyVector3FromLocalVector2(ref vertices[vertexIndex0 + 2], localVertices[2],0);
_concatenatedMatrix.ApplyVector3FromLocalVector2(ref vertices[vertexIndex0 + 3], localVertices[3],0);
// actually apply the texture vertices
uvs[vertexIndex0 + 0] = localUVVertices[0];
uvs[vertexIndex0 + 1] = localUVVertices[1];
uvs[vertexIndex0 + 2] = localUVVertices[2];
uvs[vertexIndex0 + 3] = localUVVertices[3];
_renderLayer.HandleVertsChange();
}
}
public float quadSize {
get {return quadSize_;}
set {
quadSize_ = value;
_isMeshDirty = true;
}
}
public float angle {
get {return angle_;}
set {
angle_ = value;
_isMeshDirty = true;
}
}
}
// made by Whitaker Trebella
// follow me on twitter: @wtrebella
// HUGE THANKS TO MATT RIX (@MattRix) who created Futile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment