FBO texture copy not working on Android - rendered texture filled with whatever is at texture coord 0, 0
Date : March 29 2020, 07:55 AM
I wish this help you The declaration of your vertex shader output and fragment shader input do not mach for the texture coordinate varying (different precision qualifiers). Ordinarily this would not be an issue, but for reasons I will discuss below using highp in your fragment shader may come back to bite you in the butt. Vertex shader: attribute vec4 position;
attribute mediump vec4 textureCoordinate;
varying mediump vec2 coordinate;
void main()
{
gl_Position = position;
coordinate = textureCoordinate.xy;
}
varying highp vec2 coordinate;
uniform sampler2D texture;
void main()
{
gl_FragColor = texture2D(texture, coordinate);
}
#define GL_FRAGMENT_PRECISION_HIGH 1
|
How to set texture to automatically match with a change in window size by using same texture
Date : March 29 2020, 07:55 AM
I wish this helpful for you I made a small 2D game by using unity3D(5.0.2f1). I dragged in some textures to use as the background in a scene. It worked well in the Unity Editor, but not when I built and ran it in a different window size. It didn't look as what I had seen in Unity Editor. , Maybe these will help! //file ExtensionsHandy.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public static class ExtensionsHandy
{
public static float OrthoScreenWidth(this Camera c)
{
return
c.ViewportToWorldPoint(new Vector3(1,1,10)).x
- c.ViewportToWorldPoint(new Vector3(0,1,10)).x;
}
public static float OrthoScreenHeight(this Camera c)
{
return
c.ViewportToWorldPoint(new Vector3(0,1,10)).y
- c.ViewportToWorldPoint(new Vector3(0,0,10)).y;
}
}
void Start()
{
float w = Camera.main.OrthoScreenWidth();
Debug.Log("w is " +w.ToString("f4");
}
transform.ScreenRight();
transform.ScreenTop();
void Update()
{
transform.ScreenRight();
}
public static Vector3 WP( this Camera c, float x, float y, float z)
{
return c.ViewportToWorldPoint(new Vector3(x,y,z));
}
public static void ScreenLeft(this GameObject moveme)
{
Vector3 rr = moveme.transform.position;
Vector3 leftTop = Camera.main.WP(0f,1f,0f);
float leftX = leftTop.x;
rr.x = leftX;
moveme.transform.position = rr;
}
public static void ScreenRight(this GameObject moveme)
{
Vector3 rr = moveme.transform.position;
Vector3 rightTop = Camera.main.WP(1f,1f,0f);
float rightX = rightTop.x;
rr.x = rightX;
moveme.transform.position = rr;
}
public static void ScreenBottom(this GameObject moveme)
{
Vector3 rr = moveme.transform.position;
Vector3 bottomLeft = Camera.main.WP(0f,0f,0f);
float bottomY = bottomLeft.y;
rr.y = bottomY;
moveme.transform.position = rr;
}
public static void ScreenTop(this GameObject moveme)
{
Vector3 rr = moveme.transform.position;
Vector3 topLeft = Camera.main.WP(0f,1f,0f);
float topY = topLeft.y;
rr.y = topY;
moveme.transform.position = rr;
}
|
How to change texture of an object to a new texture via a trigger
Date : March 29 2020, 07:55 AM
I hope this helps . I Have a trigger in my game and want to change the texture when the player enters a certain area. I have set up the trigger but can't get the image of the waal (object) to change? , Here is the code for changing wall material texture - var Wall1: GameObject; \\Existing Game Object
var wall1TC: Texture; \\Texture to change Existing Game Object to.
function OnTriggerEnter() {
Wall1.GetComponent.<Renderer>().material.mainTexture = wall1TC;
}
|
RENDER WARNING: texture bound to texture unit 0 is not renderable. It maybe non-power-of-2 and have incompatible texture
Date : March 29 2020, 07:55 AM
To fix the issue you can do The issue is you're using COLOR_ATTACHMENT1 and skipping COLOR_ATTACHMENT0. IIRC you need to start at attachment 0 and work your way up. const gl = document.createElement("canvas").getContext("webgl2");
testAttachment(gl.COLOR_ATTACHMENT1);
testAttachment(gl.COLOR_ATTACHMENT0);
function testAttachment(attachmentPoint) {
function createTexture() {
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
var level = 0;
var internalFormat = gl.RGBA32UI;
var border = 0;
var format = gl.RGBA_INTEGER;
var type = gl.UNSIGNED_INT;
var data = null;
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat,
1, 1, border, format, type, data);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
return texture;
}
var texture = createTexture();
var fb = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
var level = 0;
gl.framebufferTexture2D(gl.FRAMEBUFFER, attachmentPoint, gl.TEXTURE_2D, texture, level);
const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
console.log(glEnumToString(gl, attachmentPoint), glEnumToString(gl, status));
if (status !== gl.FRAMEBUFFER_COMPLETE) {
return;
}
const vs = `#version 300 es
void main() {
gl_Position = vec4(0,0,0,1);
gl_PointSize = 100.0;
}
`;
const fs = `#version 300 es
uniform highp usampler2D color;
out uvec4 outColor;
void main() {
outColor = texture(color, gl_PointCoord);
}
`;
const prg = twgl.createProgram(gl, [vs, fs]);
gl.useProgram(prg);
// need a different input texture than output texture
const inTex = createTexture();
// no need to set uniforms since they default to 0
// so using texture unit 0
gl.drawArrays(gl.POINTS, 0, 1);
// check that it rendered without error
console.log(glEnumToString(gl, gl.getError()));
}
function glEnumToString(gl, value) {
if (value === 0) {
return "NONE";
}
for (let key in gl) {
if (gl[key] === value) {
return key;
}
}
return "0x" + value.toString(16);
}
<script src="https://twgljs.org/dist/4.x/twgl.min.js"></script>
|
Change the colors of a texture based on another texture in Unity?
Date : March 29 2020, 07:55 AM
hope this fix your issue GetPixels() returns an array of Color. If you want to get the color of single pixel, use GetPixel() instead. In your code try to change Color pixels2 = source.GetPixels(x, y);to Color pixels2 = source.GetPixel(x, y); Update 1: Try to test this code. Working perfectly for me. int width = 150;
int height = 150;
Texture2D secondTexture;
public Texture2D source; //texture for a test, add in inspector
public Renderer rend; //Object for a test, add in inspector
void Start()
{
secondTexture = new Texture2D(width, height);
var pixels = new Color[width * height];
for (var x = 0; x < width; x++)
{
for (var y = 0; y < height; y++)
{
Color pixels2 = source.GetPixel(x, y);
if(pixels2 == Color.white)
{
//Paint the pixels in secondTexture that match X and Y of sourceTexture with blue per example
pixels[x + y * width] = Color.blue;
}
else
{
//Paint the rest of the pixels with black
pixels[x + y * width] = Color.black;
}
}
}
secondTexture.SetPixels(pixels);
secondTexture.wrapMode = TextureWrapMode.Clamp;
secondTexture.Apply();
rend.material.mainTexture = secondTexture;
}
|