Page 1 sur 1
shader grid transparent volumétrique ?
Publié : 23 Fév 2024 05:45
par Kaloverian
bonjour,
Voici un shader grid 2D réaliser avec le Shader Graph,luminescent et transparent,appliqué sur un plan:
- shader grid luminescent.jpg (89.74 Kio) Consulté 2113 fois
et une grille 3D trouvée sur internet:
- grid 3D.jpg (85.19 Kio) Consulté 2113 fois
Mon but est:
à partir d'un grand cube (1 seul objet),est-il possible de créer un shader volumétrique alliant les 2 images précédentes et à placer dans un cube?
Si oui,comment?
merci de votre aide
Re: shader grid transparent volumétrique ?
Publié : 23 Fév 2024 10:16
par boubouk50
Tu veux créer une grille 3D avec un cube et un shader ?
C'est possible, mais ce sont des maths pas simples.
Le plus simple est de créer ta grille à partir de plans et d'un shader 2D de grille.
Re: shader grid transparent volumétrique ?
Publié : 25 Fév 2024 05:14
par Kaloverian
J'ai demandé à chatgpt de me coder un script C# pour réaliser une texture 3D selon mon souhait initial.
Voici le code retourné:
Code : Tout sélectionner
using UnityEngine;
public class GridTextureGenerator : MonoBehaviour
{
public int width = 64;
public int height = 64;
public int depth = 64;
public float cellSize = 10.0f;
public float lineWidth = 2.0f;
public float intensity = 1.0f;
public float threshold = 0.5f;
void Start()
{
GenerateGridTexture();
}
void GenerateGridTexture()
{
Color[] colors = new Color[width * height * depth];
for (int z = 0; z < depth; z++)
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
float fx = x * cellSize;
float fy = y * cellSize;
float fz = z * cellSize;
// Compute distance to the nearest grid line
float distanceX = Mathf.Abs(fx % cellSize - cellSize * 0.5f) - lineWidth * 0.5f;
float distanceY = Mathf.Abs(fy % cellSize - cellSize * 0.5f) - lineWidth * 0.5f;
float distanceZ = Mathf.Abs(fz % cellSize - cellSize * 0.5f) - lineWidth * 0.5f;
// Combine distances to get a 3D grid pattern
float distance = Mathf.Min(Mathf.Min(distanceX, distanceY), distanceZ);
// Compute transparency based on the threshold
float alpha = distance > threshold ? 0.0f : 1.0f;
// Set color with transparency
colors[x + y * width + z * width * height] = new Color(intensity, 0.0f, 0.0f, alpha);
}
}
}
// Create a 3D texture and assign the colors
Texture3D texture = new Texture3D(width, height, depth, TextureFormat.RGBA32, true);
texture.SetPixels(colors);
texture.Apply();
// Assign the texture to a material or use it as needed
GetComponent<Renderer>().material.SetTexture("_GridTexture", texture);
}
}
J'ai insérer ce script sur un cube dépourvu de matérial et ça n'a rien donné !
Pourquoi cela?
Re: shader grid transparent volumétrique ?
Publié : 26 Fév 2024 10:19
par boubouk50
Une texture3D ne fonctionne pas comme cela, tu ne peux pas juste l'affecter à un mesh.
Normalement, tu peux la visualiser dans l'éditeur et devrait te donner le bon résultat. Déjà, regarde si c'est le cas.
Mais dans la scène, il faut la visualiser avec un shader spécifique :
https://docs.unity3d.com/Manual/class-Texture3D.html