本文整理汇总了C#中UnityEngine.Texture2D.Apply方法的典型用法代码示例。如果您正苦于以下问题:C# Texture2D.Apply方法的具体用法?C# Texture2D.Apply怎么用?C# Texture2D.Apply使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Texture2D
的用法示例。
在下文中一共展示了Texture2D.Apply方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UnityGwenRenderer
/// <summary>
/// Initializes a new instance of the <see cref="UnityGwenRenderer"/> class.
/// </summary>
/// <param name="target">Unity render target.</param>
public UnityGwenRenderer()
{
whiteTex = new Texture2D(1, 1);
whiteTex.SetPixel(0, 0, UnityEngine.Color.white);
whiteTex.Apply();
m_ViewScale = Vector2.one;
}
示例2: AltGenTex
Texture2D AltGenTex(Vector3[] verts, ModuleBase module)
{
Texture2D tex = new Texture2D((int) Mathf.Sqrt(verts.Length), (int) Mathf.Sqrt(verts.Length));
int reso = (int) Mathf.Sqrt(verts.Length);
int pixelx = 0;
int pixely = 0;
for (int i = 0; i < verts.Length; i++)
{
verts[i] = transform.TransformPoint(verts[i]);
if (pixelx == reso)
{
pixelx = 0;
pixely += 1;
}
float noiseval = (float) module.GetValue(verts[i]);
noiseval = Mathf.Clamp(noiseval + 0.5f, 0f, 1f);
Color pixelColor = new Color(noiseval, noiseval, noiseval, 0);
tex.SetPixel(pixelx, pixely, pixelColor);
tex.Apply();
pixelx += 1;
}
tex.Apply();
return tex;
}
示例3: DrawRectWithOutline
public static void DrawRectWithOutline( Rect rect, Color color, Color colorOutline )
{
#if UNITY_EDITOR
Vector3[] rectVerts = { new Vector3(rect.x, rect.y, 0),
new Vector3(rect.x + rect.width, rect.y, 0),
new Vector3(rect.x + rect.width, rect.y + rect.height, 0),
new Vector3(rect.x, rect.y + rect.height, 0) };
Handles.DrawSolidRectangleWithOutline(rectVerts, color, colorOutline);
#else
Texture2D texture = new Texture2D(1, 1);
texture.SetPixel(0,0,colorOutline);
texture.Apply();
Rect rLine = new Rect( rect.x, rect.y, rect.width, 1 );
GUI.DrawTexture(rLine, texture);
rLine.y = rect.y + rect.height - 1;
GUI.DrawTexture(rLine, texture);
rLine = new Rect( rect.x, rect.y+1, 1, rect.height-2 );
GUI.DrawTexture(rLine, texture);
rLine.x = rect.x + rect.width - 1;
GUI.DrawTexture(rLine, texture);
rect.x += 1;
rect.y += 1;
rect.width -= 2;
rect.height -= 2;
texture.SetPixel(0,0,color);
texture.Apply();
GUI.DrawTexture(rect, texture);
#endif
}
示例4: StartProcess
public void StartProcess () {
// TextureScale.Bilinear (screenshot, 90, 160);
copy = Instantiate(screenshot);
pixels = copy.GetPixels();
for(int i=0; i<pixels.Length; i++){
r_color_map[(i/screenshot.width), i%screenshot.width,0] = (int)(pixels[i].r*255);
r_color_map[(i/screenshot.width), i%screenshot.width,1] =(int)(pixels[i].g*255);
r_color_map[i/screenshot.width, i%screenshot.width,2] =(int)(pixels[i].b*255);
float gray = pixels[i].grayscale;
pixels[i] = new Color(1f-gray, 1f-gray, 1f-gray, pixels[i].a);
}
copy.SetPixels(pixels);
copy.Apply();
copy = ChangeTextureContrast(copy, 0.999f);
copy.Apply();
AnalyTexture(copy.GetPixels());
try{
SaveLoad.SaveColorMap(r_color_map);
SaveLoad.SaveEdgeMap(r_edge_map);
SaveLoad.SaveEdgeGroup(r_edge_group);
}catch{};
Finished = true;
MFS = GetComponent<MapFromScreenshot>();
if(MFS!=null && MFS!=null){ //try to move map infront camera(in map creating page)
transform.position = new Vector3(-113f, -190f, -354f);
transform.eulerAngles = new Vector3(-59f/360f, -26f/360f, -90f/360f);
transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
}
// -------------------------------------for debug GUI ---------------------------------------------------------
for(int i =0;i<pixels.Length; i++){
pixels[i] = Color.black;
if(r_edge_map[i/copy.width, i%copy.width]==1)
pixels[i] = Color.white;
}
foreach(List<int[]> group in r_edge_group){
float r_color = Random.Range(0f,1f);
float r_color_g = Random.Range(0f,1f);
float r_color_b = Random.Range(0f,1f);
foreach(int[] point in group){
pixels[point[1]*copy.width + point[0]] = new Color(r_color, r_color_g, r_color_b);
}
}
copy.SetPixels(pixels);
copy.Apply();
show = false;
//-------------------------------------------------------------------------------------------------------------
}
示例5: ConvertToImage
IEnumerator ConvertToImage()
{
//grab the main camera and mess with it for rendering the object - make sure orthographic
Camera cam = Camera.main;
cam.orthographic = true;
//render to screen rect area equal to out image size
float rw = imageWidth;
rw /= Screen.width;
float rh = imageHeight;
rh /= Screen.height;
cam.rect = new Rect(0,0,rw,rh);
//grab size of object to render - place/size camera to fit
Bounds bb = objectToRender.GetComponent<Renderer>().bounds;
//place camera looking at centre of object - and backwards down the z-axis from it
cam.transform.position = bb.center;
cam.transform.position.Set(cam.transform.position.x, cam.transform.position.y, -1.0f + (bb.min.z * 2.0f));
//make clip planes fairly optimal and enclose whole mesh
cam.nearClipPlane = 0.5f;
cam.farClipPlane = -cam.transform.position.z + 10.0f + bb.max.z;
//set camera size to just cover entire mesh
cam.orthographicSize = 1.01f * Mathf.Max( (bb.max.y - bb.min.y)/2.0f, (bb.max.x - bb.min.x)/2.0f);
cam.transform.position.Set(cam.transform.position.x, cam.orthographicSize * 0.05f, cam.transform.position.y);
//render
yield return new WaitForEndOfFrame();
var tex = new Texture2D( imageWidth, imageHeight, TextureFormat.ARGB32, false );
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, imageWidth, imageHeight), 0, 0 );
tex.Apply();
//turn all pixels == background-color to transparent
Color bCol = cam.backgroundColor;
Color alpha = bCol;
alpha.a = 0.0f;
for(int y = 0; y < imageHeight; y++)
{
for(int x = 0; x < imageWidth; x++)
{
Color c = tex.GetPixel(x,y);
if (c.r == bCol.r)
tex.SetPixel(x,y,alpha);
}
}
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Destroy( tex );
System.IO.File.WriteAllBytes(Application.dataPath + "/../billboard.png", bytes);
}
示例6: ShowElevation
public void ShowElevation(GameObject plane, Map2 map)
{
var textureWidth = (int)Map1.Width * _textureScale;
var textureHeight = (int)Map1.Height * _textureScale;
var texture = new Texture2D(textureWidth, textureHeight, TextureFormat.RGB565, true);
texture.SetPixels(Enumerable.Repeat(BiomeProperties.Colors[Biome.Ocean], textureWidth * textureHeight).ToArray());
//绘制陆地
var lands = map.Graph.centers.Where(p => !p.ocean);
foreach (var land in lands)
texture.FillPolygon(
land.corners.Select(p => p.point * _textureScale).ToArray(),
BiomeProperties.Colors[Biome.Beach] * land.elevation);
//绘制边缘
var lines = map.Graph.edges.Where(p => p.v0 != null).Select(p => new[]
{
p.v0.point.x, p.v0.point.y,
p.v1.point.x, p.v1.point.y
}).ToArray();
foreach (var line in lines)
DrawLine(texture, line[0], line[1], line[2], line[3], Color.black);
//绘制中心点
var points = map.Graph.centers.Select(p => p.point).ToList();
foreach (var p in points)
texture.SetPixel((int)(p.x * _textureScale), (int)(p.y * _textureScale), Color.red);
texture.Apply();
plane.GetComponent<Renderer>().material.mainTexture = texture;
}
示例7: GetAd
public IEnumerator GetAd(int id)
{
BusinessCard = null;
adReady = false;
hasAd = false;
BusinessID = id;
string adURL = serverURL + "getAd?b=" + id;
string bcURL = serverURL + "bizcard?id=" + id;
WWW card = new WWW(bcURL);
yield return card;
BusinessCard = new Texture2D(Convert.ToInt32(card.texture.width),
Convert.ToInt32(card.texture.height),
TextureFormat.ARGB32, false);
BusinessCard.SetPixels(card.texture.GetPixels());
BusinessCard.Apply();
WWW page = new WWW(adURL);
yield return page;
if (page.error == null && page.text[0] == '{')
{
Debug.Log(page.text);
// Get the ad as a Dictionary object.
hasAd = true;
Dictionary<string, object> ad = Json.Deserialize(page.text) as Dictionary<string, object>;
AdImages(ad);
AdInfo = new AdData(ad);
}
adReady = true;
}
示例8: Generate2DTexture
public void Generate2DTexture()
{
texture2D = new Texture2D(n,n,TextureFormat.ARGB32,true);
int size = n*n;
Color[] cols = new Color[size];
float u,v;
int idx = 0;
Color c = Color.white;
for(int i = 0; i < n; i++) {
u = i/(float)n;
for(int j = 0; j < n; j++, ++idx) {
v = j/(float)n;
float noise = Mathf.PerlinNoise(u,v);
c.r = c.g = c.b = noise;
cols[idx] = c;
}
}
texture2D.SetPixels(cols);
texture2D.Apply();
renderer.material.SetTexture("g_tex", texture2D);
//Color[] cs = texture3D.GetPixels();
//for(int i = 0; i < 10; i++)
// Debug.Log (cs[i]);
}
示例9: OnGUI
void OnGUI()
{
if (gameOver) {
string gameOverText = "";
int textSize = Screen.width / 40;
textSize = textSize < 12 ? 12 : textSize;
Texture2D texture = new Texture2D(1, 1);
texture.SetPixel(0, 0, new Color(0.0f, 0.0f, 0.0f, 0.5f));
texture.Apply();
GUI.skin.box.normal.background = texture;
if (EnemyInterScript.numEnemies != 0)
{
gameOverText = "<size=" + textSize + ">Those sneaky cyber people kicked your butt..\n Monday could not get any worse than this.\n"
+ "Initiating time relapse...\n Press Enter to restart or press Escape to exit.</size>";
}
else
{
gameOverText = "<size=" + textSize + ">Ritual!!! You win!!!\n"
+ "Initiating time relapse...\n Press Enter to restart or press Escape to exit.</size>";
}
GUI.Box(new Rect(Screen.width * 0.25f, Screen.height * 0.25f, Screen.width * 0.5f, Screen.height * 0.5f), GUIContent.none);
GUI.Label(new Rect(Screen.width * 0.25f, Screen.height * 0.25f, Screen.width * 0.5f, Screen.height * 0.5f), gameOverText);
}
}
示例10: ConvertTangentBasis
/// <summary>
/// Converts vector data stored within a texture using the specified basis. Assume all calculations are performed in tangent space.
/// </summary>
/// <param name='basis'>
/// Basis to multiply the vector values against.
/// </param>
/// <param name='vectorData'>
/// Texture2D containing vector data. Textures are passed by reference, so make sure to copy beforehand if you don't want to overwrite your data!
/// </param>
public static void ConvertTangentBasis(Matrix4x4 basis, ref Texture2D vectorData, bool recomputeZ = false)
{
Color[] colorData = vectorData.GetPixels();
Texture2D tmpTexture = new Texture2D(vectorData.width, vectorData.height, TextureFormat.ARGB32, false);
for (int i = 0; i < colorData.Length; i++)
{
Color vecData = new Color(colorData[i].r, colorData[i].g, colorData[i].b, 1);
vecData.r = Vector3.Dot(new Vector3(basis.m00, basis.m01, basis.m02), UnpackUnitVector(new Vector3(colorData[i].r, colorData[i].g, colorData[i].b))) * 0.5f + 0.5f;
vecData.g = Vector3.Dot(new Vector3(basis.m10, basis.m11, basis.m12), UnpackUnitVector(new Vector3(colorData[i].r, colorData[i].g, colorData[i].b))) * 0.5f + 0.5f;
if (recomputeZ)
{
vecData.r = vecData.r * 2 - 1;
vecData.g = vecData.g * 2 - 1;
vecData.b = Mathf.Sqrt(1 - vecData.r * vecData.r - vecData.g * vecData.g) * 0.5f + 0.5f;
vecData.r = vecData.r * 0.5f + 0.5f;
vecData.g = vecData.g * 0.5f + 0.5f;
} else {
vecData.b = Vector3.Dot(new Vector3(basis.m20, basis.m21, basis.m22), UnpackUnitVector(new Vector3(colorData[i].r, colorData[i].g, colorData[i].b))) * 0.5f + 0.5f;
}
colorData[i] = vecData;
}
tmpTexture.SetPixels(colorData);
tmpTexture.Apply();
vectorData = tmpTexture;
}
示例11: ScreenshotEncode
IEnumerator ScreenshotEncode()
{
// wait for graphics to render
yield return new WaitForEndOfFrame();
// create a texture to pass to encoding
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
// put buffer into texture
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();
// split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
yield return 0;
byte[] bytes = texture.EncodeToPNG();
count = PlayerPrefs.GetInt("count");
// save our test image (could also upload to WWW)
File.WriteAllBytes(Application.dataPath + "/../testscreen-" + count + ".png", bytes);
count++;
PlayerPrefs.SetInt("count", count);
// Added by Karl. - Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots.
DestroyObject(texture);
Debug.Log( Application.dataPath + "/../testscreen-" + count + ".png" );
}
示例12: CreateGrassTexture
private void CreateGrassTexture()
{
grassTexture = new Texture2D(2,2);
grassTexture.SetPixels(new Color[]{Color.green,Color.green,Color.green,Color.green});
grassTexture.filterMode = FilterMode.Point;
grassTexture.Apply();
}
示例13: Monitor
public Monitor(Int32[] arr, UInt16 ptr, UInt16 chars, UInt16 colPtr, UInt16 modePointer)
{
mem = arr;
pointer = ptr;
charSetPtr = chars;
colors = new Color[16];
modePtr = modePointer;
for (int i = 0; i < 16; ++i) {
colors[i] = new Color();
colors[i].a = 1.0f;
}
colorPointer = colPtr;
image = new Texture2D(256, 256, TextureFormat.ARGB32, false);
windowPos = new Rect();
if ((windowPos.x == 0) && (windowPos.y == 0))//windowPos is used to position the GUI window, lets set it in the center of the screen
{
windowPos = new Rect(Screen.width / 2, Screen.height / 2, 100, 100);
}
//Set all the pixels to black. If you don't do this the image contains random junk.
for (int y = 0; y < image.height; y++) {
for (int x = 0; x < image.width; x++) {
image.SetPixel(x, y, Color.black);
}
}
image.Apply();
}
示例14: CreateSprite
public override Texture2D CreateSprite (Texture2D texture)
{
Color[] pixels = texture.GetPixels ();
Color[,] pixels2D = new Color[texture.width, texture.height];
for (int i = 0; i < pixels.Length; i++) {
int x = i % texture.width;
int y = i / texture.height;
pixels2D [x, y] = pixels [i];
}
int amountOfEyes = (int)(sightAngle / 45);
for (int i = 0; i < amountOfEyes; i++) {
int radius = (texture.width - eyeSize) / 2;
float xPos = Mathf.Sin (Mathf.Deg2Rad * (i * 45));
float yPos = Mathf.Cos (Mathf.Deg2Rad * (i * 45));
Vector2 pos = new Vector2 (xPos, yPos) * radius + new Vector2 (texture.width - eyeSize, texture.height - eyeSize) / 2;
ApplyKernel (ref pixels2D, texture.width, texture.height, pos);
}
for (int x = 0; x < pixels2D.GetLength (0); x++) {
for (int y = 0; y < pixels2D.GetLength (1); y++) {
pixels [x + y * texture.width] = pixels2D [x, y];
}
}
texture.SetPixels (pixels);
texture.Apply ();
return texture;
}
示例15: TakeScreenShot
public IEnumerator TakeScreenShot()
{
yield return new WaitForEndOfFrame();
Camera camOV = OVcamera.GetComponent<Camera>();
RenderTexture currentRT = RenderTexture.active;
RenderTexture.active = camOV.targetTexture;
camOV.Render();
Texture2D imageOverview = new Texture2D(camOV.targetTexture.width, camOV.targetTexture.height, TextureFormat.RGB24, false);
imageOverview.ReadPixels(new Rect(0, 0, camOV.targetTexture.width, camOV.targetTexture.height), 0, 0);
imageOverview.Apply();
RenderTexture.active = currentRT;
// Encode texture into PNG
byte[] bytes = imageOverview.EncodeToPNG();
// save in memory
//string filename = fileName(Convert.ToInt32(imageOverview.width), Convert.ToInt32(imageOverview.height));
string filename = fileName(resWidth, resHeight);
//path = Application.persistentDataPath + "/Snapshots/" + filename;
path = filename;
System.IO.File.WriteAllBytes(path, bytes);
}