Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dsilvavini · Feb 06, 2013 at 04:12 PM · texturetexture2drendertextureglfloatingpoint

How do I initialize a float texture by script?

My problem is that I want to initialize a texture with vertex positions to feed a shader. I'm thinking of trying a Texture2D with setPixel() but as the docs say there is no float format for creating a texture2D which allows me to use setPixel. Another idea is to use a init shader with a RenderTexture but in a fragment shader I can't know which pixel is being processed, so I wouldn't know which position to write in the texture. My last idea is to generate the float texture with OpenGL calls, but in the GL class docs I couldn't find anything related with glTexImage2D(). I'm new to Unity so any help will be very appreciated.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dsilvavini · Feb 10, 2013 at 01:59 AM 0
Share

After some work, I see that the RenderTexture + fragment shader can work if I draw a X*X pixel rect and use uv coords to write to the right place. The only problem that I'm getting right now is how to draw that X*X pixel rect...

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by dsilvavini · Feb 11, 2013 at 04:09 AM

The RenderTexture + fragment shader approach have solved the problem. Here's the code to initialize a X*X pixel plane using an orthographic camera (cam in the snippet). The cam orthographic size must be set to dimensions.x/2 and it must render to a dimensions.x*dimensions.y RenderTexture (set in inspector).

 using UnityEngine;
 using System.Collections;
 
 public class EffectScreenQuad : MonoBehaviour
 {
     public Vector2 dimensions; //dimensions.x == dimmensions.y
     public Camera cam;
     
     // Use this for initialization
     void Start ()
     {
         Vector3[] vertices  = new Vector3[4];
         Vector3[] normals   = new Vector3[4];
         Vector2[] uv        = new Vector2[4];
         int[] triangles     = new int[6];
         
         //Create plane vertices that fill a dimensions.x*dimension.y pixels rect
         Vector3 lowerLeftPlaneScreenPos    = new Vector3(0 , 0 , 0);
         Vector3 lowerRightPlaneScreenPos= new Vector3(dimensions.x , 0 , 0);
         Vector3 upperLeftPlaneScreenPos = new Vector3(0 , dimensions.y , 0);
         
         Vector3 lowerLeftPlaneWorldPos  = cam.ScreenToWorldPoint(lowerLeftPlaneScreenPos);
         Vector3 lowerRightPlaneWorldPos = cam.ScreenToWorldPoint(lowerRightPlaneScreenPos);
         Vector3 upperLeftPlaneWorldPos = cam.ScreenToWorldPoint(upperLeftPlaneScreenPos);
         Vector3 heightDelta = (upperLeftPlaneWorldPos - lowerLeftPlaneWorldPos)/2;
         Vector3 widthDelta = (lowerRightPlaneWorldPos - lowerLeftPlaneWorldPos)/2;
         
         vertices[0] = lowerLeftPlaneWorldPos;
         vertices[1] = lowerRightPlaneWorldPos;
         vertices[2] = upperLeftPlaneWorldPos;
         vertices[3] = heightDelta + widthDelta;
         
         Vector3 normal = Vector3.Cross(widthDelta , heightDelta);
         normal.Normalize();
         for (int i = 0; i < 4; i++)
             normals[i] = normal;
 
         uv[0] = Vector2.zero;
         uv[1] = Vector2.right;
         uv[2] = Vector2.up;
         uv[3] = Vector2.right + Vector2.up;

         //Unity3D culls counterclockwise faces...
         triangles[0] = 0;
         triangles[1] = 2;
         triangles[2] = 1;
         triangles[3] = 1;
         triangles[4] = 2;
         triangles[5] = 3;
 
         MeshFilter mesh_filter        = gameObject.GetComponent("MeshFilter") as MeshFilter;
         mesh_filter.mesh = new Mesh ();
         mesh_filter.mesh.vertices     = vertices;
         mesh_filter.mesh.normals       = normals;
         mesh_filter.mesh.uv            = uv;
         mesh_filter.mesh.triangles     = triangles;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Converting a RenderTexture to a Texture2D for use in a shader 2 Answers

Texture manipulation while keeping the work on the vid card. 1 Answer

ReadPixels returns RGBA(0,0,0,0) 0 Answers

Save image from camera with post processing. 0 Answers

Raycast project to texture at runtime 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges