Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bitelchus101 · Apr 04, 2016 at 02:39 PM · texturecubevolumetricloading textures

Problem creating 3D texture - Stack of 2D Textures

Hi,

I'm having an issue creating a 3D texture and filling it with images. What I want to do is creating a Volumetric/3D texture with the RGBA values of my images, and put it in order in the texture. In order to do this, I create a Cube, and apply this shader and script:


Script

 using UnityEditor;
 using UnityEngine;
 
 public class Create3DTex : MonoBehaviour
 {
 
     private Texture3D tex;
     private Texture2D[] slices;
     private Texture2D tex2d;
 
     void Start()
     {
         int n = 91;
         slices = new Texture2D[n];
         for (int j = 0; j < n; j++)
         {
             tex2d = (Texture2D)UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Resources/Textures/texture" + j + ".png", typeof(Texture2D));
             SetTextureImporterFormat(tex2d, true);
             slices[j] = tex2d;
         }
         int size = 64;
         tex = new Texture3D(size, size, size, TextureFormat.ARGB32, false);
         var cols = new Color[size * size * size];
         float mul = 1.0f / (size - 1);
         int idx = 0;
         var c = new Color[size * size * size];
 
         var countOffset = (slices.Length - 1) / (float)size;
         var sliceCount = 0;
         var sliceCountFloat = 0f;
         for (int z = 0; z < size; ++z)
         {
             sliceCountFloat += countOffset;
             sliceCount = Mathf.FloorToInt(sliceCountFloat);
             for (int y = 0; y < size; ++y)
             {
                 for (int x = 0; x < size; ++x, ++idx)
                 {
                     cols[idx] = slices[sliceCount].GetPixelBilinear(x / (float)size, y / (float)size);
                 }
             }
         }
         tex.SetPixels(cols);
         tex.Apply();
         GetComponent<Renderer>().material.SetTexture("_Volume", tex);
     }
 
     public static void SetTextureImporterFormat(Texture2D texture, bool isReadable)
     {
         if (null == texture) return;
 
         string assetPath = AssetDatabase.GetAssetPath(texture);
         var tImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
         if (tImporter != null)
         {
             tImporter.textureType = TextureImporterType.Advanced;
 
             tImporter.isReadable = isReadable;
 
             AssetDatabase.ImportAsset(assetPath);
             AssetDatabase.Refresh();
         }
     }
 }

Shader

  Shader "DX11/Sample 3D Texture" {
     Properties {
         _Volume ("Texture", 3D) = "" {}
     }
     SubShader {
         Pass {
          
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma exclude_renderers flash gles
              
             #include "UnityCG.cginc"
              
             struct vs_input {
                 float4 vertex : POSITION;
             };
              
             struct ps_input {
                 float4 pos : SV_POSITION;
                 float3 uv : TEXCOORD0;
             };
              
              
             ps_input vert (vs_input v)
             {
                 ps_input o;
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.uv = v.vertex.xyz;
                 return o;
             }
              
             sampler3D _Volume;
              
             float4 frag (ps_input i) : COLOR
             {
                 return tex3D (_Volume, i.uv);
             }
              
             ENDCG
              
             }
         }
          
         Fallback "VertexLit"
     }

Its seems that it loads something, but I dont know how to make the cube transparent and the textures doesnt look correctly. It shows something like this. Any ideas? Thanks in advance.

alt text

captura.png (62.0 kB)
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

62 People are following this question.

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

Related Questions

Getting red question mark when using Texture2D.LoadImage! 1 Answer

use sprites as material in 3D shader 0 Answers

Unity 5 ipad2 display sprites from 2048 texture 1 Answer

Texture goes missing when importing objects and materials from Maya 0 Answers

Texture looks completely different from texturing program 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