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
3
Question by numberkruncher · Jun 10, 2012 at 03:55 PM · shadercgglsl

Can sampler2DArray be used?

Is it possible to use sampler2DArray in GLSL shaders? Is there an alternative that works with CG shaders?

 Shader "TestShader" {
     Properties {
         _TestTex ("Test Tex (RGB)", 2D) = "white" {}
     }
     SubShader { Pass {
         GLSLPROGRAM
         #ifdef FRAGMENT
             uniform sampler2DArray _TestTex;

             // Rest of shader...
         #endif

         // vertex shader...
         ENDGLSL
     }}
 }

EDIT: MY ATTEMPT

 // Upgrade NOTE: replaced 'samplerRect' with 'sampler2D'
 // Upgrade NOTE: replaced 'texRECT' with 'tex2D'
 
 Shader "Tilemap/Tilemap2" {
   Properties {
     _MainTex ("Tileset", 2D) = "black" {}
     _Tilemap ("Tilemap", 2D) = "black" {}
     _TileCount ("Input/Output", vector) = (0,0,0,0)
   }
   SubShader {
     Tags { "RenderType"="Opaque" }
     LOD 200
     pass
     {  
       CGPROGRAM
       #pragma vertex vert
       #pragma fragment frag
       #include "UnityCG.cginc"
 
       uniform sampler2D _MainTex;
       uniform sampler2D _Tilemap;
       float4 _TileCount;
       
       struct appdata {
         float4 vertex    : POSITION;
         float2 texcoord    : TEXCOORD0;
       };
       
       float4 _MainTex_ST;
       float4 _Tilemap_ST;
       
       struct v2f {
         float4 pos    : SV_POSITION;
         float2 uv    : TEXCOORD0;
       };
       
       v2f vert(appdata v) {
         v2f result;
         result.pos = mul(UNITY_MATRIX_MVP, v.vertex);
         result.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
         return result;
       }
 
     float2 mod(const float2 a, const float2 b) {
         return floor(frac(a / b) * b);
     }
 
     half4 frag(v2f i) : COLOR {
         float2 mappingScale = _TileCount.zw;
         float2 mappingAddress = i.uv.xy * mappingScale;
         float4 whichTile = tex2D(_Tilemap, mod(mappingAddress, mappingScale));
           
           float2 tileScale = _TileCount.xy;
           //float2 tileScaledTex = i.uv.xy * float2(mappingScale.x / tileScale.x, mappingScale.y / tileScale.y);
           
           return tex2D(_MainTex, (whichTile.xy + frac(mappingAddress)) / tileScale);//, ddx(tileScaledTex), ddy(tileScaledTex));
       }
       ENDCG
     }
   } 
   FallBack "Diffuse"
 }
Comment
Add comment · Show 9
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 Jessy · Jun 10, 2012 at 05:01 PM 0
Share

How could you use it? Unity doesn't provide any way to bind one.

avatar image numberkruncher · Jun 10, 2012 at 05:05 PM 0
Share

@Jessy I am not entirely sure how these things work. I came across the following page: http://www.java-ga$$anonymous$$g.org/topics/rendering-tiles-fast-isometric-too/25292/view.html which makes it seem as though a single texture is specified...

avatar image numberkruncher · Jun 10, 2012 at 05:06 PM 0
Share

Obviously that link is not for Unity, but it is GLSL

avatar image numberkruncher · Jun 10, 2012 at 05:43 PM 0
Share

Without bleeding between tiles

avatar image Jessy · Jun 10, 2012 at 06:15 PM 0
Share

It's not a limitation of languages (barring ShaderLab); just Unity's lack of implementation at present. I can't think of anything that's as usable as vertices. Has that really become a bottleneck for you, with batching involved?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jessy · Jun 10, 2012 at 05:38 PM

The Java code at your link uses glBindTexture(GL_TEXTURE_2D_ARRAY, ... http://www.opengl.org/sdk/docs/man3/xhtml/glBindTexture.xml

But Unity does not have a property type that maps to it: http://unity3d.com/support/documentation/Components/SL-Properties

Also, I don't know if you care, but OpenGL ES only supports the two property types that Unity does (GL_TEXTURE_RECTANGLE was removed for Unity3: http://unity3d.com/support/documentation/Manual/SL-V3Conversion.html ). http://www.khronos.org/opengles/sdk/docs/man/xhtml/glBindTexture.xml

Comment
Add comment · Show 1 · 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
avatar image numberkruncher · Jun 10, 2012 at 05:43 PM 0
Share

@Jessy thanks for the info! Do you think that it is feasible to create a similar shader using CG for Unity?

avatar image
0

Answer by numberkruncher · Jun 11, 2012 at 01:49 AM

@Jessy Is altering a small index texture not more efficient than managing lots of game objects (or altering meshes dynamically). Yes the GC gets hit hard right now!

During my searches I came across the following link which makes a lot more sense to me: http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter12.html

I have done my best to convert this into something Unity can understand. It is working better, in that the shader is showing the right number of tiles on my plane, it is cutting the atlas into the right number of tiles. I can manually insert the tile index that I want into the shader which causes all tiles to be the one specified.

I cannot figure out the format of the _Tilemap image.

I will update question with my source.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I get GLSL shaders to work in Unity? 0 Answers

Can a CG shader fail to work on hardware? 1 Answer

How can I make my own shader include file (.cginc or .glslinc)? 2 Answers

shader inverse(float4x4) function 2 Answers

CG shader is treated as GLSL and doesn't compile 0 Answers


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